7e47b980e0faa62d2bbffcbf8de88b8f7d57da23
[nonrtric.git] / sdnc-a1-controller / northbound / nonrt-ric-api / provider / src / main / java / org / onap / sdnc / northbound / provider / NonrtRicApiProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.sdnc.northbound.provider;
22
23 import com.google.common.util.concurrent.CheckedFuture;
24 import com.google.common.util.concurrent.Futures;
25 import com.google.common.util.concurrent.ListenableFuture;
26 import java.util.List;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import org.onap.sdnc.northbound.restadapter.NearRicUrlProvider;
31 import org.onap.sdnc.northbound.restadapter.RestAdapter;
32 import org.onap.sdnc.northbound.restadapter.RestAdapterImpl;
33 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
34 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
35 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
38 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
39 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.A1ADAPTERAPIService;
40 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeletePolicyInput;
41 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeletePolicyOutput;
42 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeletePolicyOutputBuilder;
43 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesInput;
44 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesOutput;
45 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesOutputBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusInput;
47 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusOutput;
48 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusOutputBuilder;
49 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesInput;
50 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutput;
51 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutputBuilder;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeInput;
53 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutput;
54 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutputBuilder;
55 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyInput;
56 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutput;
57 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutputBuilder;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import org.springframework.http.ResponseEntity;
63
64 /**
65  * Defines a base implementation for your provider. This class overrides the
66  * generated interface from the YANG model and implements the request model for
67  * the A1 interface. This class identifies the Near-RIC throught the IP passed
68  * over the payload and calls the corresponding Near-RIC over Rest API
69  *
70  * <pre>
71  *
72  * @author lathishbabu.ganesan@est.tech
73  *
74  */
75
76 @SuppressWarnings("squid:S1874") // "@Deprecated" code should not be used
77 public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService {
78
79   protected static final String APP_NAME = "nonrt-ric-api";
80   protected static final String NO_SERVICE_LOGIC_ACTIVE = "No service logic active for ";
81   private static final String NON_NULL_PARAM = "non-null";
82   private static final String NULL_PARAM = "null";
83
84   private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class);
85   private final ExecutorService executor;
86
87   protected DataBroker dataBroker;
88   protected NotificationPublishService notificationService;
89   protected RpcProviderRegistry rpcRegistry;
90   protected BindingAwareBroker.RpcRegistration<?> rpcRegistration;
91   private RestAdapter restAdapter;
92   private NearRicUrlProvider nearRicUrlProvider;
93
94   public NonrtRicApiProvider(DataBroker dataBroker, NotificationPublishService notificationPublishService,
95       RpcProviderRegistry rpcProviderRegistry) {
96     log.info("Creating provider for {}", APP_NAME);
97     executor = Executors.newFixedThreadPool(1);
98     setDataBroker(dataBroker);
99     setNotificationService(notificationPublishService);
100     setRpcRegistry(rpcProviderRegistry);
101     initialize();
102
103   }
104
105   public void initialize() {
106     log.info("Initializing provider for {}", APP_NAME);
107     createContainers();
108     restAdapter = new RestAdapterImpl();
109     nearRicUrlProvider = new NearRicUrlProvider();
110     log.info("Initialization complete for {}", APP_NAME);
111   }
112
113   protected void initializeChild() {
114     // Override if you have custom initialization intelligence
115   }
116
117   @Override
118   public void close() throws Exception {
119     log.info("Closing provider for {}", APP_NAME);
120     executor.shutdown();
121     rpcRegistration.close();
122     log.info("Successfully closed provider for {}", APP_NAME);
123   }
124
125   public void setDataBroker(DataBroker dataBroker) {
126     this.dataBroker = dataBroker;
127     if (log.isDebugEnabled()) {
128       log.debug("DataBroker set to {}", dataBroker == null ? NULL_PARAM : NON_NULL_PARAM);
129     }
130   }
131
132   public void setNotificationService(NotificationPublishService notificationService) {
133     this.notificationService = notificationService;
134     if (log.isDebugEnabled()) {
135       log.debug("Notification Service set to {}", notificationService == null ? NULL_PARAM : NON_NULL_PARAM);
136     }
137   }
138
139   public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
140     this.rpcRegistry = rpcRegistry;
141     if (log.isDebugEnabled()) {
142       log.debug("RpcRegistry set to {}", rpcRegistry == null ? NULL_PARAM : NON_NULL_PARAM);
143     }
144   }
145
146   private void createContainers() {
147
148     final WriteTransaction t = dataBroker.newReadWriteTransaction();
149
150     try {
151       CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
152       checkedFuture.get();
153       log.info("Create containers succeeded!");
154
155     } catch (InterruptedException | ExecutionException e) {
156       log.error("Create containers failed: ", e);
157       Thread.currentThread().interrupt();
158     }
159   }
160
161   @Override
162   public ListenableFuture<RpcResult<GetPolicyTypeIdentitiesOutput>> getPolicyTypeIdentities(
163       GetPolicyTypeIdentitiesInput input) {
164     log.info("Start of getPolicyTypeIdentities");
165     GetPolicyTypeIdentitiesOutputBuilder responseBuilder = new GetPolicyTypeIdentitiesOutputBuilder();
166     String uri = nearRicUrlProvider.policyTypesUrl(String.valueOf(input.getNearRtRicUrl()));
167     ResponseEntity<List<String>> response = restAdapter.get(uri, List.class);
168     if (response.hasBody()) {
169       log.info("Response getPolicyTypeIdentities : {} ", response.getBody());
170       responseBuilder.setPolicyTypeIdList(response.getBody());
171     }
172     log.info("End of getPolicyTypeIdentities");
173     RpcResult<GetPolicyTypeIdentitiesOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeIdentitiesOutput>status(true)
174         .withResult(responseBuilder.build()).build();
175     return Futures.immediateFuture(rpcResult);
176   }
177
178   @Override
179   public ListenableFuture<RpcResult<GetPolicyIdentitiesOutput>> getPolicyIdentities(GetPolicyIdentitiesInput input) {
180     log.info("Start of getPolicyIdentities");
181     GetPolicyIdentitiesOutputBuilder responseBuilder = new GetPolicyIdentitiesOutputBuilder();
182     String uri = nearRicUrlProvider.policiesUrl(String.valueOf(input.getNearRtRicUrl()));
183     ResponseEntity<List<String>> response = restAdapter.get(uri, List.class);
184     if (response.hasBody()) {
185       log.info("Response getPolicyIdentities : {} ", response.getBody());
186       responseBuilder.setPolicyIdList(response.getBody());
187     }
188     log.info("End of getPolicyIdentities");
189     RpcResult<GetPolicyIdentitiesOutput> rpcResult = RpcResultBuilder.<GetPolicyIdentitiesOutput>status(true)
190         .withResult(responseBuilder.build()).build();
191     return Futures.immediateFuture(rpcResult);
192   }
193
194   @Override
195   public ListenableFuture<RpcResult<GetPolicyTypeOutput>> getPolicyType(GetPolicyTypeInput input) {
196     log.info("Start of getPolicyType; Policy Type Id : {} ", input.getPolicyTypeId());
197     GetPolicyTypeOutputBuilder responseBuilder = new GetPolicyTypeOutputBuilder();
198     String uri = nearRicUrlProvider.getPolicyTypeUrl(String.valueOf(input.getNearRtRicUrl()),
199         String.valueOf(input.getPolicyTypeId()));
200     ResponseEntity<String> response = restAdapter.get(uri, String.class);
201     if (response.hasBody()) {
202       log.info("Response getPolicyType : {} ", response.getBody());
203       responseBuilder.setPolicyType(response.getBody());
204     }
205     log.info("End of getPolicyType");
206     RpcResult<GetPolicyTypeOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeOutput>status(true)
207         .withResult(responseBuilder.build()).build();
208     return Futures.immediateFuture(rpcResult);
209   }
210
211   @Override
212   public ListenableFuture<RpcResult<PutPolicyOutput>> putPolicy(PutPolicyInput input) {
213     log.info("Start of putPolicy");
214     PutPolicyOutputBuilder responseBuilder = new PutPolicyOutputBuilder();
215     String uri = nearRicUrlProvider.putPolicyUrl(String.valueOf(input.getNearRtRicUrl()),
216         String.valueOf(input.getPolicyId()), String.valueOf(input.getPolicyTypeId()));
217     log.info("PUT Request input.getPolicy() : {} ", input.getPolicy());
218     ResponseEntity<String> response = restAdapter.put(uri, input.getPolicy(), String.class);
219     if (response.hasBody()) {
220       log.info("Response putPolicy : {} ", response.getBody());
221       responseBuilder.setReturnedPolicy(response.getBody());
222     }
223     log.info("End of putPolicy");
224     RpcResult<PutPolicyOutput> rpcResult = RpcResultBuilder.<PutPolicyOutput>status(true)
225         .withResult(responseBuilder.build()).build();
226     return Futures.immediateFuture(rpcResult);
227   }
228
229   @Override
230   public ListenableFuture<RpcResult<DeletePolicyOutput>> deletePolicy(DeletePolicyInput input) {
231     log.info("Start of deletePolicy");
232     DeletePolicyOutputBuilder responseBuilder = new DeletePolicyOutputBuilder();
233     String uri = nearRicUrlProvider.deletePolicyUrl(String.valueOf(input.getNearRtRicUrl()),
234         String.valueOf(input.getPolicyId()));
235     restAdapter.delete(uri);
236     log.info("End of deletePolicy");
237     RpcResult<DeletePolicyOutput> rpcResult = RpcResultBuilder.<DeletePolicyOutput>status(true)
238         .withResult(responseBuilder.build()).build();
239     return Futures.immediateFuture(rpcResult);
240   }
241
242   @Override
243   public ListenableFuture<RpcResult<GetPolicyStatusOutput>> getPolicyStatus(GetPolicyStatusInput input) {
244     log.debug("Policy Id : {} ", input.getPolicyId());
245     GetPolicyStatusOutputBuilder responseBuilder = new GetPolicyStatusOutputBuilder();
246     String uri = nearRicUrlProvider.getPolicyStatusUrl(input.getNearRtRicUrl(), input.getPolicyId());
247     ResponseEntity<String> response = restAdapter.get(uri, String.class);
248     if (response.hasBody()) {
249       log.info("Response getPolicyStatus : {} ", response.getBody());
250       responseBuilder.setPolicyStatus(response.getBody());
251     }
252     RpcResult<GetPolicyStatusOutput> rpcResult = RpcResultBuilder.<GetPolicyStatusOutput>status(true)
253         .withResult(responseBuilder.build()).build();
254     return Futures.immediateFuture(rpcResult);
255   }
256 }