Last active
August 6, 2020 08:56
-
-
Save rwestphal/6528ebeb8a612711ca3771b843a4eb14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
#$VERBOSE = true | |
require 'date' | |
this_dir = File.expand_path(File.dirname(__FILE__)) | |
lib_dir = File.join(this_dir, "ruby") | |
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) | |
require "grpc" | |
require "frr-northbound_services_pb" | |
def main | |
host = "localhost:50051" | |
channel_args = { "grpc.max_send_message_length" => -1, | |
"grpc.max_receive_message_length" => -1 } | |
stub = Frr::Northbound::Stub.new(host, :this_channel_is_insecure, channel_args: channel_args) | |
# Get operational data. | |
puts "> Get" | |
request = Frr::GetRequest.new | |
request.type = :ALL | |
request.encoding = :JSON | |
request.with_defaults = true | |
request.path.push("/frr-ripd:ripd") | |
response = stub.get(request) | |
response.each do |r| | |
puts " timestamp: #{r.timestamp} (#{Time.at(r.timestamp).to_datetime})" | |
puts " encoding: #{r.data.encoding}" | |
r.data.data.each_line { |line| puts " #{line}" } | |
end | |
# Update the configuration. | |
puts "> LockConfig" | |
stub.lock_config(Frr::LockConfigRequest.new) | |
puts "> CreateCandidate" | |
response = stub.create_candidate(Frr::CreateCandidateRequest.new) | |
candidate_id = response.candidate_id | |
puts "> LoadToCandidate" | |
request = Frr::LoadToCandidateRequest.new | |
request.candidate_id = candidate_id | |
request.type = :MERGE; | |
request.config = Frr::DataTree.new | |
request.config.encoding = :JSON; | |
request.config.data = %{ | |
{ | |
"frr-ripd:ripd": { | |
"instance": { | |
"explicit-neighbor": [ | |
"1.1.1.1" | |
], | |
"static-route": [ | |
"10.0.1.0/24", | |
"10.0.2.0/24", | |
"10.0.3.0/24" | |
] | |
} | |
} | |
} | |
} | |
stub.load_to_candidate(request) | |
puts "> Commit" | |
request = Frr::CommitRequest.new | |
request.candidate_id = candidate_id | |
request.phase = :ALL | |
request.comment = "Configured by a Ruby script!" | |
response = stub.commit(request) | |
puts " transaction_id: #{response.transaction_id}" | |
puts "> UnlockConfig" | |
stub.unlock_config(Frr::UnlockConfigRequest.new) | |
puts "> DeleteCandidate" | |
req = Frr::DeleteCandidateRequest.new(candidate_id: candidate_id) | |
response = stub.delete_candidate(req) | |
# Execute a YANG RPC. | |
puts "> Execute" | |
request = Frr::ExecuteRequest.new | |
request.path = "/frr-ripd:clear-rip-route" | |
#request.input.push("") | |
response = stub.execute(request) | |
response.output.each do |o| | |
puts " #{o.path}: #{o.value}" | |
end | |
rescue GRPC::BadStatus => e | |
puts "Error #{e.code}: #{e.details}" | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to run this example code?