Migration to TAPI 2.4 Step3
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / utils / TapiContext.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.utils;
9
10 import java.nio.charset.Charset;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Optional;
14 import java.util.UUID;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
18 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context;
19 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.ContextBuilder;
20 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.LayerProtocolName;
21 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid;
22 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.Name;
23 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.NameBuilder;
24 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.tapi.context.ServiceInterfacePoint;
25 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.tapi.context.ServiceInterfacePointKey;
26 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.Context1;
27 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.Context1Builder;
28 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.OwnedNodeEdgePoint1;
29 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.OwnedNodeEdgePoint1Builder;
30 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.cep.list.ConnectionEndPoint;
31 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.cep.list.ConnectionEndPointKey;
32 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.connection.LowerConnection;
33 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.connection.LowerConnectionKey;
34 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.connectivity.context.Connection;
35 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.connectivity.context.ConnectionKey;
36 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.connectivity.context.ConnectivityService;
37 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.connectivity.context.ConnectivityServiceKey;
38 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContextBuilder;
39 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.context.topology.context.topology.node.owned.node.edge.point.CepList;
40 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.context.topology.context.topology.node.owned.node.edge.point.CepListBuilder;
41 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.context.NotificationContextBuilder;
42 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext;
43 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContextBuilder;
44 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.OwnedNodeEdgePoint;
45 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.OwnedNodeEdgePointBuilder;
46 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.OwnedNodeEdgePointKey;
47 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Link;
48 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.LinkKey;
49 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node;
50 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.NodeBuilder;
51 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.NodeKey;
52 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.NwTopologyServiceBuilder;
53 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.Topology;
54 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.TopologyKey;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class TapiContext {
60
61     private static final Logger LOG = LoggerFactory.getLogger(TapiContext.class);
62     public static final String TAPI_CONTEXT = "T-API context";
63     public static final String NODE_NOT_PRESENT = "Node is not present in datastore";
64     private final NetworkTransactionService networkTransactionService;
65
66     public TapiContext(NetworkTransactionService networkTransactionService) {
67         this.networkTransactionService = networkTransactionService;
68         createTapiContext();
69     }
70
71     private void createTapiContext() {
72         try {
73             // Augmenting tapi context to include topology and connectivity contexts
74             Name contextName = new NameBuilder().setValue(TAPI_CONTEXT).setValueName("TAPI Context Name").build();
75
76             Context1 connectivityContext =
77                 new Context1Builder()
78                     .setConnectivityContext(
79                         new ConnectivityContextBuilder()
80                             .setConnection(new HashMap<>())
81                             .setConnectivityService(new HashMap<>())
82                             .build())
83                     .build();
84
85             Name nwTopoServiceName =
86                 new NameBuilder()
87                     .setValue("Network Topo Service")
88                     .setValueName("Network Topo Service Name")
89                     .build();
90
91             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1 topologyContext
92                 = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1Builder()
93                     .setTopologyContext(new TopologyContextBuilder()
94                         .setNwTopologyService(new NwTopologyServiceBuilder()
95                             .setTopology(new HashMap<>())
96                             .setUuid(
97                                 new Uuid(
98                                     UUID.nameUUIDFromBytes("Network Topo Service".getBytes(Charset.forName("UTF-8")))
99                                         .toString()))
100                             .setName(Map.of(nwTopoServiceName.key(), nwTopoServiceName))
101                             .build())
102                         .setTopology(new HashMap<>())
103                         .build())
104                     .build();
105
106             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.Context1 notificationContext
107                 = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.Context1Builder()
108                     .setNotificationContext(new NotificationContextBuilder()
109                         .setNotification(new HashMap<>())
110                         .setNotifSubscription(new HashMap<>())
111                         .build())
112                     .build();
113
114             ContextBuilder contextBuilder = new ContextBuilder()
115                     .setName(Map.of(contextName.key(), contextName))
116                     .setUuid(
117                         new Uuid(UUID.nameUUIDFromBytes(TAPI_CONTEXT.getBytes(Charset.forName("UTF-8"))).toString()))
118                     .setServiceInterfacePoint(new HashMap<>())
119                     .addAugmentation(connectivityContext)
120                     .addAugmentation(topologyContext)
121                     .addAugmentation(notificationContext);
122
123             // todo: add notification context
124             InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
125             // put in datastore
126             this.networkTransactionService.put(LogicalDatastoreType.OPERATIONAL, contextIID, contextBuilder.build());
127             this.networkTransactionService.commit().get();
128             LOG.info("TAPI context created successfully.");
129         } catch (InterruptedException | ExecutionException e) {
130             LOG.error("Failed to create TAPI context", e);
131         }
132     }
133
134     public Context getTapiContext() {
135         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
136         //  There is no Identifiable in Context model
137         InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
138         try {
139             Optional<Context> optionalContext = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL,
140                     contextIID).get();
141             if (!optionalContext.isPresent()) {
142                 LOG.error("Tapi context is not present in datastore");
143                 return null;
144             }
145             return optionalContext.orElseThrow();
146         } catch (InterruptedException | ExecutionException e) {
147             LOG.error("Couldnt read tapi context from datastore", e);
148             return null;
149         }
150     }
151
152     public void deleteTapiContext() {
153
154     }
155
156     public void updateTopologyContext(Map<TopologyKey, Topology> topologyMap) {
157         // TODO: solve error when merging: Topology is not a valid child of topology context?
158         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
159         try {
160             TopologyContext topologyContext = new TopologyContextBuilder()
161                     //.setNwTopologyService(new NwTopologyServiceBuilder().build())
162                     .setTopology(topologyMap)
163                     .build();
164             InstanceIdentifier<TopologyContext> topologycontextIID =
165                     InstanceIdentifier.builder(Context.class).augmentation(org.opendaylight.yang.gen.v1.urn
166                             .onf.otcc.yang.tapi.topology.rev221121.Context1.class)
167                             .child(TopologyContext.class)
168                             .build();
169             // merge in datastore
170             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topologycontextIID,
171                     topologyContext);
172             this.networkTransactionService.commit().get();
173             LOG.info("TAPI topology merged successfully.");
174         } catch (InterruptedException | ExecutionException e) {
175             LOG.error("Failed to merge TAPI topology", e);
176         }
177     }
178
179     public void updateSIPContext(Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap) {
180         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
181         try {
182             ContextBuilder contextBuilder = new ContextBuilder().setServiceInterfacePoint(sipMap);
183             InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
184             // merge in datastore
185             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, contextIID,
186                     contextBuilder.build());
187             this.networkTransactionService.commit().get();
188             LOG.info("TAPI SIPs merged successfully.");
189         } catch (InterruptedException | ExecutionException e) {
190             LOG.error("Failed to merge TAPI SIPs", e);
191         }
192     }
193
194     public void updateConnectivityContext(Map<ConnectivityServiceKey, ConnectivityService> connServMap,
195                                           Map<ConnectionKey, Connection> connectionFullMap) {
196         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
197         try {
198             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext
199                 connectivityContext = new ConnectivityContextBuilder()
200                 .setConnectivityService(connServMap)
201                 .setConnection(connectionFullMap)
202                 .build();
203             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn
204                 .onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext> connectivitycontextIID =
205                     InstanceIdentifier.builder(Context.class).augmentation(Context1.class)
206                         .child(org.opendaylight.yang.gen.v1.urn
207                             .onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext.class)
208                         .build();
209             // merge in datastore
210             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, connectivitycontextIID,
211                 connectivityContext);
212             this.networkTransactionService.commit().get();
213             LOG.info("TAPI connectivity merged successfully.");
214             LOG.debug("TAPI connectivity merged successfully for services {}", connServMap.entrySet().iterator()
215                     .next().getKey().toString());
216         } catch (InterruptedException | ExecutionException e) {
217             LOG.error("Failed to merge TAPI connectivity", e);
218         }
219     }
220
221     public void updateTopologyWithCep(Uuid topoUuid, Uuid nodeUuid, Uuid nepUuid, ConnectionEndPoint cep) {
222         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
223         InstanceIdentifier<OwnedNodeEdgePoint> onepIID = InstanceIdentifier.builder(Context.class)
224             .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1.class)
225             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext.class)
226             .child(Topology.class, new TopologyKey(topoUuid))
227             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node.class,
228                     new NodeKey(nodeUuid))
229             .child(OwnedNodeEdgePoint.class, new OwnedNodeEdgePointKey(nepUuid))
230             .build();
231         try {
232             Optional<OwnedNodeEdgePoint> optionalOnep = this.networkTransactionService.read(
233                     LogicalDatastoreType.OPERATIONAL, onepIID).get();
234             if (!optionalOnep.isPresent()) {
235                 LOG.error("ONEP is not present in datastore");
236                 return;
237             }
238             OwnedNodeEdgePoint onep = optionalOnep.orElseThrow();
239             LOG.info("ONEP found = {}", onep);
240             // TODO -> If cep exists -> skip merging to datasore
241             OwnedNodeEdgePoint1 onep1 = onep.augmentation(OwnedNodeEdgePoint1.class);
242             if (onep1 != null && onep1.getCepList() != null && onep1.getCepList().getConnectionEndPoint() != null
243                     && onep1.getCepList().getConnectionEndPoint().containsKey(new ConnectionEndPointKey(cep.key()))) {
244                 LOG.info("CEP already in topology, skipping merge");
245                 return;
246             }
247             // Updated ONEP
248             CepList cepList = new CepListBuilder().setConnectionEndPoint(Map.of(cep.key(), cep)).build();
249             OwnedNodeEdgePoint1 onep1Bldr = new OwnedNodeEdgePoint1Builder().setCepList(cepList).build();
250             OwnedNodeEdgePoint newOnep = new OwnedNodeEdgePointBuilder(onep)
251                     .addAugmentation(onep1Bldr)
252                     .build();
253             LOG.info("New ONEP is {}", newOnep);
254             // merge in datastore
255             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, onepIID,
256                     newOnep);
257             this.networkTransactionService.commit().get();
258             LOG.info("CEP added successfully.");
259         } catch (InterruptedException | ExecutionException e) {
260             LOG.error("Couldnt update cep in topology", e);
261         }
262     }
263
264     public Node getTapiNode(Uuid topoUuid, Uuid nodeUuid) {
265         InstanceIdentifier<Node> nodeIID = InstanceIdentifier.builder(Context.class)
266             .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1.class)
267             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext.class)
268             .child(Topology.class, new TopologyKey(topoUuid))
269             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node.class,
270                         new NodeKey(nodeUuid)).build();
271         try {
272             Optional<Node> optNode = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, nodeIID)
273                     .get();
274             if (!optNode.isPresent()) {
275                 LOG.error(NODE_NOT_PRESENT);
276                 return null;
277             }
278             // TODO -> Need to remove CEPs from NEPs. If not error from get Topology details output
279             Node node = optNode.orElseThrow();
280             LOG.debug("NEPs of node before creating map to be returned to the getTapiNode function = {}",
281                 node.getOwnedNodeEdgePoint().size());
282             Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepMap = new HashMap<>();
283             for (OwnedNodeEdgePoint onep: node.getOwnedNodeEdgePoint().values()) {
284                 if (onep.augmentation(OwnedNodeEdgePoint1.class) == null) {
285                     onepMap.put(onep.key(), onep);
286                     continue;
287                 }
288                 OwnedNodeEdgePointBuilder newOnepBuilder = new OwnedNodeEdgePointBuilder()
289                     .setUuid(onep.getUuid())
290                     .setLayerProtocolName(onep.getLayerProtocolName())
291                     .setName(onep.getName())
292                     .setSupportedCepLayerProtocolQualifierInstances(
293                         onep.getSupportedCepLayerProtocolQualifierInstances())
294                     .setAdministrativeState(onep.getAdministrativeState())
295                     .setOperationalState(onep.getOperationalState())
296                     .setLifecycleState(onep.getLifecycleState())
297 //                    .setTerminationDirection(onep.getTerminationDirection())
298 //                    .setTerminationState(onep.getTerminationState())
299                     .setDirection(onep.getDirection())
300                     .setLinkPortRole(onep.getLinkPortRole());
301                 if (onep.getMappedServiceInterfacePoint() != null) {
302                     newOnepBuilder.setMappedServiceInterfacePoint(onep.getMappedServiceInterfacePoint());
303                 }
304                 OwnedNodeEdgePoint newOnep = newOnepBuilder.build();
305                 onepMap.put(newOnep.key(), newOnep);
306             }
307             LOG.debug("NEPs of node after creating map to be returned to the getTapiNode function = {}",
308                 onepMap.size());
309             return new NodeBuilder(node)
310                 .setOwnedNodeEdgePoint(onepMap)
311                 .build();
312         } catch (InterruptedException | ExecutionException e) {
313             LOG.error("Couldnt read node in topology", e);
314             return null;
315         }
316     }
317
318     public OwnedNodeEdgePoint getTapiNEP(Uuid topoUuid, Uuid nodeUuid, Uuid nepUuid) {
319         InstanceIdentifier<OwnedNodeEdgePoint> nepIID = InstanceIdentifier.builder(Context.class)
320             .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1.class)
321             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext.class)
322             .child(Topology.class, new TopologyKey(topoUuid))
323             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node.class,
324                 new NodeKey(nodeUuid)).child(OwnedNodeEdgePoint.class, new OwnedNodeEdgePointKey(nepUuid)).build();
325         try {
326             Optional<OwnedNodeEdgePoint> optNode = this.networkTransactionService
327                     .read(LogicalDatastoreType.OPERATIONAL, nepIID)
328                     .get();
329             if (!optNode.isPresent()) {
330                 LOG.error(NODE_NOT_PRESENT);
331                 return null;
332             }
333             return optNode.orElseThrow();
334         } catch (InterruptedException | ExecutionException e) {
335             LOG.error("Couldnt read NEP in topology", e);
336             return null;
337         }
338     }
339
340     public Link getTapiLink(Uuid topoUuid, Uuid linkUuid) {
341         InstanceIdentifier<Link> linkIID = InstanceIdentifier.builder(Context.class)
342             .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1.class)
343             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext.class)
344             .child(Topology.class, new TopologyKey(topoUuid))
345             .child(Link.class, new LinkKey(linkUuid)).build();
346         try {
347             Optional<Link> optLink = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, linkIID)
348                     .get();
349             if (!optLink.isPresent()) {
350                 LOG.error(NODE_NOT_PRESENT);
351                 return null;
352             }
353             return optLink.orElseThrow();
354         } catch (InterruptedException | ExecutionException e) {
355             LOG.error("Couldnt read link in topology", e);
356             return null;
357         }
358     }
359
360     public Map<TopologyKey, Topology> getTopologyContext() {
361         InstanceIdentifier<TopologyContext> topologycontextIID =
362                 InstanceIdentifier.builder(Context.class).augmentation(org.opendaylight.yang.gen.v1.urn
363                         .onf.otcc.yang.tapi.topology.rev221121.Context1.class)
364                         .child(TopologyContext.class)
365                         .build();
366         try {
367             Optional<TopologyContext> optTopoContext = this.networkTransactionService.read(
368                     LogicalDatastoreType.OPERATIONAL, topologycontextIID).get();
369             if (!optTopoContext.isPresent()) {
370                 LOG.error("Topology context is not present in datastore");
371                 return null;
372             }
373             return optTopoContext.orElseThrow().getTopology();
374         } catch (InterruptedException | ExecutionException e) {
375             LOG.error("Couldnt read topology context", e);
376             return null;
377         }
378     }
379
380     public ConnectivityService getConnectivityService(Uuid serviceUuid) {
381         try {
382             // First read connectivity service with service uuid and update info
383             InstanceIdentifier<ConnectivityService> connectivityServIID =
384                 InstanceIdentifier.builder(Context.class).augmentation(Context1.class)
385                     .child(org.opendaylight.yang.gen.v1.urn
386                         .onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext.class)
387                     .child(ConnectivityService.class, new ConnectivityServiceKey(serviceUuid))
388                     .build();
389
390             Optional<ConnectivityService> optConnServ =
391                 this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, connectivityServIID).get();
392             if (!optConnServ.isPresent()) {
393                 LOG.error("Connectivity service not found in tapi context");
394                 return null;
395             }
396             return optConnServ.orElseThrow();
397         } catch (InterruptedException | ExecutionException e) {
398             LOG.error("Connectivity service not found in tapi context. Error:", e);
399             return null;
400         }
401     }
402
403     public void deleteConnectivityService(Uuid serviceUuid) {
404         // TODO: handle case where the infrastructure service is removed before the top level service?
405         ConnectivityService connectivityService = getConnectivityService(serviceUuid);
406         if (connectivityService == null) {
407             LOG.error("Service doesnt exist in tapi context");
408             return;
409         }
410         for (org.opendaylight.yang.gen.v1
411                 .urn.onf.otcc.yang.tapi.connectivity.rev221121.connectivity.service.Connection connection:
412                     connectivityService.getConnection().values()) {
413             deleteConnection(connection.getConnectionUuid(), serviceUuid, connectivityService.getLayerProtocolName());
414         }
415         InstanceIdentifier<ConnectivityService> connectivityServIID =
416                 InstanceIdentifier.builder(Context.class).augmentation(Context1.class)
417                         .child(org.opendaylight.yang.gen.v1.urn
418                                 .onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext.class)
419                         .child(ConnectivityService.class, new ConnectivityServiceKey(serviceUuid))
420                         .build();
421         try {
422             this.networkTransactionService.delete(LogicalDatastoreType.OPERATIONAL, connectivityServIID);
423             this.networkTransactionService.commit().get();
424             LOG.info("Connectivity service deleted");
425         } catch (InterruptedException | ExecutionException e) {
426             LOG.error("Failed to delete Connectivity service", e);
427         }
428     }
429
430     private void deleteConnection(Uuid connectionUuid, Uuid serviceUuid, LayerProtocolName serviceLayer) {
431         // First read connectivity service with service uuid and update info
432         InstanceIdentifier<org.opendaylight.yang.gen.v1
433             .urn.onf.otcc.yang.tapi.connectivity.rev221121.connectivity.context.Connection> connectionIID =
434             InstanceIdentifier.builder(Context.class).augmentation(Context1.class)
435                 .child(org.opendaylight.yang.gen.v1.urn
436                     .onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext.class)
437                 .child(org.opendaylight.yang.gen.v1.urn
438                         .onf.otcc.yang.tapi.connectivity.rev221121.connectivity.context.Connection.class,
439                     new org.opendaylight.yang.gen.v1.urn
440                         .onf.otcc.yang.tapi.connectivity.rev221121.connectivity.context.ConnectionKey(
441                         connectionUuid))
442                 .build();
443         Connection connection = getConnection(connectionUuid);
444         if (connection != null && isNotUsedByOtherService(connection, serviceUuid)) {
445             Map<LowerConnectionKey, LowerConnection> lowerConnectionMap = connection.getLowerConnection();
446             if (lowerConnectionMap != null) {
447                 for (LowerConnection lowerConnection:lowerConnectionMap.values()) {
448                     // check layer of connection, for DSR service we only need to delete DSR layer
449                     // connection and XC at ODU. For ODU, only need to delete ODU connections and for
450                     // photonic media services all the photonic media. And when it is ETH we need to delete
451                     // everything and also without checking the lower connection layer
452                     Connection conn1 = getConnection(lowerConnection.getConnectionUuid());
453                     if (conn1 == null) {
454                         // connection not found in tapi context
455                         continue;
456                     }
457                     LayerProtocolName lowerConnLayer = conn1.getLayerProtocolName();
458                     switch (serviceLayer.getIntValue()) {
459                         case 0:
460                         case 3:
461                             // PHOTONIC & ODU
462                             if (lowerConnLayer.equals(serviceLayer)) {
463                                 deleteConnection(lowerConnection.getConnectionUuid(), serviceUuid, serviceLayer);
464                             }
465                             break;
466                         case 1:
467                             // ETH
468                             deleteConnection(lowerConnection.getConnectionUuid(), serviceUuid, serviceLayer);
469                             break;
470                         case 2:
471                             // DSR
472                             if (lowerConnLayer.equals(serviceLayer) || (lowerConnLayer.equals(LayerProtocolName.ODU)
473                                     && conn1.getName().values().stream().anyMatch(
474                                             name -> name.getValue().contains("XC")))) {
475                                 deleteConnection(lowerConnection.getConnectionUuid(), serviceUuid, serviceLayer);
476                             }
477                             break;
478                         default:
479                             LOG.info("Unknown service Layer: {}", serviceLayer.getName());
480                     }
481                 }
482             }
483         }
484         try {
485             this.networkTransactionService.delete(LogicalDatastoreType.OPERATIONAL, connectionIID);
486             this.networkTransactionService.commit().get();
487         } catch (InterruptedException | ExecutionException e) {
488             LOG.error("Failed to delete TAPI Connection", e);
489         }
490     }
491
492     private boolean isNotUsedByOtherService(Connection connection, Uuid serviceUuid) {
493         Map<ConnectivityServiceKey, ConnectivityService> connServicesMap = getConnectivityServices();
494         if (connServicesMap == null) {
495             LOG.info("isNotUsedByOtherService: No service in tapi context!");
496             return true;
497         }
498         for (ConnectivityService connService: connServicesMap.values()) {
499             if (connService.getConnection() == null || connService.getUuid().equals(serviceUuid)) {
500                 LOG.info("isNotUsedByOtherService: There are no connections in service {} or service in loop is the "
501                         + "service to be deleted", connService.getUuid().getValue());
502                 continue;
503             }
504             if (connService.getConnection().containsKey(
505                     new org.opendaylight.yang.gen.v1
506                         .urn.onf.otcc.yang.tapi.connectivity.rev221121.connectivity.service.ConnectionKey(
507                             connection.getUuid()))) {
508                 LOG.info("isNotUsedByOtherService: Connection {} is in used by service {}. Cannot remove it from "
509                         + "context", connection.getUuid().getValue(), connService.getUuid().getValue());
510                 return false;
511             }
512             LOG.info("isNotUsedByOtherService: Going to check lower connections");
513             for (org.opendaylight.yang.gen.v1.urn
514                         .onf.otcc.yang.tapi.connectivity.rev221121.connectivity.service.Connection
515                     conn:connService.getConnection().values()) {
516                 Connection connection1 = getConnection(conn.getConnectionUuid());
517                 if (connection1 == null || connection1.getLowerConnection() == null) {
518                     continue;
519                 }
520                 if (connection1.getLowerConnection().containsKey(new LowerConnectionKey(connection.getUuid()))) {
521                     LOG.info("isNotUsedByOtherService: Lower Connection {} is in used by service {}. Cannot remove it "
522                             + "from context", connection.getUuid().getValue(), connService.getUuid().getValue());
523                     return false;
524                 }
525             }
526         }
527         LOG.info("isNotUsedByOtherService: No other service uses connection {}, therefore it can be safely deleted",
528                 connection.getUuid());
529         return true;
530     }
531
532     public Connection getConnection(Uuid connectionUuid) {
533         try {
534             // First read connectivity service with service uuid and update info
535             InstanceIdentifier<Connection> connIID =
536                 InstanceIdentifier.builder(Context.class).augmentation(Context1.class)
537                     .child(org.opendaylight.yang.gen.v1.urn
538                         .onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext.class)
539                     .child(Connection.class, new ConnectionKey(connectionUuid))
540                     .build();
541
542             Optional<Connection> optConn =
543                 this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, connIID).get();
544             if (!optConn.isPresent()) {
545                 LOG.error("Connection not found in tapi context");
546                 return null;
547             }
548             return optConn.orElseThrow();
549         } catch (InterruptedException | ExecutionException e) {
550             LOG.error("Connection not found in tapi context. Error:", e);
551             return null;
552         }
553     }
554
555     public Map<ConnectivityServiceKey, ConnectivityService> getConnectivityServices() {
556         try {
557             // First read connectivity service with service uuid and update info
558             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn
559                 .onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext> connectivityContextIID =
560                 InstanceIdentifier.builder(Context.class).augmentation(Context1.class)
561                     .child(org.opendaylight.yang.gen.v1.urn
562                         .onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext.class)
563                     .build();
564
565             Optional<org.opendaylight.yang.gen.v1.urn
566                 .onf.otcc.yang.tapi.connectivity.rev221121.context.ConnectivityContext> optConnContext =
567                     this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, connectivityContextIID)
568                         .get();
569             if (!optConnContext.isPresent()) {
570                 LOG.error("Connectivity context not found in tapi context");
571                 return null;
572             }
573             return optConnContext.orElseThrow().getConnectivityService();
574         } catch (InterruptedException | ExecutionException e) {
575             LOG.error("Connectivity context not found in tapi context. Error:", e);
576             return null;
577         }
578     }
579
580     public ConnectionEndPoint getTapiCEP(Uuid topoUuid, Uuid nodeUuid, Uuid nepUuid, Uuid cepUuid) {
581         InstanceIdentifier<OwnedNodeEdgePoint> nepIID = InstanceIdentifier.builder(Context.class)
582             .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1.class)
583             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext.class)
584             .child(Topology.class, new TopologyKey(topoUuid))
585             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node.class,
586                 new NodeKey(nodeUuid)).child(OwnedNodeEdgePoint.class, new OwnedNodeEdgePointKey(nepUuid)).build();
587         try {
588             Optional<OwnedNodeEdgePoint> optNode = this.networkTransactionService
589                 .read(LogicalDatastoreType.OPERATIONAL, nepIID).get();
590             if (!optNode.isPresent()) {
591                 LOG.error(NODE_NOT_PRESENT);
592                 return null;
593             }
594             if (optNode.orElseThrow().augmentation(OwnedNodeEdgePoint1.class) == null) {
595                 LOG.error("Node doesnt have ceps");
596                 return null;
597             }
598             return optNode.orElseThrow().augmentation(OwnedNodeEdgePoint1.class).getCepList().getConnectionEndPoint()
599                 .get(new ConnectionEndPointKey(cepUuid));
600         } catch (InterruptedException | ExecutionException e) {
601             LOG.error("Couldnt read node in topology", e);
602             return null;
603         }
604     }
605 }