Search in sources :

Example 1 with OpenstackNetworkAdminService

use of org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService in project onos by opennetworkinglab.

the class OpenstackDiffStateCommand method doExecute.

@Override
protected void doExecute() {
    OpenstackSecurityGroupAdminService osSgService = get(OpenstackSecurityGroupAdminService.class);
    OpenstackNetworkAdminService osNetService = get(OpenstackNetworkAdminService.class);
    OpenstackRouterAdminService osRouterService = get(OpenstackRouterAdminService.class);
    OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
    Map<String, String> headerMap = new HashMap<>();
    headerMap.put(HTTP_HEADER_ACCEPT, HTTP_HEADER_VALUE_JSON);
    Optional<OpenstackNode> node = osNodeService.nodes(CONTROLLER).stream().findFirst();
    if (!node.isPresent()) {
        error("Keystone auth info has not been configured. " + "Please specify auth info via network-cfg.json.");
        return;
    }
    OSClient osClient = OpenstackNetworkingUtil.getConnectedClient(node.get());
    if (osClient == null) {
        return;
    }
    if (!clear) {
        show = true;
    }
    if (show && clear) {
        error("Either --show (-s) or --clear (-c) option should be specified.");
    }
    if (show) {
        print("\nComparing OpenStack floating IPs");
    } else if (clear) {
        print("\nClearing OpenStack floating IPs");
    }
    Set<String> cachedFips = osRouterService.floatingIps().stream().map(NetFloatingIP::getId).collect(Collectors.toSet());
    Set<String> osFips = osClient.headers(headerMap).networking().floatingip().list().stream().map(NetFloatingIP::getId).collect(Collectors.toSet());
    print(FLOATING_IP_FORMAT, "ID", "Floating IP", "Fixed IP");
    getDiff(cachedFips, osFips).forEach(fipId -> {
        printFloatingIp(osRouterService.floatingIp(fipId));
        if (clear) {
            osRouterService.removeFloatingIp(fipId);
        }
    });
    Set<String> cachedPorts = osNetService.ports().stream().map(IdEntity::getId).collect(Collectors.toSet());
    Set<String> osPorts = osClient.headers(headerMap).networking().port().list().stream().map(IdEntity::getId).collect(Collectors.toSet());
    if (show) {
        print("\nComparing OpenStack router interfaces");
    } else if (clear) {
        print("\nClearing OpenStack router interfaces");
    }
    print(ROUTER_INTF_FORMAT, "ID", "Tenant ID", "Subnet ID");
    getDiff(cachedPorts, osPorts).forEach(portId -> {
        RouterInterface ri = osRouterService.routerInterface(portId);
        if (ri != null) {
            printRouterIntf(ri);
            if (clear) {
                osRouterService.removeRouterInterface(portId);
            }
        }
    });
    if (show) {
        print("\nComparing OpenStack ports");
    } else if (clear) {
        print("\nClearing OpenStack ports");
    }
    print(PORT_FORMAT, "ID", "Network", "MAC", "Fixed IPs");
    getDiff(cachedPorts, osPorts).forEach(portId -> {
        printPort(osNetService.port(portId), osNetService);
        if (clear) {
            osNetService.removePort(portId);
        }
    });
    if (show) {
        print("\nComparing OpenStack routers");
    } else if (clear) {
        print("\nClearing OpenStack routers");
    }
    Set<String> cachedRouters = osRouterService.routers().stream().map(IdEntity::getId).collect(Collectors.toSet());
    Set<String> osRouters = osClient.headers(headerMap).networking().router().list().stream().map(IdEntity::getId).collect(Collectors.toSet());
    print(ROUTER_FORMAT, "ID", "Name", "External", "Internal");
    getDiff(cachedRouters, osRouters).forEach(routerId -> {
        printRouter(osRouterService.router(routerId), osNetService);
        if (clear) {
            osRouterService.removeRouter(routerId);
        }
    });
    if (show) {
        print("\nComparing OpenStack subnets");
    } else if (clear) {
        print("\nClearing OpenStack subnets");
    }
    Set<String> cachedSubnets = osNetService.subnets().stream().map(IdEntity::getId).collect(Collectors.toSet());
    Set<String> osSubnets = osClient.headers(headerMap).networking().subnet().list().stream().map(IdEntity::getId).collect(Collectors.toSet());
    print(SUBNET_FORMAT, "ID", "Network", "CIDR");
    getDiff(cachedSubnets, osSubnets).forEach(subnetId -> {
        printSubnet(osNetService.subnet(subnetId), osNetService);
        if (clear) {
            osNetService.removeSubnet(subnetId);
        }
    });
    if (show) {
        print("\nComparing OpenStack networks");
    } else if (clear) {
        print("\nClearing OpenStack networks");
    }
    Set<String> cachedNets = osNetService.networks().stream().map(IdEntity::getId).collect(Collectors.toSet());
    Set<String> osNets = osClient.headers(headerMap).networking().network().list().stream().map(IdEntity::getId).collect(Collectors.toSet());
    print(NETWORK_FORMAT, "ID", "Name", "VNI", "Subnets");
    getDiff(cachedNets, osNets).forEach(netId -> {
        printNetwork(osNetService.network(netId));
        if (clear) {
            osNetService.removeNetwork(netId);
        }
    });
    if (show) {
        print("\nComparing OpenStack security groups");
    } else if (clear) {
        print("\nClearing OpenStack security groups");
    }
    Set<String> cachedSgs = osSgService.securityGroups().stream().map(IdEntity::getId).collect(Collectors.toSet());
    Set<String> osSgs = osClient.headers(headerMap).networking().securitygroup().list().stream().map(IdEntity::getId).collect(Collectors.toSet());
    print(SECURITY_GROUP_FORMAT, "ID", "Name");
    getDiff(cachedSgs, osSgs).forEach(sgId -> {
        printSecurityGroup(osSgService.securityGroup(sgId));
        if (clear) {
            osSgService.removeSecurityGroup(sgId);
        }
    });
}
Also used : OpenstackNodeService(org.onosproject.openstacknode.api.OpenstackNodeService) HashMap(java.util.HashMap) OpenstackRouterAdminService(org.onosproject.openstacknetworking.api.OpenstackRouterAdminService) OSClient(org.openstack4j.api.OSClient) RouterInterface(org.openstack4j.model.network.RouterInterface) OpenstackNode(org.onosproject.openstacknode.api.OpenstackNode) OpenstackSecurityGroupAdminService(org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService) OpenstackNetworkAdminService(org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService)

Example 2 with OpenstackNetworkAdminService

use of org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService in project onos by opennetworkinglab.

the class OpenstackSyncStateCommand method doExecute.

@Override
protected void doExecute() {
    OpenstackSecurityGroupAdminService osSgAdminService = get(OpenstackSecurityGroupAdminService.class);
    OpenstackNetworkAdminService osNetAdminService = get(OpenstackNetworkAdminService.class);
    OpenstackRouterAdminService osRouterAdminService = get(OpenstackRouterAdminService.class);
    OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
    Map<String, String> headerMap = new HashMap<>();
    headerMap.put(HTTP_HEADER_ACCEPT, HTTP_HEADER_VALUE_JSON);
    Optional<OpenstackNode> node = osNodeService.nodes(CONTROLLER).stream().findFirst();
    if (!node.isPresent()) {
        error("Keystone auth info has not been configured. " + "Please specify auth info via network-cfg.json.");
        return;
    }
    OSClient osClient = OpenstackNetworkingUtil.getConnectedClient(node.get());
    if (osClient == null) {
        return;
    }
    print("Synchronizing OpenStack security groups");
    print(SECURITY_GROUP_FORMAT, "ID", "Name");
    osClient.headers(headerMap).networking().securitygroup().list().forEach(osSg -> {
        if (osSgAdminService.securityGroup(osSg.getId()) != null) {
            osSgAdminService.updateSecurityGroup(osSg);
        } else {
            osSgAdminService.createSecurityGroup(osSg);
        }
        printSecurityGroup(osSg);
    });
    print("\nSynchronizing OpenStack networks");
    print(NETWORK_FORMAT, "ID", "Name", "VNI", "Subnets");
    osClient.headers(headerMap).networking().network().list().forEach(osNet -> {
        if (osNetAdminService.network(osNet.getId()) != null) {
            osNetAdminService.updateNetwork(osNet);
        } else {
            osNetAdminService.createNetwork(osNet);
        }
        printNetwork(osNet);
    });
    print("\nSynchronizing OpenStack subnets");
    print(SUBNET_FORMAT, "ID", "Network", "CIDR");
    osClient.headers(headerMap).networking().subnet().list().forEach(osSubnet -> {
        if (osNetAdminService.subnet(osSubnet.getId()) != null) {
            osNetAdminService.updateSubnet(osSubnet);
        } else {
            osNetAdminService.createSubnet(osSubnet);
        }
        printSubnet(osSubnet, osNetAdminService);
    });
    print("\nSynchronizing OpenStack ports");
    print(PORT_FORMAT, "ID", "Network", "MAC", "Fixed IPs");
    osClient.headers(headerMap).networking().port().list().forEach(osPort -> {
        if (osNetAdminService.port(osPort.getId()) != null) {
            osNetAdminService.updatePort(osPort);
        } else {
            osNetAdminService.createPort(osPort);
        }
        printPort(osPort, osNetAdminService);
    });
    print("\nSynchronizing OpenStack routers");
    print(ROUTER_FORMAT, "ID", "Name", "External", "Internal");
    osClient.headers(headerMap).networking().router().list().forEach(osRouter -> {
        if (osRouterAdminService.router(osRouter.getId()) != null) {
            osRouterAdminService.updateRouter(osRouter);
        } else {
            osRouterAdminService.createRouter(osRouter);
        }
        // FIXME do we need to manage router interfaces separately?
        osNetAdminService.ports().stream().filter(osPort -> Objects.equals(osPort.getDeviceId(), osRouter.getId()) && Objects.equals(osPort.getDeviceOwner(), DEVICE_OWNER_IFACE)).forEach(osPort -> addRouterIface(osPort, osRouterAdminService));
        printRouter(osRouter, osNetAdminService);
    });
    print("\nSynchronizing OpenStack floating IPs");
    print(FLOATING_IP_FORMAT, "ID", "Floating IP", "Fixed IP");
    osClient.headers(headerMap).networking().floatingip().list().forEach(osFloating -> {
        if (osRouterAdminService.floatingIp(osFloating.getId()) != null) {
            osRouterAdminService.updateFloatingIp(osFloating);
        } else {
            osRouterAdminService.createFloatingIp(osFloating);
        }
        printFloatingIp(osFloating);
    });
}
Also used : OpenstackNetworkingUtil.printPort(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.printPort) OpenstackNode(org.onosproject.openstacknode.api.OpenstackNode) OSClient(org.openstack4j.api.OSClient) ROUTER_FORMAT(org.onosproject.openstacknetworking.api.Constants.ROUTER_FORMAT) OpenstackNetworkingUtil(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil) OpenstackNetworkingUtil.printNetwork(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.printNetwork) OpenstackRouterAdminService(org.onosproject.openstacknetworking.api.OpenstackRouterAdminService) HashMap(java.util.HashMap) Command(org.apache.karaf.shell.api.action.Command) CONTROLLER(org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER) OpenstackSecurityGroupAdminService(org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService) OpenstackNetworkingUtil.printFloatingIp(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.printFloatingIp) OpenstackNetworkingUtil.printSubnet(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.printSubnet) NETWORK_FORMAT(org.onosproject.openstacknetworking.api.Constants.NETWORK_FORMAT) OpenstackNetworkingUtil.addRouterIface(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.addRouterIface) OpenstackNetworkAdminService(org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService) Map(java.util.Map) FLOATING_IP_FORMAT(org.onosproject.openstacknetworking.api.Constants.FLOATING_IP_FORMAT) SUBNET_FORMAT(org.onosproject.openstacknetworking.api.Constants.SUBNET_FORMAT) PORT_FORMAT(org.onosproject.openstacknetworking.api.Constants.PORT_FORMAT) OpenstackNodeService(org.onosproject.openstacknode.api.OpenstackNodeService) OpenstackNetworkingUtil.printSecurityGroup(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.printSecurityGroup) SECURITY_GROUP_FORMAT(org.onosproject.openstacknetworking.api.Constants.SECURITY_GROUP_FORMAT) Objects(java.util.Objects) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Optional(java.util.Optional) OpenstackNetworkingUtil.printRouter(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.printRouter) OpenstackNodeService(org.onosproject.openstacknode.api.OpenstackNodeService) HashMap(java.util.HashMap) OpenstackRouterAdminService(org.onosproject.openstacknetworking.api.OpenstackRouterAdminService) OSClient(org.openstack4j.api.OSClient) OpenstackNode(org.onosproject.openstacknode.api.OpenstackNode) OpenstackSecurityGroupAdminService(org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService) OpenstackNetworkAdminService(org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService)

Example 3 with OpenstackNetworkAdminService

use of org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService in project onos by opennetworkinglab.

the class UpdateExternalPeerRouterVlanCommand method doExecute.

@Override
protected void doExecute() {
    OpenstackNetworkAdminService osNetAdminService = get(OpenstackNetworkAdminService.class);
    OpenstackRouterService osRouterService = get(OpenstackRouterService.class);
    IpAddress externalPeerIpAddress = IpAddress.valueOf(IpAddress.Version.INET, Ip4Address.valueOf(ipAddress).toOctets());
    if (osNetAdminService.externalPeerRouters().isEmpty()) {
        print(NO_ELEMENT);
        return;
    } else if (osNetAdminService.externalPeerRouters().stream().noneMatch(router -> router.ipAddress().toString().equals(ipAddress))) {
        print(NO_ELEMENT);
        return;
    }
    Subnet subnet = osNetAdminService.subnets().stream().filter(s -> s.getGateway().equals(ipAddress)).findAny().orElse(null);
    if (subnet == null) {
        return;
    }
    Network network = osNetAdminService.network(subnet.getNetworkId());
    if (network == null) {
        return;
    }
    Router router = osRouterService.routers().stream().filter(r -> r.getExternalGatewayInfo().getNetworkId().equals(network.getId())).findAny().orElse(null);
    if (router == null) {
        return;
    }
    try {
        if (vlanId.equals(NONE)) {
            osNetAdminService.updateExternalPeerRouterVlan(externalPeerIpAddress, VlanId.NONE);
            osNetAdminService.deriveExternalPeerRouterMac(router.getExternalGatewayInfo(), router, VlanId.NONE);
        } else {
            osNetAdminService.updateExternalPeerRouterVlan(externalPeerIpAddress, VlanId.vlanId(vlanId));
            osNetAdminService.deriveExternalPeerRouterMac(router.getExternalGatewayInfo(), router, VlanId.vlanId(vlanId));
        }
    } catch (IllegalArgumentException e) {
        log.error("Exception occurred because of {}", e.toString());
    }
    print(FORMAT, "Router IP", "Mac Address", "VLAN ID");
    List<ExternalPeerRouter> routers = Lists.newArrayList(osNetAdminService.externalPeerRouters());
    for (ExternalPeerRouter r : routers) {
        print(FORMAT, r.ipAddress(), r.macAddress().toString(), r.vlanId());
    }
}
Also used : Ip4Address(org.onlab.packet.Ip4Address) OpenstackRouterService(org.onosproject.openstacknetworking.api.OpenstackRouterService) Network(org.openstack4j.model.network.Network) VlanId(org.onlab.packet.VlanId) Argument(org.apache.karaf.shell.api.action.Argument) Subnet(org.openstack4j.model.network.Subnet) AbstractShellCommand.get(org.onosproject.cli.AbstractShellCommand.get) Command(org.apache.karaf.shell.api.action.Command) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) List(java.util.List) Lists(com.google.common.collect.Lists) OpenstackNetworkAdminService(org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Router(org.openstack4j.model.network.Router) Completion(org.apache.karaf.shell.api.action.Completion) ExternalPeerRouter(org.onosproject.openstacknetworking.api.ExternalPeerRouter) IpAddress(org.onlab.packet.IpAddress) ExternalPeerRouter(org.onosproject.openstacknetworking.api.ExternalPeerRouter) Network(org.openstack4j.model.network.Network) Router(org.openstack4j.model.network.Router) ExternalPeerRouter(org.onosproject.openstacknetworking.api.ExternalPeerRouter) IpAddress(org.onlab.packet.IpAddress) Subnet(org.openstack4j.model.network.Subnet) OpenstackNetworkAdminService(org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService) OpenstackRouterService(org.onosproject.openstacknetworking.api.OpenstackRouterService)

Example 4 with OpenstackNetworkAdminService

use of org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService in project onos by opennetworkinglab.

the class DeleteExternalPeerRouterCommand method doExecute.

@Override
protected void doExecute() {
    OpenstackNetworkAdminService service = get(OpenstackNetworkAdminService.class);
    if (service.externalPeerRouters().stream().noneMatch(router -> router.ipAddress().toString().equals(ipAddress))) {
        print(NO_ELEMENT);
        return;
    }
    try {
        service.deleteExternalPeerRouter(ipAddress);
    } catch (IllegalArgumentException e) {
        log.error("Exception occurred because of {}", e);
    }
    print(FORMAT, "Router IP", "Mac Address", "VLAN ID");
    List<ExternalPeerRouter> routers = Lists.newArrayList(service.externalPeerRouters());
    for (ExternalPeerRouter router : routers) {
        print(FORMAT, router.ipAddress(), router.macAddress().toString(), router.vlanId());
    }
}
Also used : ExternalPeerRouter(org.onosproject.openstacknetworking.api.ExternalPeerRouter) OpenstackNetworkAdminService(org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService)

Example 5 with OpenstackNetworkAdminService

use of org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService in project onos by opennetworkinglab.

the class OpenstackFlowTraceCommand method doExecute.

@Override
protected void doExecute() {
    OpenstackNodeAdminService osNodeService = get(OpenstackNodeAdminService.class);
    InstancePortAdminService instancePortService = get(InstancePortAdminService.class);
    OpenstackNetworkAdminService osNetService = get(OpenstackNetworkAdminService.class);
    Optional<InstancePort> srcInstance = instancePortService.instancePorts().stream().filter(port -> port.ipAddress().toString().equals(srcIp)).findAny();
    if (!srcInstance.isPresent()) {
        print(NO_ELEMENT);
        return;
    }
    OpenstackNode srcNode = osNodeService.node(srcInstance.get().deviceId());
    if (srcNode == null || srcNode.sshAuthInfo() == null) {
        log.error("Openstack node {} is null or has no SSH authentication information.\n" + " Please refers to the sample network-cfg.json in " + "OpenstackNode app to push SSH authentication information", srcNode == null ? "" : srcNode.hostname());
        return;
    }
    if (dstIp.equals(osNetService.gatewayIp(srcInstance.get().portId()))) {
        dstIp = srcIp;
    }
    // print uplink flow trace result
    String requestStringUplink = traceRequestString(srcIp, dstIp, srcInstance.get(), osNetService, true);
    print(FLOW_TRACE_REQUEST_STRING_UPLINK + requestStringUplink);
    String requestStringDownlink = traceRequestString(srcIp, dstIp, srcInstance.get(), osNetService, false);
    print(FLOW_TRACE_REQUEST_STRING_DOWNLINK + requestStringDownlink);
    String traceResult = sendTraceRequestToNode(requestStringUplink + '\n' + requestStringDownlink, srcNode);
    print(traceResult);
}
Also used : OpenstackNode(org.onosproject.openstacknode.api.OpenstackNode) InstancePort(org.onosproject.openstacknetworking.api.InstancePort) Argument(org.apache.karaf.shell.api.action.Argument) OpenstackNodeAdminService(org.onosproject.openstacknode.api.OpenstackNodeAdminService) AbstractShellCommand.get(org.onosproject.cli.AbstractShellCommand.get) Command(org.apache.karaf.shell.api.action.Command) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) InstancePortAdminService(org.onosproject.openstacknetworking.api.InstancePortAdminService) OpenstackNetworkAdminService(org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Optional(java.util.Optional) Completion(org.apache.karaf.shell.api.action.Completion) OpenstackNetworkingUtil.traceRequestString(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.traceRequestString) OpenstackNetworkingUtil.sendTraceRequestToNode(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.sendTraceRequestToNode) OpenstackNodeAdminService(org.onosproject.openstacknode.api.OpenstackNodeAdminService) InstancePortAdminService(org.onosproject.openstacknetworking.api.InstancePortAdminService) InstancePort(org.onosproject.openstacknetworking.api.InstancePort) OpenstackNode(org.onosproject.openstacknode.api.OpenstackNode) OpenstackNetworkingUtil.traceRequestString(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.traceRequestString) OpenstackNetworkAdminService(org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService)

Aggregations

OpenstackNetworkAdminService (org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService)6 Command (org.apache.karaf.shell.api.action.Command)4 Service (org.apache.karaf.shell.api.action.lifecycle.Service)4 AbstractShellCommand (org.onosproject.cli.AbstractShellCommand)4 Argument (org.apache.karaf.shell.api.action.Argument)3 Completion (org.apache.karaf.shell.api.action.Completion)3 AbstractShellCommand.get (org.onosproject.cli.AbstractShellCommand.get)3 ExternalPeerRouter (org.onosproject.openstacknetworking.api.ExternalPeerRouter)3 OpenstackNode (org.onosproject.openstacknode.api.OpenstackNode)3 Lists (com.google.common.collect.Lists)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 Ip4Address (org.onlab.packet.Ip4Address)2 IpAddress (org.onlab.packet.IpAddress)2 VlanId (org.onlab.packet.VlanId)2 OpenstackRouterAdminService (org.onosproject.openstacknetworking.api.OpenstackRouterAdminService)2 OpenstackSecurityGroupAdminService (org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService)2 OpenstackNodeService (org.onosproject.openstacknode.api.OpenstackNodeService)2 OSClient (org.openstack4j.api.OSClient)2