NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / evpn / manager / NeutronEvpnManager.java
index 33ef39844b375a6dc457beeae6b7afdd627f9bc3..749a6dc264584d8e6b63b8ee1e8e1ef8d046282c 100644 (file)
@@ -7,22 +7,22 @@
  */
 package org.opendaylight.netvirt.neutronvpn.evpn.manager;
 
-import com.google.common.base.Optional;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
 import java.util.function.Consumer;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netvirt.neutronvpn.NeutronvpnManager;
 import org.opendaylight.netvirt.neutronvpn.NeutronvpnUtils;
 import org.opendaylight.netvirt.vpnmanager.api.VpnHelper;
-import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTarget;
-import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
 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.netvirt.neutronvpn.l3vpn.rev200204.vpn.instances.VpnInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.instances.vpn.instance.vpntargets.VpnTarget;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.CreateEVPNInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.CreateEVPNOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.CreateEVPNOutputBuilder;
@@ -67,24 +67,19 @@ public class NeutronEvpnManager {
         SettableFuture<RpcResult<CreateEVPNOutput>> result = SettableFuture.create();
         List<RpcError> errorList = new ArrayList<>();
         int failurecount = 0;
-        int warningcount = 0;
         List<String> existingRDs = neutronvpnUtils.getExistingRDs();
 
-        List<Evpn> vpns = input.getEvpn();
-        for (Evpn vpn : vpns) {
+        for (Evpn vpn : input.nonnullEvpn()) {
             if (vpn.getRouteDistinguisher() == null || vpn.getImportRT() == null || vpn.getExportRT() == null) {
                 errorList.add(RpcResultBuilder.newWarning(RpcError.ErrorType.PROTOCOL, "invalid-input",
                         formatAndLog(LOG::warn, "Creation of EVPN failed for VPN {} due to absence of RD/iRT/eRT input",
                                 vpn.getId().getValue())));
-                warningcount++;
                 continue;
             }
-            VpnInstance.Type vpnInstanceType = VpnInstance.Type.L2;
             if (vpn.getRouteDistinguisher().size() > 1) {
                 errorList.add(RpcResultBuilder.newWarning(RpcError.ErrorType.PROTOCOL, "invalid-input",
                         formatAndLog(LOG::warn, "Creation of EVPN failed for VPN {} due to multiple RD input {}",
                                 vpn.getId().getValue(), vpn.getRouteDistinguisher())));
-                warningcount++;
                 continue;
             }
             if (existingRDs.contains(vpn.getRouteDistinguisher().get(0))) {
@@ -93,13 +88,18 @@ public class NeutronEvpnManager {
                                 "Creation of EVPN failed for VPN {} as another VPN with the same RD {} is already "
                                         + "configured",
                                 vpn.getId().getValue(), vpn.getRouteDistinguisher().get(0))));
-                warningcount++;
                 continue;
             }
             try {
-                neutronvpnManager.createVpn(vpn.getId(), vpn.getName(), vpn.getTenantId(), vpn.getRouteDistinguisher(),
-                        vpn.getImportRT(), vpn.getExportRT(), null /*router-id*/, null /*network-id*/,
-                        vpnInstanceType, 0 /*l2vni*/);
+                List<String> rdList = vpn.getRouteDistinguisher() != null
+                        ? new ArrayList<>(vpn.getRouteDistinguisher()) : new ArrayList<>();
+                List<String> importRdList = vpn.getImportRT() != null
+                        ? new ArrayList<>(vpn.getImportRT()) : new ArrayList<>();
+                List<String> exportRdList = vpn.getExportRT() != null
+                        ? new ArrayList<>(vpn.getExportRT()) : new ArrayList<>();
+                neutronvpnManager.createVpn(vpn.getId(), vpn.getName(), vpn.getTenantId(), rdList,
+                        importRdList, exportRdList, null /*router-id*/, null /*network-id*/,
+                        true /*isL2Vpn*/, 0 /*l2vni*/);
             } catch (Exception ex) {
                 errorList.add(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION,
                         formatAndLog(LOG::error, "Creation of EVPN failed for VPN {}", vpn.getId().getValue(), ex),
@@ -134,8 +134,8 @@ public class NeutronEvpnManager {
             vpns = VpnHelper.getAllVpnInstances(dataBroker);
             if (!vpns.isEmpty()) {
                 for (VpnInstance vpn : vpns) {
-                    if (vpn.getIpv4Family().getRouteDistinguisher() != null
-                            && vpn.getType() == VpnInstance.Type.L2) {
+                    if (vpn.getRouteDistinguisher() != null
+                            && vpn.isL2vpn()) {
                         vpns.add(vpn);
                     }
                 }
@@ -147,8 +147,8 @@ public class NeutronEvpnManager {
         } else {
             String name = inputVpnId.getValue();
             VpnInstance vpnInstance = VpnHelper.getVpnInstance(dataBroker, name);
-            if (vpnInstance != null && vpnInstance.getIpv4Family().getRouteDistinguisher() != null
-                    && vpnInstance.getType() == VpnInstance.Type.L2) {
+            if (vpnInstance != null && vpnInstance.getRouteDistinguisher() != null
+                    && vpnInstance.isL2vpn()) {
                 vpns.add(vpnInstance);
             } else {
                 result.set(RpcResultBuilder.<GetEVPNOutput>failed().withWarning(RpcError.ErrorType.PROTOCOL,
@@ -162,11 +162,10 @@ public class NeutronEvpnManager {
             InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class).child(VpnMap
                     .class, new VpnMapKey(vpnId)).build();
             EvpnInstancesBuilder evpn = new EvpnInstancesBuilder();
-            List<String> rd = vpnInstance.getIpv4Family().getRouteDistinguisher();
-            List<VpnTarget> vpnTargetList = vpnInstance.getIpv4Family().getVpnTargets().getVpnTarget();
+            List<String> rd = vpnInstance.getRouteDistinguisher();
             List<String> ertList = new ArrayList<>();
             List<String> irtList = new ArrayList<>();
-            for (VpnTarget vpnTarget : vpnTargetList) {
+            for (VpnTarget vpnTarget : vpnInstance.getVpnTargets().nonnullVpnTarget()) {
                 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ExportExtcommunity) {
                     ertList.add(vpnTarget.getVrfRTValue());
                 }
@@ -187,7 +186,7 @@ public class NeutronEvpnManager {
                     VpnMap vpnMap = optionalVpnMap.get();
                     evpn.setTenantId(vpnMap.getTenantId()).setName(vpnMap.getName());
                 }
-            } catch (ReadFailedException e) {
+            } catch (ExecutionException | InterruptedException e) {
                 LOG.error("Error reading the VPN map for {}", vpnMapIdentifier, e);
                 result.set(RpcResultBuilder.<GetEVPNOutput>failed().withError(RpcError.ErrorType.APPLICATION,
                         "Error reading the VPN map for " + vpnMapIdentifier, e).build());
@@ -202,40 +201,32 @@ public class NeutronEvpnManager {
     }
 
     public ListenableFuture<RpcResult<DeleteEVPNOutput>> deleteEVPN(DeleteEVPNInput input) {
-        DeleteEVPNOutputBuilder opBuilder = new DeleteEVPNOutputBuilder();
-        SettableFuture<RpcResult<DeleteEVPNOutput>> result = SettableFuture.create();
         List<RpcError> errorList = new ArrayList<>();
 
-        int failurecount = 0;
-        int warningcount = 0;
-        List<Uuid> vpns = input.getId();
-        for (Uuid vpn : vpns) {
-            RpcError error;
-            String msg;
-            VpnInstance vpnInstance = VpnHelper.getVpnInstance(dataBroker, vpn.getValue());
-            if (vpnInstance != null) {
-                neutronvpnManager.removeVpn(vpn);
-            } else {
-                errorList.add(RpcResultBuilder.newWarning(RpcError.ErrorType.PROTOCOL, "invalid-value",
+        if (input.getId() != null) {
+            for (Uuid vpn : input.getId()) {
+                VpnInstance vpnInstance = VpnHelper.getVpnInstance(dataBroker, vpn.getValue());
+                if (vpnInstance != null) {
+                    neutronvpnManager.removeVpn(vpn);
+                } else {
+                    errorList.add(RpcResultBuilder.newWarning(RpcError.ErrorType.PROTOCOL, "invalid-value",
                         formatAndLog(LOG::warn, "EVPN with vpnid: {} does not exist", vpn.getValue())));
-                warningcount++;
+                }
             }
         }
-        if (failurecount != 0) {
-            result.set(RpcResultBuilder.<DeleteEVPNOutput>failed().withRpcErrors(errorList).build());
-        } else {
-            List<String> errorResponseList = new ArrayList<>();
-            if (!errorList.isEmpty()) {
-                for (RpcError rpcError : errorList) {
-                    errorResponseList.add("ErrorType: " + rpcError.getErrorType() + ", ErrorTag: " + rpcError.getTag()
-                            + ", ErrorMessage: " + rpcError.getMessage());
-                }
-            } else {
-                errorResponseList.add("Deletion of EVPN operation successful");
+        List<String> errorResponseList = new ArrayList<>();
+        if (!errorList.isEmpty()) {
+            for (RpcError rpcError : errorList) {
+                errorResponseList.add("ErrorType: " + rpcError.getErrorType() + ", ErrorTag: " + rpcError.getTag()
+                        + ", ErrorMessage: " + rpcError.getMessage());
             }
-            opBuilder.setResponse(errorResponseList);
-            result.set(RpcResultBuilder.success(opBuilder.build()).build());
+        } else {
+            errorResponseList.add("Deletion of EVPN operation successful");
         }
+        DeleteEVPNOutputBuilder opBuilder = new DeleteEVPNOutputBuilder();
+        opBuilder.setResponse(errorResponseList);
+        SettableFuture<RpcResult<DeleteEVPNOutput>> result = SettableFuture.create();
+        result.set(RpcResultBuilder.success(opBuilder.build()).build());
         return result;
     }