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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.features.a1.adapter;
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.DeleteA1PolicyInputBuilder;
36 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.DeleteA1PolicyOutput;
37 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.DeleteA1PolicyOutputBuilder;
38 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyInput;
39 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyInputBuilder;
40 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyOutput;
41 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyOutputBuilder;
42 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyStatusInput;
43 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyStatusInputBuilder;
44 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyStatusOutput;
45 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyStatusOutputBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyTypeInput;
47 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyTypeInputBuilder;
48 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyTypeOutput;
49 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.GetA1PolicyTypeOutputBuilder;
50 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.PutA1PolicyInput;
51 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.PutA1PolicyInputBuilder;
52 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.PutA1PolicyOutput;
53 import org.opendaylight.yang.gen.v1.org.onap.a1.adapter.rev200122.PutA1PolicyOutputBuilder;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * Defines a base implementation for your provider. This class overrides the generated interface from the YANG model and
61 * implements the request model for the A1 interface. This class identifies the Near-RT RIC throught the IP passed over
62 * the payload and calls the corresponding Near-RT RIC over Rest API
67 @SuppressWarnings("squid:S1874") // "@Deprecated" code should not be used
68 public class A1AdapterProvider implements AutoCloseable, A1ADAPTERAPIService {
70 private static final Logger log = LoggerFactory.getLogger(A1AdapterProvider.class);
72 private static final String APPLICATION_NAME = "a1Adapter-api";
74 private final ExecutorService executor;
75 protected DataBroker dataBroker;
76 protected NotificationPublishService notificationService;
77 protected RpcProviderRegistry rpcRegistry;
78 protected BindingAwareBroker.RpcRegistration<A1ADAPTERAPIService> rpcRegistration;
79 private final A1AdapterClient a1AdapterClient;
81 public A1AdapterProvider(final DataBroker dataBroker, final NotificationPublishService notificationPublishService,
82 final RpcProviderRegistry rpcProviderRegistry, final A1AdapterClient a1AdapterClient) {
84 log.info("Creating provider for {}", APPLICATION_NAME);
85 executor = Executors.newFixedThreadPool(1);
86 this.dataBroker = dataBroker;
87 this.notificationService = notificationPublishService;
88 this.rpcRegistry = rpcProviderRegistry;
89 this.a1AdapterClient = a1AdapterClient;
93 public void initialize() {
94 log.info("Initializing provider for {}", APPLICATION_NAME);
95 rpcRegistration = rpcRegistry.addRpcImplementation(A1ADAPTERAPIService.class, this);
96 log.info("Initialization complete for {}", APPLICATION_NAME);
99 protected void initializeChild() {
100 // Override if you have custom initialization intelligence
104 public void close() throws Exception {
105 log.info("Closing provider for {}", APPLICATION_NAME);
107 rpcRegistration.close();
108 log.info("Successfully closed provider for {}", APPLICATION_NAME);
112 public ListenableFuture<RpcResult<DeleteA1PolicyOutput>> deleteA1Policy(DeleteA1PolicyInput input) {
113 log.info("Start of deleteA1Policy");
114 final String svcOperation = "deleteA1Policy";
115 Properties parms = new Properties();
116 DeleteA1PolicyOutputBuilder deleteResponse = new DeleteA1PolicyOutputBuilder();
117 // add input to parms
118 log.info("Adding INPUT data for " + svcOperation + " input: " + input);
119 DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder(input);
120 MdsalHelper.toProperties(parms, inputBuilder.build());
121 log.info("Printing SLI parameters to be passed");
122 // iterate properties file to get key-value pairs
123 for (String key : parms.stringPropertyNames()) {
124 String value = parms.getProperty(key);
125 log.info("The SLI parameter in " + key + " is: " + value);
127 // Call SLI sync method
129 if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
130 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
132 Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", deleteResponse, parms);
133 log.info("responseParms::"+responseParms.getProperty("body"));
134 deleteResponse.setHttpStatus(200);
135 } catch (Exception e) {
136 log.error("Caught exception executing service logic for " + svcOperation, e);
137 deleteResponse.setHttpStatus(500);
140 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
141 deleteResponse.setHttpStatus(503);
143 } catch (Exception e) {
144 log.error("Caught exception looking for service logic", e);
145 deleteResponse.setHttpStatus(500);
147 RpcResult<DeleteA1PolicyOutput> rpcResult =
148 RpcResultBuilder.<DeleteA1PolicyOutput>status(true).withResult(deleteResponse.build()).build();
149 log.info("End of getA1Policy");
150 return Futures.immediateFuture(rpcResult);
154 public ListenableFuture<RpcResult<GetA1PolicyOutput>> getA1Policy(GetA1PolicyInput input) {
155 log.info("Start of getA1Policy");
156 final String svcOperation = "getA1Policy";
157 Properties parms = new Properties();
158 GetA1PolicyOutputBuilder policyResponse = new GetA1PolicyOutputBuilder();
159 // add input to parms
160 log.info("Adding INPUT data for " + svcOperation + " input: " + input);
161 GetA1PolicyInputBuilder inputBuilder = new GetA1PolicyInputBuilder(input);
162 MdsalHelper.toProperties(parms, inputBuilder.build());
163 log.info("Printing SLI parameters to be passed");
164 // iterate properties file to get key-value pairs
165 for (String key : parms.stringPropertyNames()) {
166 String value = parms.getProperty(key);
167 log.info("The SLI parameter in " + key + " is: " + value);
169 // Call SLI sync method
171 if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
172 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
174 Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", policyResponse, parms);
175 log.info("responseParms::"+responseParms.getProperty("body"));
176 policyResponse.setBody(responseParms.getProperty("body"));
177 policyResponse.setHttpStatus(200);
178 } catch (Exception e) {
179 log.error("Caught exception executing service logic for " + svcOperation, e);
180 policyResponse.setHttpStatus(500);
183 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
184 policyResponse.setHttpStatus(503);
186 } catch (Exception e) {
187 log.error("Caught exception looking for service logic", e);
188 policyResponse.setHttpStatus(500);
190 RpcResult<GetA1PolicyOutput> rpcResult =
191 RpcResultBuilder.<GetA1PolicyOutput>status(true).withResult(policyResponse.build()).build();
192 log.info("End of getA1Policy");
193 return Futures.immediateFuture(rpcResult);
197 public ListenableFuture<RpcResult<GetA1PolicyStatusOutput>> getA1PolicyStatus(GetA1PolicyStatusInput input) {
198 log.info("Start of getA1PolicyStatus");
199 final String svcOperation = "getA1PolicyStatus";
200 Properties parms = new Properties();
201 GetA1PolicyStatusOutputBuilder policyStatusResponse = new GetA1PolicyStatusOutputBuilder();
202 // add input to parms
203 log.info("Adding INPUT data for " + svcOperation + " input: " + input);
204 GetA1PolicyStatusInputBuilder inputBuilder = new GetA1PolicyStatusInputBuilder(input);
205 MdsalHelper.toProperties(parms, inputBuilder.build());
206 log.info("Printing SLI parameters to be passed");
207 // iterate properties file to get key-value pairs
208 for (String key : parms.stringPropertyNames()) {
209 String value = parms.getProperty(key);
210 log.info("The SLI parameter in " + key + " is: " + value);
212 // Call SLI sync method
214 if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
215 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
217 Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", policyStatusResponse, parms);
218 log.info("responseParms::"+responseParms.getProperty("body"));
219 policyStatusResponse.setBody(responseParms.getProperty("body"));
220 policyStatusResponse.setHttpStatus(200);
221 } catch (Exception e) {
222 log.error("Caught exception executing service logic for " + svcOperation, e);
223 policyStatusResponse.setHttpStatus(500);
226 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
227 policyStatusResponse.setHttpStatus(503);
229 } catch (Exception e) {
230 log.error("Caught exception looking for service logic", e);
231 policyStatusResponse.setHttpStatus(500);
233 RpcResult<GetA1PolicyStatusOutput> rpcResult =
234 RpcResultBuilder.<GetA1PolicyStatusOutput>status(true).withResult(policyStatusResponse.build()).build();
235 log.info("End of getA1PolicyStatus");
236 return Futures.immediateFuture(rpcResult);
240 public ListenableFuture<RpcResult<GetA1PolicyTypeOutput>> getA1PolicyType(GetA1PolicyTypeInput input) {
241 log.info("Start of getA1PolicyType");
242 final String svcOperation = "getA1PolicyType";
243 Properties parms = new Properties();
244 GetA1PolicyTypeOutputBuilder policyTypeResponse = new GetA1PolicyTypeOutputBuilder();
245 // add input to parms
246 log.info("Adding INPUT data for " + svcOperation + " input: " + input);
247 GetA1PolicyTypeInputBuilder inputBuilder = new GetA1PolicyTypeInputBuilder(input);
248 MdsalHelper.toProperties(parms, inputBuilder.build());
249 log.info("Printing SLI parameters to be passed");
250 // iterate properties file to get key-value pairs
251 for (String key : parms.stringPropertyNames()) {
252 String value = parms.getProperty(key);
253 log.info("The SLI parameter in " + key + " is: " + value);
255 // Call SLI sync method
257 if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
258 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
260 Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", policyTypeResponse, parms);
261 log.info("responseParms::"+responseParms.getProperty("body"));
262 policyTypeResponse.setBody(responseParms.getProperty("body"));
263 policyTypeResponse.setHttpStatus(200);
264 } catch (Exception e) {
265 log.error("Caught exception executing service logic for " + svcOperation, e);
266 policyTypeResponse.setHttpStatus(500);
269 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
270 policyTypeResponse.setHttpStatus(503);
272 } catch (Exception e) {
273 log.error("Caught exception looking for service logic", e);
274 policyTypeResponse.setHttpStatus(500);
276 RpcResult<GetA1PolicyTypeOutput> rpcResult =
277 RpcResultBuilder.<GetA1PolicyTypeOutput>status(true).withResult(policyTypeResponse.build()).build();
278 log.info("End of getA1PolicyType");
279 return Futures.immediateFuture(rpcResult);
283 public ListenableFuture<RpcResult<PutA1PolicyOutput>> putA1Policy(PutA1PolicyInput input) {
284 log.info("Start of putA1Policy");
285 final String svcOperation = "putA1Policy";
286 Properties parms = new Properties();
287 PutA1PolicyOutputBuilder policyResponse = new PutA1PolicyOutputBuilder();
288 // add input to parms
289 log.info("Adding INPUT data for " + svcOperation + " input: " + input);
290 PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder(input);
291 MdsalHelper.toProperties(parms, inputBuilder.build());
292 log.info("Printing SLI parameters to be passed");
293 // iterate properties file to get key-value pairs
294 for (String key : parms.stringPropertyNames()) {
295 String value = parms.getProperty(key);
296 log.info("The SLI parameter in " + key + " is: " + value);
298 // Call SLI sync method
300 if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
301 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
303 Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", policyResponse, parms);
304 log.info("responseParms::"+responseParms.getProperty("body"));
305 policyResponse.setBody(responseParms.getProperty("body"));
306 policyResponse.setHttpStatus(200);
307 } catch (Exception e) {
308 log.error("Caught exception executing service logic for " + svcOperation, e);
309 policyResponse.setHttpStatus(500);
312 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
313 policyResponse.setHttpStatus(503);
315 } catch (Exception e) {
316 log.error("Caught exception looking for service logic", e);
317 policyResponse.setHttpStatus(500);
319 RpcResult<PutA1PolicyOutput> rpcResult =
320 RpcResultBuilder.<PutA1PolicyOutput>status(true).withResult(policyResponse.build()).build();
321 log.info("End of putA1Policy");
322 return Futures.immediateFuture(rpcResult);