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.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;
55 public class TapiContext {
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;
61 public TapiContext(NetworkTransactionService networkTransactionService) {
62 this.networkTransactionService = networkTransactionService;
66 private void createTapiContext() {
68 // Augmenting tapi context to include topology and connectivity contexts
69 Name contextName = new NameBuilder().setValue(TAPI_CONTEXT).setValueName("TAPI Context Name").build();
71 Context1 connectivityContext =
73 .setConnectivityContext(
74 new ConnectivityContextBuilder()
75 .setConnection(new HashMap<>())
76 .setConnectivityService(new HashMap<>())
80 Name nwTopoServiceName =
82 .setValue("Network Topo Service")
83 .setValueName("Network Topo Service Name")
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<>())
93 UUID.nameUUIDFromBytes("Network Topo Service".getBytes(Charset.forName("UTF-8")))
95 .setName(Map.of(nwTopoServiceName.key(), nwTopoServiceName))
97 .setTopology(new HashMap<>())
101 ContextBuilder contextBuilder = new ContextBuilder()
102 .setName(Map.of(contextName.key(), contextName))
104 new Uuid(UUID.nameUUIDFromBytes(TAPI_CONTEXT.getBytes(Charset.forName("UTF-8"))).toString()))
105 .setServiceInterfacePoint(new HashMap<>())
106 .addAugmentation(connectivityContext)
107 .addAugmentation(topologyContext);
109 // todo: add notification context
110 InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
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);
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();
125 Optional<Context> optionalContext = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL,
127 if (!optionalContext.isPresent()) {
128 LOG.error("Tapi context is not present in datastore");
131 return optionalContext.get();
132 } catch (InterruptedException | ExecutionException e) {
133 LOG.error("Couldnt read tapi context from datastore", e);
138 public void deleteTapiContext() {
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??
146 TopologyContext topologyContext = new TopologyContextBuilder()
147 //.setNwTopologyService(new NwTopologyServiceBuilder().build())
148 .setTopology(topologyMap)
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)
155 // merge in datastore
156 this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topologycontextIID,
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);
165 public void updateSIPContext(Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap) {
166 // TODO: verify this is correct. Should we identify the context IID with the context UUID??
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);
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??
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)
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)
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);
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))
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");
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");
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)
237 LOG.info("New ONEP is {}", newOnep);
238 // merge in datastore
239 this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, onepIID,
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);
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();
256 Optional<Node> optNode = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, nodeIID)
258 if (!optNode.isPresent()) {
259 LOG.error("Node is not present in datastore");
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);
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());
285 onepMap.put(newOnepBuilder.key(), newOnepBuilder.build());
287 return new NodeBuilder(node)
288 .setOwnedNodeEdgePoint(onepMap)
290 } catch (InterruptedException | ExecutionException e) {
291 LOG.error("Couldnt read node in topology", e);
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();
304 Optional<OwnedNodeEdgePoint> optNode = this.networkTransactionService
305 .read(LogicalDatastoreType.OPERATIONAL, nepIID)
307 if (!optNode.isPresent()) {
308 LOG.error("Node is not present in datastore");
311 return optNode.get();
312 } catch (InterruptedException | ExecutionException e) {
313 LOG.error("Couldnt read NEP in topology", e);
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();
325 Optional<Link> optLink = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, linkIID)
327 if (!optLink.isPresent()) {
328 LOG.error("Node is not present in datastore");
331 return optLink.get();
332 } catch (InterruptedException | ExecutionException e) {
333 LOG.error("Couldnt read link in topology", e);
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)
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");
351 return optTopoContext.get().getTopology();
352 } catch (InterruptedException | ExecutionException e) {
353 LOG.error("Couldnt read topology context", e);