NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / rpcservice / NatRpcServiceImpl.java
index e7e25a14e65d318e3f2ee1eab7dbae2e78acf45d..8d182d7b3edc6f1d56be0f05f4d53ad96d0a7ad9 100644 (file)
@@ -8,15 +8,13 @@
 package org.opendaylight.netvirt.natservice.rpcservice;
 
 import com.google.common.util.concurrent.Futures;
-
+import com.google.common.util.concurrent.ListenableFuture;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.Future;
-
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.genius.mdsalutil.NwConstants;
+import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.netvirt.natservice.internal.NatConstants;
 import org.opendaylight.netvirt.natservice.internal.NatUtil;
 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
@@ -48,7 +46,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.s
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
-
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -66,7 +64,7 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
     }
 
     @Override
-    public Future<RpcResult<GetNatTranslationsOnVpnOutput>> getNatTranslationsOnVpn(
+    public ListenableFuture<RpcResult<GetNatTranslationsOnVpnOutput>> getNatTranslationsOnVpn(
             GetNatTranslationsOnVpnInput input) {
         RpcResultBuilder<GetNatTranslationsOnVpnOutput> rpcResultBuilder = null;
 
@@ -76,11 +74,11 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
                     input.getVpnUuid().getValue());
             rpcResultBuilder = RpcResultBuilder.<GetNatTranslationsOnVpnOutput>failed()
                     .withError(RpcError.ErrorType.APPLICATION, errMsg);
-            return Futures.immediateFuture(rpcResultBuilder.build());
+            return rpcResultBuilder.buildFuture();
         }
-        List<RouterNat> natRouterList = new ArrayList<RouterNat>();
+        List<RouterNat> natRouterList = new ArrayList<>();
         for (Uuid routerUuid : routerUuidList) {
-            long routerId = NatUtil.getVpnId(dataBroker, routerUuid.getValue());
+            Uint32 routerId = NatUtil.getVpnId(dataBroker, routerUuid.getValue());
             if (routerId == NatConstants.INVALID_ID) {
                 LOG.warn("getNatTranslationsOnVpn : Invalid RouterID found {}", routerId);
                 continue;
@@ -95,16 +93,16 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
     }
 
     @Override
-    public Future<RpcResult<GetNatTranslationsOnRouterOutput>> getNatTranslationsOnRouter(
+    public ListenableFuture<RpcResult<GetNatTranslationsOnRouterOutput>> getNatTranslationsOnRouter(
             GetNatTranslationsOnRouterInput input) {
         RpcResultBuilder<GetNatTranslationsOnRouterOutput> rpcResultBuilder = null;
-        long routerId = NatUtil.getVpnId(dataBroker, input.getRouterUuid().getValue());
+        Uint32 routerId = NatUtil.getVpnId(dataBroker, input.getRouterUuid().getValue());
         if (routerId == NatConstants.INVALID_ID) {
             String errMsg = String.format("404 Not Found - No Router found with UUID {%s}",
                     input.getRouterUuid().getValue());
             rpcResultBuilder = RpcResultBuilder.<GetNatTranslationsOnRouterOutput>failed()
                     .withError(RpcError.ErrorType.APPLICATION, errMsg);
-            return Futures.immediateFuture(rpcResultBuilder.build());
+            return rpcResultBuilder.buildFuture();
         }
 
         List<RouterNat> routerNatList = constructNatInformation(input.getRouterUuid(), routerId);
@@ -113,13 +111,14 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
                 .setRouterNat(routerNatList);
         rpcResultBuilder = RpcResultBuilder.success();
         rpcResultBuilder.withResult(output.build());
-        return Futures.immediateFuture(rpcResultBuilder.build());
+        return rpcResultBuilder.buildFuture();
     }
 
-    public Future<RpcResult<GetNatTranslationsForNetworkAndIpaddressOutput>> getNatTranslationsForNetworkAndIpaddress(
-            GetNatTranslationsForNetworkAndIpaddressInput input) {
+    @Override
+    public ListenableFuture<RpcResult<GetNatTranslationsForNetworkAndIpaddressOutput>>
+            getNatTranslationsForNetworkAndIpaddress(GetNatTranslationsForNetworkAndIpaddressInput input) {
 
-        String ipAddress = String.valueOf(input.getIpAddress().getValue());
+        String ipAddress = input.getIpAddress().stringValue();
         RpcResultBuilder<GetNatTranslationsForNetworkAndIpaddressOutput> rpcResultBuilder = null;
         GetNatTranslationsForNetworkAndIpaddressOutputBuilder output = null;
 
@@ -129,17 +128,17 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
                     input.getNetworkUuid().getValue());
             rpcResultBuilder = RpcResultBuilder.<GetNatTranslationsForNetworkAndIpaddressOutput>failed()
                     .withError(RpcError.ErrorType.APPLICATION, errMsg);
-            return Futures.immediateFuture(rpcResultBuilder.build());
+            return rpcResultBuilder.buildFuture();
         }
         Subnet subNet = null;
         Boolean isIpInSubnet = Boolean.FALSE;
         outerloop:
         for (Uuid subnetUuid: subnetUuidList) {
             subNet = nvpnManager.getNeutronSubnet(subnetUuid);
-            for (AllocationPools allocationPool : subNet.getAllocationPools()) {
+            for (AllocationPools allocationPool : subNet.nonnullAllocationPools()) {
                 if (NatUtil.isIpInSubnet(ipAddress,
-                        String.valueOf(allocationPool.getStart().getValue()),
-                        String.valueOf(allocationPool.getEnd().getValue()))) {
+                        allocationPool.getStart().stringValue(),
+                        allocationPool.getEnd().stringValue())) {
                     LOG.debug("getNatTranslationsForNetworkAndIpaddress : IP Adderess {} falls within the Subnet {}",
                             ipAddress, subNet.getUuid().getValue());
                     isIpInSubnet = Boolean.TRUE;
@@ -153,26 +152,25 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
                     + " of Network {%s}", ipAddress, input.getNetworkUuid().getValue());
             rpcResultBuilder = RpcResultBuilder.<GetNatTranslationsForNetworkAndIpaddressOutput>failed()
                     .withError(RpcError.ErrorType.APPLICATION, errMsg);
-            return Futures.immediateFuture(rpcResultBuilder.build());
+            return rpcResultBuilder.buildFuture();
         }
 
         Subnetmap subnetMap = NatUtil.getSubnetMap(dataBroker, subNet.getUuid());
-        long routerId = NatUtil.getVpnId(dataBroker, subnetMap.getRouterId().getValue());
+        Uint32 routerId = NatUtil.getVpnId(dataBroker, subnetMap.getRouterId().getValue());
 
         List<Ports> fipPorts = NatUtil.getFloatingIpPortsForRouter(dataBroker, subnetMap.getRouterId());
         if (fipPorts.isEmpty()) {
             LOG.warn("getNatTranslationsForNetworkAndIpaddress : No DNAT IP Mapping found for IP {}", ipAddress);
         } else {
             for (Ports fipPort : fipPorts) {
-                List<InternalToExternalPortMap> ipMapping = fipPort.getInternalToExternalPortMap();
-                for (InternalToExternalPortMap fipMap : ipMapping) {
+                for (InternalToExternalPortMap fipMap : fipPort.nonnullInternalToExternalPortMap()) {
                     if (fipMap.getInternalIp().equals(ipAddress)) {
                         output = new GetNatTranslationsForNetworkAndIpaddressOutputBuilder()
                                     .setExternalIp(fipMap.getExternalIp())
                                     .setNatTranslation("DNAT");
                         rpcResultBuilder = RpcResultBuilder.success();
                         rpcResultBuilder.withResult(output.build());
-                        return Futures.immediateFuture(rpcResultBuilder.build());
+                        return rpcResultBuilder.buildFuture();
                     }
                 }
             }
@@ -182,8 +180,8 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
         if (ipPortMapping == null) {
             LOG.warn("getNatTranslationsForNetworkAndIpaddress : No SNAT IP Mapping found for IP {}", ipAddress);
         } else {
-            for (IntextIpProtocolType protocolType : ipPortMapping.getIntextIpProtocolType()) {
-                for (IpPortMap ipPortMap : protocolType.getIpPortMap()) {
+            for (IntextIpProtocolType protocolType : ipPortMapping.nonnullIntextIpProtocolType()) {
+                for (IpPortMap ipPortMap : protocolType.nonnullIpPortMap()) {
                     String[] internalIpPort = ipPortMap.getIpPortInternal().split(NwConstants.MACADDR_SEP);
                     if (ipAddress.equals(internalIpPort[0])) {
 
@@ -196,7 +194,7 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
                                 .setProtocol(protocolType.getProtocol().getName());
                         rpcResultBuilder = RpcResultBuilder.success();
                         rpcResultBuilder.withResult(output.build());
-                        return Futures.immediateFuture(rpcResultBuilder.build());
+                        return rpcResultBuilder.buildFuture();
                     }
                 }
             }
@@ -205,10 +203,10 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
         String errMsg = String.format("404 Not Found - No NAT Translation found for IP {%s}", ipAddress);
         rpcResultBuilder = RpcResultBuilder.<GetNatTranslationsForNetworkAndIpaddressOutput>failed()
                 .withError(RpcError.ErrorType.APPLICATION, errMsg);
-        return Futures.immediateFuture(rpcResultBuilder.build());
+        return rpcResultBuilder.buildFuture();
     }
 
-    private List<RouterNat> constructNatInformation(Uuid routerUuid, long routerId) {
+    private List<RouterNat> constructNatInformation(Uuid routerUuid, Uint32 routerId) {
 
         String neutronRouterName = NatUtil.getNeutronRouterNamebyUuid(dataBroker, routerUuid);
 
@@ -222,10 +220,10 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
         } else {
 
             // Capturing SNAT information
-            List<SnatIpMapping> snatIpMapping = new ArrayList<SnatIpMapping>();
+            List<SnatIpMapping> snatIpMapping = new ArrayList<>();
 
-            for (IntextIpProtocolType protocolType : ipPortMapping.getIntextIpProtocolType()) {
-                for (IpPortMap ipPortMap : protocolType.getIpPortMap()) {
+            for (IntextIpProtocolType protocolType : ipPortMapping.nonnullIntextIpProtocolType()) {
+                for (IpPortMap ipPortMap : protocolType.nonnullIpPortMap()) {
                     String[] internalPortMap = ipPortMap.getIpPortInternal().split(NwConstants.MACADDR_SEP);
                     SnatIpMappingBuilder natIpMappingBuilder = new SnatIpMappingBuilder()
                             .setInternalIp(internalPortMap[0]).setInternalPort(internalPortMap[1])
@@ -239,14 +237,13 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
         }
 
         // Capturing DNAT information
-        List<DnatIpMapping> dnatIpMapping = new ArrayList<DnatIpMapping>();
+        List<DnatIpMapping> dnatIpMapping = new ArrayList<>();
         List<Ports> fipPorts = NatUtil.getFloatingIpPortsForRouter(dataBroker, routerUuid);
         if (fipPorts.isEmpty()) {
             LOG.warn("constructNatInformation : No DNAT IP Mapping found for router-uuid {}", routerUuid.getValue());
         } else {
             for (Ports fipPort : fipPorts) {
-                List<InternalToExternalPortMap> ipMapping = fipPort.getInternalToExternalPortMap();
-                for (InternalToExternalPortMap fipMap : ipMapping) {
+                for (InternalToExternalPortMap fipMap : fipPort.nonnullInternalToExternalPortMap()) {
                     DnatIpMappingBuilder natIpMappingBuilder = new DnatIpMappingBuilder()
                             .setExternalIp(fipMap.getExternalIp()).setInternalIp(fipMap.getInternalIp());
                     dnatIpMapping.add(natIpMappingBuilder.build());
@@ -255,8 +252,8 @@ public class NatRpcServiceImpl implements OdlNatRpcService {
             natRouterBuilder.setDnatIpMapping(dnatIpMapping);
         }
 
-        List<RouterNat> natRouterList = new ArrayList<RouterNat>();
+        List<RouterNat> natRouterList = new ArrayList<>();
         natRouterList.add(natRouterBuilder.build());
         return natRouterList;
     }
-}
\ No newline at end of file
+}