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);
102 public void initialize() {
103 log.info("Initializing provider for {}", APP_NAME);
105 restAdapter = new RestAdapterImpl();
106 log.info("Initialization complete for {}", APP_NAME);
109 protected void initializeChild() {
110 // Override if you have custom initialization intelligence
114 public void close() throws Exception {
115 log.info("Closing provider for {}", APP_NAME);
117 rpcRegistration.close();
118 log.info("Successfully closed provider for {}", APP_NAME);
121 public void setDataBroker(DataBroker dataBroker) {
122 this.dataBroker = dataBroker;
123 if (log.isDebugEnabled()) {
124 log.debug("DataBroker set to {}", dataBroker == null ? NULL_PARAM : NON_NULL_PARAM);
128 public void setNotificationService(NotificationPublishService notificationService) {
129 this.notificationService = notificationService;
130 if (log.isDebugEnabled()) {
131 log.debug("Notification Service set to {}", notificationService == null ? NULL_PARAM : NON_NULL_PARAM);
135 public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
136 this.rpcRegistry = rpcRegistry;
137 if (log.isDebugEnabled()) {
138 log.debug("RpcRegistry set to {}", rpcRegistry == null ? NULL_PARAM : NON_NULL_PARAM);
142 private void createContainers() {
144 final WriteTransaction t = dataBroker.newReadWriteTransaction();
147 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
149 log.info("Create containers succeeded!");
151 } catch (InterruptedException | ExecutionException e) {
152 log.error("Create containers failed: ", e);
153 Thread.currentThread().interrupt();
158 public ListenableFuture<RpcResult<PutA1PolicyOutput>> putA1Policy(PutA1PolicyInput input) {
159 log.info("Start of putPolicy");
160 PutA1PolicyOutputBuilder responseBuilder = new PutA1PolicyOutputBuilder();
163 final Uri uri = input.getNearRtRicUrl();
164 log.info("PUT Request input.GetA1Policy() : {} ", uri);
165 ResponseEntity<String> response = restAdapter.put(uri.getValue(), input.getBody(), String.class);
166 if (response.hasBody()) {
167 log.info("Response PutA1Policy : {} ", response.getBody());
168 responseBuilder.setBody(response.getBody());
170 responseBuilder.setHttpStatus(response.getStatusCodeValue());
171 } catch (RestClientResponseException ex) {
172 log.error("Caught RestClientResponseException: {}", ex.getMessage());
173 if (ex.getResponseBodyAsByteArray() != null) {
174 responseBuilder.setBody(ex.getResponseBodyAsString());
176 responseBuilder.setHttpStatus(ex.getRawStatusCode());
179 log.info("End of PutA1Policy");
180 RpcResult<PutA1PolicyOutput> rpcResult = RpcResultBuilder.<PutA1PolicyOutput>status(true)
181 .withResult(responseBuilder.build()).build();
182 return Futures.immediateFuture(rpcResult);
186 public ListenableFuture<RpcResult<DeleteA1PolicyOutput>> deleteA1Policy(DeleteA1PolicyInput input) {
187 log.info("Start of DeleteA1Policy");
188 DeleteA1PolicyOutputBuilder responseBuilder = new DeleteA1PolicyOutputBuilder();
191 final Uri uri = input.getNearRtRicUrl();
192 ResponseEntity<Object> response = restAdapter.delete(uri.getValue());
193 if (response.hasBody()) {
194 log.info("Response DeleteA1Policy : {} ", response.getBody());
195 responseBuilder.setBody(response.getBody().toString());
197 responseBuilder.setHttpStatus(response.getStatusCodeValue());
198 } catch (RestClientResponseException ex) {
199 log.error("Caught RestClientResponseException: {}", ex.getMessage());
200 if (ex.getResponseBodyAsByteArray() != null) {
201 responseBuilder.setBody(ex.getResponseBodyAsString());
203 responseBuilder.setHttpStatus(ex.getRawStatusCode());
206 log.info("End of DeleteA1Policy");
207 RpcResult<DeleteA1PolicyOutput> rpcResult = RpcResultBuilder.<DeleteA1PolicyOutput>status(true)
208 .withResult(responseBuilder.build()).build();
209 return Futures.immediateFuture(rpcResult);
212 private GetA1PolicyOutput getA1(GetA1PolicyInput input) {
213 log.info("Start of getA1");
214 GetA1PolicyOutputBuilder responseBuilder = new GetA1PolicyOutputBuilder();
217 final Uri uri = input.getNearRtRicUrl();
218 ResponseEntity<String> response = restAdapter.get(uri.getValue(), String.class);
219 if (response.hasBody()) {
220 log.info("Response getA1 : {} ", response.getBody());
221 responseBuilder.setBody(response.getBody());
223 responseBuilder.setHttpStatus(response.getStatusCodeValue());
224 } catch (RestClientResponseException ex) {
225 log.error("Caught RestClientResponseException: {}", ex.getMessage());
226 if (ex.getResponseBodyAsByteArray() != null) {
227 responseBuilder.setBody(ex.getResponseBodyAsString());
229 responseBuilder.setHttpStatus(ex.getRawStatusCode());
232 log.info("End of getA1");
233 return responseBuilder.build();
237 public ListenableFuture<RpcResult<GetA1PolicyOutput>> getA1Policy(GetA1PolicyInput input) {
238 GetA1PolicyOutput output = getA1(input);
239 RpcResult<GetA1PolicyOutput> rpcResult = RpcResultBuilder.<GetA1PolicyOutput>status(true).withResult(output)
241 return Futures.immediateFuture(rpcResult);
245 public ListenableFuture<RpcResult<GetA1PolicyStatusOutput>> getA1PolicyStatus(GetA1PolicyStatusInput input) {
246 GetA1PolicyInputBuilder getInputBuilder = new GetA1PolicyInputBuilder();
247 getInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl());
248 GetA1PolicyOutput getOutput = getA1(getInputBuilder.build());
250 GetA1PolicyStatusOutputBuilder outputBuilder = new GetA1PolicyStatusOutputBuilder();
251 outputBuilder.setBody(getOutput.getBody());
252 outputBuilder.setHttpStatus(getOutput.getHttpStatus());
254 return Futures.immediateFuture(RpcResultBuilder.<GetA1PolicyStatusOutput>status(true) //
255 .withResult(outputBuilder.build()) //
260 public ListenableFuture<RpcResult<GetA1PolicyTypeOutput>> getA1PolicyType(GetA1PolicyTypeInput input) {
261 GetA1PolicyInputBuilder getInputBuilder = new GetA1PolicyInputBuilder();
262 getInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl());
263 GetA1PolicyOutput getOutput = getA1(getInputBuilder.build());
265 GetA1PolicyTypeOutputBuilder outputBuilder = new GetA1PolicyTypeOutputBuilder();
266 outputBuilder.setBody(getOutput.getBody());
267 outputBuilder.setHttpStatus(getOutput.getHttpStatus());
269 return Futures.immediateFuture(RpcResultBuilder.<GetA1PolicyTypeOutput>status(true) //
270 .withResult(outputBuilder.build()) //