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