Creation of OTS-OMS interfaces
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingImpl.java
index 3dc9f3f6be339f7ba87045191f2476e46c586c8f..d523c1a28ce2098177157c47954e624cd283a9b0 100644 (file)
@@ -9,7 +9,6 @@
 package org.opendaylight.transportpce.common.mapping;
 
 import com.google.common.util.concurrent.CheckedFuture;
-
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -22,7 +21,6 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
-
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
@@ -405,7 +403,7 @@ public class PortMappingImpl implements PortMapping {
     private Mapping createMappingObject(String nodeId, Ports port, String circuitPackName,
         String logicalConnectionPoint) {
         MappingBuilder mpBldr = new MappingBuilder();
-        mpBldr.setKey(new MappingKey(logicalConnectionPoint)).setLogicalConnectionPoint(logicalConnectionPoint)
+        mpBldr.withKey(new MappingKey(logicalConnectionPoint)).setLogicalConnectionPoint(logicalConnectionPoint)
             .setSupportingCircuitPackName(circuitPackName).setSupportingPort(port.getPortName());
 
         // Get OMS and OTS interface provisioned on the TTP's
@@ -436,7 +434,7 @@ public class PortMappingImpl implements PortMapping {
     }
 
     private static CpToDegree createCpToDegreeObject(String circuitPackName, String degreeNumber) {
-        return new CpToDegreeBuilder().setKey(new CpToDegreeKey(circuitPackName)).setCircuitPackName(circuitPackName)
+        return new CpToDegreeBuilder().withKey(new CpToDegreeKey(circuitPackName)).setCircuitPackName(circuitPackName)
             .setDegreeNumber(new Long(degreeNumber)).build();
     }
 
@@ -476,7 +474,7 @@ public class PortMappingImpl implements PortMapping {
     private boolean postPortMapping(Info deviceInfo, List<Mapping> portMapList, Integer nodeType,
         List<CpToDegree> cp2DegreeList) {
         NodesBuilder nodesBldr = new NodesBuilder();
-        nodesBldr.setKey(new NodesKey(deviceInfo.getNodeId())).setNodeId(deviceInfo.getNodeId());
+        nodesBldr.withKey(new NodesKey(deviceInfo.getNodeId())).setNodeId(deviceInfo.getNodeId());
         nodesBldr.setNodeType(NodeTypes.forValue(nodeType));
 
         if (portMapList != null) {
@@ -592,4 +590,41 @@ public class PortMappingImpl implements PortMapping {
 
     }
 
+    @Override
+    public boolean updateMapping(String nodeId, Mapping oldMapping) {
+        InstanceIdentifier<Ports> portIId = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
+            CircuitPacks.class, new CircuitPacksKey(oldMapping.getSupportingCircuitPackName())).child(Ports.class,
+                new PortsKey(oldMapping.getSupportingPort()));
+        if (oldMapping != null && nodeId != null) {
+            try {
+                Optional<Ports> portObject = this.deviceTransactionManager.getDataFromDevice(nodeId,
+                    LogicalDatastoreType.OPERATIONAL, portIId, Timeouts.DEVICE_READ_TIMEOUT,
+                    Timeouts.DEVICE_READ_TIMEOUT_UNIT);
+                if (portObject.isPresent()) {
+                    Ports port = portObject.get();
+                    Mapping newMapping = createMappingObject(nodeId, port, oldMapping.getSupportingCircuitPackName(),
+                        oldMapping.getLogicalConnectionPoint());
+
+                    final WriteTransaction writeTransaction = this.dataBroker.newWriteOnlyTransaction();
+                    InstanceIdentifier<Mapping> mapIID = InstanceIdentifier.create(Network.class).child(Nodes.class,
+                        new NodesKey(nodeId)).child(Mapping.class, new MappingKey(oldMapping
+                            .getLogicalConnectionPoint()));
+                    writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, mapIID, newMapping);
+                    CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
+                    submit.checkedGet();
+                    return true;
+                }
+                return false;
+            } catch (TransactionCommitFailedException e) {
+                LOG.error("Transaction Commit Error updating Mapping {} for node {}", oldMapping
+                    .getLogicalConnectionPoint(), nodeId, e);
+                return false;
+            }
+        } else {
+            LOG.error("Impossible to update mapping");
+            return false;
+        }
+
+    }
+
 }