def vrf_operation(channel, oper): # Create the gRPC stub. stub = sl_route_ipv4_pb2_grpc.SLRoutev4OperStub(channel) # Create the SLVrfRegMsg message used for VRF registrations vrfMsg = sl_route_common_pb2.SLVrfRegMsg() # Create a list to maintain the SLVrfReg objects (in case of batch VRF # registrations) # In this example, we fill in only a single SLVrfReg object vrfList = [] # Create an SLVrfReg object and set its attributes vrfObj = sl_route_common_pb2.SLVrfReg() # Set VRF name. vrfObj.VrfName = 'default' # Set Administrative distance vrfObj.AdminDistance = 2 # Set VRF purge interval vrfObj.VrfPurgeIntervalSeconds = 500 # # Add the registration message to the list # In case of bulk, we can append other VRF objects to the list vrfList.append(vrfObj) # Now that the list is completed, assign it to the SLVrfRegMsg vrfMsg.VrfRegMsgs.extend(vrfList) # Set the Operation vrfMsg.Oper = oper # # Make an RPC call # Timeout = 10 # Seconds response = stub.SLRoutev4VrfRegOp(vrfMsg, Timeout) # # Check the received result from the Server # if (response.StatusSummary.Status == sl_common_types_pb2.SLErrorStatus.SL_SUCCESS): print("VRF %s Success!" %( list(sl_common_types_pb2.SLRegOp.keys())[oper])) else: print("Error code for VRF %s is 0x%x! Response:" % ( list(sl_common_types_pb2.SLRegOp.keys())[oper], response.StatusSummary.Status )) print(response) # If we have partial failures within the batch, let's print them if (response.StatusSummary.Status == sl_common_types_pb2.SLErrorStatus.SL_SOME_ERR): for result in response.Results: print("Error code for %s is 0x%x" %(result.VrfName, result.ErrStatus.Status )) os._exit(0)
def vrf_registration_serializer(batch): """Virtual routing and forwarding serializer.""" serializer = sl_route_common_pb2.SLVrfRegMsg() messages = [] for message in batch['vrfs']: registration_message = sl_route_common_pb2.SLVrfReg() if 'vrf_name' in message: registration_message.VrfName = message['vrf_name'] if 'admin_dist' in message: registration_message.AdminDistance = message['admin_dist'] if 'purge_time' in message: registration_message.VrfPurgeIntervalSeconds = ( message['purge_time']) messages.append(registration_message) serializer.VrfRegMsgs.extend(messages) return serializer