521857d6a125e0c6ce1a50649296ca3ad7e5c6e0
[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.TopologyContextBuilder;
39 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.OwnedNodeEdgePoint;
40 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.OwnedNodeEdgePointBuilder;
41 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.OwnedNodeEdgePointKey;
42 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.NodeKey;
43 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.NwTopologyServiceBuilder;
44 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.Topology;
45 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.TopologyKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class TapiContext {
51
52     private static final Logger LOG = LoggerFactory.getLogger(TapiContext.class);
53     public static final String TAPI_CONTEXT = "T-API context";
54     private final NetworkTransactionService networkTransactionService;
55
56     public TapiContext(NetworkTransactionService networkTransactionService) {
57         this.networkTransactionService = networkTransactionService;
58         createTapiContext();
59     }
60
61     private void createTapiContext() {
62         try {
63             // Augmenting tapi context to include topology and connectivity contexts
64             Name contextName = new NameBuilder().setValue(TAPI_CONTEXT).setValueName("TAPI Context Name").build();
65
66             Context1 connectivityContext =
67                 new Context1Builder()
68                     .setConnectivityContext(
69                         new ConnectivityContextBuilder()
70                             .setConnection(new HashMap<>())
71                             .setConnectivityService(new HashMap<>())
72                             .build())
73                     .build();
74
75             Name nwTopoServiceName =
76                 new NameBuilder()
77                     .setValue("Network Topo Service")
78                     .setValueName("Network Topo Service Name")
79                     .build();
80
81             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1 topologyContext
82                 = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1Builder()
83                     .setTopologyContext(new TopologyContextBuilder()
84                         .setNwTopologyService(new NwTopologyServiceBuilder()
85                             .setTopology(new HashMap<>())
86                             .setUuid(
87                                 new Uuid(
88                                     UUID.nameUUIDFromBytes("Network Topo Service".getBytes(Charset.forName("UTF-8")))
89                                         .toString()))
90                             .setName(Map.of(nwTopoServiceName.key(), nwTopoServiceName))
91                             .build())
92                         .setTopology(new HashMap<>())
93                         .build())
94                     .build();
95
96             ContextBuilder contextBuilder = new ContextBuilder()
97                     .setName(Map.of(contextName.key(), contextName))
98                     .setUuid(
99                         new Uuid(UUID.nameUUIDFromBytes(TAPI_CONTEXT.getBytes(Charset.forName("UTF-8"))).toString()))
100                     .setServiceInterfacePoint(new HashMap<>())
101                     .addAugmentation(connectivityContext)
102                     .addAugmentation(topologyContext);
103
104             // todo: add notification context
105             InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
106             // put in datastore
107             this.networkTransactionService.put(LogicalDatastoreType.OPERATIONAL, contextIID, contextBuilder.build());
108             this.networkTransactionService.commit().get();
109             LOG.info("TAPI context created successfully.");
110         } catch (InterruptedException | ExecutionException e) {
111             LOG.error("Failed to create TAPI context", e);
112         }
113     }
114
115     public Context getTapiContext() {
116         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
117         //  There is no Identifiable in Context model
118         InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
119         try {
120             Optional<Context> optionalContext = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL,
121                     contextIID).get();
122             if (!optionalContext.isPresent()) {
123                 LOG.error("Tapi context is not present in datastore");
124                 return null;
125             }
126             return optionalContext.get();
127         } catch (InterruptedException | ExecutionException e) {
128             LOG.error("Couldnt read tapi context from datastore", e);
129             return null;
130         }
131     }
132
133     public void deleteTapiContext() {
134
135     }
136
137     public void updateTopologyContext(Map<TopologyKey, Topology> topologyMap) {
138         // TODO: solve error when merging: Topology is not a valid child of topology context?
139         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
140         try {
141             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext
142                     topologyContext = new TopologyContextBuilder()
143                     //.setNwTopologyService(new NwTopologyServiceBuilder().build())
144                     .setTopology(topologyMap)
145                     .build();
146             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn
147                     .onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext> topologycontextIID =
148                     InstanceIdentifier.builder(Context.class).augmentation(org.opendaylight.yang.gen.v1.urn
149                             .onf.otcc.yang.tapi.topology.rev181210.Context1.class)
150                             .child(org.opendaylight.yang.gen.v1.urn
151                                     .onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext.class)
152                             .build();
153             // merge in datastore
154             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topologycontextIID,
155                     topologyContext);
156             this.networkTransactionService.commit().get();
157             LOG.info("TAPI topology merged successfully.");
158         } catch (InterruptedException | ExecutionException e) {
159             LOG.error("Failed to merge TAPI topology", e);
160         }
161     }
162
163     public void updateSIPContext(Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap) {
164         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
165         try {
166             ContextBuilder contextBuilder = new ContextBuilder().setServiceInterfacePoint(sipMap);
167             InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
168             // merge in datastore
169             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, contextIID,
170                     contextBuilder.build());
171             this.networkTransactionService.commit().get();
172             LOG.info("TAPI SIPs merged successfully.");
173         } catch (InterruptedException | ExecutionException e) {
174             LOG.error("Failed to merge TAPI SIPs", e);
175         }
176     }
177
178     public void updateConnectivityContext(Map<ConnectivityServiceKey, ConnectivityService> connServMap,
179                                           Map<ConnectionKey, Connection> connectionFullMap) {
180         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
181         try {
182             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.context.ConnectivityContext
183                     connectivityContext = new ConnectivityContextBuilder()
184                     .setConnectivityService(connServMap)
185                     .setConnection(connectionFullMap)
186                     .build();
187             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn
188                     .onf.otcc.yang.tapi.connectivity.rev181210.context.ConnectivityContext> connectivitycontextIID =
189                     InstanceIdentifier.builder(Context.class).augmentation(Context1.class)
190                             .child(org.opendaylight.yang.gen.v1.urn
191                                     .onf.otcc.yang.tapi.connectivity.rev181210.context.ConnectivityContext.class)
192                             .build();
193             // merge in datastore
194             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, connectivitycontextIID,
195                     connectivityContext);
196             this.networkTransactionService.commit().get();
197             LOG.info("TAPI connectivity merged successfully.");
198         } catch (InterruptedException | ExecutionException e) {
199             LOG.error("Failed to merge TAPI connectivity", e);
200         }
201     }
202
203     public void updateTopologyWithCep(Uuid topoUuid, Uuid nodeUuid, Uuid nepUuid, ConnectionEndPoint cep) {
204         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
205         InstanceIdentifier<OwnedNodeEdgePoint> onepIID = InstanceIdentifier.builder(Context.class)
206             .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1.class)
207             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext.class)
208             .child(Topology.class, new TopologyKey(topoUuid))
209             .child(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Node.class,
210                     new NodeKey(nodeUuid))
211             .child(OwnedNodeEdgePoint.class, new OwnedNodeEdgePointKey(nepUuid))
212             .build();
213         try {
214             Optional<OwnedNodeEdgePoint> optionalOnep = this.networkTransactionService.read(
215                     LogicalDatastoreType.OPERATIONAL, onepIID).get();
216             if (!optionalOnep.isPresent()) {
217                 LOG.error("ONEP is not present in datastore");
218                 return;
219             }
220             OwnedNodeEdgePoint onep = optionalOnep.get();
221             LOG.info("ONEP found = {}", onep);
222             // TODO -> If cep exists -> skip merging to datasore
223             OwnedNodeEdgePoint1 onep1 = onep.augmentation(OwnedNodeEdgePoint1.class);
224             if (onep1 != null && onep1.getCepList() != null && onep1.getCepList().getConnectionEndPoint() != null
225                     && onep1.getCepList().getConnectionEndPoint().containsKey(new ConnectionEndPointKey(cep.key()))) {
226                 LOG.info("CEP already in topology, skipping merge");
227                 return;
228             }
229             // Updated ONEP
230             CepList cepList = new CepListBuilder().setConnectionEndPoint(Map.of(cep.key(), cep)).build();
231             OwnedNodeEdgePoint1 onep1Bldr = new OwnedNodeEdgePoint1Builder().setCepList(cepList).build();
232             OwnedNodeEdgePoint newOnep = new OwnedNodeEdgePointBuilder(onep)
233                     .addAugmentation(onep1Bldr)
234                     .build();
235             LOG.info("New ONEP is {}", newOnep);
236             // merge in datastore
237             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, onepIID,
238                     newOnep);
239             this.networkTransactionService.commit().get();
240             LOG.info("CEP added successfully.");
241         } catch (InterruptedException | ExecutionException e) {
242             LOG.error("Couldnt update cep in topology", e);
243         }
244     }
245 }