Package org.midonet.client.resource

Examples of org.midonet.client.resource.RouterPort


        boolean resources = false;
        Router tenantRouter = null;
        Router providerRouter = null;
        RouterPort[] ports = null;

        RouterPort tenantUplink = null;
        RouterPort providerDownlink = null;

        RuleChain preNat = null;
        RuleChain post = null;
        String accountIdStr = null;
        String routerName = null;
View Full Code Here


        Router tenantRouter = null;
        Router providerRouter = null;

        RouterPort[] ports = null;

        RouterPort tenantUplink = null;
        RouterPort providerDownlink = null;

        RuleChain preFilter = null;
        RuleChain preNat = null;
        RuleChain post = null;
View Full Code Here

                // Get provider router
                Router providerRouter = api.getRouter(_providerRouterId);

                Port[] ports = getOrCreatePublicBridgePorts(nic, netBridge, providerRouter);

                RouterPort providerDownlink = (RouterPort)ports[1];

                // Set route from router to bridge for this particular IP. Prepare
                // is called in both starting a new VM and restarting a VM, so the
                // NIC may
                boolean routeExists = false;
                for (Route route : providerRouter.getRoutes(new MultivaluedMapImpl())) {
                    String ip4 = route.getDstNetworkAddr();
                    if (ip4 != null && ip4.equals(nic.getIp4Address())) {
                        routeExists = true;
                        break;
                    }
                }

                if (!routeExists) {
                    providerRouter.addRoute()
                        .type("Normal")
                        .weight(100)
                        .srcNetworkAddr("0.0.0.0")
                        .srcNetworkLength(0)
                        .dstNetworkAddr(nic.getIp4Address())
                        .dstNetworkLength(32)
                        .nextHopPort(providerDownlink.getId())
                        .nextHopGateway(null)
                        .create();
                }
            }
View Full Code Here

        RuleChain postNat = getChain(accountIdStr, networkUUIDStr, RuleChainCode.TR_POST);
        RuleChain preFilter = getChain(accountIdStr, networkUUIDStr, RuleChainCode.TR_PREFILTER);
        Router providerRouter = api.getRouter(_providerRouterId);
        Router tenantRouter = getOrCreateGuestNetworkRouter(network);
        RouterPort[] ports = getOrCreateProviderRouterPorts(tenantRouter, providerRouter);
        RouterPort providerDownlink = ports[1];

        // Rules in the preNat table
        Map<String, Rule> existingPreNatRules = new HashMap<String, Rule>();
        for (Rule existingRule : preNat.getRules()) {
            // The "port forwarding" rules we're interested in are dnat rules where src / dst ports are specified
            if (existingRule.getType().equals(DtoRule.DNAT) && existingRule.getTpDst() != null) {
                String ruleString = new SimpleFirewallRule(existingRule).toStringArray()[0];
                existingPreNatRules.put(ruleString, existingRule);
            }
        }

        /*
         * Counts of rules associated with an IP address. Use this to check
         * how many rules we have of a given IP address. When it reaches 0,
         * we can delete the route associated with it.
         */
        Map<String, Integer> ipRuleCounts = new HashMap<String, Integer>();
        for (Rule rule : preNat.getRules()) {
            String ip = rule.getNwDstAddress();
            if (ip != null && rule.getNwDstLength() == 32) {
                if (ipRuleCounts.containsKey(ip)) {
                    ipRuleCounts.put(ip, new Integer(ipRuleCounts.get(ip).intValue() + 1));
                } else {
                    ipRuleCounts.put(ip, new Integer(1));
                }
            }
        }

        /*
         * Routes associated with IP. When we delete all the rules associated
         * with a given IP, we can delete the route associated with it.
         */
        Map<String, Route> routes = new HashMap<String, Route>();
        for (Route route : providerRouter.getRoutes(new MultivaluedMapImpl())) {
            String ip = route.getDstNetworkAddr();
            if (ip != null && route.getDstNetworkLength() == 32) {
                routes.put(ip, route);
            }
        }

        for (PortForwardingRule rule : rules) {
            IpAddress dstIp = _networkModel.getIp(rule.getSourceIpAddressId());
            PortForwardingRuleTO ruleTO = new PortForwardingRuleTO(rule, null, dstIp.getAddress().addr());
            SimpleFirewallRule fwRule = new SimpleFirewallRule(ruleTO);
            String[] ruleStrings = fwRule.toStringArray();

            if (rule.getState() == FirewallRule.State.Revoke) {
                /*
                 * Lookup in existingRules, delete if present
                 * We need to delete from both the preNat table and the
                 * postNat table.
                 */
                for (String revokeRuleString : ruleStrings) {
                    Rule foundPreNatRule = existingPreNatRules.get(revokeRuleString);
                    if (foundPreNatRule != null) {
                        String ip = foundPreNatRule.getNwDstAddress();
                        // is this the last rule associated with this IP?
                        Integer cnt = ipRuleCounts.get(ip);
                        if (cnt != null) {
                            if (cnt == 1) {
                                ipRuleCounts.remove(ip);
                                // no more rules for this IP. delete the route.
                                Route route = routes.remove(ip);
                                route.delete();
                            } else {
                                ipRuleCounts.put(ip, new Integer(ipRuleCounts.get(ip).intValue() - 1));
                            }
                        }
                        foundPreNatRule.delete();
                    }
                }
            } else if (rule.getState() == FirewallRule.State.Add) {
                for (int i = 0; i < ruleStrings.length; i++) {
                    String ruleString = ruleStrings[i];
                    Rule foundRule = existingPreNatRules.get(ruleString);
                    if (foundRule == null) {

                        String vmIp = ruleTO.getDstIp();
                        String publicIp = dstIp.getAddress().addr();
                        int privPortStart = ruleTO.getDstPortRange()[0];
                        int privPortEnd = ruleTO.getDstPortRange()[1];
                        int pubPortStart = ruleTO.getSrcPortRange()[0];
                        int pubPortEnd = ruleTO.getSrcPortRange()[1];

                        DtoRule.DtoNatTarget[] preTargets = new DtoRule.DtoNatTarget[] {new DtoRule.DtoNatTarget(vmIp, vmIp, privPortStart, privPortEnd)};

                        Rule preNatRule =
                            preNat.addRule()
                                .type(DtoRule.DNAT)
                                .flowAction(DtoRule.Accept)
                                .nwDstAddress(publicIp)
                                .nwDstLength(32)
                                .tpDst(new DtoRange(pubPortStart, pubPortEnd))
                                .natTargets(preTargets)
                                .nwProto(SimpleFirewallRule.stringToProtocolNumber(rule.getProtocol()))
                                .position(1);

                        Integer cnt = ipRuleCounts.get(publicIp);
                        if (cnt != null) {
                            ipRuleCounts.put(publicIp, new Integer(cnt.intValue() + 1));
                        } else {
                            ipRuleCounts.put(publicIp, new Integer(1));
                        }
                        String preNatRuleStr = new SimpleFirewallRule(preNatRule).toStringArray()[0];
                        existingPreNatRules.put(preNatRuleStr, preNatRule);
                        preNatRule.create();

                        if (routes.get(publicIp) == null) {
                            Route route =
                                providerRouter.addRoute()
                                    .type("Normal")
                                    .weight(100)
                                    .srcNetworkAddr("0.0.0.0")
                                    .srcNetworkLength(0)
                                    .dstNetworkAddr(publicIp)
                                    .dstNetworkLength(32)
                                    .nextHopPort(providerDownlink.getId());
                            route.create();
                            routes.put(publicIp, route);
                        }

                        // If Firewall is in our service offering, set up the
View Full Code Here

    }

    protected RouterPort[] getOrCreateProviderRouterPorts(Router tenantRouter, Router providerRouter) {
        RouterPort[] ports = new RouterPort[2];

        RouterPort tenantUplink = null;
        RouterPort providerDownlink = null;

        // Check if the ports and connection already exist
        for (Port peerPort : tenantRouter.getPeerPorts((new MultivaluedMapImpl()))) {
            if (peerPort != null && peerPort instanceof RouterPort) {
                RouterPort checkPort = (RouterPort)peerPort;
                if (checkPort.getDeviceId().compareTo(providerRouter.getId()) == 0) {
                    providerDownlink = checkPort;
                    tenantUplink = (RouterPort)api.getPort(checkPort.getPeerId());
                    break;
                }
            }
        }
View Full Code Here

    private Port[] getOrCreatePublicBridgePorts(NicProfile nic, Bridge publicBridge, Router providerRouter) {
        Port[] ports = new Port[2];

        BridgePort bridgeUplink = null;
        RouterPort providerDownlink = null;

        // Check if the ports and connection already exist
        for (Port peerPort : publicBridge.getPeerPorts()) {
            if (peerPort != null && peerPort instanceof RouterPort) {
                RouterPort checkPort = (RouterPort)peerPort;
                // Check it's a port on the providerRouter with the right gateway address
                if (checkPort.getDeviceId().compareTo(providerRouter.getId()) == 0 && checkPort.getPortAddress().equals(nic.getGateway())) {
                    providerDownlink = checkPort;
                    bridgeUplink = (BridgePort)api.getPort(checkPort.getPeerId());
                    break;
                }
            }
        }
View Full Code Here

        // Add interior port on bridge side
        BridgePort bridgePort = netBridge.addInteriorPort().create();

        // Add interior port on router side, with network details
        RouterPort routerPort = netRouter.addInteriorRouterPort();
        String cidr = network.getCidr();
        String cidrSubnet = NetUtils.getCidrSubNet(cidr);
        int cidrSize = (int)NetUtils.getCidrSize(NetUtils.cidr2Netmask(cidr));

        routerPort.networkAddress(cidrSubnet);
        routerPort.networkLength(cidrSize);
        routerPort.portAddress(network.getGateway());

        // If this is a VPC, then we will be using NetworkACLs, which is
        // implemented via chains on the router port to that network.
        if (getIsVpc(network)) {
            // Create ACL filter chain for traffic coming INTO the network
            // (outbound from the port
            int pos = 1;

            RuleChain inc = api.addChain().name(getChainName(String.valueOf(network.getId()), routerName, RuleChainCode.ACL_INGRESS)).tenantId(accountIdStr).create();

            // If it is ARP, accept it
            inc.addRule().type(DtoRule.Accept).dlType(0x0806).position(pos++).create();

            // If it is ICMP to the router, accept that
            inc.addRule()
                .type(DtoRule.Accept)
                .nwProto(SimpleFirewallRule.stringToProtocolNumber("icmp"))
                .nwDstAddress(network.getGateway())
                .nwDstLength(32)
                .position(pos++)
                .create();

            // If it is connection tracked, accept that as well
            inc.addRule().type(DtoRule.Accept).matchReturnFlow(true).position(pos++).create();

            inc.addRule().type(DtoRule.Drop).position(pos).create();

            //
            RuleChain out = api.addChain().name(getChainName(String.valueOf(network.getId()), routerName, RuleChainCode.ACL_EGRESS)).tenantId(accountIdStr).create();

            // Creating the first default rule here that does nothing
            // but start connection tracking.
            out.addRule().type(DtoRule.Accept).matchForwardFlow(true).position(1).create();

            routerPort.outboundFilterId(inc.getId());
            routerPort.inboundFilterId(out.getId());
        }

        routerPort.create();

        // Link them up
        bridgePort.link(routerPort.getId()).update();

        // Set up default route from router to subnet
        netRouter.addRoute()
            .type("Normal")
            .weight(100)
            .srcNetworkAddr("0.0.0.0")
            .srcNetworkLength(0)
            .dstNetworkAddr(cidrSubnet)
            .dstNetworkLength(cidrSize)
            .nextHopPort(routerPort.getId())
            .nextHopGateway(null)
            .create();
    }
View Full Code Here

    private void cleanBridge(Bridge br) {

        for (Port peerPort : br.getPeerPorts()) {
            if (peerPort != null && peerPort instanceof RouterPort) {
                RouterPort checkPort = (RouterPort)peerPort;
                if (checkPort.getType().equals("ExteriorRouter")) {
                    checkPort.vifId(null).update();
                } else if (checkPort.getType().equals("InteriorRouter")) {
                    checkPort.unlink();
                }
                checkPort.delete();
            }
        }

        for (BridgePort p : br.getPorts()) {
View Full Code Here

        Router tenantRouter = getGuestNetworkRouter(id, accountUuid, isVpc);

        // Delete any peer ports corresponding to this router
        for (Port peerPort : tenantRouter.getPeerPorts((new MultivaluedMapImpl()))) {
            if (peerPort != null && peerPort instanceof RouterPort) {
                RouterPort checkPort = (RouterPort)peerPort;
                if (checkPort.getType().equals("ExteriorRouter")) {
                    checkPort.vifId(null).update();
                } else if (checkPort.getType().equals("InteriorRouter")) {
                    checkPort.unlink();
                }
                checkPort.delete();
            } else if (peerPort != null && peerPort instanceof BridgePort) {
                BridgePort checkPort = (BridgePort)peerPort;
                if (checkPort.getType().equals("ExteriorBridge")) {
                    checkPort.vifId(null).update();
                } else if (checkPort.getType().equals("InteriorBridge")) {
                    checkPort.unlink();
                }
                checkPort.delete();
            }
        }

        if (tenantRouter != null) {
            // Remove all peer ports if any exist
View Full Code Here

        boolean resources = false;
        Router tenantRouter = null;
        Router providerRouter = null;
        RouterPort[] ports = null;

        RouterPort tenantUplink = null;
        RouterPort providerDownlink = null;

        RuleChain preNat = null;
        RuleChain post = null;
        String accountIdStr = null;
        String routerName = null;
View Full Code Here

TOP

Related Classes of org.midonet.client.resource.RouterPort

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.