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