Bump odlparent->6.0.0,mdsal->5.0.3
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpManager.java
index 6f0f7616f7a0ac388a9f64dabf57a47f083fcacd..54fd5ba4cbce0cd5efe143f5d763eed1cfe8b38d 100644 (file)
@@ -7,16 +7,21 @@
  */
 package org.opendaylight.netvirt.dhcpservice;
 
-import java.math.BigInteger;
+import com.google.common.base.Optional;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.ExecutionException;
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Singleton;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.genius.infra.Datastore.Configuration;
+import org.opendaylight.genius.infra.TypedReadWriteTransaction;
+import org.opendaylight.genius.infra.TypedWriteTransaction;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.mdsalutil.ActionInfo;
 import org.opendaylight.genius.mdsalutil.FlowEntity;
@@ -32,11 +37,19 @@ import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
 import org.opendaylight.netvirt.elanmanager.api.IElanService;
-import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dhcpservice.config.rev150710.DhcpserviceConfig;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -45,7 +58,6 @@ public class DhcpManager {
 
     private static final Logger LOG = LoggerFactory.getLogger(DhcpManager.class);
     private final IMdsalApiManager mdsalUtil;
-    private final INeutronVpnManager neutronVpnService;
     private final DhcpserviceConfig config;
     private final DataBroker broker;
     private final DhcpExternalTunnelManager dhcpExternalTunnelManager;
@@ -53,6 +65,8 @@ public class DhcpManager {
     private final IElanService elanService;
     private final JobCoordinator jobCoordinator;
     private DhcpPortCache dhcpPortCache;
+    private final ItmRpcService itmRpcService;
+    private final DhcpServiceCounters dhcpServiceCounters;
 
     private volatile int dhcpOptLeaseTime = 0;
     private volatile String dhcpOptDefDomainName;
@@ -61,13 +75,12 @@ public class DhcpManager {
 
     @Inject
     public DhcpManager(final IMdsalApiManager mdsalApiManager,
-            final INeutronVpnManager neutronVpnManager,
             final DhcpserviceConfig config, final DataBroker dataBroker,
             final DhcpExternalTunnelManager dhcpExternalTunnelManager, final IInterfaceManager interfaceManager,
             @Named("elanService") IElanService ielanService, final DhcpPortCache dhcpPortCache,
-            final JobCoordinator jobCoordinator) {
+            final JobCoordinator jobCoordinator, final ItmRpcService itmRpcService,
+            DhcpServiceCounters dhcpServiceCounters) {
         this.mdsalUtil = mdsalApiManager;
-        this.neutronVpnService = neutronVpnManager;
         this.config = config;
         this.broker = dataBroker;
         this.dhcpExternalTunnelManager = dhcpExternalTunnelManager;
@@ -75,6 +88,8 @@ public class DhcpManager {
         this.elanService = ielanService;
         this.dhcpPortCache = dhcpPortCache;
         this.jobCoordinator = jobCoordinator;
+        this.itmRpcService = itmRpcService;
+        this.dhcpServiceCounters = dhcpServiceCounters;
         configureLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
     }
 
@@ -83,7 +98,7 @@ public class DhcpManager {
         LOG.trace("Netvirt DHCP Manager Init .... {}",config.isControllerDhcpEnabled());
         if (config.isControllerDhcpEnabled()) {
             dhcpInterfaceEventListener = new DhcpInterfaceEventListener(this, broker, dhcpExternalTunnelManager,
-                    interfaceManager, elanService, dhcpPortCache, jobCoordinator);
+                    interfaceManager, elanService, dhcpPortCache, jobCoordinator, itmRpcService);
             dhcpInterfaceConfigListener = new DhcpInterfaceConfigListener(broker, dhcpExternalTunnelManager, this,
                     jobCoordinator);
             LOG.info("DHCP Service initialized");
@@ -91,7 +106,7 @@ public class DhcpManager {
     }
 
     @PreDestroy
-    public void close() throws Exception {
+    public void close() {
         if (dhcpInterfaceEventListener != null) {
             dhcpInterfaceEventListener.close();
         }
@@ -131,6 +146,7 @@ public class DhcpManager {
         this.dhcpOptLeaseTime = leaseTime;
     }
 
+    @Nullable
     public Subnet getNeutronSubnet(Port port) {
         if (port != null) {
             // DHCP Service is only interested in IPv4 IPs/Subnets
@@ -139,41 +155,62 @@ public class DhcpManager {
         return null;
     }
 
+    @Nullable
     public Subnet getNeutronSubnet(List<FixedIps> fixedIps) {
         for (FixedIps fixedIp: fixedIps) {
             if (fixedIp.getIpAddress().getIpv4Address() != null) {
-                return neutronVpnService.getNeutronSubnet(fixedIp.getSubnetId());
+                return getNeutronSubnet(fixedIp.getSubnetId());
             }
         }
         return null;
     }
 
+    @Nullable
+    private Subnet getNeutronSubnet(Uuid subnetId) {
+        Subnet subnet = null;
+        InstanceIdentifier<Subnet> inst = InstanceIdentifier.create(Neutron.class).child(Subnets.class).child(Subnet
+                .class, new SubnetKey(subnetId));
+        Optional<Subnet> sn = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
+        if (sn.isPresent()) {
+            subnet = sn.get();
+        }
+        LOG.trace("Subnet {} = {}", subnetId, subnet);
+        return subnet;
+    }
+
+    @Nullable
     public Port getNeutronPort(String name) {
-        try {
-            return neutronVpnService.getNeutronPort(name);
-        } catch (IllegalArgumentException e) {
-            return null;
+        Port prt = null;
+        InstanceIdentifier<Port> inst = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class,
+                new PortKey(new Uuid(name)));
+        Optional<Port> port = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
+        if (port.isPresent()) {
+            prt = port.get();
         }
+        LOG.trace("Port {} = {}", name, prt);
+        return prt;
     }
 
-    public void installDhcpEntries(BigInteger dpnId, String vmMacAddress, WriteTransaction tx) {
+    public void installDhcpEntries(@Nullable Uint64 dpnId, @Nullable String vmMacAddress,
+            TypedReadWriteTransaction<Configuration> tx) throws ExecutionException, InterruptedException {
         DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.ADD_FLOW,
-                mdsalUtil, tx);
+                mdsalUtil, dhcpServiceCounters, tx);
     }
 
-    public void unInstallDhcpEntries(BigInteger dpId, String vmMacAddress, WriteTransaction tx) {
+    public void unInstallDhcpEntries(@Nullable Uint64 dpId, @Nullable String vmMacAddress,
+            TypedReadWriteTransaction<Configuration> tx) throws ExecutionException, InterruptedException {
         DhcpServiceUtils.setupDhcpFlowEntry(dpId, NwConstants.DHCP_TABLE, vmMacAddress, NwConstants.DEL_FLOW,
-                mdsalUtil, tx);
+                mdsalUtil, dhcpServiceCounters, tx);
     }
 
-    public void setupDefaultDhcpFlows(BigInteger dpId) {
-        setupTableMissForDhcpTable(dpId);
+    public void setupDefaultDhcpFlows(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
+        setupTableMissForDhcpTable(tx, dpId);
         if (config.isDhcpDynamicAllocationPoolEnabled()) {
-            setupDhcpAllocationPoolFlow(dpId);
+            setupDhcpAllocationPoolFlow(tx, dpId);
         }
     }
 
-    private void setupTableMissForDhcpTable(BigInteger dpId) {
+    private void setupTableMissForDhcpTable(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         List<MatchInfo> matches = new ArrayList<>();
         List<InstructionInfo> instructions = new ArrayList<>();
         List<ActionInfo> actionsInfos = new ArrayList<>();
@@ -182,12 +219,12 @@ public class DhcpManager {
         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.DHCP_TABLE, "DHCPTableMissFlow",
                 0, "DHCP Table Miss Flow", 0, 0,
                 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
-        DhcpServiceCounters.install_dhcp_table_miss_flow.inc();
-        mdsalUtil.installFlow(flowEntity);
-        setupTableMissForHandlingExternalTunnel(dpId);
+        dhcpServiceCounters.installDhcpTableMissFlow();
+        mdsalUtil.addFlow(tx, flowEntity);
+        setupTableMissForHandlingExternalTunnel(tx, dpId);
     }
 
-    private void setupDhcpAllocationPoolFlow(BigInteger dpId) {
+    private void setupDhcpAllocationPoolFlow(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         List<MatchInfo> matches = DhcpServiceUtils.getDhcpMatch();
         List<InstructionInfo> instructions = new ArrayList<>();
         List<ActionInfo> actionsInfos = new ArrayList<>();
@@ -197,11 +234,11 @@ public class DhcpManager {
                 "DhcpAllocationPoolFlow", DhcpMConstants.DEFAULT_DHCP_ALLOCATION_POOL_FLOW_PRIORITY,
                 "Dhcp Allocation Pool Flow", 0, 0, DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
         LOG.trace("Installing DHCP Allocation Pool Flow DpId {}", dpId);
-        DhcpServiceCounters.install_dhcp_flow.inc();
-        mdsalUtil.installFlow(flowEntity);
+        dhcpServiceCounters.installDhcpFlow();
+        mdsalUtil.addFlow(tx, flowEntity);
     }
 
-    private void setupTableMissForHandlingExternalTunnel(BigInteger dpId) {
+    private void setupTableMissForHandlingExternalTunnel(TypedWriteTransaction<Configuration> tx, Uint64 dpId) {
         List<MatchInfo> matches = new ArrayList<>();
         List<InstructionInfo> instructions = new ArrayList<>();
         instructions.add(new InstructionGotoTable(NwConstants.EXTERNAL_TUNNEL_TABLE));
@@ -210,7 +247,7 @@ public class DhcpManager {
                 "DHCPTableMissFlowForExternalTunnel",
                 0, "DHCP Table Miss Flow For External Tunnel", 0, 0,
                 DhcpMConstants.COOKIE_DHCP_BASE, matches, instructions);
-        DhcpServiceCounters.install_dhcp_table_miss_flow_for_external_table.inc();
-        mdsalUtil.installFlow(flowEntity);
+        dhcpServiceCounters.installDhcpTableMissFlowForExternalTable();
+        mdsalUtil.addFlow(tx, flowEntity);
     }
 }