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.connectivity.rev181210.Context1;
24 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.Context1Builder;
25 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.context.ConnectivityContextBuilder;
26 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.context.TopologyContextBuilder;
27 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.NwTopologyServiceBuilder;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class TapiContext {
34 private static final Logger LOG = LoggerFactory.getLogger(TapiContext.class);
35 public static final String TAPI_CONTEXT = "T-API context";
36 private final NetworkTransactionService networkTransactionService;
38 public TapiContext(NetworkTransactionService networkTransactionService) {
39 this.networkTransactionService = networkTransactionService;
43 private void createTapiContext() {
45 // Augmenting tapi context to include topology and connectivity contexts
46 Name contextName = new NameBuilder().setValue(TAPI_CONTEXT).setValueName("TAPI Context Name").build();
48 Context1 connectivityContext =
50 .setConnectivityContext(
51 new ConnectivityContextBuilder()
52 .setConnection(new HashMap<>())
53 .setConnectivityService(new HashMap<>())
57 Name nwTopoServiceName =
59 .setValue("Network Topo Service")
60 .setValueName("Network Topo Service Name")
63 org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1 topologyContext
64 = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.Context1Builder()
65 .setTopologyContext(new TopologyContextBuilder()
66 .setNwTopologyService(new NwTopologyServiceBuilder()
67 .setTopology(new HashMap<>())
70 UUID.nameUUIDFromBytes("Network Topo Service".getBytes(Charset.forName("UTF-8")))
72 .setName(Map.of(nwTopoServiceName.key(), nwTopoServiceName))
74 .setTopology(new HashMap<>())
78 ContextBuilder contextBuilder = new ContextBuilder()
79 .setName(Map.of(contextName.key(), contextName))
81 new Uuid(UUID.nameUUIDFromBytes(TAPI_CONTEXT.getBytes(Charset.forName("UTF-8"))).toString()))
82 .setServiceInterfacePoint(new HashMap<>())
83 .addAugmentation(connectivityContext)
84 .addAugmentation(topologyContext);
86 // todo: add notification context
87 InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
89 this.networkTransactionService.put(LogicalDatastoreType.OPERATIONAL, contextIID, contextBuilder.build());
90 this.networkTransactionService.commit().get();
91 LOG.info("TAPI context created successfully.");
92 } catch (InterruptedException | ExecutionException e) {
93 LOG.error("Failed to create TAPI context", e);
97 public Context getTapiContext() {
98 // TODO: verify this is correct. Should we identify the context IID with the context UUID??
99 // There is no Identifiable in Context model
100 InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
102 Optional<Context> optionalContext = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL,
104 if (!optionalContext.isPresent()) {
105 LOG.error("Tapi context is not present in datastore");
108 return optionalContext.get();
109 } catch (InterruptedException | ExecutionException e) {
110 LOG.error("Couldnt read tapi context from datastore", e);
115 public void deleteTapiContext() {