5c195f04416f995d53a3526e7a2400d427f8d5c9
[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.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;
31
32 public class TapiContext {
33
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;
37
38     public TapiContext(NetworkTransactionService networkTransactionService) {
39         this.networkTransactionService = networkTransactionService;
40         createTapiContext();
41     }
42
43     private void createTapiContext() {
44         try {
45             // Augmenting tapi context to include topology and connectivity contexts
46             Name contextName = new NameBuilder().setValue(TAPI_CONTEXT).setValueName("TAPI Context Name").build();
47
48             Context1 connectivityContext =
49                 new Context1Builder()
50                     .setConnectivityContext(
51                         new ConnectivityContextBuilder()
52                             .setConnection(new HashMap<>())
53                             .setConnectivityService(new HashMap<>())
54                             .build())
55                     .build();
56
57             Name nwTopoServiceName =
58                 new NameBuilder()
59                     .setValue("Network Topo Service")
60                     .setValueName("Network Topo Service Name")
61                     .build();
62
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<>())
68                             .setUuid(
69                                 new Uuid(
70                                     UUID.nameUUIDFromBytes("Network Topo Service".getBytes(Charset.forName("UTF-8")))
71                                         .toString()))
72                             .setName(Map.of(nwTopoServiceName.key(), nwTopoServiceName))
73                             .build())
74                         .setTopology(new HashMap<>())
75                         .build())
76                     .build();
77
78             ContextBuilder contextBuilder = new ContextBuilder()
79                     .setName(Map.of(contextName.key(), contextName))
80                     .setUuid(
81                         new Uuid(UUID.nameUUIDFromBytes(TAPI_CONTEXT.getBytes(Charset.forName("UTF-8"))).toString()))
82                     .setServiceInterfacePoint(new HashMap<>())
83                     .addAugmentation(connectivityContext)
84                     .addAugmentation(topologyContext);
85
86             // todo: add notification context
87             InstanceIdentifier<Context> contextIID = InstanceIdentifier.builder(Context.class).build();
88             // put in datastore
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);
94         }
95     }
96
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();
101         try {
102             Optional<Context> optionalContext = this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL,
103                     contextIID).get();
104             if (!optionalContext.isPresent()) {
105                 LOG.error("Tapi context is not present in datastore");
106                 return null;
107             }
108             return optionalContext.get();
109         } catch (InterruptedException | ExecutionException e) {
110             LOG.error("Couldnt read tapi context from datastore", e);
111             return null;
112         }
113     }
114
115     public void deleteTapiContext() {
116
117     }
118
119 }