Merge dev/fluorine work across to master
[unimgr.git] / ovs-driver / src / main / java / org / opendaylight / unimgr / mef / nrp / ovs / activator / OvsActivatorHelper.java
index a728e267fb98ed1723dee4987ab0f4fb4c529fbc..2f8e52d0e27f8751e6896ddf120c00ddfbb2e899 100644 (file)
@@ -7,24 +7,27 @@
  */
 package org.opendaylight.unimgr.mef.nrp.ovs.activator;
 
+import static java.util.stream.Collectors.toSet;
+
+import com.google.common.collect.BiMap;
+import com.google.common.collect.HashBiMap;
+
 import java.util.List;
+import java.util.Optional;
+import java.util.Set;
 
 import org.opendaylight.unimgr.mef.nrp.api.EndPoint;
 import org.opendaylight.unimgr.mef.nrp.common.ResourceNotAvailableException;
-import org.opendaylight.unimgr.mef.nrp.ovs.exception.VlanNotSetException;
 import org.opendaylight.unimgr.mef.nrp.ovs.transaction.TopologyTransaction;
-import org.opendaylight.unimgr.utils.NullAwareDatastoreGetter;
-import org.opendaylight.yang.gen.v1.urn.mef.yang.nrm.connectivity.rev171221.carrier.eth.connectivity.end.point.resource.IngressBwpFlow;
-import org.opendaylight.yang.gen.v1.urn.mef.yang.nrp._interface.rev171221.nrp.connectivity.service.end.point.attrs.NrpCarrierEthConnectivityEndPointResource;
+import org.opendaylight.unimgr.mef.nrp.ovs.util.NullAwareDatastoreGetter;
+import org.opendaylight.yang.gen.v1.urn.mef.yang.nrm.connectivity.rev180321.carrier.eth.connectivity.end.point.resource.IngressBwpFlow;
+import org.opendaylight.yang.gen.v1.urn.mef.yang.nrp._interface.rev180321.nrp.connectivity.service.end.point.attrs.NrpCarrierEthConnectivityEndPointResource;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.BiMap;
-import com.google.common.collect.HashBiMap;
-
 /**
  * Helper class for OvsDriver activation.
  *
@@ -36,9 +39,11 @@ class OvsActivatorHelper {
     private String tpName;
     private BiMap<String, String> portMap;
 
-    private static final String CTAG_VLAN_ID_NOT_SET_ERROR_MESSAGE = "C-Tag VLAN Id not set for End Point '%s'.";
-    private static final String INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE = "Ingress bwp flow is not set for End Point '%s'.";
+    private static final String INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE
+            = "Ingress bwp flow is not set for End Point '%s'.";
     private static final String ATTRS_NOT_SET_ERROR_MESSAGE = "End Point '%s' does not have '%s' set.";
+    private static final String VLANS_DIFFERENT_ERROR_MESSAFE
+            = "External VLANs defined on end points has to be the same or not defined. Current values %s";
 
 
     private static final Logger LOG = LoggerFactory.getLogger(OvsActivatorHelper.class);
@@ -46,25 +51,31 @@ class OvsActivatorHelper {
     OvsActivatorHelper(TopologyTransaction topologyTransaction, EndPoint endPoint) {
         this.nodes = topologyTransaction.readNodes();
         this.endPoint = endPoint;
-        tpName = getPortName(endPoint.getEndpoint().getServiceInterfacePoint().getValue());
-        this.portMap = createPortMap(nodes);
+        tpName = getPortName(endPoint.getEndpoint().getServiceInterfacePoint().getServiceInterfacePointId().getValue());
+        this.portMap = createPortMap();
+    }
+
+    private OvsActivatorHelper(EndPoint endPoint) {
+        this.endPoint = endPoint;
     }
 
     /**
-     * Returns VLAN Id of the service
+     * Returns VLAN Id of the endPoint.
      *
-     * @return Integer with VLAN Id
+     * @return int with VLAN Id
      */
-    int getCeVlanId() throws ResourceNotAvailableException {
-
-        if ( (endPoint.getAttrs() != null) && (endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource() != null) ) {
-            NrpCarrierEthConnectivityEndPointResource attr = endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource();
-            if ( (attr.getCeVlanIdListAndUntag()!=null) && !(attr.getCeVlanIdListAndUntag().getVlanId().isEmpty()) ) {
+    Optional<Integer> getCeVlanId() throws ResourceNotAvailableException {
+
+        if ((endPoint.getAttrs() != null)
+                && (endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource() != null)) {
+            NrpCarrierEthConnectivityEndPointResource attr
+                    = endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource();
+            if ((attr.getCeVlanIdListAndUntag() != null)
+                    && !(attr.getCeVlanIdListAndUntag().getVlanId().isEmpty())) {
                 //for now we support only one CE VLAN
-                return attr.getCeVlanIdListAndUntag().getVlanId().get(0).getVlanId().getValue().intValue();
+                return Optional.of(attr.getCeVlanIdListAndUntag().getVlanId().get(0).getVlanId().getValue().intValue());
             } else {
-                LOG.warn(String.format(CTAG_VLAN_ID_NOT_SET_ERROR_MESSAGE, tpName));
-                throw new VlanNotSetException(String.format(CTAG_VLAN_ID_NOT_SET_ERROR_MESSAGE, tpName));
+                return Optional.empty(); //port-base service
             }
         } else {
             String className = NrpCarrierEthConnectivityEndPointResource.class.toString();
@@ -73,25 +84,6 @@ class OvsActivatorHelper {
         }
     }
 
-    /**
-     * Returns VLAN Id to be used internally in OvSwitch network
-     *
-     * @return Integer with VLAN Id
-     */
-    int getInternalVlanId() throws ResourceNotAvailableException {
-
-        return getCeVlanId();
-//             VlanUtils vlanUtils = new VlanUtils(nodes);
-//             Disable VLAN pool, refactor in the future
-//        if (vlanUtils.isVlanInUse(serviceVlanId)) {
-//            LOG.debug("VLAN ID = '" + serviceVlanId + "' already in use.");
-//            return vlanUtils.generateVlanID();
-//        } else {
-//            LOG.debug("VLAN ID = '" + serviceVlanId + "' not in use.");
-//            return serviceVlanId;
-//        }
-    }
-
     /**
      * Returns port name in openflow plugin naming convention (e.g. openflow:1:4)
      *
@@ -102,65 +94,80 @@ class OvsActivatorHelper {
     }
 
     /**
-     * Returns port name for specifiec port name in openflow convention
+     * Returns port name for specifiec port name in openflow convention.
      * @param openFlowPortName port in openflow plugin naming convention
      * @return String with port name
      */
     String getTpNameFromOpenFlowPortName(String openFlowPortName) {
-       return portMap.inverse().get(openFlowPortName);
+        return portMap.inverse().get(openFlowPortName);
     }
 
-    private BiMap<String, String> createPortMap(List<NullAwareDatastoreGetter<Node>> nodes) {
-       BiMap<String, String> portMap = HashBiMap.create();
+    private BiMap<String, String> createPortMap() {
+        BiMap<String, String> result = HashBiMap.create();
         for (NullAwareDatastoreGetter<Node> node : nodes) {
             if (node.get().isPresent()) {
                 for (NodeConnector nodeConnector : node.get().get().getNodeConnector()) {
                     String ofName = nodeConnector.getId().getValue();
-                    FlowCapableNodeConnector flowCapableNodeConnector = nodeConnector.getAugmentation(FlowCapableNodeConnector.class);
+                    FlowCapableNodeConnector flowCapableNodeConnector
+                            = nodeConnector.augmentation(FlowCapableNodeConnector.class);
                     String name = flowCapableNodeConnector.getName();
-                    portMap.put(name, ofName);
+                    result.put(name, ofName);
                 }
             }
         }
-        return portMap;
+        return result;
     }
 
     protected static String getPortName(String sip) {
         String[] tab = sip.split(":");
-        return tab[tab.length-1];
+        return tab[tab.length - 1];
     }
 
-       public long getQosMinRate() throws ResourceNotAvailableException {
+    public long getQosMinRate() throws ResourceNotAvailableException {
         IngressBwpFlow ingressBwpFlow = getIngressBwpFlow();
-        if(ingressBwpFlow != null) {
+        if (ingressBwpFlow != null) {
             //TODO add validation
             return ingressBwpFlow.getCir().getValue();
         }
 
-               LOG.warn(String.format(INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE, tpName));
+        LOG.warn(String.format(INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE, tpName));
         throw new ResourceNotAvailableException(String.format(INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE, tpName));
+    }
 
-       }
-
-       public long getQosMaxRate() throws ResourceNotAvailableException {
+    public long getQosMaxRate() throws ResourceNotAvailableException {
 
         IngressBwpFlow ingressBwpFlow = getIngressBwpFlow();
-        if(ingressBwpFlow != null) {
+        if (ingressBwpFlow != null) {
             //TODO add validation
             return ingressBwpFlow.getCir().getValue() + ingressBwpFlow.getEir().getValue();
         }
 
         LOG.warn(String.format(INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE, tpName));
         throw new ResourceNotAvailableException(String.format(INGRESS_BWP_FLOW_NOT_SET_ERROR_MESSAGE, tpName));
-       }
+    }
 
     private IngressBwpFlow getIngressBwpFlow() {
-        if ( (endPoint.getAttrs() == null) || (endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource() == null) )
+        if ((endPoint.getAttrs() == null)
+                || (endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource() == null)) {
             return null;
+        }
         return endPoint.getAttrs().getNrpCarrierEthConnectivityEndPointResource().getIngressBwpFlow();
     }
 
     protected boolean isIBwpConfigured() {
         return getIngressBwpFlow() != null;
     }
+
+    public static void validateExternalVLANs(List<EndPoint> endPoints) throws ResourceNotAvailableException {
+        Set<Optional> vlans = endPoints.stream().map(ep -> {
+            try {
+                return new OvsActivatorHelper(ep).getCeVlanId();
+            } catch (ResourceNotAvailableException e) {
+                return Optional.empty();
+            }
+        }).filter(i -> i.isPresent()).collect(toSet());
+        if (vlans.size() > 1) {
+            throw new ResourceNotAvailableException(String.format(VLANS_DIFFERENT_ERROR_MESSAFE, vlans.toString()));
+        }
+    }
 }