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.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;
36 public class TapiContext {
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;
42 public TapiContext(NetworkTransactionService networkTransactionService) {
43 this.networkTransactionService = networkTransactionService;
47 private void createTapiContext() {
49 // Augmenting tapi context to include topology and connectivity contexts
50 Name contextName = new NameBuilder().setValue(TAPI_CONTEXT).setValueName("TAPI Context Name").build();
52 Context1 connectivityContext =
54 .setConnectivityContext(
55 new ConnectivityContextBuilder()
56 .setConnection(new HashMap<>())
57 .setConnectivityService(new HashMap<>())
61 Name nwTopoServiceName =
63 .setValue("Network Topo Service")
64 .setValueName("Network Topo Service Name")
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<>())
74 UUID.nameUUIDFromBytes("Network Topo Service".getBytes(Charset.forName("UTF-8")))
76 .setName(Map.of(nwTopoServiceName.key(), nwTopoServiceName))
78 .setTopology(new HashMap<>())
82 ContextBuilder contextBuilder = new ContextBuilder()
83 .setName(Map.of(contextName.key(), contextName))
85 new Uuid(UUID.nameUUIDFromBytes(TAPI_CONTEXT.getBytes(Charset.forName("UTF-8"))).toString()))
86 .setServiceInterfacePoint(new HashMap<>())
87 .addAugmentation(connectivityContext)
88 .addAugmentation(topologyContext);
90 // todo: add notification context
91 InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
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);
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();
106 Optional<Context> optionalContext = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL,
108 if (!optionalContext.isPresent()) {
109 LOG.error("Tapi context is not present in datastore");
112 return optionalContext.get();
113 } catch (InterruptedException | ExecutionException e) {
114 LOG.error("Couldnt read tapi context from datastore", e);
119 public void deleteTapiContext() {
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??
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)
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)
139 // merge in datastore
140 this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topologycontextIID,
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);
149 public void updateSIPContext(Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap) {
150 // TODO: verify this is correct. Should we identify the context IID with the context UUID??
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);