a1b9ef927c5bd45941ccea9414c49cf9b1d643d0
[ccsdk/oran.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.features.a1.adapter;
22
23 import com.google.common.util.concurrent.Futures;
24 import com.google.common.util.concurrent.ListenableFuture;
25 import java.util.Properties;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import org.onap.ccsdk.sli.core.sli.provider.MdsalHelper;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
31 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
32 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
33 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.A1ADAPTERAPIService;
34 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.DeleteA1PolicyInput;
35 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.DeleteA1PolicyOutput;
36 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyInput;
37 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyOutput;
38 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyStatusInput;
39 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyStatusOutput;
40 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyTypeInput;
41 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyTypeInputBuilder;
42 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyTypeOutput;
43 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyTypeOutputBuilder;
44 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.PutA1PolicyInput;
45 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.PutA1PolicyOutput;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * Defines a base implementation for your provider. This class overrides the generated interface
53  * from the YANG model and implements the request model for the A1 interface. This class identifies
54  * the Near-RT RIC throught the IP passed over the payload and calls the corresponding Near-RT RIC
55  * over Rest API
56  *
57  * <pre>
58  *
59  */
60 @SuppressWarnings("squid:S1874") // "@Deprecated" code should not be used
61 public class A1AdapterProvider implements AutoCloseable, A1ADAPTERAPIService {
62
63   private static final Logger log = LoggerFactory.getLogger(A1AdapterProvider.class);
64
65   private static final String APPLICATION_NAME = "a1Adapter-api";
66
67   private final ExecutorService executor;
68   protected DataBroker dataBroker;
69   protected NotificationPublishService notificationService;
70   protected RpcProviderRegistry rpcRegistry;
71   protected BindingAwareBroker.RpcRegistration<A1ADAPTERAPIService> rpcRegistration;
72   private final A1AdapterClient a1AdapterClient;
73
74   public A1AdapterProvider(final DataBroker dataBroker,
75       final NotificationPublishService notificationPublishService,
76       final RpcProviderRegistry rpcProviderRegistry, final A1AdapterClient a1AdapterClient) {
77
78     log.info("Creating provider for {}", APPLICATION_NAME);
79     executor = Executors.newFixedThreadPool(1);
80     this.dataBroker = dataBroker;
81     this.notificationService = notificationPublishService;
82     this.rpcRegistry = rpcProviderRegistry;
83     this.a1AdapterClient = a1AdapterClient;
84     initialize();
85   }
86
87   public void initialize() {
88     log.info("Initializing provider for {}", APPLICATION_NAME);
89     rpcRegistration = rpcRegistry.addRpcImplementation(A1ADAPTERAPIService.class, this);
90     log.info("Initialization complete for {}", APPLICATION_NAME);
91   }
92
93   protected void initializeChild() {
94     // Override if you have custom initialization intelligence
95   }
96
97   @Override
98   public void close() throws Exception {
99     log.info("Closing provider for {}", APPLICATION_NAME);
100     executor.shutdown();
101     rpcRegistration.close();
102     log.info("Successfully closed provider for {}", APPLICATION_NAME);
103   }
104
105   @Override
106   public ListenableFuture<RpcResult<DeleteA1PolicyOutput>> deleteA1Policy(
107       DeleteA1PolicyInput input) {
108     // TODO Auto-generated method stub
109     return null;
110   }
111
112   @Override
113   public ListenableFuture<RpcResult<GetA1PolicyOutput>> getA1Policy(GetA1PolicyInput input) {
114     log.info("Start of getA1Policy");
115     return null;
116   }
117
118   @Override
119   public ListenableFuture<RpcResult<GetA1PolicyStatusOutput>> getA1PolicyStatus(
120       GetA1PolicyStatusInput input) {
121     // TODO Auto-generated method stub
122     return null;
123   }
124
125   @Override
126   public ListenableFuture<RpcResult<GetA1PolicyTypeOutput>> getA1PolicyType(
127       GetA1PolicyTypeInput input) {
128     log.info("Start of getA1PolicyType");
129     final String svcOperation = "getA1PolicyType";
130     Properties parms = new Properties();
131     GetA1PolicyTypeOutputBuilder policyTypeResponse = new GetA1PolicyTypeOutputBuilder();
132     // add input to parms
133     log.info("Adding INPUT data for " + svcOperation + " input: " + input);
134     GetA1PolicyTypeInputBuilder inputBuilder = new GetA1PolicyTypeInputBuilder(input);
135     MdsalHelper.toProperties(parms, inputBuilder.build());
136     log.info("Printing SLI parameters to be passed");
137     // iterate properties file to get key-value pairs
138     for (String key : parms.stringPropertyNames()) {
139       String value = parms.getProperty(key);
140       log.info("The SLI parameter in " + key + " is: " + value);
141     }
142     // Call SLI sync method
143     try {
144       if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
145         log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
146         try {
147           a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", policyTypeResponse,
148               parms);
149           policyTypeResponse.setHttpStatus(200);
150         } catch (Exception e) {
151           log.error("Caught exception executing service logic for " + svcOperation, e);
152           policyTypeResponse.setHttpStatus(500);
153         }
154       } else {
155         log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
156         policyTypeResponse.setHttpStatus(503);
157       }
158     } catch (Exception e) {
159       log.error("Caught exception looking for service logic", e);
160       policyTypeResponse.setHttpStatus(500);
161     }
162     RpcResult<GetA1PolicyTypeOutput> rpcResult = RpcResultBuilder
163         .<GetA1PolicyTypeOutput>status(true).withResult(policyTypeResponse.build()).build();
164     log.info("End of getA1PolicyType");
165     return Futures.immediateFuture(rpcResult);
166   }
167
168   @Override
169   public ListenableFuture<RpcResult<PutA1PolicyOutput>> putA1Policy(PutA1PolicyInput input) {
170     // TODO Auto-generated method stub
171     return null;
172   }
173
174 }