Misc changes to use neutronvpn for dhcpservice 39/32439/3
authorVishal Thapar <vishal.thapar@ericsson.com>
Tue, 12 Jan 2016 18:57:34 +0000 (19:57 +0100)
committerVishal Thapar <vishal.thapar@ericsson.com>
Tue, 12 Jan 2016 23:17:35 +0000 (00:17 +0100)
Adds code to use neutronvpn APIs to get Port and Subnet info
Adds dhcp-config parameters to neutronvpn yang file to persist dhcp
configuration
Fix namespace issue in ITM causing bundle failure

TODO: Clustering support

Change-Id: Ica57dc4d742333996804295e4b4ec80916c7a9ee
Signed-off-by: Vishal Thapar <vishal.thapar@ericsson.com>
dhcpservice/dhcpservice-api/src/main/java/org/opendaylight/vpnservice/dhcpservice/api/DHCPMConstants.java
dhcpservice/dhcpservice-impl/pom.xml
dhcpservice/dhcpservice-impl/src/main/config/default-config.xml
dhcpservice/dhcpservice-impl/src/main/java/org/opendaylight/vpnservice/dhcpservice/DhcpConfigListener.java [new file with mode: 0644]
dhcpservice/dhcpservice-impl/src/main/java/org/opendaylight/vpnservice/dhcpservice/DhcpManager.java
dhcpservice/dhcpservice-impl/src/main/java/org/opendaylight/vpnservice/dhcpservice/DhcpProvider.java
dhcpservice/dhcpservice-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/dhcpservice/impl/rev150710/DhcpServiceImplModule.java
dhcpservice/dhcpservice-impl/src/main/yang/dhcpservice-impl.yang
itm/itm-impl/src/main/config/default-config.xml
neutronvpn/neutronvpn-api/src/main/yang/neutronvpn.yang

index 9da75e9d82580d568e15c93507151a388b45431c..bcf8612da273a357949bf0a066407a0027a541f0 100644 (file)
@@ -34,4 +34,5 @@ public final class DHCPMConstants {
     public static final short dhcpServerPort = 67;
 
     public static final int DEFAULT_LEASE_TIME = 86400;
+    public static final String DEFAULT_DOMAIN_NAME = "openstacklocal";
 }
index d6c1c00ebe1192839526919a0db19aad40a6a0fd..862601fd601300bbcf702a4a0ce592b90e811776 100644 (file)
@@ -40,6 +40,16 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <artifactId>mdsalutil-api</artifactId>
       <version>${vpnservices.version}</version>
     </dependency>
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>neutronvpn-api</artifactId>
+      <version>${vpnservices.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.karaf.shell</groupId>
+      <artifactId>org.apache.karaf.shell.console</artifactId>
+      <version>${karaf.shell.console.version}</version>
+    </dependency>
     <dependency>
       <groupId>org.opendaylight.neutron</groupId>
       <artifactId>model</artifactId>
index 64b2bfaa29ce6d13318b79a3c8b871cb05a8037d..fe6008a4a2eb2258ca657388182d2e075625a286 100644 (file)
@@ -12,6 +12,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <capability>urn:opendaylight:params:xml:ns:yang:dhcpservice:impl?module=dhcpservice-impl&amp;revision=2015-07-10</capability>
       <capability>urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding?module=opendaylight-md-sal-binding&amp;revision=2013-10-28</capability>
       <capability>urn:opendaylight:params:xml:ns:yang:mdsalutil:api?module=odl-mdsalutil&amp;revision=2015-04-10</capability>
+      <capability>urn:opendaylight:params:xml:ns:yang:neutronvpn:api?module=neutronvpn-api&amp;revision=2015-08-12</capability>
   </required-capabilities>
   <configuration>
 
@@ -36,6 +37,10 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
             <type xmlns:mdsalutil="urn:opendaylight:params:xml:ns:yang:mdsalutil:api">mdsalutil:odl-mdsalutil</type>
             <name>mdsalutil-service</name>
           </mdsalutil>
+          <neutronvpn>
+            <type xmlns:neutronvpn="urn:opendaylight:params:xml:ns:yang:neutronvpn:api">neutronvpn:neutronvpn-api</type>
+            <name>neutronvpn</name>
+          </neutronvpn>
         </module>
       </modules>
     </data>
diff --git a/dhcpservice/dhcpservice-impl/src/main/java/org/opendaylight/vpnservice/dhcpservice/DhcpConfigListener.java b/dhcpservice/dhcpservice-impl/src/main/java/org/opendaylight/vpnservice/dhcpservice/DhcpConfigListener.java
new file mode 100644 (file)
index 0000000..57512b4
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.vpnservice.dhcpservice;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.dhcp.config.Configs;
+
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
+import org.opendaylight.vpnservice.datastoreutils.AsyncDataChangeListenerBase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DhcpConfig;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DhcpConfigListener extends AsyncDataChangeListenerBase<DhcpConfig, DhcpConfigListener> implements AutoCloseable {
+
+    private static final Logger LOG = LoggerFactory.getLogger(DhcpConfigListener.class);
+
+    private ListenerRegistration<DataChangeListener> listenerRegistration;
+    private DhcpManager dhcpManager;
+
+    public DhcpConfigListener(final DataBroker db, final DhcpManager dhcpMgr) {
+        super(DhcpConfig.class, DhcpConfigListener.class);
+        dhcpManager = dhcpMgr;
+        registerListener(db);
+    }
+
+    private void registerListener(final DataBroker db) {
+        try {
+            listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
+                    getWildCardPath(), DhcpConfigListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
+        } catch (final Exception e) {
+            LOG.error("NodeListener: DataChange listener registration fail!", e);
+            throw new IllegalStateException("NodeListener: registration Listener failed.", e);
+        }
+    }
+
+    protected InstanceIdentifier<DhcpConfig> getWildCardPath() {
+        return InstanceIdentifier.create(DhcpConfig.class);
+    }
+
+    @Override
+    public void close() throws Exception {
+        if (listenerRegistration != null) {
+            try {
+                listenerRegistration.close();
+            } catch (final Exception e) {
+                LOG.error("Error when cleaning up DhcpConfigListener.", e);
+            }
+            listenerRegistration = null;
+        }
+        LOG.debug("DhcpConfig Listener Closed");
+    }
+
+    @Override
+    protected void remove(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig del) {
+        LOG.trace("DhcpConfig removed: {}", del);
+        updateConfig(null);
+    }
+
+    @Override
+    protected void update(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig original, DhcpConfig update) {
+        LOG.trace("DhcpConfig changed to {}", update);
+        updateConfig(update);
+    }
+
+    @Override
+    protected void add(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig add) {
+        LOG.trace("DhcpConfig added {}", add);
+        updateConfig(add);
+    }
+
+    private void updateConfig(DhcpConfig update) {
+        //TODO: Update operational with actual values
+        if(update == null || update.getConfigs() == null || update.getConfigs().isEmpty()) {
+            dhcpManager.setLeaseDuration(DHCPMConstants.DEFAULT_LEASE_TIME);
+            dhcpManager.setDefaultDomain(DHCPMConstants.DEFAULT_DOMAIN_NAME);
+            return;
+        }
+        Configs config = update.getConfigs().get(0);
+        if(config.getLeaseDuration() != null) {
+            dhcpManager.setLeaseDuration(config.getLeaseDuration());
+        }
+        if(config.getDefaultDomain() != null) {
+            dhcpManager.setDefaultDomain(config.getDefaultDomain());
+            //TODO: What to do if string is ""
+        }
+    }
+
+    @Override
+    protected DataChangeListener getDataChangeListener() {
+        return DhcpConfigListener.this;
+    }
+
+    @Override
+    protected DataChangeScope getDataChangeScope() {
+        return AsyncDataBroker.DataChangeScope.BASE;
+    }
+}
index 458ad968210da082b0d554468a3e3b7878dd983e..f195fa89535403a816bd697467a208c52c70997a 100644 (file)
@@ -7,8 +7,9 @@
  */
 package org.opendaylight.vpnservice.dhcpservice;
 
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
+import org.opendaylight.vpnservice.neutronvpn.interfaces.INeutronVpnManager;
 
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
 import com.google.common.base.Optional;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -46,7 +47,8 @@ public class DhcpManager implements AutoCloseable {
     private int dhcpOptLeaseTime = 0;
     private int dhcpOptRenewalTime = 0;
     private int dhcpOptRebindingTime = 0;
-    private String dhcpOptDefDomainName = "openstacklocal";
+    private String dhcpOptDefDomainName;
+    private INeutronVpnManager neutronVpnService;
 
     private static final FutureCallback<Void> DEFAULT_CALLBACK =
         new FutureCallback<Void>() {
@@ -70,6 +72,11 @@ public class DhcpManager implements AutoCloseable {
         this.mdsalUtil = mdsalManager;
     }
 
+    public void setNeutronVpnService(INeutronVpnManager neutronVpnService) {
+        logger.debug("Setting NeutronVpn dependency");
+        this.neutronVpnService = neutronVpnService;
+    }
+
     @Override
     public void close() throws Exception {
         logger.info("DHCP Manager Closed");
@@ -150,7 +157,6 @@ public class DhcpManager implements AutoCloseable {
     }
 
     public Subnet getNeutronSubnet(Port nPort) {
-        /* TODO: Once NeutronVpn is merged, use it to get Subnet
         if (nPort != null) {
             try {
                 return neutronVpnService.getNeutronSubnet(nPort.getFixedIps().get(0).getSubnetId());
@@ -158,32 +164,11 @@ public class DhcpManager implements AutoCloseable {
                 logger.warn("Failed to get Neutron Subnet from Port: {}", e);
             }
         }
-        */
-        if (nPort.getFixedIps() != null && !nPort.getFixedIps().isEmpty()) {
-            InstanceIdentifier<Subnet> sIid =
-                            InstanceIdentifier.create(Neutron.class).child(Subnets.class)
-                                .child(Subnet.class, new SubnetKey(nPort.getFixedIps().get(0).getSubnetId()));
-            Optional<Subnet> optSubnet = MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, sIid, broker);
-            if (optSubnet.isPresent()) {
-                return optSubnet.get();
-            }
-        }
         return null;
     }
 
     public Port getNeutronPort(String name) {
-        // TODO Once NeutronVpn is merged, use it to get port
-        //return neutronVpnService.getNeutronPort(name);
-        InstanceIdentifier<Ports> pIid = InstanceIdentifier.create(Neutron.class).child(Ports.class);
-        Optional<Ports> optPorts = MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, pIid, broker);
-        if(optPorts.isPresent()) {
-            for(Port port: optPorts.get().getPort()) {
-                if(port.getUuid().getValue().startsWith(name.substring(3))) {
-                    return port;
-                }
-            }
-        }
-        return null;
+        return neutronVpnService.getNeutronPort(name);
     }
 
 }
index ce1a0a11c227c06c31555ab69aa5699841b2f8a0..5f15135fb3103bb5505ad1f2b48d3d165fe8bb0b 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.vpnservice.dhcpservice;
 
-import java.math.BigInteger;
+import org.opendaylight.vpnservice.neutronvpn.interfaces.INeutronVpnManager;
 
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
@@ -28,6 +28,8 @@ public class DhcpProvider implements BindingAwareProvider, AutoCloseable {
     private NotificationProviderService notificationService;
     private DhcpManager dhcpManager;
     private NodeListener dhcpNodeListener;
+    private INeutronVpnManager neutronVpnManager;
+    private DhcpConfigListener dhcpConfigListener;
 
     @Override
     public void onSessionInitiated(ProviderContext session) {
@@ -37,10 +39,12 @@ public class DhcpProvider implements BindingAwareProvider, AutoCloseable {
             final PacketProcessingService pktProcessingService = session.getRpcService(PacketProcessingService.class);
             dhcpManager = new DhcpManager(dataBroker);
             dhcpManager.setMdsalManager(mdsalManager);
+            dhcpManager.setNeutronVpnService(neutronVpnManager);
             dhcpPktHandler = new DhcpPktHandler(dataBroker, dhcpManager);
             dhcpPktHandler.setPacketProcessingService(pktProcessingService);
             packetListener = notificationService.registerNotificationListener(dhcpPktHandler);
             dhcpNodeListener = new NodeListener(dataBroker, dhcpManager);
+            dhcpConfigListener = new DhcpConfigListener(dataBroker, dhcpManager);
         } catch (Exception e) {
             LOG.error("Error initializing services", e);
         }
@@ -51,6 +55,10 @@ public class DhcpProvider implements BindingAwareProvider, AutoCloseable {
         this.mdsalManager = mdsalManager;
     }
 
+    public void setNeutronVpnManager(INeutronVpnManager neutronVpnManager) {
+        this.neutronVpnManager = neutronVpnManager;
+    }
+
     @Override
     public void close() throws Exception {
         if(packetListener != null) {
index bf453d425700d35ddc1799eb78109c5443edc767..eac19a9934d7f5b397c8d6f23abb795b81040558 100644 (file)
@@ -21,6 +21,7 @@ public class DhcpServiceImplModule extends org.opendaylight.yang.gen.v1.urn.open
         DhcpProvider dhcpProvider = new DhcpProvider();
         dhcpProvider.setNotificationProviderService(getNotificationServiceDependency());
         dhcpProvider.setMdsalManager(getMdsalutilDependency());
+        dhcpProvider.setNeutronVpnManager(getNeutronvpnDependency());
         getBrokerDependency().registerProvider(dhcpProvider);
         return dhcpProvider;
     }
index dcbec022c08d041a7a3d61e388c32f4be7d10a4c..856b7716dc26a1244c8aa30f0618474184208ebc 100644 (file)
@@ -6,6 +6,7 @@ module dhcpservice-impl {
     import config { prefix config; revision-date 2013-04-05; }
     import opendaylight-md-sal-binding { prefix md-sal-binding; revision-date 2013-10-28;}
     import odl-mdsalutil { prefix odl-mdsal; revision-date 2015-04-10;}
+    import neutronvpn-api { prefix nvpn; revision-date 2015-08-12;}
 
     description
         "Service definition for dhcpservice project";
@@ -56,6 +57,15 @@ module dhcpservice-impl {
                     }
                 }
             }
+
+            container neutronvpn {
+                uses config:service-ref {
+                    refine type {
+                        mandatory true;
+                        config:required-identity nvpn:neutronvpn-api;
+                    }
+                }
+            }
         }
     }
 }
index cb0636057c2e256a34c9c015b2f9a89cec879c1c..302d1f6ef2d6b6435beadb3e4f4ca77017304c90 100644 (file)
@@ -9,8 +9,8 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
 -->
 <snapshot>
   <required-capabilities>
-      <capability>urn:opendaylight:params:xml:ns:yang:itm?module=itm&amp;revision=2015-07-01</capability>
-      <capability>urn:opendaylight:params:xml:ns:yang:itm:impl?module=itm-impl&amp;revision=2014-12-10</capability>
+      <capability>urn:opendaylight:vpnservice:itm?module=itm&amp;revision=2015-07-01</capability>
+      <capability>urn:opendaylight:vpnservice:itm:impl?module=itm-impl&amp;revision=2014-12-10</capability>
       <capability>urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding?module=opendaylight-md-sal-binding&amp;revision=2013-10-28</capability>
       <capability>urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding:impl?module=opendaylight-sal-binding-broker-impl&amp;revision=2013-10-28</capability>
       <capability>urn:opendaylight:params:xml:ns:yang:mdsalutil:api?module=odl-mdsalutil&amp;revision=2015-04-10</capability>
@@ -21,7 +21,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
     <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
       <modules xmlns="urn:opendaylight:params:xml:ns:yang:controller:config">
         <module>
-          <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:itm:impl">prefix:itm</type>
+          <type xmlns:prefix="urn:opendaylight:vpnservice:itm:impl">prefix:itm</type>
           <name>itm-default</name>
           <broker>
             <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">binding:binding-broker-osgi-registry</type>
@@ -51,7 +51,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       </modules>
       <services xmlns="urn:opendaylight:params:xml:ns:yang:controller:config">
         <service>
-          <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:itm">prefix:itm</type>
+          <type xmlns:prefix="urn:opendaylight:vpnservice:itm">prefix:itm</type>
           <instance>
             <name>itm</name>
             <provider>/modules/module[type='itm'][name='itm-default']</provider>
index c178534066d44407ceefa838123d8594aa6f7bbf..bc306bbfd8ba33c4e310c27afcc867b085ae5792 100644 (file)
@@ -151,6 +151,21 @@ module neutronvpn {
         }
     }
 
+    /* container for DHCP Configuration */
+    container dhcp-config {
+        list configs {
+            leaf lease-duration {
+                type    int32;
+                description "default lease duration for dhcp lease.
+                             -1 means infinite";
+            }
+            leaf default-domain {
+                type string;
+                description "default domain-name. used in dhcp reply";
+            }
+        }
+    }
+
     rpc createL3VPN{
         description "Create one or more L3 VPN";
         input {