Ejemplo n.º 1
0
    def advertise(self, params):
        templates = params.get("templates") if params.get("templates") else []
        errmsg = ""
        for template in templates:
            vnf_ip = template.get("vnf_ip") + ":50051" if template.get(
                "vnf_ip") else "localhost:50051"
            with grpc.insecure_channel(target=vnf_ip,
                                       options=[("grpc.enable_retries", 0),
                                                ("grpc.keepalive_timeout_ms",
                                                 10000)]) as channel:

                advertises = template.get("advertise_set", [])
                grpc_client = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)
                for advertise in advertises:
                    advertise_req = nfd_agent_pb2.NLSRAdvertiseReq()
                    if advertise.get('mode'):
                        advertise_req.mode = advertise.get('mode')
                    if advertise.get('prefix'):
                        advertise_req.prefix = advertise.get('prefix')
                    if advertise.get('save'):
                        advertise_req.save = advertise.get('save')

                    grpc_res = grpc_client.NLSRAdvertiseName(advertise_req)
                    ack_code = grpc_res.ack_code

                    if ack_code == "err":
                        errmsg = grpc_res.ack_msg
                        break

        return {
            "id": params.get('id'),
            "result": "OK" if not errmsg else "ERROR",
            "errmsg": errmsg
        }
Ejemplo n.º 2
0
    def unset(self, params):
        templates = params.get("templates") if params.get("templates") else []
        errmsg = ""
        for template in templates:
            vnf_ip = template.get("vnf_ip") + ":50051" if template.get(
                "vnf_ip") else "localhost:50051"
            strategy_unset = template.get("strategy_unset", [])
            errmsg = ""
            with grpc.insecure_channel(target=vnf_ip,
                                       options=[("grpc.enable_retries", 0),
                                                ("grpc.keepalive_timeout_ms",
                                                 10000)]) as channel:

                grpc_client = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)
                for strategy in strategy_unset:
                    strategy_req = nfd_agent_pb2.NFDStrategyReq()
                    if strategy.get("prefix"):
                        strategy_req.prefix = strategy.get("prefix")
                        grpc_res = grpc_client.NFDStrategyUnset(strategy_req)
                        ack_code = grpc_res.ack_code

                        if ack_code == "err":
                            errmsg = grpc_res.ack_msg
                            break
        return {
            "vnf_ip": params.get('vnf_ip'),
            "result": "OK" if not errmsg else "ERROR",
            "errmsg": errmsg
        }
Ejemplo n.º 3
0
    def get(self, ip):
        with grpc.insecure_channel(target=ip + ":50051",
                                   options=[("grpc.enable_retries", 0),
                                            ("grpc.keepalive_timeout_ms",
                                             10000)]) as channel:
            grpc_client = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)

            grpc_res = grpc_client.NFDRouteList(
                nfd_agent_pb2.NFDRouteListReq(nexthop='0', origin=''))
            fib_list = grpc_res.route
            result = []

            if grpc_res.ack.ack_code == 'ok':
                for fib in fib_list:
                    prefix, nexthop, cost = ['', '', '']
                    keys = fib.split()
                    for key in keys:
                        if 'prefix=' in key:
                            prefix = key[len('prefix='):]
                        if 'nexthop=' in key:
                            nexthop = key[len('nexthop='):]
                        if 'cost=' in key:
                            cost = key[len('cost='):]
                    result.append({
                        "prefix": prefix,
                        "nexthops": nexthop,
                        "cost": cost
                    })

            return {"fib_list": result}
Ejemplo n.º 4
0
    def delete(self, params):
        templates = params.get("templates") if params.get("templates") else []
        errmsg = ""
        for template in templates:
            vnf_ip = template.get("vnf_ip") + ":50051" if template.get(
                "vnf_ip") else "localhost:50051"
            with grpc.insecure_channel(target=vnf_ip,
                                       options=[("grpc.enable_retries", 0),
                                                ("grpc.keepalive_timeout_ms",
                                                 10000)]) as channel:

                routes = template.get("route_del", [])
                grpc_client = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)
                for route in routes:
                    route_req = nfd_agent_pb2.NFDRouteReq()
                    if route.get("prefix"):
                        route_req.prefix = route.get("prefix")
                    if route.get("nexthop"):
                        route_req.nexthop = route.get("nexthop")

                    grpc_res = grpc_client.NFDRouteRemove(route_req)
                    ack_code = grpc_res.ack_code

                    if ack_code == "err":
                        errmsg = grpc_res.ack_msg
                        break

        return {
            "id": params.get('id'),
            "result": "OK" if not errmsg else "ERROR",
            "errmsg": errmsg
        }
Ejemplo n.º 5
0
def wait_on_client():
    try:
        while True:
            host_name, host_ip = get_Host_name_IP()
            if host_name is None or host_ip is None:
                time.sleep(_MIN_IN_SECONDS)
                continue
            break

        while True:
            mgmt_ip = os.getenv('vICSNF_MGMTIP', '127.0.0.1')
            mgmt_port = os.getenv('vICSNF_MGMTPORT', '50051')
            with grpc.insecure_channel('%s:%s' %
                                       (mgmt_ip, mgmt_port)) as channel:
                try:
                    grpc.channel_ready_future(channel).result(timeout=10)
                except grpc.FutureTimeoutError:
                    log.error("Unable connection to external server")
                    time.sleep(_MIN_IN_SECONDS)
                    continue
                stub = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)

                # https://pythonhosted.org/ifaddr/
                # all interface and ip address
                adapters = ifaddr.get_adapters()
                host_adapters = []
                for adapter in adapters:
                    if adapter.nice_name == 'lo': continue
                    for ip in adapter.ips:
                        if ip.ip == host_ip:
                            host_ifname = adapter.nice_name
                            break
                        elif '::' in ip.ip:
                            continue
                        else:
                            host_adapter = nfd_agent_pb2.HostAdapters(
                                nic_ipaddr=ip.ip,
                                nic_prefix=str(ip.network_prefix),
                                nic_name=adapter.nice_name)
                            host_adapters.append(host_adapter)

                nfdhost = nfd_agent_pb2.NFDHost(name=host_name,
                                                ipaddr=host_ip,
                                                ifname=host_ifname,
                                                host_adapters=host_adapters)

                ack_reply = stub.NFDHostNotify(nfdhost)
                if ack_reply.ack_code is None or ack_reply.ack_code != 'ok':
                    continue
            log.info("NFDHostNotify is %s" % ack_reply.ack_code)
            break
    except KeyboardInterrupt:
        sys.exit('Error connecting to server')
Ejemplo n.º 6
0
def client_to_agent(command_opt, **kwargs):
    with grpc.insecure_channel(target='localhost:50051',
                               options=[('grpc.enable_retries', 0),
                                        ('grpc.keepalive_timeout_ms', 10000)
                                        ]) as channel:
        stub = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)

        #############################
        ## face command
        if command_opt == 'face list':
            nfd_face_list(nfd_stup=stub, **kwargs)
        elif command_opt == 'face show':
            nfd_face_list(nfd_stup=stub, **kwargs)
        elif command_opt == 'face create':
            nfd_face_create(nfd_stup=stub, **kwargs)
        elif command_opt == 'face destroy':
            nfd_face_destroy(nfd_stup=stub, **kwargs)

        #############################
        ## fib command
        elif command_opt == 'fib list':
            nfd_fib_list(nfd_stup=stub, **kwargs)

        #############################
        ## route command
        elif command_opt == 'route list':
            nfd_route_list(nfd_stup=stub, **kwargs)
        elif command_opt == 'route show':
            nfd_route_show(nfd_stup=stub, **kwargs)
        elif command_opt == 'route add':
            nfd_route_add(nfd_stup=stub, **kwargs)
        elif command_opt == 'route remove':
            nfd_route_remove(nfd_stup=stub, **kwargs)

        #############################
        ## status command
        elif command_opt == 'status report':
            nfd_status_report(nfd_stup=stub, **kwargs)

        #############################
        ## status command
        elif command_opt == 'strategy list':
            nfd_strategy_list(nfd_stup=stub, **kwargs)
        elif command_opt == 'strategy show':
            nfd_strategy_show(nfd_stup=stub, **kwargs)
        elif command_opt == 'strategy set':
            nfd_strategy_set(nfd_stup=stub, **kwargs)
        elif command_opt == 'strategy unset':
            nfd_strategy_unset(nfd_stup=stub, **kwargs)

        else:
            print("Currently, %s command is not support" % command_opt)
Ejemplo n.º 7
0
    def delete(self, params):
        #vnfs = json.loads(params.get("vnfs")) if params.get("vnfs") else []
        vnfs = params.get("vnfs") if params.get("vnfs") else []
        result_vnfs = []
        for i in vnfs:

            faces = i.get("faces") if i.get("faces") else []
            faces_deleted = []
            vnf_address = i.get("vnf_address") + ":50051" if i.get(
                "vnf_address") else "localhost:50051"

            with grpc.insecure_channel(target=vnf_address,
                                       options=[("grpc.enable_retries", 0),
                                                ("grpc.keepalive_timeout_ms",
                                                 10000)]) as channel:
                grpc_client = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)
                for face in faces:
                    face_delete_req = nfd_agent_pb2.NFDFaceIDReq()
                    face_delete_req.faceid = int(face.get("link_id"))

                    if face.get("neighbor_hostname"):
                        face_delete_req.neighbor_hostname = face.get(
                            "neighbor_hostname")
                    if face.get("neighbor_ip"):
                        face_delete_req.neighbor_ip = face.get("neighbor_ip")
                    if face.get("neighbor_site_route"):
                        face_delete_req.neighbor_site_route = face.get(
                            "neighbor_site_route")

                    grpc_res = grpc_client.NFDFaceDestroy(face_delete_req)
                    ack_msg = grpc_res.ack_msg
                    face_id = re.findall("id=(.*?) ", ack_msg)
                    faces_deleted.append({
                        "faceid": face_id,
                        "status": "deleted"
                    })

            result_vnfs.append({
                "vnf_name": i.get("vnf_name"),
                "vnf_address": i.get("vnf_address"),
                "faces": faces_deleted
            })

        return {
            "id": params.get('id'),
            "vnfs": result_vnfs,
            "result": "OK",
            "errmsg": ""
        }
Ejemplo n.º 8
0
    def get(self, ip):
        with grpc.insecure_channel(target=ip + ":50051",
                                   options=[("grpc.enable_retries", 0),
                                            ("grpc.keepalive_timeout_ms",
                                             10000)]) as channel:
            grpc_client = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)

            grpc_res = grpc_client.NLSRLsdbList(empty_pb2.Empty())
            lsdbs = grpc_res.lsdbs
            result = []

            if grpc_res.ack.ack_code == 'ok':
                result = list(
                    map(
                        lambda lsdb: {
                            "origin_router": lsdb.origin_router,
                            "prefix": lsdb.prefix
                        }, lsdbs))

            return {"lsdb_list": result}
Ejemplo n.º 9
0
    def get(self, ip):
        with grpc.insecure_channel(target=ip + ":50051",
                                   options=[("grpc.enable_retries", 0),
                                            ("grpc.keepalive_timeout_ms",
                                             10000)]) as channel:
            grpc_client = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)

            grpc_res = grpc_client.NFDFaceList(
                nfd_agent_pb2.NFDFaceIDReq(faceid=0))
            faces = grpc_res.faces
            result = []

            if grpc_res.ack.ack_code == 'ok':
                result = list(
                    map(
                        lambda f: {
                            "faceid": f.faceid,
                            "remote": f.remote,
                            "local": f.local
                        }, faces))

            return {"faces": result}
Ejemplo n.º 10
0
    def get(self, ip):
        with grpc.insecure_channel(target=ip + ":50051",
                                   options=[("grpc.enable_retries", 0),
                                            ("grpc.keepalive_timeout_ms",
                                             10000)]) as channel:
            grpc_client = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)

            grpc_res = grpc_client.NFDStrategyList(empty_pb2.Empty())
            strategy_list = grpc_res.strategies
            result = []

            if grpc_res.ack.ack_code == 'ok':
                for item in strategy_list:
                    prefix, strategy = ['', '']
                    keys = item.split()
                    for key in keys:
                        if 'prefix=' in key:
                            prefix = key[len('prefix='):]
                        if 'strategy=' in key:
                            strategy = key[len('strategy='):]
                    result.append({"prefix": prefix, "strategy": strategy})

            return {"strategy_list": result}
Ejemplo n.º 11
0
    def create(self, params):
        #vnfs = json.loads(params.get("vnfs")) if params.get("vnfs") else []
        vnfs = params.get("vnfs") if params.get("vnfs") else []
        result_vnfs = []

        for i in vnfs:
            faces = i.get("faces") if i.get("faces") else []
            faces_created = []
            vnf_address = i.get("vnf_address") + ":50051" if i.get(
                "vnf_address") else "localhost:50051"
            with grpc.insecure_channel(target=vnf_address,
                                       options=[("grpc.enable_retries", 0),
                                                ("grpc.keepalive_timeout_ms",
                                                 10000)]) as channel:
                grpc_client = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)
                for face in faces:
                    face_create_req = nfd_agent_pb2.NFDFaceCreateReq()
                    if face.get("remote"):
                        face_create_req.remote = face.get("remote")
                    if face.get("persistency"):
                        face_create_req.persistency = face.get("persistency")
                    if face.get("local"):
                        face_create_req.local = face.get("local")
                    if face.get("reliability"):
                        face_create_req.reliability = face.get("reliability")
                    if face.get("congestion_marking"):
                        face_create_req.congestion_marking = face.get(
                            "congestion_marking")
                    if face.get("congestion_marking_interval"):
                        face_create_req.congestion_marking_interval = face.get(
                            "congestion_marking_interval")
                    if face.get("congestion_marking_interval"):
                        face_create_req.congestion_marking_interval = face.get(
                            "congestion_marking_interval")
                    if face.get("default_congestion_threshold"):
                        face_create_req.default_congestion_threshold = face.get(
                            "default_congestion_threshold")
                    if face.get("mtu"):
                        face_create_req.mtu = face.get("mtu")
                    if face.get("neighbor_hostname"):
                        face_create_req.neighbor_hostname = face.get(
                            "neighbor_hostname")
                    if face.get("neighbor_ip"):
                        face_create_req.neighbor_ip = face.get("neighbor_ip")
                    if face.get("neighbor_site_route"):
                        face_create_req.neighbor_site_route = face.get(
                            "neighbor_site_route")

                    #print(face_create_req)
                    grpc_res = grpc_client.NFDFaceCreate(face_create_req)
                    #print(grpc_res)

                    ack_msg = grpc_res.ack_msg
                    face_id = re.findall("id=(.*?) ", ack_msg)
                    faces_created.append({
                        "remote": i.get("remote"),
                        "faceid": face_id
                    })

            result_vnfs.append({
                "vnf_name": i.get("vnf_name"),
                "vnf_address": i.get("vnf_address"),
                "faces": faces_created
            })

        return {
            "id": params.get('id'),
            "vnfs": result_vnfs,
            "result": "OK",
            "errmsg": ""
        }
Ejemplo n.º 12
0
 def __init__(self, nfd_agent_ip):
     with grpc.insecure_channel(target=nfd_agent_ip,
                                options=[("grpc.enable_retries", 0),
                                         ("grpc.keepalive_timeout_ms",
                                          10000)]) as channel:
         self.stub = nfd_agent_pb2_grpc.NFDRouterAgentStub(channel)