Bug 7757 -Removing qos deprecations in carbon
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointUpdateCommand.java
index a7c788db57795b771f8fb245f25bac6b7bea3cfe..b027cb1ab7482f175310cb16857658f7908e86f5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ * Copyright © 2015, 2017 Brocade Communications Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,26 +8,42 @@
 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
 
 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
+import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
 
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
-
+import java.util.concurrent.ExecutionException;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
 import org.opendaylight.ovsdb.schema.openvswitch.Port;
+import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
+import org.opendaylight.ovsdb.southbound.SouthboundProvider;
 import org.opendaylight.ovsdb.utils.yang.YangUtils;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQosRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceBfd;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
@@ -35,47 +51,51 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.re
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.Sets;
-
-public class TerminationPointUpdateCommand extends AbstractTransactCommand {
+public class TerminationPointUpdateCommand implements TransactCommand {
 
     private static final Logger LOG = LoggerFactory.getLogger(TerminationPointUpdateCommand.class);
 
-    public TerminationPointUpdateCommand(BridgeOperationalState state,
-            AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
-        super(state, changes);
+    @Override
+    public void execute(TransactionBuilder transaction, BridgeOperationalState state,
+            AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events,
+            InstanceIdentifierCodec instanceIdentifierCodec) {
+        execute(transaction, state,
+                TransactUtils.extractCreatedOrUpdated(events, OvsdbTerminationPointAugmentation.class),
+                instanceIdentifierCodec);
     }
 
     @Override
-    public void execute(TransactionBuilder transaction) {
-        LOG.trace("TerminationPointUpdateCommand called");
-        Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> created =
-            TransactUtils.extractCreated(getChanges(),OvsdbTerminationPointAugmentation.class);
-        for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
-                 OvsdbTerminationPointAugmentation> terminationPointEntry : created.entrySet()) {
-            updateTerminationPoint(transaction, terminationPointEntry.getKey(), terminationPointEntry.getValue());
-        }
-        Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> updated =
-                TransactUtils.extractUpdated(getChanges(), OvsdbTerminationPointAugmentation.class);
+    public void execute(TransactionBuilder transaction, BridgeOperationalState state,
+            Collection<DataTreeModification<Node>> modifications, InstanceIdentifierCodec instanceIdentifierCodec) {
+        execute(transaction, state,
+                TransactUtils.extractCreatedOrUpdated(modifications, OvsdbTerminationPointAugmentation.class),
+                instanceIdentifierCodec);
+    }
+
+    private void execute(TransactionBuilder transaction, BridgeOperationalState state,
+            Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation>
+                    createdOrUpdated,
+            InstanceIdentifierCodec instanceIdentifierCodec) {
         for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
-                 OvsdbTerminationPointAugmentation> terminationPointEntry : updated.entrySet()) {
-            updateTerminationPoint(transaction, terminationPointEntry.getKey(),
-                    terminationPointEntry.getValue());
+                 OvsdbTerminationPointAugmentation> terminationPointEntry : createdOrUpdated.entrySet()) {
+            updateTerminationPoint(transaction, state, terminationPointEntry.getKey(),
+                    terminationPointEntry.getValue(), instanceIdentifierCodec);
         }
     }
 
-    public void updateTerminationPoint(TransactionBuilder transaction,
+    public void updateTerminationPoint(TransactionBuilder transaction, BridgeOperationalState state,
             InstanceIdentifier<OvsdbTerminationPointAugmentation> iid,
-            OvsdbTerminationPointAugmentation terminationPoint) {
-        if (terminationPoint instanceof OvsdbTerminationPointAugmentation) {
+            OvsdbTerminationPointAugmentation terminationPoint, InstanceIdentifierCodec instanceIdentifierCodec) {
+
+        if (terminationPoint != null) {
             LOG.debug("Received request to update termination point {}",
-                    terminationPoint.getName());
+                   terminationPoint.getName());
 
             // Update interface
             Interface ovsInterface =
@@ -89,18 +109,30 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
                     .build());
 
             TerminationPointCreateCommand.stampInstanceIdentifier(transaction,
-                    iid.firstIdentifierOf(TerminationPoint.class), terminationPoint.getName());
+                    iid.firstIdentifierOf(OvsdbTerminationPointAugmentation.class), terminationPoint.getName(),
+                    instanceIdentifierCodec);
 
             // Update port
-            Port port = TyperUtils.getTypedRowWrapper(
-                    transaction.getDatabaseSchema(), Port.class);
-            updatePort(terminationPoint,port);
-            Port extraPort = TyperUtils.getTypedRowWrapper(
-                    transaction.getDatabaseSchema(), Port.class);
-            extraPort.setName("");
-            transaction.add(op.update(port)
-                    .where(extraPort.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
-                    .build());
+            // Bug#6136
+            Optional<OvsdbBridgeAugmentation> ovsdbBridgeOptional = state.getOvsdbBridgeAugmentation(iid);
+            if (ovsdbBridgeOptional != null && ovsdbBridgeOptional.isPresent()) {
+                OvsdbBridgeAugmentation operBridge = ovsdbBridgeOptional.get();
+                if (operBridge != null) {
+                    Port port = TyperUtils.getTypedRowWrapper(
+                        transaction.getDatabaseSchema(), Port.class);
+                    updatePort(terminationPoint, port, operBridge);
+                    Port extraPort = TyperUtils.getTypedRowWrapper(
+                        transaction.getDatabaseSchema(), Port.class);
+                    extraPort.setName("");
+                    transaction.add(op.update(port)
+                        .where(extraPort.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
+                        .build());
+                    LOG.info("Updated Termination Point : {}  with Uuid : {}",
+                        terminationPoint.getName(), terminationPoint.getPortUuid());
+                }
+            } else {
+                LOG.warn("OVSDB bridge node was not found: {}", iid);
+            }
         }
     }
 
@@ -113,32 +145,68 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
         updateInterfaceOtherConfig(terminationPoint, ovsInterface);
         updateInterfaceExternalIds(terminationPoint, ovsInterface);
         updateInterfaceLldp(terminationPoint, ovsInterface);
+        updateInterfaceBfd(terminationPoint, ovsInterface);
+        updateInterfacePolicing(terminationPoint, ovsInterface);
     }
 
     private void updatePort(
             final OvsdbTerminationPointAugmentation terminationPoint,
-            final Port port) {
+            final Port port,
+            final OvsdbBridgeAugmentation operBridge) {
 
         updatePortOtherConfig(terminationPoint, port);
         updatePortVlanTag(terminationPoint, port);
         updatePortVlanTrunk(terminationPoint, port);
         updatePortVlanMode(terminationPoint, port);
         updatePortExternalIds(terminationPoint, port);
-        updatePortQos(terminationPoint, port);
+        updatePortQos(terminationPoint, port, operBridge);
     }
 
     private void updatePortQos(
             final OvsdbTerminationPointAugmentation terminationPoint,
-            final Port port) {
-
-        Set<UUID> uuidSet = Sets.newHashSet();
-        Uuid qosUuid = terminationPoint.getQos();
-        if (qosUuid != null) {
-            uuidSet.add(new UUID(qosUuid.getValue()));
+            final Port port,
+            final OvsdbBridgeAugmentation operBridge) {
+
+        Set<UUID> uuidSet = new HashSet<>();
+
+        // First check if QosEntry is present and use that
+        if (terminationPoint.getQosEntry() != null && !terminationPoint.getQosEntry().isEmpty()) {
+            OvsdbQosRef qosRef = terminationPoint.getQosEntry().iterator().next().getQosRef();
+            Uri qosId = qosRef.getValue().firstKeyOf(QosEntries.class).getQosId();
+            OvsdbNodeAugmentation operNode = getOperNode(operBridge);
+            if (operNode != null && operNode.getQosEntries() != null
+                    && !operNode.getQosEntries().isEmpty()) {
+                for (QosEntries qosEntry : operNode.getQosEntries()) {
+                    if (qosEntry.getQosId().equals(qosId)) {
+                        uuidSet.add(new UUID(qosEntry.getQosUuid().getValue()));
+                    }
+                }
+            }
+            if (uuidSet.size() == 0) {
+                uuidSet.add(new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
+                        + TransactUtils.bytesToHexString(qosId.getValue().getBytes())));
+            }
         }
         port.setQos(uuidSet);
     }
 
+    private OvsdbNodeAugmentation getOperNode(final OvsdbBridgeAugmentation operBridge) {
+        @SuppressWarnings("unchecked")
+        InstanceIdentifier<Node> iidNode = (InstanceIdentifier<Node>)operBridge.getManagedBy().getValue();
+        OvsdbNodeAugmentation operNode = null;
+        ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction();
+        CheckedFuture<Optional<Node>, ReadFailedException> future =
+                transaction.read(LogicalDatastoreType.OPERATIONAL, iidNode);
+        try {
+            Optional<Node> nodeOptional = future.get();
+            if (nodeOptional.isPresent()) {
+                operNode = nodeOptional.get().getAugmentation(OvsdbNodeAugmentation.class);
+            }
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.warn("Error reading from datastore", e);
+        }
+        return operNode;
+    }
 
     private void updateOfPort(
             final OvsdbTerminationPointAugmentation terminationPoint,
@@ -146,7 +214,7 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
 
         Long ofPort = terminationPoint.getOfport();
         if (ofPort != null) {
-            ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
+            ovsInterface.setOpenFlowPort(Collections.singleton(ofPort));
         }
     }
 
@@ -156,7 +224,7 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
 
         Integer ofPortRequest = terminationPoint.getOfportRequest();
         if (ofPortRequest != null) {
-            ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
+            ovsInterface.setOpenFlowPortRequest(Collections.singleton(ofPortRequest.longValue()));
         }
     }
 
@@ -207,7 +275,7 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
                 }
             }
         } catch (SchemaVersionMismatchException e) {
-            LOG.debug("lldp column for Interface Table unsupported for this version of ovsdb schema", e);
+            schemaMismatchLog("lldp", "Interface", e);
         }
     }
 
@@ -231,6 +299,40 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
         }
     }
 
+    private void updateInterfaceBfd(
+            final OvsdbTerminationPointAugmentation terminationPoint,
+            final Interface ovsInterface) {
+
+        try {
+            List<InterfaceBfd> interfaceBfdList =
+                    terminationPoint.getInterfaceBfd();
+            if (interfaceBfdList != null && !interfaceBfdList.isEmpty()) {
+                try {
+                    ovsInterface.setBfd(YangUtils.convertYangKeyValueListToMap(interfaceBfdList,
+                            InterfaceBfd::getBfdKey, InterfaceBfd::getBfdValue));
+                } catch (NullPointerException e) {
+                    LOG.warn("Incomplete OVSDB interface bfd", e);
+                }
+            }
+        } catch (SchemaVersionMismatchException e) {
+            schemaMismatchLog("bfd", "Interface", e);
+        }
+    }
+
+    private void updateInterfacePolicing(
+            final OvsdbTerminationPointAugmentation terminationPoint,
+            final Interface ovsInterface) {
+
+        Long ingressPolicingRate = terminationPoint.getIngressPolicingRate();
+        if (ingressPolicingRate != null) {
+            ovsInterface.setIngressPolicingRate(ingressPolicingRate);
+        }
+        Long ingressPolicingBurst = terminationPoint.getIngressPolicingBurst();
+        if (ingressPolicingBurst != null) {
+            ovsInterface.setIngressPolicingBurst(ingressPolicingBurst);
+        }
+    }
+
     private void updatePortExternalIds(
             final OvsdbTerminationPointAugmentation terminationPoint,
             final Port port) {
@@ -264,7 +366,7 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
         if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
             Set<Long> portTrunks = new HashSet<>();
             List<Trunks> modelTrunks = terminationPoint.getTrunks();
-            for (Trunks trunk: modelTrunks) {
+            for (Trunks trunk : modelTrunks) {
                 if (trunk.getTrunk() != null) {
                     portTrunks.add(trunk.getTrunk().getValue().longValue());
                 }
@@ -279,7 +381,7 @@ public class TerminationPointUpdateCommand extends AbstractTransactCommand {
         if (terminationPoint.getVlanMode() != null) {
             Set<String> portVlanMode = new HashSet<>();
             VlanMode modelVlanMode = terminationPoint.getVlanMode();
-            portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
+            portVlanMode.add(SouthboundConstants.VlanModes.values()[modelVlanMode.getIntValue() - 1].getMode());
             port.setVlanMode(portVlanMode);
         }
     }