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