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