1fc8c5c50c0b905db4ecdc00b22d3fe63ad32cce
[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 }