Search in sources :

Example 1 with CONTROLLER

use of org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER 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 2 with CONTROLLER

use of org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER in project onos by opennetworkinglab.

the class OpenstackManagementWebResource method syncStates.

/**
 * Synchronizes the network states with openstack.
 *
 * @return 200 OK with sync result, 404 not found
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("sync/states")
public Response syncStates() {
    Map<String, String> headerMap = new HashMap();
    headerMap.put(HTTP_HEADER_ACCEPT, HTTP_HEADER_VALUE_JSON);
    Optional<OpenstackNode> node = osNodeAdminService.nodes(CONTROLLER).stream().findFirst();
    if (!node.isPresent()) {
        log.error(AUTH_INFO_NOT_FOUND);
        throw new ItemNotFoundException(AUTH_INFO_NOT_FOUND);
    }
    OSClient osClient = OpenstackNetworkingUtil.getConnectedClient(node.get());
    if (osClient == null) {
        log.error(AUTH_INFO_NOT_CORRECT);
        throw new ItemNotFoundException(AUTH_INFO_NOT_CORRECT);
    }
    try {
        osClient.headers(headerMap).networking().securitygroup().list().forEach(osSg -> {
            if (osSgAdminService.securityGroup(osSg.getId()) != null) {
                osSgAdminService.updateSecurityGroup(osSg);
            } else {
                osSgAdminService.createSecurityGroup(osSg);
            }
        });
    } catch (Exception e) {
        log.warn("Failed to retrieve security group due to {}", e.getMessage());
        return Response.serverError().build();
    }
    try {
        osClient.headers(headerMap).networking().network().list().forEach(osNet -> {
            if (osNetAdminService.network(osNet.getId()) != null) {
                osNetAdminService.updateNetwork(osNet);
            } else {
                osNetAdminService.createNetwork(osNet);
            }
        });
    } catch (Exception e) {
        log.warn("Failed to retrieve network due to {}", e.getMessage());
        return Response.serverError().build();
    }
    try {
        osClient.headers(headerMap).networking().subnet().list().forEach(osSubnet -> {
            if (osNetAdminService.subnet(osSubnet.getId()) != null) {
                osNetAdminService.updateSubnet(osSubnet);
            } else {
                osNetAdminService.createSubnet(osSubnet);
            }
        });
    } catch (Exception e) {
        log.warn("Failed to retrieve subnet due to {}", e.getMessage());
        return Response.serverError().build();
    }
    try {
        osClient.headers(headerMap).networking().port().list().forEach(osPort -> {
            if (osNetAdminService.port(osPort.getId()) != null) {
                osNetAdminService.updatePort(osPort);
            } else {
                osNetAdminService.createPort(osPort);
            }
        });
    } catch (Exception e) {
        log.warn("Failed to retrieve port due to {}", e.getMessage());
        return Response.serverError().build();
    }
    try {
        osClient.headers(headerMap).networking().router().list().forEach(osRouter -> {
            if (osRouterAdminService.router(osRouter.getId()) != null) {
                osRouterAdminService.updateRouter(osRouter);
            } else {
                osRouterAdminService.createRouter(osRouter);
            }
            osNetAdminService.ports().stream().filter(osPort -> Objects.equals(osPort.getDeviceId(), osRouter.getId()) && Objects.equals(osPort.getDeviceOwner(), DEVICE_OWNER_IFACE)).forEach(osPort -> addRouterIface(osPort, osRouterAdminService));
        });
    } catch (Exception e) {
        log.warn("Failed to retrieve router due to {}", e.getMessage());
        return Response.serverError().build();
    }
    try {
        osClient.headers(headerMap).networking().floatingip().list().forEach(osFloating -> {
            if (osRouterAdminService.floatingIp(osFloating.getId()) != null) {
                osRouterAdminService.updateFloatingIp(osFloating);
            } else {
                osRouterAdminService.createFloatingIp(osFloating);
            }
        });
    } catch (Exception e) {
        log.warn("Failed to retrieve floating IP due to {}", e.getMessage());
        return Response.serverError().build();
    }
    return ok(mapper().createObjectNode()).build();
}
Also used : AbstractWebResource(org.onosproject.rest.AbstractWebResource) Produces(javax.ws.rs.Produces) OpenstackRoutingArpHandler(org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler) OSClient(org.openstack4j.api.OSClient) OpenstackNetworkingUtil(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil) CoreService(org.onosproject.core.CoreService) OpenstackRouterAdminService(org.onosproject.openstacknetworking.api.OpenstackRouterAdminService) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) Constants(org.onosproject.openstacknetworking.api.Constants) OpenstackHaService(org.onosproject.openstacknetworking.api.OpenstackHaService) COMPLETE(org.onosproject.openstacknode.api.NodeState.COMPLETE) OpenstackNetworkingUtil.checkArpMode(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode) OpenstackSecurityGroupAdminService(org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService) MediaType(javax.ws.rs.core.MediaType) FlowRuleService(org.onosproject.net.flow.FlowRuleService) Consumes(javax.ws.rs.Consumes) OpenstackNetworkingUtil.addRouterIface(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.addRouterIface) Map(java.util.Map) ApplicationId(org.onosproject.core.ApplicationId) Thread.sleep(java.lang.Thread.sleep) Response.status(javax.ws.rs.core.Response.status) Tools.nullIsIllegal(org.onlab.util.Tools.nullIsIllegal) ItemNotFoundException(org.onlab.util.ItemNotFoundException) OpenstackNetworkingUtil.getPropertyValueAsBoolean(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValueAsBoolean) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Objects(java.util.Objects) List(java.util.List) NetFloatingIP(org.openstack4j.model.network.NetFloatingIP) Response(javax.ws.rs.core.Response) StreamSupport.stream(java.util.stream.StreamSupport.stream) Optional(java.util.Optional) GATEWAY(org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY) PathParam(javax.ws.rs.PathParam) OpenstackNode(org.onosproject.openstacknode.api.OpenstackNode) GET(javax.ws.rs.GET) COMPUTE(org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE) OpenstackNodeAdminService(org.onosproject.openstacknode.api.OpenstackNodeAdminService) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) OpenstackSwitchingArpHandler(org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler) CONTROLLER(org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) OpenstackNetworkAdminService(org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService) IpAddress(org.onlab.packet.IpAddress) ComponentConfigService(org.onosproject.cfg.ComponentConfigService) OpenstackNetworkingUtil.checkActivationFlag(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkActivationFlag) Logger(org.slf4j.Logger) OpenstackNetworkingUtil.getPropertyValue(org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue) OpenstackSecurityGroupHandler(org.onosproject.openstacknetworking.impl.OpenstackSecurityGroupHandler) PUT(javax.ws.rs.PUT) OpenstackRoutingSnatHandler(org.onosproject.openstacknetworking.impl.OpenstackRoutingSnatHandler) Comparator(java.util.Comparator) NodeState(org.onosproject.openstacknode.api.NodeState) HashMap(java.util.HashMap) OSClient(org.openstack4j.api.OSClient) OpenstackNode(org.onosproject.openstacknode.api.OpenstackNode) ItemNotFoundException(org.onlab.util.ItemNotFoundException) ItemNotFoundException(org.onlab.util.ItemNotFoundException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 OpenstackNetworkAdminService (org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService)2 OpenstackRouterAdminService (org.onosproject.openstacknetworking.api.OpenstackRouterAdminService)2 OpenstackSecurityGroupAdminService (org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService)2 OpenstackNetworkingUtil (org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil)2 OpenstackNetworkingUtil.addRouterIface (org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.addRouterIface)2 OpenstackNode (org.onosproject.openstacknode.api.OpenstackNode)2 CONTROLLER (org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER)2 OSClient (org.openstack4j.api.OSClient)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Thread.sleep (java.lang.Thread.sleep)1 Comparator (java.util.Comparator)1 List (java.util.List)1 StreamSupport.stream (java.util.stream.StreamSupport.stream)1