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.o_ran_sc.nonrtric.sdnc_a1.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.concurrent.ExecutionException;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter.RestAdapter;
30 import org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter.RestAdapterImpl;
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
33 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
34 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
35 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
36 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
37 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.A1ADAPTERAPIService;
38 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyInput;
39 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyOutput;
40 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyOutputBuilder;
41 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyInput;
42 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyInputBuilder;
43 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyOutput;
44 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyOutputBuilder;
45 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusInput;
46 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusOutput;
47 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusOutputBuilder;
48 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeInput;
49 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeOutput;
50 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeOutputBuilder;
51 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyInput;
52 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyOutput;
53 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyOutputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
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;
60 import org.springframework.web.client.RestClientResponseException;
63 * Defines a base implementation for your provider. This class overrides the
64 * generated interface from the YANG model and implements the request model for
65 * the A1 interface. This class identifies the Near-RT RIC throught the IP passed
66 * over the payload and calls the corresponding Near-RT RIC over Rest API
70 * @author lathishbabu.ganesan@est.tech
74 @SuppressWarnings("squid:S1874") // "@Deprecated" code should not be used
75 public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService {
77 protected static final String APP_NAME = "nonrt-ric-api";
78 protected static final String NO_SERVICE_LOGIC_ACTIVE = "No service logic active for ";
79 private static final String NON_NULL_PARAM = "non-null";
80 private static final String NULL_PARAM = "null";
82 private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class);
83 private final ExecutorService executor;
85 protected DataBroker dataBroker;
86 protected NotificationPublishService notificationService;
87 protected RpcProviderRegistry rpcRegistry;
88 protected BindingAwareBroker.RpcRegistration<?> rpcRegistration;
89 private RestAdapter restAdapter;
91 public NonrtRicApiProvider(DataBroker dataBroker, NotificationPublishService notificationPublishService,
92 RpcProviderRegistry rpcProviderRegistry) {
93 log.info("Creating provider for {}", APP_NAME);
94 executor = Executors.newFixedThreadPool(1);
95 setDataBroker(dataBroker);
96 setNotificationService(notificationPublishService);
97 setRpcRegistry(rpcProviderRegistry);
101 public void initialize() {
102 log.info("Initializing provider for {}", APP_NAME);
104 restAdapter = new RestAdapterImpl();
105 log.info("Initialization complete for {}", APP_NAME);
108 protected void initializeChild() {
109 // Override if you have custom initialization intelligence
113 public void close() throws Exception {
114 log.info("Closing provider for {}", APP_NAME);
116 rpcRegistration.close();
117 log.info("Successfully closed provider for {}", APP_NAME);
120 public void setDataBroker(DataBroker dataBroker) {
121 this.dataBroker = dataBroker;
122 if (log.isDebugEnabled()) {
123 log.debug("DataBroker set to {}", dataBroker == null ? NULL_PARAM : NON_NULL_PARAM);
127 public void setNotificationService(NotificationPublishService notificationService) {
128 this.notificationService = notificationService;
129 if (log.isDebugEnabled()) {
130 log.debug("Notification Service set to {}", notificationService == null ? NULL_PARAM : NON_NULL_PARAM);
134 public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
135 this.rpcRegistry = rpcRegistry;
136 if (log.isDebugEnabled()) {
137 log.debug("RpcRegistry set to {}", rpcRegistry == null ? NULL_PARAM : NON_NULL_PARAM);
141 private void createContainers() {
143 final WriteTransaction t = dataBroker.newReadWriteTransaction();
146 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
148 log.info("Create containers succeeded!");
150 } catch (InterruptedException | ExecutionException e) {
151 log.error("Create containers failed: ", e);
152 Thread.currentThread().interrupt();
157 public ListenableFuture<RpcResult<PutA1PolicyOutput>> putA1Policy(PutA1PolicyInput input) {
158 log.info("Start of putPolicy");
159 PutA1PolicyOutputBuilder responseBuilder = new PutA1PolicyOutputBuilder();
162 final Uri uri = input.getNearRtRicUrl();
163 log.info("PUT Request input.GetA1Policy() : {} ", uri);
164 ResponseEntity<String> response = restAdapter.put(uri.getValue(), input.getBody(), String.class);
165 if (response.hasBody()) {
166 log.info("Response PutA1Policy : {} ", response.getBody());
167 responseBuilder.setBody(response.getBody());
169 responseBuilder.setHttpStatus(response.getStatusCodeValue());
170 } catch (RestClientResponseException ex) {
171 log.error("Caught RestClientResponseException: {}", ex.getMessage());
172 if (ex.getResponseBodyAsByteArray() != null) {
173 responseBuilder.setBody(ex.getResponseBodyAsString());
175 responseBuilder.setHttpStatus(ex.getRawStatusCode());
178 log.info("End of PutA1Policy");
179 RpcResult<PutA1PolicyOutput> rpcResult = RpcResultBuilder.<PutA1PolicyOutput>status(true)
180 .withResult(responseBuilder.build()).build();
181 return Futures.immediateFuture(rpcResult);
185 public ListenableFuture<RpcResult<DeleteA1PolicyOutput>> deleteA1Policy(DeleteA1PolicyInput input) {
186 log.info("Start of DeleteA1Policy");
187 DeleteA1PolicyOutputBuilder responseBuilder = new DeleteA1PolicyOutputBuilder();
190 final Uri uri = input.getNearRtRicUrl();
191 ResponseEntity<Object> response = restAdapter.delete(uri.getValue());
192 if (response.hasBody()) {
193 log.info("Response DeleteA1Policy : {} ", response.getBody());
194 responseBuilder.setBody(response.getBody().toString());
196 responseBuilder.setHttpStatus(response.getStatusCodeValue());
197 } catch (RestClientResponseException ex) {
198 log.error("Caught RestClientResponseException: {}", ex.getMessage());
199 if (ex.getResponseBodyAsByteArray() != null) {
200 responseBuilder.setBody(ex.getResponseBodyAsString());
202 responseBuilder.setHttpStatus(ex.getRawStatusCode());
205 log.info("End of DeleteA1Policy");
206 RpcResult<DeleteA1PolicyOutput> rpcResult = RpcResultBuilder.<DeleteA1PolicyOutput>status(true)
207 .withResult(responseBuilder.build()).build();
208 return Futures.immediateFuture(rpcResult);
211 protected GetA1PolicyOutput getA1(GetA1PolicyInput input) {
212 log.info("Start of getA1");
213 GetA1PolicyOutputBuilder responseBuilder = new GetA1PolicyOutputBuilder();
216 final Uri uri = input.getNearRtRicUrl();
217 ResponseEntity<String> response = restAdapter.get(uri.getValue(), String.class);
218 if (response.hasBody()) {
219 log.info("Response getA1 : {} ", response.getBody());
220 responseBuilder.setBody(response.getBody());
222 responseBuilder.setHttpStatus(response.getStatusCodeValue());
223 } catch (RestClientResponseException ex) {
224 log.error("Caught RestClientResponseException: {}", ex.getMessage());
225 if (ex.getResponseBodyAsByteArray() != null) {
226 responseBuilder.setBody(ex.getResponseBodyAsString());
228 responseBuilder.setHttpStatus(ex.getRawStatusCode());
231 log.info("End of getA1");
232 return responseBuilder.build();
236 public ListenableFuture<RpcResult<GetA1PolicyOutput>> getA1Policy(GetA1PolicyInput input) {
237 GetA1PolicyOutput output = getA1(input);
238 RpcResult<GetA1PolicyOutput> rpcResult = RpcResultBuilder.<GetA1PolicyOutput>status(true).withResult(output)
240 return Futures.immediateFuture(rpcResult);
244 public ListenableFuture<RpcResult<GetA1PolicyStatusOutput>> getA1PolicyStatus(GetA1PolicyStatusInput input) {
245 GetA1PolicyInputBuilder getInputBuilder = new GetA1PolicyInputBuilder();
246 getInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl());
247 GetA1PolicyOutput getOutput = getA1(getInputBuilder.build());
249 GetA1PolicyStatusOutputBuilder outputBuilder = new GetA1PolicyStatusOutputBuilder();
250 outputBuilder.setBody(getOutput.getBody());
251 outputBuilder.setHttpStatus(getOutput.getHttpStatus());
253 return Futures.immediateFuture(RpcResultBuilder.<GetA1PolicyStatusOutput>status(true) //
254 .withResult(outputBuilder.build()) //
259 public ListenableFuture<RpcResult<GetA1PolicyTypeOutput>> getA1PolicyType(GetA1PolicyTypeInput input) {
260 GetA1PolicyInputBuilder getInputBuilder = new GetA1PolicyInputBuilder();
261 getInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl());
262 GetA1PolicyOutput getOutput = getA1(getInputBuilder.build());
264 GetA1PolicyTypeOutputBuilder outputBuilder = new GetA1PolicyTypeOutputBuilder();
265 outputBuilder.setBody(getOutput.getBody());
266 outputBuilder.setHttpStatus(getOutput.getHttpStatus());
268 return Futures.immediateFuture(RpcResultBuilder.<GetA1PolicyTypeOutput>status(true) //
269 .withResult(outputBuilder.build()) //