Merge "Set SureFire plugin version to make tests work"
[ccsdk/oran.git] / a1-adapter / a1-adapter-api / provider / src / main / java / org / onap / ccsdk / features / a1 / adapter / A1AdapterProvider.java
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.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;
58
59 /**
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
63  *
64  */
65 @SuppressWarnings("squid:S1874") // "@Deprecated" code should not be used
66 public class A1AdapterProvider implements AutoCloseable, A1ADAPTERAPIService {
67
68     private static final Logger log = LoggerFactory.getLogger(A1AdapterProvider.class);
69
70     private static final String APPLICATION_NAME = "a1Adapter-api";
71
72     private final ExecutorService executor;
73     protected DataBroker dataBroker;
74     protected NotificationPublishService notificationService;
75     protected RpcProviderRegistry rpcRegistry;
76     protected BindingAwareBroker.RpcRegistration<A1ADAPTERAPIService> rpcRegistration;
77     private final A1AdapterClient a1AdapterClient;
78
79     public A1AdapterProvider(final DataBroker dataBroker, final NotificationPublishService notificationPublishService,
80             final RpcProviderRegistry rpcProviderRegistry, final A1AdapterClient a1AdapterClient) {
81
82         log.info("Creating provider for {}", APPLICATION_NAME);
83         executor = Executors.newFixedThreadPool(1);
84         this.dataBroker = dataBroker;
85         this.notificationService = notificationPublishService;
86         this.rpcRegistry = rpcProviderRegistry;
87         this.a1AdapterClient = a1AdapterClient;
88         initialize();
89     }
90
91     public void initialize() {
92         log.info("Initializing provider for {}", APPLICATION_NAME);
93         rpcRegistration = rpcRegistry.addRpcImplementation(A1ADAPTERAPIService.class, this);
94         log.info("Initialization complete for {}", APPLICATION_NAME);
95     }
96
97     protected void initializeChild() {
98         // Override if you have custom initialization intelligence
99     }
100
101     @Override
102     public void close() throws Exception {
103         log.info("Closing provider for {}", APPLICATION_NAME);
104         executor.shutdown();
105         rpcRegistration.close();
106         log.info("Successfully closed provider for {}", APPLICATION_NAME);
107     }
108
109     @Override
110     public ListenableFuture<RpcResult<DeleteA1PolicyOutput>> deleteA1Policy(DeleteA1PolicyInput input) {
111         log.info("Start of deleteA1Policy");
112         final String svcOperation = "deleteA1Policy";
113         Properties parms = new Properties();
114         DeleteA1PolicyOutputBuilder deleteResponse = new DeleteA1PolicyOutputBuilder();
115         // add input to parms
116         log.info("Adding INPUT data for " + svcOperation + " input: " + input);
117         DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder(input);
118         MdsalHelper.toProperties(parms, inputBuilder.build());
119         log.info("Printing SLI parameters to be passed");
120         // iterate properties file to get key-value pairs
121         for (String key : parms.stringPropertyNames()) {
122             String value = parms.getProperty(key);
123             log.info("The SLI parameter in " + key + " is: " + value);
124         }
125         // Call SLI sync method
126         try {
127             if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
128                 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
129                 try {
130                     Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", deleteResponse, parms);
131                     log.info("responseBody::"+responseParms.getProperty("responseBody"));
132                     log.info("responseCode::"+responseParms.getProperty("response-code"));
133                     log.info("responseMessage::"+responseParms.getProperty("response-message"));
134                     deleteResponse.setHttpStatus(Integer.valueOf(responseParms.getProperty("response-code")));
135                 } catch (Exception e) {
136                     log.error("Caught exception executing service logic for " + svcOperation, e);
137                     deleteResponse.setHttpStatus(500);
138                 }
139             } else {
140                 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
141                 deleteResponse.setHttpStatus(503);
142             }
143         } catch (Exception e) {
144             log.error("Caught exception looking for service logic", e);
145             deleteResponse.setHttpStatus(500);
146         }
147         RpcResult<DeleteA1PolicyOutput> rpcResult =
148                 RpcResultBuilder.<DeleteA1PolicyOutput>status(true).withResult(deleteResponse.build()).build();
149         log.info("End of deleteA1Policy");
150         return Futures.immediateFuture(rpcResult);
151     }
152
153     @Override
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);
168         }
169         // Call SLI sync method
170         try {
171             if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
172                 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
173                 try {
174                     Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", policyResponse, parms);
175                     log.info("responseBody::"+responseParms.getProperty("responseBody"));
176                     policyResponse.setBody(responseParms.getProperty("responseBody"));
177                     log.info("responseCode::"+responseParms.getProperty("response-code"));
178                     log.info("responseMessage::"+responseParms.getProperty("response-message"));
179                     policyResponse.setHttpStatus(Integer.valueOf(responseParms.getProperty("response-code")));
180                 } catch (Exception e) {
181                     log.error("Caught exception executing service logic for " + svcOperation, e);
182                     policyResponse.setHttpStatus(500);
183                 }
184             } else {
185                 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
186                 policyResponse.setHttpStatus(503);
187             }
188         } catch (Exception e) {
189             log.error("Caught exception looking for service logic", e);
190             policyResponse.setHttpStatus(500);
191         }
192         RpcResult<GetA1PolicyOutput> rpcResult =
193                 RpcResultBuilder.<GetA1PolicyOutput>status(true).withResult(policyResponse.build()).build();
194         log.info("End of getA1Policy");
195         return Futures.immediateFuture(rpcResult);
196     }
197
198     @Override
199     public ListenableFuture<RpcResult<GetA1PolicyStatusOutput>> getA1PolicyStatus(GetA1PolicyStatusInput input) {
200         log.info("Start of getA1PolicyStatus");
201         final String svcOperation = "getA1PolicyStatus";
202         Properties parms = new Properties();
203         GetA1PolicyStatusOutputBuilder policyStatusResponse = new GetA1PolicyStatusOutputBuilder();
204         // add input to parms
205         log.info("Adding INPUT data for " + svcOperation + " input: " + input);
206         GetA1PolicyStatusInputBuilder inputBuilder = new GetA1PolicyStatusInputBuilder(input);
207         MdsalHelper.toProperties(parms, inputBuilder.build());
208         log.info("Printing SLI parameters to be passed");
209         // iterate properties file to get key-value pairs
210         for (String key : parms.stringPropertyNames()) {
211             String value = parms.getProperty(key);
212             log.info("The SLI parameter in " + key + " is: " + value);
213         }
214         // Call SLI sync method
215         try {
216             if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
217                 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
218                 try {
219                     Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", policyStatusResponse, parms);
220                     log.info("responseBody::"+responseParms.getProperty("responseBody"));
221                     policyStatusResponse.setBody(responseParms.getProperty("responseBody"));
222                     log.info("responseCode::"+responseParms.getProperty("response-code"));
223                     log.info("responseMessage::"+responseParms.getProperty("response-message"));
224                     policyStatusResponse.setHttpStatus(Integer.valueOf(responseParms.getProperty("response-code")));
225                 } catch (Exception e) {
226                     log.error("Caught exception executing service logic for " + svcOperation, e);
227                     policyStatusResponse.setHttpStatus(500);
228                 }
229             } else {
230                 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
231                 policyStatusResponse.setHttpStatus(503);
232             }
233         } catch (Exception e) {
234             log.error("Caught exception looking for service logic", e);
235             policyStatusResponse.setHttpStatus(500);
236         }
237         RpcResult<GetA1PolicyStatusOutput> rpcResult =
238                 RpcResultBuilder.<GetA1PolicyStatusOutput>status(true).withResult(policyStatusResponse.build()).build();
239         log.info("End of getA1PolicyStatus");
240         return Futures.immediateFuture(rpcResult);
241     }
242
243     @Override
244     public ListenableFuture<RpcResult<GetA1PolicyTypeOutput>> getA1PolicyType(GetA1PolicyTypeInput input) {
245         log.info("Start of getA1PolicyType");
246         final String svcOperation = "getA1PolicyType";
247         Properties parms = new Properties();
248         GetA1PolicyTypeOutputBuilder policyTypeResponse = new GetA1PolicyTypeOutputBuilder();
249         // add input to parms
250         log.info("Adding INPUT data for " + svcOperation + " input: " + input);
251         GetA1PolicyTypeInputBuilder inputBuilder = new GetA1PolicyTypeInputBuilder(input);
252         MdsalHelper.toProperties(parms, inputBuilder.build());
253         log.info("Printing SLI parameters to be passed");
254         // iterate properties file to get key-value pairs
255         for (String key : parms.stringPropertyNames()) {
256             String value = parms.getProperty(key);
257             log.info("The SLI parameter in " + key + " is: " + value);
258         }
259         // Call SLI sync method
260         try {
261             if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
262                 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
263                 try {
264                     Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", policyTypeResponse, parms);
265                     log.info("responseBody::"+responseParms.getProperty("responseBody"));
266                     policyTypeResponse.setBody(responseParms.getProperty("responseBody"));
267                     log.info("responseCode::"+responseParms.getProperty("response-code"));
268                     log.info("responseMessage::"+responseParms.getProperty("response-message"));
269                     policyTypeResponse.setHttpStatus(Integer.valueOf(responseParms.getProperty("response-code")));
270                 } catch (Exception e) {
271                     log.error("Caught exception executing service logic for " + svcOperation, e);
272                     policyTypeResponse.setHttpStatus(500);
273                 }
274             } else {
275                 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
276                 policyTypeResponse.setHttpStatus(503);
277             }
278         } catch (Exception e) {
279             log.error("Caught exception looking for service logic", e);
280             policyTypeResponse.setHttpStatus(500);
281         }
282         RpcResult<GetA1PolicyTypeOutput> rpcResult =
283                 RpcResultBuilder.<GetA1PolicyTypeOutput>status(true).withResult(policyTypeResponse.build()).build();
284         log.info("End of getA1PolicyType");
285         return Futures.immediateFuture(rpcResult);
286     }
287
288     @Override
289     public ListenableFuture<RpcResult<PutA1PolicyOutput>> putA1Policy(PutA1PolicyInput input) {
290         log.info("Start of putA1Policy");
291         final String svcOperation = "putA1Policy";
292         Properties parms = new Properties();
293         PutA1PolicyOutputBuilder policyResponse = new PutA1PolicyOutputBuilder();
294         // add input to parms
295         log.info("Adding INPUT data for " + svcOperation + " input: " + input);
296         PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder(input);
297         MdsalHelper.toProperties(parms, inputBuilder.build());
298         log.info("Printing SLI parameters to be passed");
299         // iterate properties file to get key-value pairs
300         for (String key : parms.stringPropertyNames()) {
301             String value = parms.getProperty(key);
302             log.info("The SLI parameter in " + key + " is: " + value);
303         }
304         // Call SLI sync method
305         try {
306             if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
307                 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
308                 try {
309                     Properties responseParms = a1AdapterClient.execute("A1-ADAPTER-API", svcOperation, null, "sync", policyResponse, parms);
310                     log.info("responseBody::"+responseParms.getProperty("responseBody"));
311                     policyResponse.setBody(responseParms.getProperty("responseBody"));
312                     log.info("responseCode::"+responseParms.getProperty("response-code"));
313                     log.info("responseMessage::"+responseParms.getProperty("response-message"));
314                     policyResponse.setHttpStatus(Integer.valueOf(responseParms.getProperty("response-code")));
315                 } catch (Exception e) {
316                     log.error("Caught exception executing service logic for " + svcOperation, e);
317                     policyResponse.setHttpStatus(500);
318                 }
319             } else {
320                 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
321                 policyResponse.setHttpStatus(503);
322             }
323         } catch (Exception e) {
324             log.error("Caught exception looking for service logic", e);
325             policyResponse.setHttpStatus(500);
326         }
327         RpcResult<PutA1PolicyOutput> rpcResult =
328                 RpcResultBuilder.<PutA1PolicyOutput>status(true).withResult(policyResponse.build()).build();
329         log.info("End of putA1Policy");
330         return Futures.immediateFuture(rpcResult);
331     }
332
333 }