Initial OR-TAPI mapping: Topology
[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.context.ConnectivityContextBuilder;
28 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.context.TopologyContextBuilder;
29 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.NwTopologyServiceBuilder;
30 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.Topology;
31 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.TopologyKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class TapiContext {
37
38     private static final Logger LOG = LoggerFactory.getLogger(TapiContext.class);
39     public static final String TAPI_CONTEXT = "T-API context";
40     private final NetworkTransactionService networkTransactionService;
41
42     public TapiContext(NetworkTransactionService networkTransactionService) {
43         this.networkTransactionService = networkTransactionService;
44         createTapiContext();
45     }
46
47     private void createTapiContext() {
48         try {
49             // Augmenting tapi context to include topology and connectivity contexts
50             Name contextName = new NameBuilder().setValue(TAPI_CONTEXT).setValueName("TAPI Context Name").build();
51
52             Context1 connectivityContext =
53                 new Context1Builder()
54                     .setConnectivityContext(
55                         new ConnectivityContextBuilder()
56                             .setConnection(new HashMap<>())
57                             .setConnectivityService(new HashMap<>())
58                             .build())
59                     .build();
60
61             Name nwTopoServiceName =
62                 new NameBuilder()
63                     .setValue("Network Topo Service")
64                     .setValueName("Network Topo Service Name")
65                     .build();
66
67             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1 topologyContext
68                 = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1Builder()
69                     .setTopologyContext(new TopologyContextBuilder()
70                         .setNwTopologyService(new NwTopologyServiceBuilder()
71                             .setTopology(new HashMap<>())
72                             .setUuid(
73                                 new Uuid(
74                                     UUID.nameUUIDFromBytes("Network Topo Service".getBytes(Charset.forName("UTF-8")))
75                                         .toString()))
76                             .setName(Map.of(nwTopoServiceName.key(), nwTopoServiceName))
77                             .build())
78                         .setTopology(new HashMap<>())
79                         .build())
80                     .build();
81
82             ContextBuilder contextBuilder = new ContextBuilder()
83                     .setName(Map.of(contextName.key(), contextName))
84                     .setUuid(
85                         new Uuid(UUID.nameUUIDFromBytes(TAPI_CONTEXT.getBytes(Charset.forName("UTF-8"))).toString()))
86                     .setServiceInterfacePoint(new HashMap<>())
87                     .addAugmentation(connectivityContext)
88                     .addAugmentation(topologyContext);
89
90             // todo: add notification context
91             InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
92             // put in datastore
93             this.networkTransactionService.put(LogicalDatastoreType.OPERATIONAL, contextIID, contextBuilder.build());
94             this.networkTransactionService.commit().get();
95             LOG.info("TAPI context created successfully.");
96         } catch (InterruptedException | ExecutionException e) {
97             LOG.error("Failed to create TAPI context", e);
98         }
99     }
100
101     public Context getTapiContext() {
102         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
103         //  There is no Identifiable in Context model
104         InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
105         try {
106             Optional<Context> optionalContext = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL,
107                     contextIID).get();
108             if (!optionalContext.isPresent()) {
109                 LOG.error("Tapi context is not present in datastore");
110                 return null;
111             }
112             return optionalContext.get();
113         } catch (InterruptedException | ExecutionException e) {
114             LOG.error("Couldnt read tapi context from datastore", e);
115             return null;
116         }
117     }
118
119     public void deleteTapiContext() {
120
121     }
122
123     public void updateTopologyContext(Map<TopologyKey, Topology> topologyMap) {
124         // TODO: solve error when merging: Topology is not a valid child of topology context?
125         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
126         try {
127             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext
128                     topologyContext = new TopologyContextBuilder()
129                     //.setNwTopologyService(new NwTopologyServiceBuilder().build())
130                     .setTopology(topologyMap)
131                     .build();
132             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn
133                     .onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext> topologycontextIID =
134                     InstanceIdentifier.builder(Context.class).augmentation(org.opendaylight.yang.gen.v1.urn
135                             .onf.otcc.yang.tapi.topology.rev181210.Context1.class)
136                             .child(org.opendaylight.yang.gen.v1.urn
137                                     .onf.otcc.yang.tapi.topology.rev181210.context.TopologyContext.class)
138                             .build();
139             // merge in datastore
140             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topologycontextIID,
141                     topologyContext);
142             this.networkTransactionService.commit().get();
143             LOG.info("TAPI topology merged successfully.");
144         } catch (InterruptedException | ExecutionException e) {
145             LOG.error("Failed to merge TAPI topology", e);
146         }
147     }
148
149     public void updateSIPContext(Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap) {
150         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
151         try {
152             ContextBuilder contextBuilder = new ContextBuilder().setServiceInterfacePoint(sipMap);
153             InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
154             // merge in datastore
155             this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, contextIID,
156                     contextBuilder.build());
157             this.networkTransactionService.commit().get();
158             LOG.info("TAPI SIPs merged successfully.");
159         } catch (InterruptedException | ExecutionException e) {
160             LOG.error("Failed to merge TAPI SIPs", e);
161         }
162     }
163 }