TapiLink creation refactoring
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / topology / TapiNetworkModelServiceImpl.java
1 /*
2  * Copyright © 2021 Nokia, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.tapi.topology;
9
10 import java.nio.charset.Charset;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.Comparator;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Optional;
19 import java.util.UUID;
20 import java.util.concurrent.ExecutionException;
21 import java.util.stream.Collectors;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
24 import org.opendaylight.transportpce.tapi.R2RTapiLinkDiscovery;
25 import org.opendaylight.transportpce.tapi.TapiStringConstants;
26 import org.opendaylight.transportpce.tapi.utils.TapiLink;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.mapping.Mapping;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.network.Nodes;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.NodeTypes;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.types.rev191129.XpdrNodeTypes;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.types.rev200327.xpdr.odu.switching.pools.OduSwitchingPools;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.types.rev200327.xpdr.odu.switching.pools.OduSwitchingPoolsBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.types.rev200327.xpdr.odu.switching.pools.odu.switching.pools.NonBlockingList;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.types.rev200327.xpdr.odu.switching.pools.odu.switching.pools.NonBlockingListBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.types.rev200327.xpdr.odu.switching.pools.odu.switching.pools.NonBlockingListKey;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmNodeType;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.xpdr.tp.supported.interfaces.SupportedInterfaceCapability;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.xpdr.tp.supported.interfaces.SupportedInterfaceCapabilityBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.xpdr.tp.supported.interfaces.SupportedInterfaceCapabilityKey;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.If100GE;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.If100GEODU4;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.If10GE;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.If10GEODU2;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.If10GEODU2e;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.If1GE;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.If1GEODU0;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.IfOCH;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.IfOCHOTU4ODU4;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.port.types.rev200327.SupportedIfCapability;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.switching.pool.types.rev191129.SwitchingPoolTypes;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.TpId;
55 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.AdministrativeState;
56 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.Context;
57 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.ContextBuilder;
58 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.LAYERPROTOCOLQUALIFIER;
59 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.LayerProtocolName;
60 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.LifecycleState;
61 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.OperationalState;
62 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.PortDirection;
63 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.PortRole;
64 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.TerminationDirection;
65 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.TerminationState;
66 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.Uuid;
67 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.capacity.pac.AvailableCapacityBuilder;
68 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.capacity.pac.TotalPotentialCapacityBuilder;
69 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.Name;
70 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.NameBuilder;
71 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.NameKey;
72 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.tapi.context.ServiceInterfacePoint;
73 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.tapi.context.ServiceInterfacePointBuilder;
74 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.tapi.context.ServiceInterfacePointKey;
75 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection;
76 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectionBuilder;
77 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectionKey;
78 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectivityService;
79 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectivityServiceBuilder;
80 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectivityServiceKey;
81 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.EndPoint;
82 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.EndPointKey;
83 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.context.ConnectivityContext;
84 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.dsr.rev181210.DIGITALSIGNALTYPE100GigE;
85 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.dsr.rev181210.DIGITALSIGNALTYPE10GigELAN;
86 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.dsr.rev181210.DIGITALSIGNALTYPEGigE;
87 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.odu.rev181210.ODUTYPEODU0;
88 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.odu.rev181210.ODUTYPEODU2;
89 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.odu.rev181210.ODUTYPEODU2E;
90 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.odu.rev181210.ODUTYPEODU4;
91 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.photonic.media.rev181210.PHOTONICLAYERQUALIFIEROMS;
92 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.photonic.media.rev181210.PHOTONICLAYERQUALIFIEROTSi;
93 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1;
94 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.ForwardingRule;
95 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.RuleType;
96 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext;
97 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.NodeRuleGroup;
98 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.NodeRuleGroupBuilder;
99 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.NodeRuleGroupKey;
100 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.OwnedNodeEdgePoint;
101 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.OwnedNodeEdgePointBuilder;
102 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.OwnedNodeEdgePointKey;
103 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.edge.point.MappedServiceInterfacePoint;
104 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.edge.point.MappedServiceInterfacePointBuilder;
105 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.edge.point.MappedServiceInterfacePointKey;
106 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.Rule;
107 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.RuleBuilder;
108 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.RuleKey;
109 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.risk.parameter.pac.RiskCharacteristic;
110 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.risk.parameter.pac.RiskCharacteristicBuilder;
111 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Link;
112 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.LinkKey;
113 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Node;
114 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.NodeBuilder;
115 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.NodeKey;
116 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.Topology;
117 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.TopologyBuilder;
118 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.TopologyKey;
119 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.transfer.cost.pac.CostCharacteristic;
120 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.transfer.cost.pac.CostCharacteristicBuilder;
121 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.transfer.timing.pac.LatencyCharacteristic;
122 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.transfer.timing.pac.LatencyCharacteristicBuilder;
123 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
124 import org.opendaylight.yangtools.yang.common.Uint16;
125 import org.opendaylight.yangtools.yang.common.Uint32;
126 import org.slf4j.Logger;
127 import org.slf4j.LoggerFactory;
128
129 public class TapiNetworkModelServiceImpl implements TapiNetworkModelService {
130
131     private static final Logger LOG = LoggerFactory.getLogger(TapiNetworkModelServiceImpl.class);
132     private final Uuid tapiTopoUuid = new Uuid(UUID.nameUUIDFromBytes(TapiStringConstants.T0_FULL_MULTILAYER
133             .getBytes(Charset.forName("UTF-8"))).toString());
134     private final NetworkTransactionService networkTransactionService;
135     private Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap;
136     private final R2RTapiLinkDiscovery linkDiscovery;
137     private final TapiLink tapiLink;
138
139     public TapiNetworkModelServiceImpl(final R2RTapiLinkDiscovery linkDiscovery,
140                                        NetworkTransactionService networkTransactionService, TapiLink tapiLink) {
141         this.networkTransactionService = networkTransactionService;
142         this.sipMap = new HashMap<>();
143         this.linkDiscovery = linkDiscovery;
144         this.tapiLink = tapiLink;
145     }
146
147     @Override
148     public void createTapiNode(String orNodeId, int orNodeVersion, Nodes node) {
149         // TODO -> Implementation with PortMappingListener
150         // check if port mapping exists or not...
151         if (node.getMapping() == null) {
152             LOG.warn("Could not generate port mapping for {} skipping network model creation", orNodeId);
153             return;
154         }
155         this.sipMap.clear();
156         LOG.info("Mapping of node {}: {}", orNodeId, node.getMapping().values());
157
158         // check type of device, check version and create node mapping
159         if (NodeTypes.Rdm.getIntValue() == node.getNodeInfo().getNodeType().getIntValue()) {
160             // ROADM device
161             // transform flat mapping list to per degree and per srg mapping lists
162             Map<String, List<Mapping>> mapDeg = new HashMap<>();
163             Map<String, List<Mapping>> mapSrg = new HashMap<>();
164             List<Mapping> mappingList = new ArrayList<>(node.nonnullMapping().values());
165             mappingList.sort(Comparator.comparing(Mapping::getLogicalConnectionPoint));
166
167             List<String> nodeShardList = getRoadmNodelist(mappingList);
168
169             // populate degree and srg LCP map
170             for (String str : nodeShardList) {
171                 List<Mapping> interList = mappingList.stream().filter(x -> x.getLogicalConnectionPoint().contains(str))
172                         .collect(Collectors.toList());
173                 if (str.contains("DEG")) {
174                     mapDeg.put(str, interList);
175                 } else if (str.contains("SRG")) {
176                     mapSrg.put(str, interList);
177                 } else {
178                     LOG.error("unknown element");
179                 }
180             }
181             // Transform LCPs into ONEP
182             Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepMap =
183                 new HashMap<>(transformDegToOnep(orNodeId, mapDeg));
184             onepMap.putAll(transformSrgToOnep(orNodeId, mapSrg));
185
186             // create tapi Node
187             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Node roadmNode =
188                     createRoadmTapiNode(orNodeId, onepMap);
189             mergeNodeinTopology(Map.of(roadmNode.key(), roadmNode));
190             mergeSipsinContext(this.sipMap);
191             // TODO add states corresponding to device config -> based on mapping.
192             //  This should be possible after Gilles work is merged
193
194             // rdm to rdm link creation if neighbour roadm is mounted
195             LOG.info("checking if neighbor roadm exists");
196             Map<LinkKey, Link> rdm2rdmLinks = this.linkDiscovery.readLLDP(new NodeId(orNodeId), orNodeVersion,
197                 this.tapiTopoUuid);
198             if (!rdm2rdmLinks.isEmpty()) {
199                 mergeLinkinTopology(rdm2rdmLinks);
200             }
201             LOG.info("TAPI node for or node {} successfully merged", orNodeId);
202         } else if (NodeTypes.Xpdr.getIntValue() ==  node.getNodeInfo().getNodeType().getIntValue()) {
203             List<Mapping> networkMappings = node.nonnullMapping().values()
204                     .stream().filter(k -> k.getLogicalConnectionPoint()
205                             .contains("NETWORK")).collect(Collectors.toList());
206             Map<Integer, String> xpdrMap = new HashMap<>();
207             for (Mapping mapping : networkMappings) {
208                 Integer xpdrNb = Integer.parseInt(mapping.getLogicalConnectionPoint().split("XPDR")[1].split("-")[0]);
209                 String nodeId = node.getNodeId() + TapiStringConstants.XPDR + xpdrNb;
210                 if (!xpdrMap.containsKey(xpdrNb)) {
211                     List<Mapping> xpdrNetMaps = node.nonnullMapping().values()
212                         .stream().filter(k -> k.getLogicalConnectionPoint()
213                             .contains("XPDR" + xpdrNb + TapiStringConstants.NETWORK)).collect(Collectors.toList());
214                     List<Mapping> xpdrClMaps = node.nonnullMapping().values()
215                         .stream().filter(k -> k.getLogicalConnectionPoint()
216                             .contains("XPDR" + xpdrNb + TapiStringConstants.CLIENT)).collect(Collectors.toList());
217                     xpdrMap.put(xpdrNb, node.getNodeId());
218
219                     // create switching pool
220                     OduSwitchingPools oorOduSwitchingPool = createSwitchPoolForXpdr(
221                         mapping.getXponderType().getIntValue(), xpdrClMaps, xpdrNetMaps, xpdrNb);
222
223                     // node transformation
224                     Map<NodeKey, Node> nodeMap = new HashMap<>(transformXpdrToTapiNode(
225                         nodeId, xpdrClMaps, xpdrNetMaps, mapping.getXponderType(), oorOduSwitchingPool));
226                     // add nodes and sips to tapi context
227                     mergeNodeinTopology(nodeMap);
228                     mergeSipsinContext(this.sipMap);
229                 }
230             }
231             LOG.info("TAPI node for or node {} successfully merged", orNodeId);
232         }
233         // Device not managed yet
234     }
235
236     private Map<NodeKey, Node> transformXpdrToTapiNode(String nodeId, List<Mapping> xpdrClMaps,
237                                                        List<Mapping> xpdrNetMaps, XpdrNodeTypes xponderType,
238                                                        OduSwitchingPools oorOduSwitchingPool) {
239         Map<NodeKey, Node> nodeMap = new HashMap<>();
240         LOG.info("creation of a DSR/ODU node for {}", nodeId);
241         Uuid nodeUuidDsr = new Uuid(UUID.nameUUIDFromBytes((String.join("+", nodeId, TapiStringConstants.DSR))
242             .getBytes(Charset.forName("UTF-8"))).toString());
243         Name nameDsr = new NameBuilder().setValueName("dsr/odu node name").setValue(
244             String.join("+", nodeId, TapiStringConstants.DSR)).build();
245         List<LayerProtocolName> dsrLayerProtocols = Arrays.asList(LayerProtocolName.DSR,
246             LayerProtocolName.ODU);
247         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology
248             .Node dsrNode = createTapiXpdrNode(Map.of(nameDsr.key(), nameDsr), dsrLayerProtocols,
249             nodeId, nodeUuidDsr, xpdrClMaps, xpdrNetMaps, xponderType, oorOduSwitchingPool);
250
251         nodeMap.put(dsrNode.key(), dsrNode);
252
253         // node creation [otsi]
254         LOG.info("creation of an OTSi node for {}", nodeId);
255         Uuid nodeUuidOtsi = new Uuid(UUID.nameUUIDFromBytes((String.join("+", nodeId, TapiStringConstants.OTSI))
256             .getBytes(Charset.forName("UTF-8"))).toString());
257         Name nameOtsi =  new NameBuilder().setValueName("otsi node name").setValue(
258             String.join("+", nodeId, TapiStringConstants.OTSI)).build();
259         List<LayerProtocolName> otsiLayerProtocols = Arrays.asList(LayerProtocolName.PHOTONICMEDIA);
260         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology
261             .Node otsiNode = createTapiXpdrNode(Map.of(nameOtsi.key(), nameOtsi), otsiLayerProtocols,
262             nodeId, nodeUuidOtsi, xpdrClMaps, xpdrNetMaps, xponderType, null);
263
264         nodeMap.put(otsiNode.key(), otsiNode);
265
266         // transitional link cration between network nep of DSR/ODU node and iNep of otsi node
267         LOG.info("creation of transitional links between DSR/ODU and OTSi nodes");
268         Map<LinkKey, Link> linkMap = createTapiTransitionalLinks(nodeId, xpdrNetMaps, nodeUuidDsr,
269             nodeUuidOtsi);
270         mergeLinkinTopology(linkMap);
271
272         return nodeMap;
273     }
274
275     private OduSwitchingPools createSwitchPoolForXpdr(int xpdrType, List<Mapping> xpdrClMaps, List<Mapping> xpdrNetMaps,
276                                                       Integer xpdrNb) {
277         // todo: are switching pool correct here??
278         switch (xpdrType) {
279             case 1:
280                 // Tpdr
281                 return createTpdrSwitchPool();
282             case 2:
283                 // Mux
284                 return createMuxSwitchPool(xpdrClMaps, xpdrNetMaps, xpdrNb);
285             case 3:
286                 // Switch
287                 return createSwtchSwitchPool(xpdrClMaps, xpdrNetMaps, xpdrNb);
288             default:
289                 LOG.warn("Xpdr type {} not supported", xpdrType);
290         }
291         return null;
292     }
293
294     private Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> transformSrgToOnep(String orNodeId,
295                                                                               Map<String, List<Mapping>> mapSrg) {
296         Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepMap = new HashMap<>();
297         for (Map.Entry<String, List<Mapping>> entry : mapSrg.entrySet()) {
298             // For each srg node. Loop through the LCPs and create neps and sips for PP
299             for (Mapping m:entry.getValue()) {
300                 if (!m.getLogicalConnectionPoint().contains("PP")) {
301                     LOG.info("LCP {} is not an external TP of SRG node", m.getLogicalConnectionPoint());
302                     continue;
303                 }
304                 Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> srgNeps =
305                     createRoadmNeps(orNodeId, m.getLogicalConnectionPoint(), true,
306                             transformOperState(m.getPortOperState()), transformAdminState(m.getPortAdminState()));
307                 onepMap.putAll(srgNeps);
308             }
309         }
310         return onepMap;
311     }
312
313     private Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> transformDegToOnep(String orNodeId,
314                                                                               Map<String, List<Mapping>> mapDeg) {
315         Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepMap = new HashMap<>();
316         for (Map.Entry<String, List<Mapping>> entry : mapDeg.entrySet()) {
317             // For each degree node. Loop through the LCPs and create neps and sips for TTP
318             for (Mapping m:entry.getValue()) {
319                 if (!m.getLogicalConnectionPoint().contains("TTP")) {
320                     LOG.info("LCP {} is not an external TP of DEGREE node", m.getLogicalConnectionPoint());
321                     continue;
322                 }
323                 Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> degNeps =
324                     createRoadmNeps(orNodeId, m.getLogicalConnectionPoint(), false,
325                             transformOperState(m.getPortOperState()), transformAdminState(m.getPortAdminState()));
326                 onepMap.putAll(degNeps);
327             }
328         }
329         return onepMap;
330     }
331
332     private List<String> getRoadmNodelist(List<Mapping> mappingList) {
333         List<String> nodeShardList = new ArrayList<>();
334         for (Mapping mapping : mappingList) {
335             // TODO -> maybe we need to check the id based on the version
336             String str = mapping.getLogicalConnectionPoint().split("-")[0];
337             LOG.info("LCP = {}", str);
338             if (!nodeShardList.contains(str)) {
339                 nodeShardList.add(str);
340             }
341         }
342         return nodeShardList;
343     }
344
345     @Override
346     public void deleteTapinode(String nodeId) {
347         // TODO: check for null objects
348         // Check if it is ROADM or XPDR --> create the uuids of the node and delete from topology the node.
349         // This will delete NEPs. Then check for links that have this node and delete them.
350         // Then check SIPs and delete them. Then services and connections with SIPs and put them to another state.
351         LOG.info("Deleting node {} from TAPI topology", nodeId);
352         InstanceIdentifier<Topology> topologyIID = InstanceIdentifier.builder(Context.class)
353                 .augmentation(Context1.class).child(TopologyContext.class).child(Topology.class,
354                         new TopologyKey(tapiTopoUuid)).build();
355         Topology topology = null;
356         try {
357             Optional<Topology> optTopology =
358                     this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, topologyIID).get();
359             if (!optTopology.isPresent()) {
360                 LOG.error("No topology object present. Error deleting node {}", nodeId);
361                 return;
362             }
363             topology = optTopology.get();
364         } catch (InterruptedException | ExecutionException e) {
365             LOG.error("Couldnt read tapi topology from datastore", e);
366         }
367         if (topology == null) {
368             LOG.error("Topology is null, nothing to delete");
369             return;
370         }
371         if (topology.getNode() == null) {
372             LOG.error("No nodes in topology");
373             return;
374         }
375         if (nodeId.contains("ROADM")) {
376             // Node is in photonic media layer and UUID can be built from nodeId + PHTN_MEDIA
377             Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", nodeId,
378                 TapiStringConstants.PHTNC_MEDIA)).getBytes(Charset.forName("UTF-8"))).toString());
379             deleteNodeFromTopo(nodeUuid);
380         }
381         if (nodeId.contains("XPDR") || nodeId.contains("SPDR") || nodeId.contains("MXPDR")) {
382             // Node is either XPDR, MXPDR or SPDR. Retrieve nodes from topology and check names
383             for (Node tapiNode:topology.getNode().values()) {
384                 if (tapiNode.getName().values().stream().anyMatch(name -> name.getValue().contains(nodeId))) {
385                     // Found node we need to delete
386                     deleteNodeFromTopo(tapiNode.getUuid());
387                 }
388             }
389         }
390         // Delete links of topology
391         Map<LinkKey, Link> linkMap = topology.getLink();
392         if (linkMap != null) {
393             for (Link link:linkMap.values()) {
394                 if (link.getName().values().stream().anyMatch(name -> name.getValue().contains(nodeId))) {
395                     deleteLinkFromTopo(link.getUuid());
396                 }
397             }
398         }
399         // Delete sips of sip map
400         InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
401         Context context = null;
402         try {
403             Optional<Context> optContext = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL,
404                     contextIID).get();
405             if (!optContext.isPresent()) {
406                 LOG.error("No context object present in datastore.");
407                 return;
408             }
409             context = optContext.get();
410         } catch (InterruptedException | ExecutionException e) {
411             LOG.error("Couldnt read tapi context from datastore", e);
412         }
413         if (context == null) {
414             LOG.error("Context is null, nothing to delete");
415             return;
416         }
417         Map<ServiceInterfacePointKey, ServiceInterfacePoint> sips = context.getServiceInterfacePoint();
418         if (sips != null) {
419             for (ServiceInterfacePoint sip:sips.values()) {
420                 if (sip.getName().values().stream().anyMatch(name -> name.getValue().contains(nodeId))) {
421                     // Update state of services that have this sip as an endpoint and also connections
422                     updateConnectivityServicesState(sip.getUuid(), nodeId);
423                     deleteSipFromTopo(sip.getUuid());
424                 }
425             }
426         }
427     }
428
429     private Node createTapiXpdrNode(Map<NameKey, Name> nameMap, List<LayerProtocolName> layerProtocols,
430                                     String nodeId, Uuid nodeUuid, List<Mapping> xpdrClMaps, List<Mapping> xpdrNetMaps,
431                                     XpdrNodeTypes xponderType, OduSwitchingPools oorOduSwitchingPool) {
432         Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepl = new HashMap<>();
433         Map<NodeRuleGroupKey, NodeRuleGroup> nodeRuleGroupList = new HashMap<>();
434         Map<RuleKey, Rule> ruleList = new HashMap<>();
435         Rule rule = new RuleBuilder()
436                 .setLocalId("forward")
437                 .setForwardingRule(ForwardingRule.MAYFORWARDACROSSGROUP)
438                 .setRuleType(RuleType.FORWARDING)
439                 .build();
440         ruleList.put(rule.key(), rule);
441         if (layerProtocols.contains(LayerProtocolName.DSR)) {
442             // neps for dsr/odu layer
443             Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> dsroduNeps =
444                     createXpdrDsrOduNeps(nodeId, xpdrClMaps, xpdrNetMaps, xponderType);
445             onepl.putAll(dsroduNeps);
446             nodeRuleGroupList = createNodeRuleGroupForDsrNode(nodeId, oorOduSwitchingPool, ruleList, onepl);
447         } else if (layerProtocols.contains(LayerProtocolName.PHOTONICMEDIA)) {
448             // neps for photonic layer
449             Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> phtmdNeps =
450                     createXpdrPhtnMdNeps(nodeId, xpdrNetMaps);
451             onepl.putAll(phtmdNeps);
452             nodeRuleGroupList = createNodeRuleGroupForOtsiNode(nodeId, xpdrNetMaps, ruleList);
453         } else {
454             LOG.error("Undefined LayerProtocolName for {} node {}", nameMap.get(nameMap.keySet().iterator().next())
455                     .getValueName(), nameMap.get(nameMap.keySet().iterator().next()).getValue());
456         }
457         // Empty random creation of mandatory fields for avoiding errors....
458         CostCharacteristic costCharacteristic = new CostCharacteristicBuilder()
459             .setCostAlgorithm("Restricted Shortest Path - RSP")
460             .setCostName("HOP_COUNT")
461             .setCostValue("12345678")
462             .build();
463         LatencyCharacteristic latencyCharacteristic = new LatencyCharacteristicBuilder()
464             .setFixedLatencyCharacteristic("12345678")
465             .setQueingLatencyCharacteristic("12345678")
466             .setJitterCharacteristic("12345678")
467             .setWanderCharacteristic("12345678")
468             .setTrafficPropertyName("FIXED_LATENCY")
469             .build();
470         return new NodeBuilder()
471             .setUuid(nodeUuid)
472             .setName(nameMap)
473             .setLayerProtocolName(layerProtocols)
474             .setAdministrativeState(AdministrativeState.UNLOCKED)
475             .setOperationalState(OperationalState.ENABLED)
476             .setLifecycleState(LifecycleState.INSTALLED)
477             .setOwnedNodeEdgePoint(onepl)
478             .setNodeRuleGroup(nodeRuleGroupList)
479             .setCostCharacteristic(Map.of(costCharacteristic.key(), costCharacteristic))
480             .setLatencyCharacteristic(Map.of(latencyCharacteristic.key(), latencyCharacteristic))
481             .setErrorCharacteristic("error")
482             .setLossCharacteristic("loss")
483             .setRepeatDeliveryCharacteristic("repeat delivery")
484             .setDeliveryOrderCharacteristic("delivery order")
485             .setUnavailableTimeCharacteristic("unavailable time")
486             .setServerIntegrityProcessCharacteristic("server integrity process")
487             .build();
488     }
489
490     private Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> createXpdrPhtnMdNeps(String nodeId,
491                                                                                 List<Mapping> xpdrNetMaps) {
492         Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepl = new HashMap<>();
493
494         // iNep creation on otsi node
495         for (int i = 0; i < xpdrNetMaps.size(); i++) {
496             Uuid nepUuid1 = new Uuid(UUID.nameUUIDFromBytes(
497                 (String.join("+", nodeId, TapiStringConstants.I_OTSI,
498                     xpdrNetMaps.get(i).getLogicalConnectionPoint())).getBytes(Charset.forName("UTF-8"))).toString());
499             Name onedName = new NameBuilder()
500                 .setValueName("iNodeEdgePoint")
501                 .setValue(String.join("+", nodeId, TapiStringConstants.I_OTSI,
502                     xpdrNetMaps.get(i).getLogicalConnectionPoint()))
503                 .build();
504
505             OwnedNodeEdgePoint onep = createNep(nepUuid1, xpdrNetMaps.get(i).getLogicalConnectionPoint(),
506                 Map.of(onedName.key(), onedName), LayerProtocolName.PHOTONICMEDIA, LayerProtocolName.PHOTONICMEDIA,
507                 true, String.join("+", nodeId, TapiStringConstants.I_OTSI),
508                 xpdrNetMaps.get(i).getSupportedInterfaceCapability(),
509                 transformOperState(xpdrNetMaps.get(i).getPortOperState()),
510                 transformAdminState(xpdrNetMaps.get(i).getPortAdminState()));
511             onepl.put(onep.key(), onep);
512         }
513         // eNep creation on otsi node
514         for (int i = 0; i < xpdrNetMaps.size(); i++) {
515             Uuid nepUuid2 = new Uuid(UUID.nameUUIDFromBytes(
516                 (String.join("+", nodeId, TapiStringConstants.E_OTSI,
517                     xpdrNetMaps.get(i).getLogicalConnectionPoint())).getBytes(Charset.forName("UTF-8"))).toString());
518             Name onedName = new NameBuilder()
519                 .setValueName("eNodeEdgePoint")
520                 .setValue(String.join("+", nodeId, TapiStringConstants.E_OTSI,
521                     xpdrNetMaps.get(i).getLogicalConnectionPoint()))
522                 .build();
523
524             OwnedNodeEdgePoint onep = createNep(nepUuid2, xpdrNetMaps.get(i).getLogicalConnectionPoint(),
525                 Map.of(onedName.key(), onedName), LayerProtocolName.PHOTONICMEDIA, LayerProtocolName.PHOTONICMEDIA,
526                 false, String.join("+", nodeId, TapiStringConstants.E_OTSI),
527                 xpdrNetMaps.get(i).getSupportedInterfaceCapability(),
528                 transformOperState(xpdrNetMaps.get(i).getPortOperState()),
529                 transformAdminState(xpdrNetMaps.get(i).getPortAdminState()));
530             onepl.put(onep.key(), onep);
531         }
532         // Photonic Media Nep creation on otsi node
533         for (int i = 0; i < xpdrNetMaps.size(); i++) {
534             Uuid nepUuid3 = new Uuid(UUID.nameUUIDFromBytes(
535                 (String.join("+", nodeId, TapiStringConstants.PHTNC_MEDIA,
536                     xpdrNetMaps.get(i).getLogicalConnectionPoint())).getBytes(Charset.forName("UTF-8"))).toString());
537             Name onedName = new NameBuilder()
538                 .setValueName("PhotMedNodeEdgePoint")
539                 .setValue(String.join("+", nodeId, TapiStringConstants.PHTNC_MEDIA,
540                     xpdrNetMaps.get(i).getLogicalConnectionPoint()))
541                 .build();
542
543             OwnedNodeEdgePoint onep = createNep(nepUuid3, xpdrNetMaps.get(i).getLogicalConnectionPoint(),
544                 Map.of(onedName.key(), onedName), LayerProtocolName.PHOTONICMEDIA, LayerProtocolName.PHOTONICMEDIA,
545                 false, String.join("+", nodeId, TapiStringConstants.PHTNC_MEDIA),
546                 xpdrNetMaps.get(i).getSupportedInterfaceCapability(),
547                 transformOperState(xpdrNetMaps.get(i).getPortOperState()),
548                 transformAdminState(xpdrNetMaps.get(i).getPortAdminState()));
549             onepl.put(onep.key(), onep);
550         }
551         return onepl;
552     }
553
554     private Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> createXpdrDsrOduNeps(String nodeId, List<Mapping> xpdrClMaps,
555                                                                                 List<Mapping> xpdrNetMaps,
556                                                                                 XpdrNodeTypes xponderType) {
557         Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepl = new HashMap<>();
558         // client nep creation on DSR node
559         for (int i = 0; i < xpdrClMaps.size(); i++) {
560             LOG.info("Client NEP = {}", String.join("+", nodeId, TapiStringConstants.DSR,
561                 xpdrClMaps.get(i).getLogicalConnectionPoint()));
562             Uuid nepUuid = new Uuid(UUID.nameUUIDFromBytes(
563                 (String.join("+", nodeId, TapiStringConstants.DSR,
564                     xpdrClMaps.get(i).getLogicalConnectionPoint())).getBytes(Charset.forName("UTF-8"))).toString());
565             NameBuilder nameBldr = new NameBuilder().setValue(String.join("+", nodeId,
566                 TapiStringConstants.DSR, xpdrClMaps.get(i).getLogicalConnectionPoint()));
567             Name name;
568             if (OpenroadmNodeType.TPDR.getName().equalsIgnoreCase(xponderType.getName())) {
569                 name = nameBldr.setValueName("100G-tpdr").build();
570             } else {
571                 name = nameBldr.setValueName("NodeEdgePoint_C").build();
572             }
573
574             OwnedNodeEdgePoint onep = createNep(nepUuid, xpdrClMaps.get(i).getLogicalConnectionPoint(),
575                 Map.of(name.key(), name), LayerProtocolName.DSR, LayerProtocolName.DSR, true,
576                 String.join("+", nodeId, TapiStringConstants.DSR),
577                 xpdrClMaps.get(i).getSupportedInterfaceCapability(),
578                 transformOperState(xpdrClMaps.get(i).getPortOperState()),
579                 transformAdminState(xpdrClMaps.get(i).getPortAdminState()));
580             onepl.put(onep.key(), onep);
581         }
582         // network nep creation on I_ODU node
583         for (int i = 0; i < xpdrNetMaps.size(); i++) {
584             LOG.info("iODU NEP = {}", String.join("+", nodeId, TapiStringConstants.I_ODU,
585                 xpdrNetMaps.get(i).getLogicalConnectionPoint()));
586             Uuid nepUuid = new Uuid(UUID.nameUUIDFromBytes(
587                 (String.join("+", nodeId, TapiStringConstants.I_ODU,
588                     xpdrNetMaps.get(i).getLogicalConnectionPoint())).getBytes(Charset.forName("UTF-8"))).toString());
589             Name onedName = new NameBuilder()
590                 .setValueName("iNodeEdgePoint_N")
591                 .setValue(String.join("+", nodeId, TapiStringConstants.I_ODU,
592                     xpdrNetMaps.get(i).getLogicalConnectionPoint()))
593                 .build();
594
595             OwnedNodeEdgePoint onep = createNep(nepUuid, xpdrNetMaps.get(i).getLogicalConnectionPoint(),
596                 Map.of(onedName.key(), onedName),
597                 LayerProtocolName.ODU, LayerProtocolName.DSR, false,
598                 String.join("+", nodeId, TapiStringConstants.I_ODU),
599                 xpdrNetMaps.get(i).getSupportedInterfaceCapability(),
600                 transformOperState(xpdrNetMaps.get(i).getPortOperState()),
601                 transformAdminState(xpdrNetMaps.get(i).getPortAdminState()));
602             onepl.put(onep.key(), onep);
603         }
604         // network nep creation on E_ODU node
605         for (int i = 0; i < xpdrNetMaps.size(); i++) {
606             LOG.info("eODU NEP = {}", String.join("+", nodeId, TapiStringConstants.E_ODU,
607                 xpdrNetMaps.get(i).getLogicalConnectionPoint()));
608             Uuid nepUuid = new Uuid(UUID.nameUUIDFromBytes(
609                 (String.join("+", nodeId, TapiStringConstants.E_ODU,
610                     xpdrNetMaps.get(i).getLogicalConnectionPoint())).getBytes(Charset.forName("UTF-8"))).toString());
611             Name onedName = new NameBuilder()
612                 .setValueName("eNodeEdgePoint_N")
613                 .setValue(String.join("+", nodeId, TapiStringConstants.E_ODU,
614                     xpdrNetMaps.get(i).getLogicalConnectionPoint()))
615                 .build();
616
617             OwnedNodeEdgePoint onep = createNep(nepUuid, xpdrNetMaps.get(i).getLogicalConnectionPoint(),
618                 Map.of(onedName.key(), onedName),
619                 LayerProtocolName.ODU, LayerProtocolName.DSR, true,
620                 String.join("+", nodeId, TapiStringConstants.E_ODU),
621                 xpdrNetMaps.get(i).getSupportedInterfaceCapability(),
622                 transformOperState(xpdrNetMaps.get(i).getPortOperState()),
623                 transformAdminState(xpdrNetMaps.get(i).getPortAdminState()));
624             onepl.put(onep.key(), onep);
625         }
626         return onepl;
627     }
628
629     private OperationalState transformOperState(String operString) {
630         State operState = org.opendaylight.transportpce.networkmodel.util.TopologyUtils.setNetworkOperState(operString);
631         return operState.equals(State.InService) ? OperationalState.ENABLED : OperationalState.DISABLED;
632     }
633
634     private AdministrativeState transformAdminState(String adminString) {
635         AdminStates adminState = org.opendaylight.transportpce.networkmodel.util.TopologyUtils
636                 .setNetworkAdminState(adminString);
637         return adminState.equals(AdminStates.InService) ? AdministrativeState.UNLOCKED : AdministrativeState.LOCKED;
638     }
639
640     private OwnedNodeEdgePoint createNep(Uuid nepUuid, String tpid, Map<NameKey, Name> nepNames,
641                                          LayerProtocolName nepProtocol, LayerProtocolName nodeProtocol, boolean withSip,
642                                          String keyword,
643                                          List<Class<? extends SupportedIfCapability>> supportedInterfaceCapability,
644                                          OperationalState operState, AdministrativeState adminState) {
645         OwnedNodeEdgePointBuilder onepBldr = new OwnedNodeEdgePointBuilder()
646                 .setUuid(nepUuid)
647                 .setLayerProtocolName(nepProtocol)
648                 .setName(nepNames);
649         if (withSip) {
650             onepBldr.setMappedServiceInterfacePoint(createMSIP(1, nepProtocol, tpid, keyword,
651                     supportedInterfaceCapability, operState, adminState));
652         }
653         LOG.debug("Node layer {}", nodeProtocol.getName());
654         onepBldr.setSupportedCepLayerProtocolQualifier(createSupportedLayerProtocolQualifier(
655                 supportedInterfaceCapability, nodeProtocol));
656         onepBldr.setLinkPortDirection(PortDirection.BIDIRECTIONAL).setLinkPortRole(PortRole.SYMMETRIC)
657                 .setAdministrativeState(adminState).setOperationalState(operState)
658                 .setLifecycleState(LifecycleState.INSTALLED).setTerminationDirection(TerminationDirection.BIDIRECTIONAL)
659                 .setTerminationState(TerminationState.TERMINATEDBIDIRECTIONAL);
660         return onepBldr.build();
661     }
662
663     private Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> createRoadmNeps(String orNodeId, String tpId,
664                                                                            boolean withSip, OperationalState operState,
665                                                                            AdministrativeState adminState) {
666         Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepMap = new HashMap<>();
667         // PHOTONIC MEDIA nep
668         Uuid nepUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", orNodeId,
669                 TapiStringConstants.PHTNC_MEDIA, tpId)).getBytes(Charset.forName("UTF-8"))).toString());
670         Name nepName = new NameBuilder()
671                 .setValueName(TapiStringConstants.PHTNC_MEDIA + "NodeEdgePoint")
672                 .setValue(String.join("+", orNodeId, TapiStringConstants.PHTNC_MEDIA, tpId))
673                 .build();
674         OwnedNodeEdgePoint onep = new OwnedNodeEdgePointBuilder()
675             .setUuid(nepUuid)
676             .setLayerProtocolName(LayerProtocolName.PHOTONICMEDIA)
677             .setName(Map.of(nepName.key(), nepName))
678             .setSupportedCepLayerProtocolQualifier(List.of(PHOTONICLAYERQUALIFIEROMS.class))
679             .setLinkPortDirection(PortDirection.BIDIRECTIONAL).setLinkPortRole(PortRole.SYMMETRIC)
680             .setAdministrativeState(adminState).setOperationalState(operState)
681             .setLifecycleState(LifecycleState.INSTALLED).setTerminationDirection(TerminationDirection.BIDIRECTIONAL)
682             .setTerminationState(TerminationState.TERMINATEDBIDIRECTIONAL)
683             .build();
684         onepMap.put(onep.key(), onep);
685
686         // MC nep
687         Uuid nepUuid1 = new Uuid(UUID.nameUUIDFromBytes((String.join("+", orNodeId,
688                 TapiStringConstants.MC, tpId)).getBytes(Charset.forName("UTF-8"))).toString());
689         Name nepName1 = new NameBuilder()
690                 .setValueName(TapiStringConstants.MC + "NodeEdgePoint")
691                 .setValue(String.join("+", orNodeId, TapiStringConstants.MC, tpId))
692                 .build();
693         OwnedNodeEdgePointBuilder onepBldr1 = new OwnedNodeEdgePointBuilder()
694                 .setUuid(nepUuid1)
695                 .setLayerProtocolName(LayerProtocolName.PHOTONICMEDIA)
696                 .setName(Map.of(nepName1.key(), nepName1))
697                 .setSupportedCepLayerProtocolQualifier(List.of(PHOTONICLAYERQUALIFIEROMS.class))
698                 .setLinkPortDirection(PortDirection.BIDIRECTIONAL).setLinkPortRole(PortRole.SYMMETRIC)
699                 .setAdministrativeState(adminState).setOperationalState(operState)
700                 .setLifecycleState(LifecycleState.INSTALLED).setTerminationDirection(TerminationDirection.BIDIRECTIONAL)
701                 .setTerminationState(TerminationState.TERMINATEDBIDIRECTIONAL);
702         if (withSip) {
703             onepBldr1.setMappedServiceInterfacePoint(createMSIP(1, LayerProtocolName.PHOTONICMEDIA,
704                 tpId, String.join("+", orNodeId, TapiStringConstants.MC), null,
705                 operState, adminState));
706         }
707         OwnedNodeEdgePoint onep1 = onepBldr1.build();
708         onepMap.put(onep1.key(), onep1);
709
710         // OTSiMC nep
711         Uuid nepUuid2 = new Uuid(UUID.nameUUIDFromBytes((String.join("+", orNodeId, TapiStringConstants.OTSI_MC,
712                 tpId)).getBytes(Charset.forName("UTF-8"))).toString());
713         Name nepName2 = new NameBuilder()
714                 .setValueName(TapiStringConstants.OTSI_MC + "NodeEdgePoint")
715                 .setValue(String.join("+", orNodeId, TapiStringConstants.OTSI_MC, tpId))
716                 .build();
717
718         OwnedNodeEdgePoint onep2 = new OwnedNodeEdgePointBuilder()
719             .setUuid(nepUuid2)
720             .setLayerProtocolName(LayerProtocolName.PHOTONICMEDIA)
721             .setName(Map.of(nepName2.key(), nepName2))
722             .setSupportedCepLayerProtocolQualifier(List.of(PHOTONICLAYERQUALIFIEROMS.class))
723             .setLinkPortDirection(PortDirection.BIDIRECTIONAL).setLinkPortRole(PortRole.SYMMETRIC)
724             .setAdministrativeState(adminState).setOperationalState(operState)
725             .setLifecycleState(LifecycleState.INSTALLED).setTerminationDirection(TerminationDirection.BIDIRECTIONAL)
726             .setTerminationState(TerminationState.TERMINATEDBIDIRECTIONAL)
727             .build();
728         onepMap.put(onep2.key(), onep2);
729         return onepMap;
730     }
731
732     private Map<MappedServiceInterfacePointKey, MappedServiceInterfacePoint>
733             createMSIP(int nb, LayerProtocolName layerProtocol, String tpid, String nodeid,
734                    List<Class<? extends SupportedIfCapability>> supportedInterfaceCapability,
735                    OperationalState operState, AdministrativeState adminState) {
736         Map<MappedServiceInterfacePointKey, MappedServiceInterfacePoint> msipl = new HashMap<>();
737         for (int i = 0; i < nb; i++) {
738             Uuid sipUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", "SIP", nodeid,
739                     tpid)).getBytes(Charset.forName("UTF-8"))).toString());
740             MappedServiceInterfacePoint msip = new MappedServiceInterfacePointBuilder()
741                     .setServiceInterfacePointUuid(sipUuid).build();
742             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.tapi.context.ServiceInterfacePoint sip
743                     = createSIP(sipUuid, layerProtocol, tpid, nodeid, supportedInterfaceCapability,
744                     operState, adminState);
745             this.sipMap.put(sip.key(), sip);
746             LOG.info("SIP created {}", sip.getUuid());
747             // this.tapiSips.put(sip.key(), sip);
748             msipl.put(msip.key(), msip);
749         }
750         return msipl;
751     }
752
753     private ServiceInterfacePoint createSIP(Uuid sipUuid, LayerProtocolName layerProtocol, String tpid, String nodeid,
754                                             List<Class<? extends SupportedIfCapability>> supportedInterfaceCapability,
755                                             OperationalState operState, AdministrativeState adminState) {
756         // TODO: what value should be set in total capacity and available capacity
757         LOG.info("SIP name = {}", String.join("+", nodeid, tpid));
758         Name sipName = new NameBuilder()
759                 .setValueName("SIP name")
760                 .setValue(String.join("+", nodeid, tpid))
761                 .build();
762         return new ServiceInterfacePointBuilder()
763                 .setUuid(sipUuid)
764                 .setName(Map.of(sipName.key(), sipName))
765                 .setLayerProtocolName(layerProtocol)
766                 .setAdministrativeState(adminState)
767                 .setOperationalState(operState)
768                 .setLifecycleState(LifecycleState.INSTALLED)
769                 .setAvailableCapacity(new AvailableCapacityBuilder().build())
770                 .setTotalPotentialCapacity(new TotalPotentialCapacityBuilder().build())
771                 .setSupportedLayerProtocolQualifier(createSupportedLayerProtocolQualifier(supportedInterfaceCapability,
772                         layerProtocol))
773                 .build();
774     }
775
776     private org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Node
777             createRoadmTapiNode(String orNodeId, Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> oneplist) {
778         // UUID
779         Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", orNodeId,
780             TapiStringConstants.PHTNC_MEDIA)).getBytes(Charset.forName("UTF-8"))).toString());
781         // Names
782         Name nodeNames =  new NameBuilder().setValueName("roadm node name")
783             .setValue(String.join("+", orNodeId, TapiStringConstants.PHTNC_MEDIA)).build();
784         // Protocol Layer
785         List<LayerProtocolName> layerProtocols = Arrays.asList(LayerProtocolName.PHOTONICMEDIA);
786         // Empty random creation of mandatory fields for avoiding errors....
787         CostCharacteristic costCharacteristic = new CostCharacteristicBuilder()
788             .setCostAlgorithm("Restricted Shortest Path - RSP")
789             .setCostName("HOP_COUNT")
790             .setCostValue("12345678")
791             .build();
792         LatencyCharacteristic latencyCharacteristic = new LatencyCharacteristicBuilder()
793             .setFixedLatencyCharacteristic("12345678")
794             .setQueingLatencyCharacteristic("12345678")
795             .setJitterCharacteristic("12345678")
796             .setWanderCharacteristic("12345678")
797             .setTrafficPropertyName("FIXED_LATENCY")
798             .build();
799         return new NodeBuilder()
800             .setUuid(nodeUuid)
801             .setName(Map.of(nodeNames.key(), nodeNames))
802             .setLayerProtocolName(layerProtocols)
803             .setAdministrativeState(AdministrativeState.UNLOCKED)
804             .setOperationalState(OperationalState.ENABLED)
805             .setLifecycleState(LifecycleState.INSTALLED)
806             .setOwnedNodeEdgePoint(oneplist)
807             .setNodeRuleGroup(createNodeRuleGroupForRdmNode(orNodeId, nodeUuid, oneplist.values()))
808             .setCostCharacteristic(Map.of(costCharacteristic.key(), costCharacteristic))
809             .setLatencyCharacteristic(Map.of(latencyCharacteristic.key(), latencyCharacteristic))
810             .setErrorCharacteristic("error")
811             .setLossCharacteristic("loss")
812             .setRepeatDeliveryCharacteristic("repeat delivery")
813             .setDeliveryOrderCharacteristic("delivery order")
814             .setUnavailableTimeCharacteristic("unavailable time")
815             .setServerIntegrityProcessCharacteristic("server integrity process")
816             .build();
817     }
818
819     private Map<NodeRuleGroupKey, NodeRuleGroup> createNodeRuleGroupForRdmNode(String orNodeId, Uuid nodeUuid,
820                                                                                Collection<OwnedNodeEdgePoint> onepl) {
821         Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.NodeEdgePointKey,
822                 org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.NodeEdgePoint>
823                 nepMap = new HashMap<>();
824         for (OwnedNodeEdgePoint onep : onepl) {
825             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.NodeEdgePoint
826                     nep = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group
827                     .NodeEdgePointBuilder()
828                     .setTopologyUuid(this.tapiTopoUuid)
829                     .setNodeUuid(nodeUuid)
830                     .setNodeEdgePointUuid(onep.key().getUuid())
831                     .build();
832             nepMap.put(nep.key(), nep);
833         }
834         Map<NodeRuleGroupKey, NodeRuleGroup> nodeRuleGroupMap = new HashMap<>();
835         Map<RuleKey, Rule> ruleList = new HashMap<>();
836         Rule rule = new RuleBuilder()
837                 .setLocalId("forward")
838                 .setForwardingRule(ForwardingRule.MAYFORWARDACROSSGROUP)
839                 .setRuleType(RuleType.FORWARDING)
840                 .build();
841         ruleList.put(rule.key(), rule);
842         NodeRuleGroup nodeRuleGroup = new NodeRuleGroupBuilder()
843                 .setUuid(new Uuid(UUID.nameUUIDFromBytes((orNodeId + " node rule group")
844                         .getBytes(Charset.forName("UTF-8"))).toString()))
845                 .setRule(ruleList)
846                 .setNodeEdgePoint(nepMap)
847                 .build();
848         nodeRuleGroupMap.put(nodeRuleGroup.key(), nodeRuleGroup);
849         return nodeRuleGroupMap;
850     }
851
852     private Map<LinkKey, Link> createTapiTransitionalLinks(String nodeId, List<Mapping> xpdrNetMaps, Uuid nodeUuidDsr,
853                                                            Uuid nodeUuidOtsi) {
854         Map<LinkKey, Link> linkMap = new HashMap<>();
855         for (Mapping mapping : xpdrNetMaps) {
856             Link transiLink = tapiLink.createTapiLink(nodeId, mapping.getLogicalConnectionPoint(), nodeId,
857                 mapping.getLogicalConnectionPoint(), TapiStringConstants.TRANSITIONAL_LINK, TapiStringConstants.DSR,
858                 TapiStringConstants.OTSI, TapiStringConstants.I_ODU, TapiStringConstants.I_OTSI,
859                 "inService", "inService", Arrays.asList(LayerProtocolName.ODU,
860                     LayerProtocolName.PHOTONICMEDIA),
861                 Arrays.asList(LayerProtocolName.ODU.getName(), LayerProtocolName.PHOTONICMEDIA.getName()),
862                 this.tapiTopoUuid);
863             linkMap.put(transiLink.key(), transiLink);
864         }
865         // return a map of links and then we can do merge the corresponding link map into the topology context
866         return linkMap;
867     }
868
869     private OduSwitchingPools createTpdrSwitchPool() {
870         return new OduSwitchingPoolsBuilder().build();
871     }
872
873     private OduSwitchingPools createSwtchSwitchPool(List<Mapping> xpdrClMaps, List<Mapping> xpdrNetMaps,
874                                                     Integer xpdrNb) {
875         List<TpId> tpl = new ArrayList<>();
876         TpId tpId = null;
877         for (int i = 1; i <= xpdrClMaps.size(); i++) {
878             tpId = new TpId("XPDR" + xpdrNb + TapiStringConstants.CLIENT + i);
879             tpl.add(tpId);
880         }
881         for (int i = 1; i <= xpdrNetMaps.size(); i++) {
882             tpId = new TpId("XPDR" + xpdrNb + TapiStringConstants.NETWORK + i);
883             tpl.add(tpId);
884         }
885         Map<NonBlockingListKey, NonBlockingList> nbMap = new HashMap<>();
886         NonBlockingList nbl = new NonBlockingListBuilder()
887                 .setNblNumber(Uint16.valueOf(1))
888                 .setTpList(tpl)
889                 .build();
890         nbMap.put(nbl.key(),nbl);
891
892         return new OduSwitchingPoolsBuilder()
893                 .setSwitchingPoolNumber(Uint16.valueOf(1))
894                 .setSwitchingPoolType(SwitchingPoolTypes.NonBlocking)
895                 .setNonBlockingList(nbMap)
896                 .build();
897     }
898
899     private OduSwitchingPools createMuxSwitchPool(List<Mapping> xpdrClMaps, List<Mapping> xpdrNetMaps, Integer xpdrNb) {
900         Map<NonBlockingListKey, NonBlockingList> nbMap = new HashMap<>();
901         for (int i = 1; i <= xpdrClMaps.size(); i++) {
902             List<TpId> tpList = new ArrayList<>();
903             TpId tpId = new TpId("XPDR" + xpdrNb + TapiStringConstants.CLIENT + i);
904             tpList.add(tpId);
905             tpId = new TpId("XPDR" + xpdrNb + "-NETWORK1");
906             tpList.add(tpId);
907             NonBlockingList nbl = new NonBlockingListBuilder()
908                     .setNblNumber(Uint16.valueOf(i))
909                     .setTpList(tpList)
910                     .setAvailableInterconnectBandwidth(Uint32.valueOf(xpdrNetMaps.size() * 10L))
911                     .setInterconnectBandwidthUnit(Uint32.valueOf(1000000000))
912                     .build();
913             nbMap.put(nbl.key(),nbl);
914         }
915         return new OduSwitchingPoolsBuilder()
916                 .setSwitchingPoolNumber(Uint16.valueOf(1))
917                 .setSwitchingPoolType(SwitchingPoolTypes.NonBlocking)
918                 .setNonBlockingList(nbMap)
919                 .build();
920     }
921
922     private Map<NodeRuleGroupKey, NodeRuleGroup> createNodeRuleGroupForOtsiNode(String nodeId,
923                                                                                 List<Mapping> xpdrNetMaps,
924                                                                                 Map<RuleKey, Rule> ruleList) {
925         Map<NodeRuleGroupKey, NodeRuleGroup> nodeRuleGroupMap = new HashMap<>();
926         // create NodeRuleGroup
927         int count = 1;
928         for (Mapping tpMapping : xpdrNetMaps) {
929             Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.NodeEdgePointKey,
930                 org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.NodeEdgePoint>
931                     nepList = new HashMap<>();
932             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group
933                     .NodeEdgePoint inep = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210
934                     .node.rule.group.NodeEdgePointBuilder()
935                     .setTopologyUuid(this.tapiTopoUuid)
936                     .setNodeUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+", nodeId,
937                             TapiStringConstants.OTSI)).getBytes(Charset.forName("UTF-8")))
938                         .toString()))
939                     .setNodeEdgePointUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+", nodeId,
940                         TapiStringConstants.I_OTSI, tpMapping.getLogicalConnectionPoint()))
941                         .getBytes(Charset.forName("UTF-8"))).toString()))
942                     .build();
943             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group
944                     .NodeEdgePoint enep = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210
945                     .node.rule.group.NodeEdgePointBuilder()
946                     .setTopologyUuid(this.tapiTopoUuid)
947                     .setNodeUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+", nodeId,
948                         TapiStringConstants.OTSI)).getBytes(Charset.forName("UTF-8"))).toString()))
949                     .setNodeEdgePointUuid(new Uuid(UUID.nameUUIDFromBytes(
950                         (String.join("+", nodeId, TapiStringConstants.E_OTSI,
951                             tpMapping.getLogicalConnectionPoint())).getBytes(Charset.forName("UTF-8"))).toString()))
952                     .build();
953             nepList.put(inep.key(), inep);
954             nepList.put(enep.key(), enep);
955             // Empty random creation of mandatory fields for avoiding errors....
956             CostCharacteristic costCharacteristic = new CostCharacteristicBuilder()
957                 .setCostAlgorithm("Restricted Shortest Path - RSP")
958                 .setCostName("HOP_COUNT")
959                 .setCostValue("12345678")
960                 .build();
961             LatencyCharacteristic latencyCharacteristic = new LatencyCharacteristicBuilder()
962                 .setFixedLatencyCharacteristic("12345678")
963                 .setQueingLatencyCharacteristic("12345678")
964                 .setJitterCharacteristic("12345678")
965                 .setWanderCharacteristic("12345678")
966                 .setTrafficPropertyName("FIXED_LATENCY")
967                 .build();
968             RiskCharacteristic riskCharacteristic = new RiskCharacteristicBuilder()
969                 .setRiskCharacteristicName("risk characteristic")
970                 .setRiskIdentifierList(List.of("risk identifier1", "risk identifier2"))
971                 .build();
972             NodeRuleGroup nodeRuleGroup = new NodeRuleGroupBuilder()
973                 .setUuid(new Uuid(
974                     UUID.nameUUIDFromBytes(("otsi node rule group " + count).getBytes(Charset.forName("UTF-8")))
975                         .toString()))
976                 .setRule(ruleList)
977                 .setNodeEdgePoint(nepList)
978                 .setRiskCharacteristic(Map.of(riskCharacteristic.key(), riskCharacteristic))
979                 .setCostCharacteristic(Map.of(costCharacteristic.key(), costCharacteristic))
980                 .setLatencyCharacteristic(Map.of(latencyCharacteristic.key(), latencyCharacteristic))
981                 .build();
982             nodeRuleGroupMap.put(nodeRuleGroup.key(), nodeRuleGroup);
983             count++;
984         }
985         return nodeRuleGroupMap;
986     }
987
988     private Map<NodeRuleGroupKey, NodeRuleGroup> createNodeRuleGroupForDsrNode(String nodeId,
989                                                                                OduSwitchingPools oorOduSwitchingPool,
990                                                                                Map<RuleKey, Rule> ruleList,
991                                                                                Map<OwnedNodeEdgePointKey,
992                                                                                        OwnedNodeEdgePoint> onepl) {
993         // create NodeRuleGroup
994         if (oorOduSwitchingPool == null) {
995             LOG.info("TPDR node --> no switching pool");
996             return new HashMap<>();
997         }
998         LOG.info("ONEPL = {}", onepl.values());
999         Map<NodeRuleGroupKey, NodeRuleGroup> nodeRuleGroupMap = new HashMap<>();
1000         int count = 1;
1001         for (NonBlockingList nbl : oorOduSwitchingPool.nonnullNonBlockingList().values()) {
1002             LOG.info("Non blocking list = {}", nbl);
1003             Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.NodeEdgePointKey,
1004                 org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.NodeEdgePoint>
1005                     nepList = new HashMap<>();
1006             for (TpId tp : nbl.getTpList()) {
1007                 LOG.info("EDOU TP = {}", String.join("+", nodeId, TapiStringConstants.E_ODU, tp.getValue()));
1008                 LOG.info("DSR TP = {}", String.join("+", nodeId, TapiStringConstants.DSR, tp.getValue()));
1009                 Uuid tpUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", nodeId,
1010                     TapiStringConstants.E_ODU, tp.getValue())).getBytes(Charset.forName("UTF-8"))).toString());
1011                 Uuid tp1Uuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", nodeId,
1012                     TapiStringConstants.DSR, tp.getValue())).getBytes(Charset.forName("UTF-8"))).toString());
1013                 if (onepl.containsKey(new OwnedNodeEdgePointKey(tpUuid))
1014                         || onepl.containsKey(new OwnedNodeEdgePointKey(tp1Uuid))) {
1015                     org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.NodeEdgePoint
1016                         nep = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group
1017                             .NodeEdgePointBuilder()
1018                             .setTopologyUuid(this.tapiTopoUuid)
1019                             .setNodeUuid(new Uuid(UUID.nameUUIDFromBytes(
1020                                 (String.join("+", nodeId,TapiStringConstants. DSR))
1021                                     .getBytes(Charset.forName("UTF-8"))).toString()))
1022                             .setNodeEdgePointUuid((tp.getValue().contains("CLIENT")) ? tp1Uuid : tpUuid)
1023                             .build();
1024                     nepList.put(nep.key(), nep);
1025                 }
1026             }
1027             // Empty random creation of mandatory fields for avoiding errors....
1028             CostCharacteristic costCharacteristic = new CostCharacteristicBuilder()
1029                 .setCostAlgorithm("Restricted Shortest Path - RSP")
1030                 .setCostName("HOP_COUNT")
1031                 .setCostValue("12345678")
1032                 .build();
1033             LatencyCharacteristic latencyCharacteristic = new LatencyCharacteristicBuilder()
1034                 .setFixedLatencyCharacteristic("12345678")
1035                 .setQueingLatencyCharacteristic("12345678")
1036                 .setJitterCharacteristic("12345678")
1037                 .setWanderCharacteristic("12345678")
1038                 .setTrafficPropertyName("FIXED_LATENCY")
1039                 .build();
1040             RiskCharacteristic riskCharacteristic = new RiskCharacteristicBuilder()
1041                 .setRiskCharacteristicName("risk characteristic")
1042                 .setRiskIdentifierList(List.of("risk identifier1", "risk identifier2"))
1043                 .build();
1044             NodeRuleGroup nodeRuleGroup = new NodeRuleGroupBuilder()
1045                 .setUuid(new Uuid(
1046                     UUID.nameUUIDFromBytes(("dsr node rule group " + count).getBytes(Charset.forName("UTF-8")))
1047                         .toString()))
1048                 .setRule(ruleList)
1049                 .setNodeEdgePoint(nepList)
1050                 .setRiskCharacteristic(Map.of(riskCharacteristic.key(), riskCharacteristic))
1051                 .setCostCharacteristic(Map.of(costCharacteristic.key(), costCharacteristic))
1052                 .setLatencyCharacteristic(Map.of(latencyCharacteristic.key(), latencyCharacteristic))
1053                 .build();
1054             nodeRuleGroupMap.put(nodeRuleGroup.key(), nodeRuleGroup);
1055             count++;
1056         }
1057         return nodeRuleGroupMap;
1058     }
1059
1060     private List<Class<? extends LAYERPROTOCOLQUALIFIER>> createSupportedLayerProtocolQualifier(
1061             List<Class<? extends SupportedIfCapability>> sicList, LayerProtocolName lpn) {
1062         if (sicList == null) {
1063             return List.of(PHOTONICLAYERQUALIFIEROMS.class);
1064         }
1065         Map<SupportedInterfaceCapabilityKey, SupportedInterfaceCapability> supIfMap = new HashMap<>();
1066         LOG.info("SIC list = {}", sicList);
1067         for (Class<? extends SupportedIfCapability> supInterCapa : sicList) {
1068             SupportedInterfaceCapability supIfCapa = new SupportedInterfaceCapabilityBuilder()
1069                     .withKey(new SupportedInterfaceCapabilityKey(convertSupIfCapa(supInterCapa)))
1070                     .setIfCapType(convertSupIfCapa(supInterCapa))
1071                     .build();
1072             supIfMap.put(supIfCapa.key(), supIfCapa);
1073         }
1074         List<Class<? extends LAYERPROTOCOLQUALIFIER>> sclpqList = new ArrayList<>();
1075         for (SupportedInterfaceCapability sic : supIfMap.values()) {
1076             switch (lpn.getName()) {
1077                 case "DSR":
1078                 case "ODU":
1079                     switch (sic.getIfCapType().getSimpleName()) {
1080                         // TODO: it may be needed to add more cases clauses if the interface capabilities of a
1081                         //  port are extended in the config file
1082                         case "If1GEODU0":
1083                             sclpqList.add(ODUTYPEODU0.class);
1084                             sclpqList.add(DIGITALSIGNALTYPEGigE.class);
1085                             break;
1086                         case "If10GEODU2e":
1087                             sclpqList.add(ODUTYPEODU2E.class);
1088                             sclpqList.add(DIGITALSIGNALTYPE10GigELAN.class);
1089                             break;
1090                         case "If10GEODU2":
1091                             sclpqList.add(ODUTYPEODU2.class);
1092                             sclpqList.add(DIGITALSIGNALTYPE10GigELAN.class);
1093                             break;
1094                         case "If10GE":
1095                             sclpqList.add(DIGITALSIGNALTYPE10GigELAN.class);
1096                             break;
1097                         case "If100GEODU4":
1098                             sclpqList.add(DIGITALSIGNALTYPE100GigE.class);
1099                             sclpqList.add(ODUTYPEODU4.class);
1100                             break;
1101                         case "If100GE":
1102                             sclpqList.add(DIGITALSIGNALTYPE100GigE.class);
1103                             break;
1104                         case "IfOCHOTU4ODU4":
1105                         case "IfOCH":
1106                             sclpqList.add(ODUTYPEODU4.class);
1107                             break;
1108                         default:
1109                             LOG.error("IfCapability type not managed");
1110                             break;
1111                     }
1112                     break;
1113                 case "PHOTONIC_MEDIA":
1114                     if (sic.getIfCapType().getSimpleName().equals("IfOCHOTU4ODU4")
1115                         || sic.getIfCapType().getSimpleName().equals("IfOCH")) {
1116                         sclpqList.add(PHOTONICLAYERQUALIFIEROTSi.class);
1117                         sclpqList.add(PHOTONICLAYERQUALIFIEROMS.class);
1118                     }
1119                     break;
1120                 default:
1121                     LOG.error("Layer Protocol Name is unknown {}", lpn.getName());
1122                     break;
1123             }
1124         }
1125         return sclpqList;
1126     }
1127
1128     private static Class<? extends SupportedIfCapability> convertSupIfCapa(Class<? extends
1129             SupportedIfCapability> ifCapType) {
1130         LOG.info("Interface Capability type = {}", ifCapType.getSimpleName());
1131         switch (ifCapType.getSimpleName()) {
1132             case "If100GEODU4":
1133                 return If100GEODU4.class;
1134             case "IfOCHOTU4ODU4":
1135                 return IfOCHOTU4ODU4.class;
1136             case "If1GEODU0":
1137                 return If1GEODU0.class;
1138             case "If10GEODU2e":
1139                 return If10GEODU2e.class;
1140             case "If10GEODU2":
1141                 return If10GEODU2.class;
1142             case "If100GE":
1143                 return If100GE.class;
1144             case "If10GE":
1145                 return If10GE.class;
1146             case "If1GE":
1147                 return If1GE.class;
1148             case "IfOCH":
1149                 return IfOCH.class;
1150             default:
1151                 return null;
1152         }
1153     }
1154
1155     private void mergeNodeinTopology(Map<NodeKey, Node> nodeMap) {
1156         // TODO is this merge correct? Should we just merge topology by changing the nodes map??
1157         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
1158         LOG.info("Creating tapi node in TAPI topology context");
1159         InstanceIdentifier<Topology> topoIID = InstanceIdentifier.builder(Context.class)
1160             .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1.class)
1161             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext.class)
1162             .child(Topology.class, new TopologyKey(this.tapiTopoUuid))
1163             .build();
1164
1165         Topology topology = new TopologyBuilder().setUuid(this.tapiTopoUuid).setNode(nodeMap).build();
1166
1167         // merge in datastore
1168         this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topoIID,
1169                 topology);
1170         try {
1171             this.networkTransactionService.commit().get();
1172         } catch (InterruptedException | ExecutionException e) {
1173             LOG.error("Error populating TAPI topology: ", e);
1174         }
1175         LOG.info("Node added succesfully.");
1176     }
1177
1178     private void mergeLinkinTopology(Map<LinkKey, Link> linkMap) {
1179         // TODO is this merge correct? Should we just merge topology by changing the nodes map??
1180         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
1181         LOG.info("Creating tapi node in TAPI topology context");
1182         InstanceIdentifier<Topology> topoIID = InstanceIdentifier.builder(Context.class)
1183                 .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1.class)
1184                 .child(org.opendaylight.yang.gen.v1.urn
1185                         .onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext.class)
1186                 .child(Topology.class, new TopologyKey(this.tapiTopoUuid))
1187                 .build();
1188
1189         Topology topology = new TopologyBuilder().setUuid(this.tapiTopoUuid).setLink(linkMap).build();
1190
1191         // merge in datastore
1192         this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topoIID,
1193                 topology);
1194         try {
1195             this.networkTransactionService.commit().get();
1196         } catch (InterruptedException | ExecutionException e) {
1197             LOG.error("Error populating TAPI topology: ", e);
1198         }
1199         LOG.info("Roadm Link added succesfully.");
1200     }
1201
1202     private void mergeSipsinContext(Map<ServiceInterfacePointKey, ServiceInterfacePoint> sips) {
1203         // TODO is this merge correct? Should we just merge topology by changing the nodes map??
1204         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
1205         try {
1206             ContextBuilder contextBuilder = new ContextBuilder();
1207             contextBuilder.setServiceInterfacePoint(sips);
1208             InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
1209             // merge in datastore
1210             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, contextIID,
1211                     contextBuilder.build());
1212             this.networkTransactionService.commit().get();
1213             LOG.info("TAPI SIPs merged successfully.");
1214         } catch (InterruptedException | ExecutionException e) {
1215             LOG.error("Failed to merge TAPI Sips", e);
1216         }
1217     }
1218
1219     private void deleteLinkFromTopo(Uuid linkUuid) {
1220         // TODO: check if this IID is correct
1221         try {
1222             InstanceIdentifier<Link> linkIID = InstanceIdentifier.builder(Context.class)
1223                 .augmentation(Context1.class).child(TopologyContext.class).child(Topology.class,
1224                     new TopologyKey(this.tapiTopoUuid)).child(Link.class, new LinkKey(linkUuid)).build();
1225             this.networkTransactionService.delete(LogicalDatastoreType.OPERATIONAL, linkIID);
1226             this.networkTransactionService.commit().get();
1227             LOG.info("TAPI link deleted successfully.");
1228         } catch (InterruptedException | ExecutionException e) {
1229             LOG.error("Failed to delete TAPI link", e);
1230         }
1231     }
1232
1233     private void deleteNodeFromTopo(Uuid nodeUuid) {
1234         // TODO: check if this IID is correct
1235         try {
1236             InstanceIdentifier<Node> nodeIDD = InstanceIdentifier.builder(Context.class)
1237                 .augmentation(Context1.class).child(TopologyContext.class).child(Topology.class,
1238                     new TopologyKey(this.tapiTopoUuid)).child(Node.class, new NodeKey(nodeUuid)).build();
1239             this.networkTransactionService.delete(LogicalDatastoreType.OPERATIONAL, nodeIDD);
1240             this.networkTransactionService.commit().get();
1241             LOG.info("TAPI Node deleted successfully.");
1242         } catch (InterruptedException | ExecutionException e) {
1243             LOG.error("Failed to delete TAPI Node", e);
1244         }
1245     }
1246
1247     private void deleteSipFromTopo(Uuid sipUuid) {
1248         // TODO: check if this IID is correct
1249         try {
1250             InstanceIdentifier<ServiceInterfacePoint> sipIID = InstanceIdentifier.builder(Context.class)
1251                     .child(ServiceInterfacePoint.class, new ServiceInterfacePointKey(sipUuid)).build();
1252             this.networkTransactionService.delete(LogicalDatastoreType.OPERATIONAL, sipIID);
1253             this.networkTransactionService.commit().get();
1254             LOG.info("TAPI SIP deleted successfully.");
1255         } catch (InterruptedException | ExecutionException e) {
1256             LOG.error("Failed to delete TAPI SIP", e);
1257         }
1258     }
1259
1260     private void updateConnectivityServicesState(Uuid sipUuid, String nodeId) {
1261         // TODO: check if this IID is correct
1262         InstanceIdentifier<ConnectivityContext> connectivitycontextIID = InstanceIdentifier.builder(Context.class)
1263                 .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.Context1.class)
1264                 .child(ConnectivityContext.class)
1265                 .build();
1266         ConnectivityContext connContext = null;
1267         try {
1268             Optional<ConnectivityContext> optConnContext =
1269                     this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, connectivitycontextIID)
1270                             .get();
1271             if (!optConnContext.isPresent()) {
1272                 LOG.error("Couldnt retrieve connectivity context from datastore");
1273                 return;
1274             }
1275             connContext = optConnContext.get();
1276         } catch (InterruptedException | ExecutionException e) {
1277             LOG.error("Couldnt read connectivity context from datastore", e);
1278         }
1279         if (connContext == null) {
1280             LOG.error("Connectivity context is empty");
1281             return;
1282         }
1283         // Loop through services, check if the endpoint uuid is equal to the sip.
1284         // If so update state.
1285         Map<ConnectivityServiceKey, ConnectivityService> connServMap = connContext.getConnectivityService();
1286         Map<ConnectionKey, Connection> connMap = connContext.getConnection();
1287         if (connServMap != null) {
1288             for (ConnectivityService service:connServMap.values()) {
1289                 Map<EndPointKey, EndPoint> serviceEndPoints = service.getEndPoint();
1290                 if (serviceEndPoints.values().stream().anyMatch(endPoint -> endPoint.getServiceInterfacePoint()
1291                     .getServiceInterfacePointUuid().equals(sipUuid))) {
1292                     LOG.info("Service using SIP of node {} identified. Update state of service", nodeId);
1293                     ConnectivityService updService = new ConnectivityServiceBuilder(service)
1294                         .setAdministrativeState(AdministrativeState.LOCKED)
1295                         .setOperationalState(OperationalState.DISABLED)
1296                         .setLifecycleState(LifecycleState.PENDINGREMOVAL)
1297                         .build();
1298                     updateConnectivityService(updService);
1299                 }
1300             }
1301         }
1302         // Update state of connections
1303         if (connMap != null) {
1304             for (Connection connection:connMap.values()) {
1305                 if (connection.getName().values().stream().anyMatch(name -> name.getValue().contains(nodeId))) {
1306                     Connection updConn = new ConnectionBuilder(connection)
1307                         .setLifecycleState(LifecycleState.PENDINGREMOVAL)
1308                         .setOperationalState(OperationalState.DISABLED)
1309                         .build();
1310                     updateConnection(updConn);
1311                 }
1312             }
1313         }
1314     }
1315
1316     private void updateConnection(Connection updConn) {
1317         // TODO: check if this IID is correct
1318         InstanceIdentifier<Connection> connectionIID = InstanceIdentifier.builder(Context.class)
1319                 .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.Context1.class)
1320                 .child(ConnectivityContext.class).child(Connection.class,
1321                         new ConnectionKey(updConn.getUuid())).build();
1322         this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, connectionIID, updConn);
1323         try {
1324             this.networkTransactionService.commit().get();
1325         } catch (InterruptedException | ExecutionException e) {
1326             LOG.error("Error committing into datastore", e);
1327         }
1328     }
1329
1330     private void updateConnectivityService(ConnectivityService updService) {
1331         // TODO: check if this IID is correct
1332         InstanceIdentifier<ConnectivityService> connectivityserviceIID = InstanceIdentifier.builder(Context.class)
1333                 .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.Context1.class)
1334                 .child(ConnectivityContext.class).child(ConnectivityService.class,
1335                         new ConnectivityServiceKey(updService.getUuid())).build();
1336         this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, connectivityserviceIID, updService);
1337         try {
1338             this.networkTransactionService.commit().get();
1339         } catch (InterruptedException | ExecutionException e) {
1340             LOG.error("Error committing into datastore", e);
1341         }
1342     }
1343
1344 }