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
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.sdnc.northbound.provider;
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;
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
72 * @author lathishbabu.ganesan@est.tech
76 @SuppressWarnings("squid:S1874") // "@Deprecated" code should not be used
77 public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService {
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";
84 private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class);
85 private final ExecutorService executor;
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;
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);
105 public void initialize() {
106 log.info("Initializing provider for {}", APP_NAME);
108 restAdapter = new RestAdapterImpl();
109 nearRicUrlProvider = new NearRicUrlProvider();
110 log.info("Initialization complete for {}", APP_NAME);
113 protected void initializeChild() {
114 // Override if you have custom initialization intelligence
118 public void close() throws Exception {
119 log.info("Closing provider for {}", APP_NAME);
121 rpcRegistration.close();
122 log.info("Successfully closed provider for {}", APP_NAME);
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);
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);
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);
146 private void createContainers() {
148 final WriteTransaction t = dataBroker.newReadWriteTransaction();
151 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
153 log.info("Create containers succeeded!");
155 } catch (InterruptedException | ExecutionException e) {
156 log.error("Create containers failed: ", e);
157 Thread.currentThread().interrupt();
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());
172 log.info("End of getPolicyTypeIdentities");
173 RpcResult<GetPolicyTypeIdentitiesOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeIdentitiesOutput>status(true)
174 .withResult(responseBuilder.build()).build();
175 return Futures.immediateFuture(rpcResult);
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());
188 log.info("End of getPolicyIdentities");
189 RpcResult<GetPolicyIdentitiesOutput> rpcResult = RpcResultBuilder.<GetPolicyIdentitiesOutput>status(true)
190 .withResult(responseBuilder.build()).build();
191 return Futures.immediateFuture(rpcResult);
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());
205 log.info("End of getPolicyType");
206 RpcResult<GetPolicyTypeOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeOutput>status(true)
207 .withResult(responseBuilder.build()).build();
208 return Futures.immediateFuture(rpcResult);
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());
223 log.info("End of putPolicy");
224 RpcResult<PutPolicyOutput> rpcResult = RpcResultBuilder.<PutPolicyOutput>status(true)
225 .withResult(responseBuilder.build()).build();
226 return Futures.immediateFuture(rpcResult);
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);
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());
252 RpcResult<GetPolicyStatusOutput> rpcResult = RpcResultBuilder.<GetPolicyStatusOutput>status(true)
253 .withResult(responseBuilder.build()).build();
254 return Futures.immediateFuture(rpcResult);