MRI version bumpup for Aluminium
[netvirt.git] / fibmanager / impl / src / main / java / org / opendaylight / netvirt / fibmanager / FibRpcServiceImpl.java
index 35c16317270551016c7f312e6552b9aed6bb374d..f0b6563abf49079e7e86f63f3214d8579db1c1aa 100644 (file)
@@ -7,21 +7,19 @@
  */
 package org.opendaylight.netvirt.fibmanager;
 
-import static org.opendaylight.netvirt.fibmanager.FibConstants.DEFAULT_FIB_FLOW_PRIORITY;
-import static org.opendaylight.netvirt.fibmanager.FibConstants.FLOWID_PREFIX;
-
 import com.google.common.util.concurrent.ListenableFuture;
-import java.math.BigInteger;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.MatchInfo;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
@@ -30,10 +28,13 @@ import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
 import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Destination;
 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
 import org.opendaylight.netvirt.vpnmanager.api.IVpnFootprintService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fib.rpc.rev160121.CleanupDpnForVpnInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fib.rpc.rev160121.CleanupDpnForVpnOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fib.rpc.rev160121.CleanupDpnForVpnOutputBuilder;
@@ -54,6 +55,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -80,15 +83,15 @@ public class FibRpcServiceImpl implements FibRpcService {
     @Override
     public ListenableFuture<RpcResult<CreateFibEntryOutput>> createFibEntry(CreateFibEntryInput input) {
 
-        BigInteger dpnId = input.getSourceDpid();
+        Uint64 dpnId = input.getSourceDpid();
         String vpnName = input.getVpnName();
-        long vpnId = getVpnId(dataBroker, vpnName);
+        Uint32 vpnId = getVpnId(dataBroker, vpnName);
         String vpnRd = getVpnRd(dataBroker, vpnName);
         String ipAddress = input.getIpAddress();
         LOG.info("Create custom FIB entry - {} on dpn {} for VPN {} ", ipAddress, dpnId, vpnName);
-        List<Instruction> instructions = input.getInstruction();
+        Map<InstructionKey, Instruction> instructionMap = input.getInstruction();
         LOG.info("ADD: Adding Custom Fib Entry rd {} prefix {} label {}", vpnRd, ipAddress, input.getServiceId());
-        makeLocalFibEntry(vpnId, dpnId, ipAddress, instructions);
+        makeLocalFibEntry(vpnId, dpnId, ipAddress, new ArrayList<Instruction>(instructionMap.values()));
         IpAddresses.IpAddressSource ipAddressSource = IpAddresses.IpAddressSource
                 .forValue(input.getIpAddressSource().getIntValue());
         vpnFootprintService.updateVpnToDpnMapping(dpnId, vpnName, vpnRd, null /* interfaceName*/,
@@ -102,9 +105,9 @@ public class FibRpcServiceImpl implements FibRpcService {
      */
     @Override
     public ListenableFuture<RpcResult<RemoveFibEntryOutput>> removeFibEntry(RemoveFibEntryInput input) {
-        BigInteger dpnId = input.getSourceDpid();
+        Uint64 dpnId = input.getSourceDpid();
         String vpnName = input.getVpnName();
-        long vpnId = getVpnId(dataBroker, vpnName);
+        Uint32 vpnId = getVpnId(dataBroker, vpnName);
         String vpnRd = getVpnRd(dataBroker, vpnName);
 
         String ipAddress = input.getIpAddress();
@@ -133,7 +136,7 @@ public class FibRpcServiceImpl implements FibRpcService {
         return RpcResultBuilder.success(new CleanupDpnForVpnOutputBuilder().build()).buildFuture();
     }
 
-    private void removeLocalFibEntry(BigInteger dpnId, long vpnId, String ipPrefix) {
+    private void removeLocalFibEntry(Uint64 dpnId, Uint32 vpnId, String ipPrefix) {
         String[] values = ipPrefix.split("/");
         String ipAddress = values[0];
         int prefixLength = values.length == 1 ? 0 : Integer.parseInt(values[1]);
@@ -148,7 +151,8 @@ public class FibRpcServiceImpl implements FibRpcService {
         }
         List<MatchInfo> matches = new ArrayList<>();
 
-        matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID));
+        matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(vpnId.longValue()),
+            MetaDataUtil.METADATA_MASK_VRFID));
 
         matches.add(MatchEthernetType.IPV4);
 
@@ -159,7 +163,7 @@ public class FibRpcServiceImpl implements FibRpcService {
         String flowRef = getFlowRef(dpnId, NwConstants.L3_FIB_TABLE, vpnId, ipAddress);
 
 
-        int priority = DEFAULT_FIB_FLOW_PRIORITY + prefixLength;
+        int priority = FibConstants.DEFAULT_FIB_FLOW_PRIORITY + prefixLength;
         Flow flowEntity = MDSALUtil.buildFlowNew(NwConstants.L3_FIB_TABLE, flowRef,
             priority, flowRef, 0, 0,
             NwConstants.COOKIE_VM_FIB_TABLE, matches, null);
@@ -169,7 +173,7 @@ public class FibRpcServiceImpl implements FibRpcService {
         LOG.info("FIB entry for prefix {} on dpn {} vpn {} removed successfully", ipAddress, dpnId,  vpnId);
     }
 
-    private void makeLocalFibEntry(long vpnId, BigInteger dpnId, String ipPrefix,
+    private void makeLocalFibEntry(Uint32 vpnId, Uint64 dpnId, String ipPrefix,
                                    List<Instruction> customInstructions) {
         String[] values = ipPrefix.split("/");
         String ipAddress = values[0];
@@ -185,7 +189,8 @@ public class FibRpcServiceImpl implements FibRpcService {
         }
         List<MatchInfo> matches = new ArrayList<>();
 
-        matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID));
+        matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(vpnId.longValue()),
+            MetaDataUtil.METADATA_MASK_VRFID));
 
         matches.add(MatchEthernetType.IPV4);
 
@@ -196,26 +201,36 @@ public class FibRpcServiceImpl implements FibRpcService {
         String flowRef = getFlowRef(dpnId, NwConstants.L3_FIB_TABLE, vpnId, ipAddress);
 
 
-        int priority = DEFAULT_FIB_FLOW_PRIORITY + prefixLength;
+        int priority = FibConstants.DEFAULT_FIB_FLOW_PRIORITY + prefixLength;
+        Map<InstructionKey, Instruction> customInstructionsMap = new HashMap<InstructionKey, Instruction>();
+        int instructionKey = 0;
+        for (Instruction instructionObj : customInstructions) {
+            customInstructionsMap.put(new InstructionKey(++instructionKey), instructionObj);
+        }
         Flow flowEntity = MDSALUtil.buildFlowNew(NwConstants.L3_FIB_TABLE, flowRef,
             priority, flowRef, 0, 0,
-            NwConstants.COOKIE_VM_FIB_TABLE, matches, customInstructions);
+            NwConstants.COOKIE_VM_FIB_TABLE, matches, customInstructionsMap);
         mdsalManager.installFlow(dpnId, flowEntity);
 
         LOG.debug("FIB entry for route {} on dpn {} installed successfully - flow {}", ipAddress, dpnId, flowEntity);
     }
 
-    private String getFlowRef(BigInteger dpnId, short tableId, long id, String ipAddress) {
-        return FLOWID_PREFIX + dpnId + NwConstants.FLOWID_SEPARATOR + tableId + NwConstants.FLOWID_SEPARATOR + id
-                + NwConstants.FLOWID_SEPARATOR + ipAddress;
+    private String getFlowRef(Uint64 dpnId, short tableId, Uint32 id, String ipAddress) {
+        return FibConstants.FLOWID_PREFIX + dpnId + NwConstants.FLOWID_SEPARATOR + tableId
+                + NwConstants.FLOWID_SEPARATOR + id + NwConstants.FLOWID_SEPARATOR + ipAddress;
     }
 
     //TODO: Below Util methods to be removed once VpnUtil methods are exposed in api bundle
     @Nullable
     public static String getVpnRd(DataBroker broker, String vpnName) {
         InstanceIdentifier<VpnInstance> id = getVpnInstanceToVpnIdIdentifier(vpnName);
-        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id).toJavaUtil().map(
-                VpnInstance::getVrfId).orElse(null);
+        try {
+            return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION, id)
+                    .map(VpnInstance::getVrfId).orElse(null);
+        } catch (ExecutionException | InterruptedException e) {
+            LOG.error("getVpnRd: Exception while reading VpnInstance DS for the vpn {}", vpnName, e);
+        }
+        return null;
     }
 
     static InstanceIdentifier<VpnInstance> getVpnInstanceToVpnIdIdentifier(String vpnName) {
@@ -224,9 +239,14 @@ public class FibRpcServiceImpl implements FibRpcService {
     }
 
 
-    static long getVpnId(DataBroker broker, String vpnName) {
+    static Uint32 getVpnId(DataBroker broker, String vpnName) {
         InstanceIdentifier<VpnInstance> id = getVpnInstanceToVpnIdIdentifier(vpnName);
-        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id).toJavaUtil().map(
-                VpnInstance::getVpnId).orElse(-1L);
+        try {
+            return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION, id)
+                    .map(VpnInstance::getVpnId).orElse(Uint32.ZERO);
+        } catch (ExecutionException | InterruptedException e) {
+            LOG.error("getVpnId: Exception while reading VpnInstance DS for the vpn {}", vpnName, e);
+        }
+        return Uint32.ZERO;
     }
 }