2 * Copyright © 2017 AT&T and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.transportpce.common.mapping;
10 import static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_1_2_1;
11 import static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_2_2_1;
12 import static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_7_1_0;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.ReadTransaction;
20 import org.opendaylight.mdsal.binding.api.WriteTransaction;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.Network;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.Nodes;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.NodesKey;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.nodes.Mapping;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.nodes.MappingBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.nodes.MappingKey;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.nodes.McCapabilities;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.nodes.McCapabilitiesKey;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.nodes.NodeInfo.OpenroadmVersion;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class PortMappingImpl implements PortMapping {
38 private static final Logger LOG = LoggerFactory.getLogger(PortMappingImpl.class);
40 private final DataBroker dataBroker;
41 private final PortMappingVersion710 portMappingVersion710;
42 private final PortMappingVersion221 portMappingVersion22;
43 private final PortMappingVersion121 portMappingVersion121;
45 public PortMappingImpl(DataBroker dataBroker, PortMappingVersion710 portMappingVersion710,
46 PortMappingVersion221 portMappingVersion22, PortMappingVersion121 portMappingVersion121) {
48 this.dataBroker = dataBroker;
49 this.portMappingVersion710 = portMappingVersion710;
50 this.portMappingVersion22 = portMappingVersion22;
51 this.portMappingVersion121 = portMappingVersion121;
55 public boolean createMappingData(String nodeId, String nodeVersion) {
56 switch (nodeVersion) {
57 case OPENROADM_DEVICE_VERSION_1_2_1:
58 return portMappingVersion121.createMappingData(nodeId);
59 case OPENROADM_DEVICE_VERSION_2_2_1:
60 return portMappingVersion22.createMappingData(nodeId);
61 case OPENROADM_DEVICE_VERSION_7_1_0:
62 return portMappingVersion710.createMappingData(nodeId);
64 LOG.error("Unable to create mapping data for unmanaged openroadm device version");
72 public Mapping getMapping(String nodeId, String logicalConnPoint) {
75 * Getting physical mapping corresponding to logical connection point
77 InstanceIdentifier<Mapping> portMappingIID = InstanceIdentifier.builder(Network.class).child(Nodes.class,
78 new NodesKey(nodeId)).child(Mapping.class, new MappingKey(logicalConnPoint)).build();
79 try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
80 Optional<Mapping> mapObject = readTx.read(LogicalDatastoreType.CONFIGURATION, portMappingIID).get();
81 if (mapObject.isPresent()) {
82 Mapping mapping = mapObject.get();
83 LOG.info("Found mapping for {} - {}. Mapping: {}", nodeId, logicalConnPoint, mapping.toString());
86 LOG.warn("Could not find mapping for logical connection point {} for nodeId {}", logicalConnPoint, nodeId);
87 } catch (InterruptedException | ExecutionException ex) {
88 LOG.error("Unable to read mapping for logical connection point : {} for nodeId {}", logicalConnPoint,
96 public McCapabilities getMcCapbilities(String nodeId, String mcLcp) {
98 * Getting physical mapping corresponding to logical connection point
100 InstanceIdentifier<McCapabilities> mcCapabilitiesIID = InstanceIdentifier.builder(Network.class)
101 .child(Nodes.class, new NodesKey(nodeId)).child(McCapabilities.class, new McCapabilitiesKey(mcLcp)).build();
102 try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
103 Optional<McCapabilities> mcCapObject = readTx.read(LogicalDatastoreType.CONFIGURATION,
104 mcCapabilitiesIID).get();
105 if (mcCapObject.isPresent()) {
106 McCapabilities mcCap = mcCapObject.get();
107 LOG.info("Found MC-cap for {} - {}. Mapping: {}", nodeId, mcLcp, mcCap.toString());
110 LOG.warn("Could not find mapping for logical connection point {} for nodeId {}", mcLcp, nodeId);
111 } catch (InterruptedException | ExecutionException ex) {
112 LOG.error("Unable to read mapping for logical connection point : {} for nodeId {}", mcLcp,
120 public void deleteMappingData(String nodeId) {
121 LOG.info("Deleting Mapping Data corresponding at node '{}'", nodeId);
122 WriteTransaction rw = this.dataBroker.newWriteOnlyTransaction();
123 InstanceIdentifier<Nodes> nodesIID = InstanceIdentifier.create(Network.class)
124 .child(Nodes.class, new NodesKey(nodeId));
125 rw.delete(LogicalDatastoreType.CONFIGURATION, nodesIID);
127 rw.commit().get(1, TimeUnit.SECONDS);
128 LOG.info("Port mapping removal for node '{}'", nodeId);
129 } catch (InterruptedException | ExecutionException | TimeoutException e) {
130 LOG.error("Error for removing port mapping infos for node '{}'", nodeId);
136 public boolean updateMapping(String nodeId, Mapping oldMapping) {
137 OpenroadmVersion openROADMversion = getNode(nodeId).getNodeInfo().getOpenroadmVersion();
138 switch (openROADMversion.getIntValue()) {
140 return portMappingVersion121.updateMapping(nodeId, oldMapping);
142 MappingBuilder oldMapping2Bldr221 = new MappingBuilder()
143 .setLogicalConnectionPoint(oldMapping.getLogicalConnectionPoint())
144 .setPortDirection(oldMapping.getPortDirection());
145 if (oldMapping.getConnectionMapLcp() != null) {
146 oldMapping2Bldr221.setConnectionMapLcp(oldMapping.getConnectionMapLcp());
148 if (oldMapping.getPartnerLcp() != null) {
149 oldMapping2Bldr221.setPartnerLcp(oldMapping.getPartnerLcp());
151 if (oldMapping.getPortQual() != null) {
152 oldMapping2Bldr221.setPortQual(oldMapping.getPortQual());
154 if (oldMapping.getSupportingCircuitPackName() != null) {
155 oldMapping2Bldr221.setSupportingCircuitPackName(oldMapping.getSupportingCircuitPackName());
157 if (oldMapping.getSupportingOms() != null) {
158 oldMapping2Bldr221.setSupportingOms(oldMapping.getSupportingOms());
160 if (oldMapping.getSupportingOts() != null) {
161 oldMapping2Bldr221.setSupportingOts(oldMapping.getSupportingOts());
163 if (oldMapping.getSupportingPort() != null) {
164 oldMapping2Bldr221.setSupportingPort(oldMapping.getSupportingPort());
166 return portMappingVersion22.updateMapping(nodeId, oldMapping2Bldr221.build());
168 MappingBuilder oldMapping2Bldr710 = new MappingBuilder()
169 .setLogicalConnectionPoint(oldMapping.getLogicalConnectionPoint())
170 .setPortDirection(oldMapping.getPortDirection());
171 if (oldMapping.getConnectionMapLcp() != null) {
172 oldMapping2Bldr710.setConnectionMapLcp(oldMapping.getConnectionMapLcp());
174 if (oldMapping.getPartnerLcp() != null) {
175 oldMapping2Bldr710.setPartnerLcp(oldMapping.getPartnerLcp());
177 if (oldMapping.getPortQual() != null) {
178 oldMapping2Bldr710.setPortQual(oldMapping.getPortQual());
180 if (oldMapping.getSupportingCircuitPackName() != null) {
181 oldMapping2Bldr710.setSupportingCircuitPackName(oldMapping.getSupportingCircuitPackName());
183 if (oldMapping.getSupportingOms() != null) {
184 oldMapping2Bldr710.setSupportingOms(oldMapping.getSupportingOms());
186 if (oldMapping.getSupportingOts() != null) {
187 oldMapping2Bldr710.setSupportingOts(oldMapping.getSupportingOts());
189 if (oldMapping.getSupportingPort() != null) {
190 oldMapping2Bldr710.setSupportingPort(oldMapping.getSupportingPort());
192 return portMappingVersion710.updateMapping(nodeId, oldMapping2Bldr710.build());
199 public Nodes getNode(String nodeId) {
200 InstanceIdentifier<Nodes> nodePortMappingIID = InstanceIdentifier.builder(Network.class).child(Nodes.class,
201 new NodesKey(nodeId)).build();
202 try (ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction()) {
203 Optional<Nodes> nodePortMapObject =
204 readTx.read(LogicalDatastoreType.CONFIGURATION, nodePortMappingIID).get();
205 if (nodePortMapObject.isPresent()) {
206 Nodes node = nodePortMapObject.get();
207 LOG.info("Found node {} in portmapping.", nodeId);
210 LOG.warn("Could not find node {} in portmapping.", nodeId);
211 } catch (InterruptedException | ExecutionException ex) {
212 LOG.error("Unable to get node {} in portmapping", nodeId);