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.GetPolicyTypeIdentitiesInput;
47 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutput;
48 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutputBuilder;
49 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeInput;
50 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutput;
51 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutputBuilder;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyInput;
53 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutput;
54 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutputBuilder;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.springframework.http.ResponseEntity;
62 * Defines a base implementation for your provider. This class overrides the generated interface
63 * from the YANG model and implements the request model for the A1 interface. This class identifies
64 * the Near-RIC throught the IP passed over the payload and calls the corresponding Near-RIC over
69 * @author lathishbabu.ganesan@est.tech
73 @SuppressWarnings("squid:S1874") // "@Deprecated" code should not be used
74 public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService {
76 protected static final String APP_NAME = "nonrt-ric-api";
77 protected static final String NO_SERVICE_LOGIC_ACTIVE = "No service logic active for ";
78 private static final String NON_NULL_PARAM = "non-null";
79 private static final String NULL_PARAM = "null";
81 private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class);
82 private final ExecutorService executor;
84 protected DataBroker dataBroker;
85 protected NotificationPublishService notificationService;
86 protected RpcProviderRegistry rpcRegistry;
87 protected BindingAwareBroker.RpcRegistration<?> rpcRegistration;
88 private RestAdapter restAdapter;
89 private NearRicUrlProvider nearRicUrlProvider;
91 public NonrtRicApiProvider(DataBroker dataBroker,
92 NotificationPublishService notificationPublishService,
93 RpcProviderRegistry rpcProviderRegistry) {
94 log.info("Creating provider for {}", APP_NAME);
95 executor = Executors.newFixedThreadPool(1);
96 setDataBroker(dataBroker);
97 setNotificationService(notificationPublishService);
98 setRpcRegistry(rpcProviderRegistry);
103 public void initialize() {
104 log.info("Initializing provider for {}", APP_NAME);
106 restAdapter = new RestAdapterImpl();
107 nearRicUrlProvider = new NearRicUrlProvider();
108 log.info("Initialization complete for {}", APP_NAME);
111 protected void initializeChild() {
112 // Override if you have custom initialization intelligence
116 public void close() throws Exception {
117 log.info("Closing provider for {}", APP_NAME);
119 rpcRegistration.close();
120 log.info("Successfully closed provider for {}", APP_NAME);
123 public void setDataBroker(DataBroker dataBroker) {
124 this.dataBroker = dataBroker;
125 if (log.isDebugEnabled()) {
126 log.debug("DataBroker set to {}", dataBroker == null ? NULL_PARAM : NON_NULL_PARAM);
130 public void setNotificationService(NotificationPublishService notificationService) {
131 this.notificationService = notificationService;
132 if (log.isDebugEnabled()) {
133 log.debug("Notification Service set to {}",
134 notificationService == null ? NULL_PARAM : NON_NULL_PARAM);
138 public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
139 this.rpcRegistry = rpcRegistry;
140 if (log.isDebugEnabled()) {
141 log.debug("RpcRegistry set to {}", rpcRegistry == null ? NULL_PARAM : NON_NULL_PARAM);
145 private void createContainers() {
147 final WriteTransaction t = dataBroker.newReadWriteTransaction();
150 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
152 log.info("Create containers succeeded!");
154 } catch (InterruptedException | ExecutionException e) {
155 log.error("Create containers failed: ", e);
156 Thread.currentThread().interrupt();
161 public ListenableFuture<RpcResult<GetPolicyTypeIdentitiesOutput>> getPolicyTypeIdentities(
162 GetPolicyTypeIdentitiesInput input) {
163 log.info("Start of getPolicyTypeIdentities");
164 GetPolicyTypeIdentitiesOutputBuilder responseBuilder = new GetPolicyTypeIdentitiesOutputBuilder();
165 String uri = nearRicUrlProvider.policyTypesUrl(String.valueOf(input.getNearRtRicUrl()));
166 ResponseEntity<List<String>> response = restAdapter.get(uri, List.class);
167 if (response.hasBody()) {
168 log.info("Response getPolicyTypeIdentities : {} ", response.getBody());
169 responseBuilder.setPolicyTypeIdList(response.getBody());
171 log.info("End of getPolicyTypeIdentities");
172 RpcResult<GetPolicyTypeIdentitiesOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeIdentitiesOutput>status(true)
173 .withResult(responseBuilder.build()).build();
174 return Futures.immediateFuture(rpcResult);
178 public ListenableFuture<RpcResult<GetPolicyIdentitiesOutput>> getPolicyIdentities(GetPolicyIdentitiesInput input) {
179 log.info("Start of getPolicyIdentities");
180 GetPolicyIdentitiesOutputBuilder responseBuilder = new GetPolicyIdentitiesOutputBuilder();
181 String uri = nearRicUrlProvider.policiesUrl(String.valueOf(input.getNearRtRicUrl()));
182 ResponseEntity<List<String>> response = restAdapter.get(uri, List.class);
183 if (response.hasBody()) {
184 log.info("Response getPolicyIdentities : {} ", response.getBody());
185 responseBuilder.setPolicyIdList(response.getBody());
187 log.info("End of getPolicyIdentities");
188 RpcResult<GetPolicyIdentitiesOutput> rpcResult = RpcResultBuilder
189 .<GetPolicyIdentitiesOutput>status(true).withResult(responseBuilder.build()).build();
190 return Futures.immediateFuture(rpcResult);
194 public ListenableFuture<RpcResult<GetPolicyTypeOutput>> getPolicyType(GetPolicyTypeInput input) {
195 log.info("Start of getPolicyType");
196 log.info("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
225 .<PutPolicyOutput>status(true).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
238 .<DeletePolicyOutput>status(true).withResult(responseBuilder.build()).build();
239 return Futures.immediateFuture(rpcResult);