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";
81 private static final String REST_CLIENT_RESPONSE_EXCEPTION_MSG = "Caught RestClientResponseException: {}";
83 private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class);
84 private final ExecutorService executor;
86 protected DataBroker dataBroker;
87 protected NotificationPublishService notificationService;
88 protected RpcProviderRegistry rpcRegistry;
89 protected BindingAwareBroker.RpcRegistration<?> rpcRegistration;
90 private RestAdapter restAdapter;
92 public NonrtRicApiProvider(DataBroker dataBroker, 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 log.info("Initialization complete for {}", APP_NAME);
110 protected void initializeChild() {
111 // Override if you have custom initialization intelligence
115 public void close() throws Exception {
116 log.info("Closing provider for {}", APP_NAME);
118 rpcRegistration.close();
119 log.info("Successfully closed provider for {}", APP_NAME);
122 public void setDataBroker(DataBroker dataBroker) {
123 this.dataBroker = dataBroker;
124 if (log.isDebugEnabled()) {
125 log.debug("DataBroker set to {}", dataBroker == null ? NULL_PARAM : NON_NULL_PARAM);
129 public void setNotificationService(NotificationPublishService notificationService) {
130 this.notificationService = notificationService;
131 if (log.isDebugEnabled()) {
132 log.debug("Notification Service set to {}", notificationService == null ? NULL_PARAM : NON_NULL_PARAM);
136 public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
137 this.rpcRegistry = rpcRegistry;
138 if (log.isDebugEnabled()) {
139 log.debug("RpcRegistry set to {}", rpcRegistry == null ? NULL_PARAM : NON_NULL_PARAM);
143 private void createContainers() {
145 final WriteTransaction t = dataBroker.newReadWriteTransaction();
148 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
150 log.info("Create containers succeeded!");
152 } catch (InterruptedException | ExecutionException e) {
153 log.error("Create containers failed: ", e);
154 Thread.currentThread().interrupt();
159 public ListenableFuture<RpcResult<PutA1PolicyOutput>> putA1Policy(PutA1PolicyInput input) {
160 log.info("Start of putPolicy");
161 PutA1PolicyOutputBuilder putPolicyResponseBuilder = new PutA1PolicyOutputBuilder();
164 final Uri uri = input.getNearRtRicUrl();
165 log.info("PUT Request input.GetA1Policy() : {} ", uri);
166 ResponseEntity<String> putPolicyResponse = restAdapter.put(uri.getValue(), input.getBody(), String.class);
167 if (putPolicyResponse.hasBody()) {
168 log.info("Response PutA1Policy : {} ", putPolicyResponse.getBody());
169 putPolicyResponseBuilder.setBody(putPolicyResponse.getBody());
171 putPolicyResponseBuilder.setHttpStatus(putPolicyResponse.getStatusCodeValue());
172 } catch (RestClientResponseException ex) {
173 log.error(REST_CLIENT_RESPONSE_EXCEPTION_MSG, ex.getMessage());
174 if (ex.getResponseBodyAsByteArray() != null) {
175 putPolicyResponseBuilder.setBody(ex.getResponseBodyAsString());
177 putPolicyResponseBuilder.setHttpStatus(ex.getRawStatusCode());
180 log.info("End of PutA1Policy");
181 RpcResult<PutA1PolicyOutput> rpcResult = RpcResultBuilder.<PutA1PolicyOutput>status(true)
182 .withResult(putPolicyResponseBuilder.build()).build();
183 return Futures.immediateFuture(rpcResult);
187 public ListenableFuture<RpcResult<DeleteA1PolicyOutput>> deleteA1Policy(DeleteA1PolicyInput input) {
188 log.info("Start of DeleteA1Policy");
189 DeleteA1PolicyOutputBuilder deletePolicyResponseBuilder = new DeleteA1PolicyOutputBuilder();
192 final Uri uri = input.getNearRtRicUrl();
193 ResponseEntity<Object> deletePolicyResponse = restAdapter.delete(uri.getValue());
194 if (deletePolicyResponse.hasBody()) {
195 log.info("Response DeleteA1Policy : {} ", deletePolicyResponse.getBody());
196 deletePolicyResponseBuilder.setBody(deletePolicyResponse.getBody().toString());
198 deletePolicyResponseBuilder.setHttpStatus(deletePolicyResponse.getStatusCodeValue());
199 } catch (RestClientResponseException ex) {
200 log.error(REST_CLIENT_RESPONSE_EXCEPTION_MSG, ex.getMessage());
201 if (ex.getResponseBodyAsByteArray() != null) {
202 deletePolicyResponseBuilder.setBody(ex.getResponseBodyAsString());
204 deletePolicyResponseBuilder.setHttpStatus(ex.getRawStatusCode());
207 log.info("End of DeleteA1Policy");
208 RpcResult<DeleteA1PolicyOutput> rpcResult = RpcResultBuilder.<DeleteA1PolicyOutput>status(true)
209 .withResult(deletePolicyResponseBuilder.build()).build();
210 return Futures.immediateFuture(rpcResult);
213 private GetA1PolicyOutput getA1(GetA1PolicyInput input) {
214 log.info("Start of getA1");
215 GetA1PolicyOutputBuilder getPolicyResponseBuilder = new GetA1PolicyOutputBuilder();
218 final Uri uri = input.getNearRtRicUrl();
219 ResponseEntity<String> getPolicyResponse = restAdapter.get(uri.getValue(), String.class);
220 if (getPolicyResponse.hasBody()) {
221 log.info("Response getA1 : {} ", getPolicyResponse.getBody());
222 getPolicyResponseBuilder.setBody(getPolicyResponse.getBody());
224 getPolicyResponseBuilder.setHttpStatus(getPolicyResponse.getStatusCodeValue());
225 } catch (RestClientResponseException ex) {
226 log.error(REST_CLIENT_RESPONSE_EXCEPTION_MSG, ex.getMessage());
227 if (ex.getResponseBodyAsByteArray() != null) {
228 getPolicyResponseBuilder.setBody(ex.getResponseBodyAsString());
230 getPolicyResponseBuilder.setHttpStatus(ex.getRawStatusCode());
233 log.info("End of getA1");
234 return getPolicyResponseBuilder.build();
238 public ListenableFuture<RpcResult<GetA1PolicyOutput>> getA1Policy(GetA1PolicyInput input) {
239 GetA1PolicyOutput output = getA1(input);
240 RpcResult<GetA1PolicyOutput> rpcResult = RpcResultBuilder.<GetA1PolicyOutput>status(true).withResult(output)
242 return Futures.immediateFuture(rpcResult);
246 public ListenableFuture<RpcResult<GetA1PolicyStatusOutput>> getA1PolicyStatus(GetA1PolicyStatusInput input) {
247 GetA1PolicyInputBuilder getPolicyStatusInputBuilder = new GetA1PolicyInputBuilder();
248 getPolicyStatusInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl());
249 GetA1PolicyOutput getOutput = getA1(getPolicyStatusInputBuilder.build());
251 GetA1PolicyStatusOutputBuilder getPolicyStatusoutputBuilder = new GetA1PolicyStatusOutputBuilder();
252 getPolicyStatusoutputBuilder.setBody(getOutput.getBody());
253 getPolicyStatusoutputBuilder.setHttpStatus(getOutput.getHttpStatus());
255 return Futures.immediateFuture(RpcResultBuilder.<GetA1PolicyStatusOutput>status(true) //
256 .withResult(getPolicyStatusoutputBuilder.build()) //
261 public ListenableFuture<RpcResult<GetA1PolicyTypeOutput>> getA1PolicyType(GetA1PolicyTypeInput input) {
262 GetA1PolicyInputBuilder getPolicyTypeInputBuilder = new GetA1PolicyInputBuilder();
263 getPolicyTypeInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl());
264 GetA1PolicyOutput getOutput = getA1(getPolicyTypeInputBuilder.build());
266 GetA1PolicyTypeOutputBuilder getPolicyTypeOutputBuilder = new GetA1PolicyTypeOutputBuilder();
267 getPolicyTypeOutputBuilder.setBody(getOutput.getBody());
268 getPolicyTypeOutputBuilder.setHttpStatus(getOutput.getHttpStatus());
270 return Futures.immediateFuture(RpcResultBuilder.<GetA1PolicyTypeOutput>status(true) //
271 .withResult(getPolicyTypeOutputBuilder.build()) //