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