Пример #1
0
    def vrf_operation(self, oper):
        # Create the gRPC stub.
        stub = sl_route_ipv4_pb2.beta_create_SLRoutev4Oper_stub(self.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 = self.vrf
        # Set Administrative distance
        vrfObj.AdminDistance = 1
        # 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):
            logger.debug("VRF %s Success!" %
                         (sl_common_types_pb2.SLRegOp.keys()[oper]))
        else:
            logger.debug("Error code for VRF %s is 0x%x! Response:" %
                         (sl_common_types_pb2.SLRegOp.keys()[oper],
                          response.StatusSummary.Status))
            logger.debug(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:
                logger.debug("Error code for %s is 0x%x" %
                             (result.VrfName, result.ErrStatus.Status))
Пример #2
0
    def slv4_rtbatch_send(self, route_batch, event):

        logger.info("Got a Route List!")
        # Create the gRPC stub for v4 routemsg
        stub = sl_route_ipv4_pb2.beta_create_SLRoutev4Oper_stub(self.channel)

        # Create the SLRoutev4Msg message holding the SLRoutev4 object list
        rtMsg = sl_route_ipv4_pb2.SLRoutev4Msg()

        rtMsg.VrfName = self.vrf

        if event == 'add':
            oper = sl_common_types_pb2.SL_OBJOP_ADD
        elif event == 'delete':
            oper = sl_common_types_pb2.SL_OBJOP_DELETE
        elif event == 'update':
            oper = sl_common_types_pb2.SL_OBJOP_UPDATE
        else:
            return
        rtMsg.Routes.extend(route_batch)

        logger.debug(rtMsg)
        #
        # Make an RPC call
        #
        Timeout = 10  # Seconds
        rtMsg.Oper = oper  # Desired ADD, UPDATE, DELETE operation
        response = stub.SLRoutev4Op(rtMsg, Timeout)
        #
        # Check the received result from the Server
        #

        if (sl_common_types_pb2.SLErrorStatus.SL_SUCCESS ==
                response.StatusSummary.Status):
            logger.debug("Route %s Success!" %
                         (sl_common_types_pb2.SLObjectOp.keys()[oper]))
        else:
            logger.error("Error code for route %s is 0x%x" %
                         (sl_common_types_pb2.SLObjectOp.keys()[oper],
                          response.StatusSummary.Status))
            # 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:
                    logger.debug("Error code for %s/%d is 0x%x" %
                                 (str(ipaddress.ip_address(result.Prefix)),
                                  result.PrefixLen, result.ErrStatus.Status))
Пример #3
0
    def prefix_in_rib(self, route):

        # Set up the stub to query Routes
        stub = sl_route_ipv4_pb2.beta_create_SLRoutev4Oper_stub(self.channel)

        # Create the container to be used in the query message
        serializer = sl_route_ipv4_pb2.SLRoutev4GetMsg()

        # IP Prefix
        prefix = int(ipaddress.ip_address(self.plugin.get_route_prefix(route)))
        # Prefix Length
        prefixLen = self.plugin.get_route_prefixlen(route)

        serializer.Prefix = int(ipaddress.ip_address(prefix))
        serializer.PrefixLen = int(prefixLen)
        serializer.EntriesCount = 1
        serializer.GetNext = False
        serializer.VrfName = self.vrf

        Timeout = 10

        response = stub.SLRoutev4Get(serializer, Timeout)

        if not response.ErrStatus.Status:
            # Check if route is present in the response
            for elem in response.Entries:
                if self.plugin.get_route_family(route) == AF_INET:
                    addr = int(ipaddress.ip_address(elem.Prefix))
                    if prefix == addr:
                        logger.debug(
                            "Route present in application RIB already")
                        return response, True
                    else:
                        return response, False
        else:
            logger.error("Error fetching route from RIB: 0x%x" %
                         (response.ErrStatus.Status))
            # 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:
                    logger.debug("Error code for %s/%d is 0x%x" %
                                 (str(ipaddress.ip_address(result.Prefix)),
                                  result.PrefixLen, result.ErrStatus.Status))

        return response, False
Пример #4
0
 def __init__(self, host, port, channel_credentials=None):
     if channel_credentials is None:
         # Instantiate insecure channel object.
         channel = implementations.insecure_channel(host, port)
     else:
         # Instantiate secure channel object.
         channel = implementations.secure_channel(host, port,
                                                  channel_credentials)
     self._stubs = (
         # 0
         sl_route_ipv4_pb2.beta_create_SLRoutev4Oper_stub(channel),
         # 1
         sl_route_ipv6_pb2.beta_create_SLRoutev6Oper_stub(channel),
         # 2
         sl_global_pb2.beta_create_SLGlobal_stub(channel),
         # 3
         sl_mpls_pb2.beta_create_SLMplsOper_stub(channel),
         # 4
         sl_bfd_ipv4_pb2.beta_create_SLBfdv4Oper_stub(channel),
         # 5
         sl_bfd_ipv6_pb2.beta_create_SLBfdv6Oper_stub(channel),
         # 6
         sl_interface_pb2.beta_create_SLInterfaceOper_stub(channel),
     )
Пример #5
0
def route_operation(channel, oper):
    # Create the gRPC stub.
    stub = sl_route_ipv4_pb2.beta_create_SLRoutev4Oper_stub(channel)

    # Create an empty list of routes.
    routeList = []

    # Create the SLRoutev4Msg message holding the SLRoutev4 object list
    rtMsg = sl_route_ipv4_pb2.SLRoutev4Msg()

    # Fill in the message attributes attributes.
    # VRF Name
    rtMsg.VrfName = 'default'

    # Fill in the routes
    for i in range(10):
        #
        # Create an SLRoutev4 object and set its attributes
        #
        route = sl_route_ipv4_pb2.SLRoutev4()
        # IP Prefix
        route.Prefix = (int(ipaddress.ip_address('20.0.' + str(i) + '.0')))
        # Prefix Length
        route.PrefixLen = 24

        # Administrative distance
        route.RouteCommon.AdminDistance = 2

        #
        # Set the route's paths.
        # A Route might have one or many paths
        #
        # Create an empty list of paths as a placeholder for these paths
        paths = []

        # Create an SLRoutePath path object.
        path = sl_route_common_pb2.SLRoutePath()
        # Fill in the path attributes.
        # Path next hop address
        path.NexthopAddress.V4Address = (int(
            ipaddress.ip_address('10.10.10.1')))
        # Next hop interface name
        path.NexthopInterface.Name = 'GigabitEthernet0/0/0/0'

        #
        # Add the path to the list
        #
        paths.append(path)

        # Let's create another path as equal cost multi-path (ECMP)
        path = sl_route_common_pb2.SLRoutePath()
        path.NexthopAddress.V4Address = (int(
            ipaddress.ip_address('10.10.10.2')))
        path.NexthopInterface.Name = 'GigabitEthernet0/0/0/0'

        #
        # Add the path to the list
        #
        paths.append(path)

        #
        # Assign the paths to the route object
        # Note: Route Delete operations do not require the paths
        #
        if oper != sl_common_types_pb2.SL_OBJOP_DELETE:
            route.PathList.extend(paths)

        #
        # Append the route to the route list (bulk routes)
        #
        routeList.append(route)

    #
    # Done building the routeList, assign it to the route message.
    #
    rtMsg.Routes.extend(routeList)

    #
    # Make an RPC call
    #
    Timeout = 10  # Seconds
    rtMsg.Oper = oper  # Desired ADD, UPDATE, DELETE operation
    response = stub.SLRoutev4Op(rtMsg, Timeout)

    #
    # Check the received result from the Server
    #
    if (sl_common_types_pb2.SLErrorStatus.SL_SUCCESS ==
            response.StatusSummary.Status):
        print "Route %s Success!" % (
            sl_common_types_pb2.SLObjectOp.keys()[oper])
    else:
        print "Error code for route %s is 0x%x" % (
            sl_common_types_pb2.SLObjectOp.keys()[oper],
            response.StatusSummary.Status)
        # 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/%d is 0x%x" % (
                    str(ipaddress.ip_address(result.Prefix)), result.PrefixLen,
                    result.ErrStatus.Status)
        os._exit(0)
Пример #6
0
def route_operation(channel, oper, loop_ip):
    # Create the gRPC stub.
    stub = sl_route_ipv4_pb2.beta_create_SLRoutev4Oper_stub(channel)

    # Create an empty list of routes.
    routeList = []

    # Create the SLRoutev4Msg message holding the SLRoutev4 object list
    rtMsg = sl_route_ipv4_pb2.SLRoutev4Msg()

    rtMsg.VrfName = 'default'

    route = sl_route_ipv4_pb2.SLRoutev4()
    # IP Prefix
    route.Prefix = (int(ipaddress.ip_address(loop_ip[0])))
    # Prefix Length
    route.PrefixLen = 32
    # Administrative distance
    route.RouteCommon.AdminDistance = 0

    paths = []

    # Create an SLRoutePath path object.
    path = sl_route_common_pb2.SLRoutePath()
    # Fill in the path attributes.
    # Path next hop address
    path.NexthopAddress.V4Address = (int(ipaddress.ip_address(loop_ip[1])))
    # Next hop interface name
    path.NexthopInterface.Name = loop_ip[2]

        #
        # Add the path to the list
        #
    paths.append(path)
    if oper != sl_common_types_pb2.SL_OBJOP_DELETE:
        route.PathList.extend(paths)
        # Append the route to the route list (bulk routes)
    routeList.append(route)

    #
    # Done building the routeList, assign it to the route message.
    #
    rtMsg.Routes.extend(routeList)

    #
    # Make an RPC call
    #
    Timeout = 10 # Seconds
    rtMsg.Oper = oper # Desired ADD, UPDATE, DELETE operation
    response = stub.SLRoutev4Op(rtMsg, Timeout)

    #
    # Check the received result from the Server
    # 
    if (sl_common_types_pb2.SLErrorStatus.SL_SUCCESS == 
            response.StatusSummary.Status):
        print "Route %s Success!" %(
            sl_common_types_pb2.SLObjectOp.keys()[oper])
    else:
        print "Error code for route %s is 0x%x" % (
            sl_common_types_pb2.SLObjectOp.keys()[oper],
            response.StatusSummary.Status
        )
        # 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/%d is 0x%x" %(
                    str(ipaddress.ip_address(result.Prefix)),
                    result.PrefixLen,
                    result.ErrStatus.Status
                )
        os._exit(0)