Fix copyright header
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / NeutronRouterAware.java
index 7de51f5b9bf8444a6d7fe89ec0dde347f4b637d9..e77afb031ff0b7fa196d5c494259d23c9a38e4c4 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * Copyright (c) 2015 Intel, Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping;
 
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -13,10 +21,11 @@ import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.groupbasedpolicy.neutron.mapper.infrastructure.Router;
+import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule.NeutronSecurityRuleAware;
 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils.ForwardingCtx;
 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.NeutronMapperIidFactory;
-import org.opendaylight.groupbasedpolicy.neutron.mapper.util.NeutronUtils;
 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.Utils;
 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
 import org.opendaylight.groupbasedpolicy.util.IidFactory;
@@ -27,16 +36,15 @@ import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
 import org.opendaylight.neutron.spi.NeutronPort;
 import org.opendaylight.neutron.spi.NeutronRouter;
 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
-import org.opendaylight.neutron.spi.NeutronSecurityRule;
 import org.opendaylight.neutron.spi.NeutronSubnet;
 import org.opendaylight.neutron.spi.Neutron_IPs;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
@@ -62,23 +70,14 @@ import com.google.common.collect.ImmutableList;
 public class NeutronRouterAware implements INeutronRouterAware {
 
     private static final Logger LOG = LoggerFactory.getLogger(NeutronRouterAware.class);
-    private static final NeutronRouterAware INSTANCE = new NeutronRouterAware();
-    private static DataBroker dataProvider;
-    private static EndpointService epService;
-
-    private NeutronRouterAware() {
-        if (NeutronRouterAware.INSTANCE != null) {
-            throw new IllegalStateException("Already instantiated");
-        }
-    }
-
-    public static NeutronRouterAware getInstance() {
-        return NeutronRouterAware.INSTANCE;
-    }
-
-    public static void init(DataBroker dataProvider, EndpointService epService) {
-        NeutronRouterAware.dataProvider = checkNotNull(dataProvider);
-        NeutronRouterAware.epService = checkNotNull(epService);
+    private final DataBroker dataProvider;
+    private final  EndpointService epService;
+    private final NeutronSecurityRuleAware secRuleAware;
+
+    public NeutronRouterAware(DataBroker dataProvider, EndpointService epService, NeutronSecurityRuleAware secRuleAware) {
+        this.dataProvider = checkNotNull(dataProvider);
+        this.epService = checkNotNull(epService);
+        this.secRuleAware = checkNotNull(secRuleAware);
     }
 
     @Override
@@ -109,7 +108,8 @@ public class NeutronRouterAware implements INeutronRouterAware {
             return;
         }
 
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
+        NeutronCRUDInterfaces neutronCRUDInterface = new NeutronCRUDInterfaces().fetchINeutronPortCRUD(this);
+        INeutronPortCRUD portInterface = neutronCRUDInterface.getPortInterface();
         if (portInterface == null) {
             LOG.warn("Illegal state - No provider for {}", INeutronPortCRUD.class.getName());
             return;
@@ -130,7 +130,8 @@ public class NeutronRouterAware implements INeutronRouterAware {
             rwTx.put(LogicalDatastoreType.CONFIGURATION, l3ContextIidForRouterId, l3Context);
         }
 
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
+        neutronCRUDInterface = neutronCRUDInterface.fetchINeutronSubnetCRUD(this);
+        INeutronSubnetCRUD subnetInterface = neutronCRUDInterface.getSubnetInterface();
         if (subnetInterface == null) {
             LOG.warn("Illegal state - No provider for {}", INeutronSubnetCRUD.class.getName());
             return;
@@ -138,10 +139,13 @@ public class NeutronRouterAware implements INeutronRouterAware {
         NeutronSubnet defaultSubnet = subnetInterface.getSubnet(router.getExternalGatewayInfo()
             .getExternalFixedIPs()
             .get(0)
-            .getSubnetUUID());;
+            .getSubnetUUID());
         IpAddress defaultGateway = null;
         if (defaultSubnet != null) {
             defaultGateway = Utils.createIpAddress(defaultSubnet.getGatewayIP());
+            //Create L3Endpoint for defaultGateway and write to externalGateways to L3Endpoints in neutron-gbp datastore
+            NetworkDomainId containment = new NetworkDomainId(defaultSubnet.getID());
+            NeutronPortAware.addL3EndpointForExternalGateway(tenantId, l3Context.getId(), defaultGateway, containment ,rwTx);
         }
         // Create L3Prefix Endpoints for all routes
         if (router.getRoutes().isEmpty()) {
@@ -153,7 +157,7 @@ public class NeutronRouterAware implements INeutronRouterAware {
             for (String route : router.getRoutes()) {
                 IpPrefix ipPrefix = Utils.createIpPrefix(route);
                 boolean addedL3Prefix = NeutronPortAware.addL3PrefixEndpoint(l3ContextIdFromRouterId, ipPrefix,
-                        defaultGateway, tenantId, rwTx, epService);
+                        defaultGateway, tenantId, epService);
                 if (!addedL3Prefix) {
                     LOG.warn("Could not add EndpointL3Prefix for Neutron route {} for router {}", route, router.getID());
                     rwTx.cancel();
@@ -178,19 +182,6 @@ public class NeutronRouterAware implements INeutronRouterAware {
             if (Strings.isNullOrEmpty(routerPort.getTenantID())) {
                 routerPort.setTenantID(router.getTenantID());
             }
-            // create security rules for router
-            List<NeutronSecurityRule> routerSecRules = createRouterSecRules(routerPort, null, rwTx);
-            if (routerSecRules == null) {
-                rwTx.cancel();
-                return;
-            }
-            for (NeutronSecurityRule routerSecRule : routerSecRules) {
-                boolean isRouterSecRuleAdded = NeutronSecurityRuleAware.addNeutronSecurityRule(routerSecRule, rwTx);
-                if (!isRouterSecRuleAdded) {
-                    rwTx.cancel();
-                    return;
-                }
-            }
 
             boolean isSuccessful = setNewL3ContextToEpsFromSubnet(tenantId, l3Context, subnet, rwTx);
             if (!isSuccessful) {
@@ -215,9 +206,9 @@ public class NeutronRouterAware implements INeutronRouterAware {
         ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
         TenantId tenantId = new TenantId(Utils.normalizeUuid(router.getTenantID()));
         Optional<EndpointGroup> potentialEpg = DataStoreHelper.removeIfExists(LogicalDatastoreType.CONFIGURATION,
-                IidFactory.endpointGroupIid(tenantId, MappingUtils.EPG_ROUTER_ID), rwTx);
+                IidFactory.endpointGroupIid(tenantId, Router.EPG_ID), rwTx);
         if (!potentialEpg.isPresent()) {
-            LOG.warn("Illegal state - Endpoint group {} does not exist.", MappingUtils.EPG_ROUTER_ID.getValue());
+            LOG.warn("Illegal state - Endpoint group {} does not exist.", Router.EPG_ID.getValue());
             rwTx.cancel();
             return;
         }
@@ -252,7 +243,8 @@ public class NeutronRouterAware implements INeutronRouterAware {
     @Override
     public void neutronRouterInterfaceAttached(NeutronRouter router, NeutronRouter_Interface routerInterface) {
         LOG.trace("neutronRouterInterfaceAttached - router: {} interface: {}", router, routerInterface);
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
+        NeutronCRUDInterfaces neutronCRUDInterface = new NeutronCRUDInterfaces().fetchINeutronPortCRUD(this);
+        INeutronPortCRUD portInterface = neutronCRUDInterface.getPortInterface();
         if (portInterface == null) {
             LOG.warn("Illegal state - No provider for {}", INeutronPortCRUD.class.getName());
             return;
@@ -285,20 +277,6 @@ public class NeutronRouterAware implements INeutronRouterAware {
         }
         rwTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.subnetIid(tenantId, subnet.getId()), subnet);
 
-        // create security rules for router
-        List<NeutronSecurityRule> routerSecRules = createRouterSecRules(routerPort, null, rwTx);
-        if (routerSecRules == null) {
-            rwTx.cancel();
-            return;
-        }
-        for (NeutronSecurityRule routerSecRule : routerSecRules) {
-            boolean isRouterSecRuleAdded = NeutronSecurityRuleAware.addNeutronSecurityRule(routerSecRule, rwTx);
-            if (!isRouterSecRuleAdded) {
-                rwTx.cancel();
-                return;
-            }
-        }
-
         boolean isSuccessful = setNewL3ContextToEpsFromSubnet(tenantId, l3Context, subnet, rwTx);
         if (!isSuccessful) {
             rwTx.cancel();
@@ -315,7 +293,7 @@ public class NeutronRouterAware implements INeutronRouterAware {
         }
         return new L3ContextBuilder().setId(new L3ContextId(router.getID()))
             .setName(l3ContextName)
-            .setDescription(new Description(MappingUtils.NEUTRON_ROUTER__ + router.getID()))
+            .setDescription(new Description(MappingUtils.NEUTRON_ROUTER + router.getID()))
             .build();
     }
 
@@ -355,7 +333,8 @@ public class NeutronRouterAware implements INeutronRouterAware {
         rwTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.l2BridgeDomainIid(tenantId, l2BridgeDomain.getId()),
                 l2BridgeDomain);
 
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
+        NeutronCRUDInterfaces neutronCRUDInterface = new NeutronCRUDInterfaces().fetchINeutronSubnetCRUD(this);
+        INeutronSubnetCRUD subnetInterface = neutronCRUDInterface.getSubnetInterface();
         if (subnetInterface == null) {
             LOG.warn("Illegal state - No provider for {}", INeutronSubnetCRUD.class.getName());
             return false;
@@ -379,6 +358,11 @@ public class NeutronRouterAware implements INeutronRouterAware {
                     .build());
             }
         }
+        if (neutronSubnet.getGatewayIP() != null) {
+            l3Eps.add(new L3Builder().setL3Context(oldL3ContextId)
+                    .setIpAddress(Utils.createIpAddress(neutronSubnet.getGatewayIP()))
+                    .build());
+        }
 
         if (!l3Eps.isEmpty()) {
             epService.unregisterEndpoint(new UnregisterEndpointInputBuilder().setL3(l3Eps).build());
@@ -386,50 +370,6 @@ public class NeutronRouterAware implements INeutronRouterAware {
         return true;
     }
 
-    public static List<NeutronSecurityRule> createRouterSecRules(NeutronPort port, EndpointGroupId consumerEpgId,
-            ReadTransaction rTx) {
-        TenantId tenantId = new TenantId(Utils.normalizeUuid(port.getTenantID()));
-        Neutron_IPs firstIp = MappingUtils.getFirstIp(port.getFixedIPs());
-        if (firstIp == null) {
-            LOG.warn("Illegal state - Router port does not have an IP address.");
-            return null;
-        }
-        SubnetId routerSubnetId = new SubnetId(firstIp.getSubnetUUID());
-        Optional<Subnet> potentialSubnet = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
-                IidFactory.subnetIid(tenantId, routerSubnetId), rTx);
-        if (!potentialSubnet.isPresent()) {
-            LOG.warn("Illegal state - Subnet {} where is router port does not exist.", routerSubnetId.getValue());
-            return null;
-        }
-        IpPrefix ipSubnet = potentialSubnet.get().getIpPrefix();
-        NeutronSecurityRule routerRuleEgress = createRouterSecRule(port.getID(), tenantId, ipSubnet, consumerEpgId,
-                true);
-        NeutronSecurityRule routerRuleIngress = createRouterSecRule(port.getID(), tenantId, ipSubnet, consumerEpgId,
-                false);
-        return ImmutableList.of(routerRuleEgress, routerRuleIngress);
-    }
-
-    private static NeutronSecurityRule createRouterSecRule(String ruleUuid, TenantId tenantId, IpPrefix ipSubnet,
-            EndpointGroupId consumerEpgId, boolean isEgress) {
-        NeutronSecurityRule dhcpSecRule = new NeutronSecurityRule();
-        dhcpSecRule.setSecurityRuleGroupID(MappingUtils.EPG_ROUTER_ID.getValue());
-        dhcpSecRule.setSecurityRuleTenantID(tenantId.getValue());
-        dhcpSecRule.setSecurityRuleRemoteIpPrefix(Utils.getStringIpPrefix(ipSubnet));
-        if (isEgress) {
-            dhcpSecRule.setSecurityRuleUUID(NeutronUtils.EGRESS + "__" + ruleUuid);
-            dhcpSecRule.setSecurityRuleDirection(NeutronUtils.EGRESS);
-        } else {
-            dhcpSecRule.setSecurityRuleUUID(NeutronUtils.INGRESS + "__" + ruleUuid);
-            dhcpSecRule.setSecurityRuleDirection(NeutronUtils.INGRESS);
-        }
-        if (ipSubnet.getIpv4Prefix() != null) {
-            dhcpSecRule.setSecurityRuleEthertype(NeutronUtils.IPv4);
-        } else {
-            dhcpSecRule.setSecurityRuleEthertype(NeutronUtils.IPv6);
-        }
-        return dhcpSecRule;
-    }
-
     @Override
     public int canDetachInterface(NeutronRouter router, NeutronRouter_Interface routerInterface) {
         LOG.trace("canDetachInterface - router: {} interface: {}", router, routerInterface);
@@ -440,7 +380,8 @@ public class NeutronRouterAware implements INeutronRouterAware {
     @Override
     public void neutronRouterInterfaceDetached(NeutronRouter router, NeutronRouter_Interface routerInterface) {
         LOG.trace("neutronRouterInterfaceDetached - router: {} interface: {}", router, routerInterface);
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);
+        NeutronCRUDInterfaces neutronCRUDInterface = new NeutronCRUDInterfaces().fetchINeutronSubnetCRUD(this);
+        INeutronSubnetCRUD subnetInterface = neutronCRUDInterface.getSubnetInterface();
         if (subnetInterface == null) {
             LOG.warn("Illegal state - No provider for {}", INeutronSubnetCRUD.class.getName());
             return;