def Handle_Notification(obj): if obj.HasField('config'): logging.info(f"GOT CONFIG :: {obj.config.key.js_path}") if obj.config.key.js_path == ".system.snmp.network_instance": ni_name = obj.config.key.keys[0] logging.info( f"Got config for agent, now will handle it :: \n{obj.config}\ Operation :: {obj.config.op}\nData :: {obj.config.data.json}" ) if obj.config.op == 2: logging.info(f"Delete snmp-agent cli scenario") # if file_name != None: # Update_Result(file_name, action='delete') response = stub.AgentUnRegister( request=sdk_service_pb2.AgentRegistrationRequest(), metadata=metadata) logging.info( 'Handle_Config: Unregister response:: {}'.format(response)) else: data = json.loads(obj.config.data.json) if 'network_instance' in data: ni = data['network_instance'] if 'enable_set_interface' in ni: enable = ni['enable_set_interface']['value'] if enable: logging.info( f"Enabling SNMP SET for net_inst={ni_name}") return ni_name logging.info(f"NOT enabling SNMP SET; data={data}") else: logging.info(f"Unexpected notification : {obj}") return None
def Exit_Gracefully(signum, frame): logging.info( "Caught signal :: {}\n will unregister ecmp_agent".format(signum)) try: response = stub.AgentUnRegister( request=sdk_service_pb2.AgentRegistrationRequest(), metadata=metadata) logging.error('try: Unregister response:: {}'.format(response)) sys.exit() except grpc._channel._Rendezvous as err: logging.info('GOING TO EXIT NOW: {}'.format(err)) sys.exit()
def Run(): sub_stub = sdk_service_pb2_grpc.SdkNotificationServiceStub(channel) response = stub.AgentRegister( request=sdk_service_pb2.AgentRegistrationRequest(), metadata=metadata) logging.info(f"Registration response : {response.status}") request = sdk_service_pb2.NotificationRegisterRequest( op=sdk_service_pb2.NotificationRegisterRequest.Create) create_subscription_response = stub.NotificationRegister(request=request, metadata=metadata) stream_id = create_subscription_response.stream_id logging.info( f"Create subscription response received. stream_id : {stream_id}") Subscribe_Notifications(stream_id) stream_request = sdk_service_pb2.NotificationStreamRequest( stream_id=stream_id) stream_response = sub_stub.NotificationStream(stream_request, metadata=metadata) count = 1 try: net_instances = {} for r in stream_response: logging.info( f"Count :: {count} NOTIFICATION:: \n{r.notification}") count += 1 for obj in r.notification: if obj.HasField( 'config') and obj.config.key.js_path == ".commit.end": logging.info(f"COMMIT: {net_instances}") if len(net_instances) > 0: logging.info(f"Enabling SNMP set for: {net_instances}") for ni in net_instances: EnableSNMPSetInterface(ni) else: net_instance = Handle_Notification(obj) if net_instance is not None: net_instances[net_instance] = True except Exception as e: traceback_str = ''.join(traceback.format_tb(e.__traceback__)) logging.error(f'Exception caught :: {e} stack:{traceback_str}') finally: Exit_Gracefully(0, 0)
def Run(): sub_stub = sdk_service_pb2_grpc.SdkNotificationServiceStub(channel) response = stub.AgentRegister( request=sdk_service_pb2.AgentRegistrationRequest(), metadata=metadata) logging.info(f"Registration response : {response.status}") app_id = get_app_id(agent_name) if not app_id: logging.error( f'idb does not have the appId for {agent_name} : {app_id}') else: logging.info(f'Got appId {app_id} for {agent_name}') request = sdk_service_pb2.NotificationRegisterRequest( op=sdk_service_pb2.NotificationRegisterRequest.Create) create_subscription_response = stub.NotificationRegister(request=request, metadata=metadata) stream_id = create_subscription_response.stream_id logging.info( f"Create subscription response received. stream_id : {stream_id}") Subscribe_Notifications(stream_id) stream_request = sdk_service_pb2.NotificationStreamRequest( stream_id=stream_id) stream_response = sub_stub.NotificationStream(stream_request, metadata=metadata) file_name = None count = 1 route_count = 0 try: for r in stream_response: logging.info( f"Count :: {count} NOTIFICATION:: \n{r.notification}") count += 1 for obj in r.notification: if obj.HasField( 'config') and obj.config.key.js_path == ".commit.end": logging.info('TO DO -commit.end config') else: file_name, route_count = Handle_Notification( obj, file_name, app_id, route_count) except grpc._channel._Rendezvous as err: logging.info('GOING TO EXIT NOW {}'.format(err)) except Exception as e: logging.error('Exception caught :: {}'.format(e)) if file_name != None: Update_Result(file_name, action='delete') try: response = stub.AgentUnRegister( request=sdk_service_pb2.AgentRegistrationRequest(), metadata=metadata) logging.error('Run try: Unregister response:: {}'.format(response)) except grpc._channel._Rendezvous as err: logging.info('GOING TO EXIT NOW: {}'.format(err)) sys.exit() return True sys.exit() return True
def Handle_Notification(obj, file_name, app_id, route_count): if obj.HasField('config') and obj.config.key.js_path != ".commit.end": logging.info(f"GOT CONFIG :: {obj.config.key.js_path}") logging.info(f"OLD FILE :: {file_name}") logging.info(f"Handle_Config with file_name as {file_name}") if "ecmp_agent" in obj.config.key.js_path: logging.info( f"Got config for agent, now will handle it :: \n{obj.config}\ Operation :: {obj.config.op}\nData :: {obj.config.data.json}" ) if obj.config.op == 2: logging.info(f"Delete fib-agent cli scenario") if file_name != None: Update_Result(file_name, action='delete') response = stub.AgentUnRegister( request=sdk_service_pb2.AgentRegistrationRequest(), metadata=metadata) route_count = 0 logging.info( 'Handle_Config: Unregister response:: {}'.format(response)) else: json_acceptable_string = obj.config.data.json.replace( "'", "\"") data = json.loads(json_acceptable_string) if 'input_fib' in data: fib = data['input_fib']['value'] #delete old file's state before updating filename var with new input-file if file_name != None: Update_Result(file_name, action='delete') logging.info( f"Got input-fib :: {data['input_fib']['value']}") #if action is not set in cli, default it to 'add' if 'action' in data: action = data['action'][7:] # skip "ACTION_xxx" else: action = 'add' logging.info(f"Action received is : {action}") file_name = fib if not ProgramFibRoutes(input_fib=fib, action=action): logging.info( f"Something went wrong during action : {action}") #update the file's status to be False and populate reason code Update_Result(file_name, action='add', result=False, reason="ERROR:Couldnt program fib") # global pushed_routes Update_Routes(programmed=route_count, actual=pushed_routes) return file_name, route_count elif obj.HasField('route'): # Update the status if route info got if obj.route.data.owner_id == app_id: addr = ipaddress.ip_address( obj.route.key.ip_prefix.ip_addr.addr).__str__() prefix = obj.route.key.ip_prefix.prefix_length # process delete if obj.route.op == 2: logging.info(f'Got delete for route {addr}/{prefix}') route_count -= 1 Update_Routes(programmed=route_count) elif obj.route.op == 1: # process modifications where we do not need any route count updates pass else: # proces add route scenario route_count += 1 #logging.info(f'Got Route added by agent: {obj.route}') nhs = '' # Considering only ip nexthops. Will process mpls_next hops once dts319345 is resolved for nh in obj.route.data.nexthop: logging.info(f'Route Nexthop to be processed - {nh}') # for MPLS nexthop v/s IP nexthop if nh.type == 2: nhs += ipaddress.ip_address( nh.mpls_nexthop.ip_nexthop.addr).__str__() + ', ' else: nhs += ipaddress.ip_address( nh.ip_nexthop.addr).__str__() + ', ' logging.info( f'Route {addr}/{prefix} added by agent with nexthops {nhs}' ) Update_Routes(programmed=route_count) else: # JvB NEW -> do ECMP traceroute for /32 IPv4 routes added prefix = obj.route.key.ip_prefix.prefix_length if prefix == 32 and obj.route.op == 0: for nh in obj.route.data.nexthop: logging.info(f'Route Nexthop to be processed - {nh}') # for MPLS nexthop v/s IP nexthop if nh.type != 2: # Not MPLS route_count += 1 call_traceroute( ipaddress.ip_address( nh.mpls_nexthop.ip_nexthop.addr).__str__()) # TODO process output, associate with route return file_name, route_count else: return file_name, route_count #always return return file_name, route_count
def Run(): sub_stub = sdk_service_pb2_grpc.SdkNotificationServiceStub(channel) response = stub.AgentRegister( request=sdk_service_pb2.AgentRegistrationRequest(), metadata=metadata) logging.info(f"Registration response : {response.status}") app_id = get_app_id(agent_name) if not app_id: logging.error( f'idb does not have the appId for {agent_name} : {app_id}') else: logging.info(f'Got appId {app_id} for {agent_name}') request = sdk_service_pb2.NotificationRegisterRequest( op=sdk_service_pb2.NotificationRegisterRequest.Create) create_subscription_response = stub.NotificationRegister(request=request, metadata=metadata) stream_id = create_subscription_response.stream_id logging.info( f"Create subscription response received. stream_id : {stream_id}") Subscribe_Notifications(stream_id) stream_request = sdk_service_pb2.NotificationStreamRequest( stream_id=stream_id) stream_response = sub_stub.NotificationStream(stream_request, metadata=metadata) file_name = None count = 1 route_count = 0 try: for r in stream_response: logging.info( f"Count :: {count} NOTIFICATION:: \n{r.notification}") count += 1 for obj in r.notification: if obj.HasField( 'config') and obj.config.key.js_path == ".commit.end": logging.info('TO DO -commit.end config') else: file_name, route_count = Handle_Notification( obj, file_name, app_id, route_count) except grpc._channel._Rendezvous as err: logging.info('GOING TO EXIT NOW, DOING FINAL git pull: {}'.format(err)) try: # Need to execute this in the mgmt network namespace, hardcoded name for now git_pull = subprocess.Popen([ '/usr/sbin/ip', 'netns', 'exec', 'srbase-mgmt', '/usr/bin/git', 'pull' ], cwd='/etc/opt/srlinux/appmgr', stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdoutput, stderroutput = git_pull.communicate() logging.info(f'git pull result: {stdoutput} err={stderroutput}') except Exception as e: logging.error(f'Exception caught in git pull :: {e}') except Exception as e: logging.error('Exception caught :: {}'.format(e)) if file_name != None: Update_Result(file_name, action='delete') try: response = stub.AgentUnRegister( request=sdk_service_pb2.AgentRegistrationRequest(), metadata=metadata) logging.error('Run try: Unregister response:: {}'.format(response)) except grpc._channel._Rendezvous as err: logging.info('GOING TO EXIT NOW: {}'.format(err)) sys.exit() return True sys.exit() return True