2 * Copyright © 2021 Nokia, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.tapi.utils;
10 import java.nio.charset.Charset;
11 import java.util.HashMap;
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;
59 public class TapiContext {
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;
66 public TapiContext(NetworkTransactionService networkTransactionService) {
67 this.networkTransactionService = networkTransactionService;
71 private void createTapiContext() {
73 // Augmenting tapi context to include topology and connectivity contexts
74 Name contextName = new NameBuilder().setValue(TAPI_CONTEXT).setValueName("TAPI Context Name").build();
76 Context1 connectivityContext =
78 .setConnectivityContext(
79 new ConnectivityContextBuilder()
80 .setConnection(new HashMap<>())
81 .setConnectivityService(new HashMap<>())
85 Name nwTopoServiceName =
87 .setValue("Network Topo Service")
88 .setValueName("Network Topo Service Name")
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<>())
98 UUID.nameUUIDFromBytes("Network Topo Service".getBytes(Charset.forName("UTF-8")))
100 .setName(Map.of(nwTopoServiceName.key(), nwTopoServiceName))
102 .setTopology(new HashMap<>())
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<>())
114 ContextBuilder contextBuilder = new ContextBuilder()
115 .setName(Map.of(contextName.key(), contextName))
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);
123 // todo: add notification context
124 InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
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);
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();
139 Optional<Context> optionalContext = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL,
141 if (!optionalContext.isPresent()) {
142 LOG.error("Tapi context is not present in datastore");
145 return optionalContext.orElseThrow();
146 } catch (InterruptedException | ExecutionException e) {
147 LOG.error("Couldnt read tapi context from datastore", e);
152 public void deleteTapiContext() {
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??
160 TopologyContext topologyContext = new TopologyContextBuilder()
161 //.setNwTopologyService(new NwTopologyServiceBuilder().build())
162 .setTopology(topologyMap)
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)
169 // merge in datastore
170 this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topologycontextIID,
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);
179 public void updateSIPContext(Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap) {
180 // TODO: verify this is correct. Should we identify the context IID with the context UUID??
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);
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??
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)
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)
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);
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))
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");
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");
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)
253 LOG.info("New ONEP is {}", newOnep);
254 // merge in datastore
255 this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, onepIID,
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);
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();
272 Optional<Node> optNode = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, nodeIID)
274 if (!optNode.isPresent()) {
275 LOG.error(NODE_NOT_PRESENT);
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);
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());
304 OwnedNodeEdgePoint newOnep = newOnepBuilder.build();
305 onepMap.put(newOnep.key(), newOnep);
307 LOG.debug("NEPs of node after creating map to be returned to the getTapiNode function = {}",
309 return new NodeBuilder(node)
310 .setOwnedNodeEdgePoint(onepMap)
312 } catch (InterruptedException | ExecutionException e) {
313 LOG.error("Couldnt read node in topology", e);
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();
326 Optional<OwnedNodeEdgePoint> optNode = this.networkTransactionService
327 .read(LogicalDatastoreType.OPERATIONAL, nepIID)
329 if (!optNode.isPresent()) {
330 LOG.error(NODE_NOT_PRESENT);
333 return optNode.orElseThrow();
334 } catch (InterruptedException | ExecutionException e) {
335 LOG.error("Couldnt read NEP in topology", e);
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();
347 Optional<Link> optLink = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, linkIID)
349 if (!optLink.isPresent()) {
350 LOG.error(NODE_NOT_PRESENT);
353 return optLink.orElseThrow();
354 } catch (InterruptedException | ExecutionException e) {
355 LOG.error("Couldnt read link in topology", e);
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)
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");
373 return optTopoContext.orElseThrow().getTopology();
374 } catch (InterruptedException | ExecutionException e) {
375 LOG.error("Couldnt read topology context", e);
380 public ConnectivityService getConnectivityService(Uuid serviceUuid) {
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))
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");
396 return optConnServ.orElseThrow();
397 } catch (InterruptedException | ExecutionException e) {
398 LOG.error("Connectivity service not found in tapi context. Error:", e);
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");
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());
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))
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);
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(
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());
454 // connection not found in tapi context
457 LayerProtocolName lowerConnLayer = conn1.getLayerProtocolName();
458 switch (serviceLayer.getIntValue()) {
462 if (lowerConnLayer.equals(serviceLayer)) {
463 deleteConnection(lowerConnection.getConnectionUuid(), serviceUuid, serviceLayer);
468 deleteConnection(lowerConnection.getConnectionUuid(), serviceUuid, serviceLayer);
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);
479 LOG.info("Unknown service Layer: {}", serviceLayer.getName());
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);
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!");
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());
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());
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) {
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());
527 LOG.info("isNotUsedByOtherService: No other service uses connection {}, therefore it can be safely deleted",
528 connection.getUuid());
532 public Connection getConnection(Uuid connectionUuid) {
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))
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");
548 return optConn.orElseThrow();
549 } catch (InterruptedException | ExecutionException e) {
550 LOG.error("Connection not found in tapi context. Error:", e);
555 public Map<ConnectivityServiceKey, ConnectivityService> getConnectivityServices() {
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)
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)
569 if (!optConnContext.isPresent()) {
570 LOG.error("Connectivity context not found in tapi context");
573 return optConnContext.orElseThrow().getConnectivityService();
574 } catch (InterruptedException | ExecutionException e) {
575 LOG.error("Connectivity context not found in tapi context. Error:", e);
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();
588 Optional<OwnedNodeEdgePoint> optNode = this.networkTransactionService
589 .read(LogicalDatastoreType.OPERATIONAL, nepIID).get();
590 if (!optNode.isPresent()) {
591 LOG.error(NODE_NOT_PRESENT);
594 if (optNode.orElseThrow().augmentation(OwnedNodeEdgePoint1.class) == null) {
595 LOG.error("Node doesnt have ceps");
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);