42cd5000f927a1ef03cf5609444f5802bb95d312
[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.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  * <pre>
65  *
66  */
67 @SuppressWarnings("squid:S1874") // "@Deprecated" code should not be used
68 public class A1AdapterProvider implements AutoCloseable, A1ADAPTERAPIService {
69
70     private static final Logger log = LoggerFactory.getLogger(A1AdapterProvider.class);
71
72     private static final String APPLICATION_NAME = "a1Adapter-api";
73
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;
80
81     public A1AdapterProvider(final DataBroker dataBroker, final NotificationPublishService notificationPublishService,
82             final RpcProviderRegistry rpcProviderRegistry, final A1AdapterClient a1AdapterClient) {
83
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;
90         initialize();
91     }
92
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);
97     }
98
99     protected void initializeChild() {
100         // Override if you have custom initialization intelligence
101     }
102
103     @Override
104     public void close() throws Exception {
105         log.info("Closing provider for {}", APPLICATION_NAME);
106         executor.shutdown();
107         rpcRegistration.close();
108         log.info("Successfully closed provider for {}", APPLICATION_NAME);
109     }
110
111     @Override
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);
126         }
127         // Call SLI sync method
128         try {
129             if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
130                 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
131                 try {
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);
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 getA1Policy");
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("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);
181                 }
182             } else {
183                 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
184                 policyResponse.setHttpStatus(503);
185             }
186         } catch (Exception e) {
187             log.error("Caught exception looking for service logic", e);
188             policyResponse.setHttpStatus(500);
189         }
190         RpcResult<GetA1PolicyOutput> rpcResult =
191                 RpcResultBuilder.<GetA1PolicyOutput>status(true).withResult(policyResponse.build()).build();
192         log.info("End of getA1Policy");
193         return Futures.immediateFuture(rpcResult);
194     }
195
196     @Override
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);
211         }
212         // Call SLI sync method
213         try {
214             if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
215                 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
216                 try {
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);
224                 }
225             } else {
226                 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
227                 policyStatusResponse.setHttpStatus(503);
228             }
229         } catch (Exception e) {
230             log.error("Caught exception looking for service logic", e);
231             policyStatusResponse.setHttpStatus(500);
232         }
233         RpcResult<GetA1PolicyStatusOutput> rpcResult =
234                 RpcResultBuilder.<GetA1PolicyStatusOutput>status(true).withResult(policyStatusResponse.build()).build();
235         log.info("End of getA1PolicyStatus");
236         return Futures.immediateFuture(rpcResult);
237     }
238
239     @Override
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);
254         }
255         // Call SLI sync method
256         try {
257             if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
258                 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
259                 try {
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);
267                 }
268             } else {
269                 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
270                 policyTypeResponse.setHttpStatus(503);
271             }
272         } catch (Exception e) {
273             log.error("Caught exception looking for service logic", e);
274             policyTypeResponse.setHttpStatus(500);
275         }
276         RpcResult<GetA1PolicyTypeOutput> rpcResult =
277                 RpcResultBuilder.<GetA1PolicyTypeOutput>status(true).withResult(policyTypeResponse.build()).build();
278         log.info("End of getA1PolicyType");
279         return Futures.immediateFuture(rpcResult);
280     }
281
282     @Override
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);
297         }
298         // Call SLI sync method
299         try {
300             if (a1AdapterClient.hasGraph("A1-ADAPTER-API", svcOperation, null, "sync")) {
301                 log.info("A1AdapterClient has a Directed Graph for '" + svcOperation + "'");
302                 try {
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);
310                 }
311             } else {
312                 log.error("No service logic active for A1Adapter: '" + svcOperation + "'");
313                 policyResponse.setHttpStatus(503);
314             }
315         } catch (Exception e) {
316             log.error("Caught exception looking for service logic", e);
317             policyResponse.setHttpStatus(500);
318         }
319         RpcResult<PutA1PolicyOutput> rpcResult =
320                 RpcResultBuilder.<PutA1PolicyOutput>status(true).withResult(policyResponse.build()).build();
321         log.info("End of putA1Policy");
322         return Futures.immediateFuture(rpcResult);
323     }
324
325 }