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.Futures;
24 import com.google.common.util.concurrent.ListenableFuture;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter.RestAdapter;
28 import org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter.RestAdapterImpl;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
31 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
32 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.A1ADAPTERAPIService;
33 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyInput;
34 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyOutput;
35 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyOutputBuilder;
36 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyInput;
37 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyInputBuilder;
38 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyOutput;
39 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyOutputBuilder;
40 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusInput;
41 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusOutput;
42 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusOutputBuilder;
43 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeInput;
44 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeOutput;
45 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeOutputBuilder;
46 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyInput;
47 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyOutput;
48 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyOutputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.http.ResponseEntity;
55 import org.springframework.web.client.RestClientResponseException;
58 * Defines a base implementation for your provider. This class overrides the
59 * generated interface from the YANG model and implements the request model for
60 * the A1 interface. This class identifies the Near-RT RIC throught the IP passed
61 * over the payload and calls the corresponding Near-RT RIC over Rest API
65 * @author lathishbabu.ganesan@est.tech
69 @SuppressWarnings("squid:S1874") // "@Deprecated" code should not be used
70 public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService {
72 protected static final String APP_NAME = "nonrt-ric-api";
73 protected static final String NO_SERVICE_LOGIC_ACTIVE = "No service logic active for ";
74 private static final String NON_NULL_PARAM = "non-null";
75 private static final String NULL_PARAM = "null";
76 private static final String REST_CLIENT_RESPONSE_EXCEPTION_MSG = "Caught RestClientResponseException: {}";
78 private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class);
79 private final ExecutorService executor;
81 protected DataBroker dataBroker;
82 protected NotificationPublishService notificationService;
83 protected RpcProviderRegistry rpcRegistry;
84 private RestAdapter restAdapter;
86 public NonrtRicApiProvider(DataBroker dataBroker, NotificationPublishService notificationPublishService,
87 RpcProviderRegistry rpcProviderRegistry) {
88 log.info("Creating provider for {}", APP_NAME);
89 executor = Executors.newFixedThreadPool(1);
90 setDataBroker(dataBroker);
91 setNotificationService(notificationPublishService);
92 setRpcRegistry(rpcProviderRegistry);
96 public void initialize() {
97 log.info("Initializing provider for {}", APP_NAME);
98 restAdapter = new RestAdapterImpl();
99 log.info("Initialization complete for {}", APP_NAME);
103 public void close() throws Exception {
104 log.info("Closing provider for {}", APP_NAME);
106 log.info("Successfully closed provider for {}", APP_NAME);
109 public void setDataBroker(DataBroker dataBroker) {
110 this.dataBroker = dataBroker;
111 if (log.isDebugEnabled()) {
112 log.debug("DataBroker set to {}", dataBroker == null ? NULL_PARAM : NON_NULL_PARAM);
116 public void setNotificationService(NotificationPublishService notificationService) {
117 this.notificationService = notificationService;
118 if (log.isDebugEnabled()) {
119 log.debug("Notification Service set to {}", notificationService == null ? NULL_PARAM : NON_NULL_PARAM);
123 public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
124 this.rpcRegistry = rpcRegistry;
125 if (log.isDebugEnabled()) {
126 log.debug("RpcRegistry set to {}", rpcRegistry == null ? NULL_PARAM : NON_NULL_PARAM);
131 public ListenableFuture<RpcResult<PutA1PolicyOutput>> putA1Policy(PutA1PolicyInput input) {
132 log.info("Start of putPolicy");
133 PutA1PolicyOutputBuilder putPolicyResponseBuilder = new PutA1PolicyOutputBuilder();
136 final Uri uri = input.getNearRtRicUrl();
137 log.info("PUT Request input.GetA1Policy() : {} ", uri);
138 ResponseEntity<String> putPolicyResponse = restAdapter.put(uri.getValue(), input.getBody(), String.class);
139 if (putPolicyResponse.hasBody()) {
140 log.info("Response PutA1Policy : {} ", putPolicyResponse.getBody());
141 putPolicyResponseBuilder.setBody(putPolicyResponse.getBody());
143 putPolicyResponseBuilder.setHttpStatus(putPolicyResponse.getStatusCodeValue());
144 } catch (RestClientResponseException ex) {
145 log.error(REST_CLIENT_RESPONSE_EXCEPTION_MSG, ex.getMessage());
146 if (ex.getResponseBodyAsByteArray() != null) {
147 putPolicyResponseBuilder.setBody(ex.getResponseBodyAsString());
149 putPolicyResponseBuilder.setHttpStatus(ex.getRawStatusCode());
152 log.info("End of PutA1Policy");
153 RpcResult<PutA1PolicyOutput> rpcResult = RpcResultBuilder.<PutA1PolicyOutput>status(true)
154 .withResult(putPolicyResponseBuilder.build()).build();
155 return Futures.immediateFuture(rpcResult);
159 public ListenableFuture<RpcResult<DeleteA1PolicyOutput>> deleteA1Policy(DeleteA1PolicyInput input) {
160 log.info("Start of DeleteA1Policy");
161 DeleteA1PolicyOutputBuilder deletePolicyResponseBuilder = new DeleteA1PolicyOutputBuilder();
164 final Uri uri = input.getNearRtRicUrl();
165 ResponseEntity<Object> deletePolicyResponse = restAdapter.delete(uri.getValue());
166 if (deletePolicyResponse.hasBody()) {
167 log.info("Response DeleteA1Policy : {} ", deletePolicyResponse.getBody());
168 deletePolicyResponseBuilder.setBody(deletePolicyResponse.getBody().toString());
170 deletePolicyResponseBuilder.setHttpStatus(deletePolicyResponse.getStatusCodeValue());
171 } catch (RestClientResponseException ex) {
172 log.error(REST_CLIENT_RESPONSE_EXCEPTION_MSG, ex.getMessage());
173 if (ex.getResponseBodyAsByteArray() != null) {
174 deletePolicyResponseBuilder.setBody(ex.getResponseBodyAsString());
176 deletePolicyResponseBuilder.setHttpStatus(ex.getRawStatusCode());
179 log.info("End of DeleteA1Policy");
180 RpcResult<DeleteA1PolicyOutput> rpcResult = RpcResultBuilder.<DeleteA1PolicyOutput>status(true)
181 .withResult(deletePolicyResponseBuilder.build()).build();
182 return Futures.immediateFuture(rpcResult);
185 protected GetA1PolicyOutput getA1(GetA1PolicyInput input) {
186 log.info("Start of getA1");
187 GetA1PolicyOutputBuilder getPolicyResponseBuilder = new GetA1PolicyOutputBuilder();
190 final Uri uri = input.getNearRtRicUrl();
191 ResponseEntity<String> getPolicyResponse = restAdapter.get(uri.getValue(), String.class);
192 if (getPolicyResponse.hasBody()) {
193 log.info("Response getA1 : {} ", getPolicyResponse.getBody());
194 getPolicyResponseBuilder.setBody(getPolicyResponse.getBody());
196 getPolicyResponseBuilder.setHttpStatus(getPolicyResponse.getStatusCodeValue());
197 } catch (RestClientResponseException ex) {
198 log.error(REST_CLIENT_RESPONSE_EXCEPTION_MSG, ex.getMessage());
199 if (ex.getResponseBodyAsByteArray() != null) {
200 getPolicyResponseBuilder.setBody(ex.getResponseBodyAsString());
202 getPolicyResponseBuilder.setHttpStatus(ex.getRawStatusCode());
205 log.info("End of getA1");
206 return getPolicyResponseBuilder.build();
210 public ListenableFuture<RpcResult<GetA1PolicyOutput>> getA1Policy(GetA1PolicyInput input) {
211 GetA1PolicyOutput output = getA1(input);
212 RpcResult<GetA1PolicyOutput> rpcResult = RpcResultBuilder.<GetA1PolicyOutput>status(true).withResult(output)
214 return Futures.immediateFuture(rpcResult);
218 public ListenableFuture<RpcResult<GetA1PolicyStatusOutput>> getA1PolicyStatus(GetA1PolicyStatusInput input) {
219 GetA1PolicyInputBuilder getPolicyStatusInputBuilder = new GetA1PolicyInputBuilder();
220 getPolicyStatusInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl());
221 GetA1PolicyOutput getOutput = getA1(getPolicyStatusInputBuilder.build());
223 GetA1PolicyStatusOutputBuilder getPolicyStatusoutputBuilder = new GetA1PolicyStatusOutputBuilder();
224 getPolicyStatusoutputBuilder.setBody(getOutput.getBody());
225 getPolicyStatusoutputBuilder.setHttpStatus(getOutput.getHttpStatus());
227 return Futures.immediateFuture(RpcResultBuilder.<GetA1PolicyStatusOutput>status(true) //
228 .withResult(getPolicyStatusoutputBuilder.build()) //
233 public ListenableFuture<RpcResult<GetA1PolicyTypeOutput>> getA1PolicyType(GetA1PolicyTypeInput input) {
234 GetA1PolicyInputBuilder getPolicyTypeInputBuilder = new GetA1PolicyInputBuilder();
235 getPolicyTypeInputBuilder.setNearRtRicUrl(input.getNearRtRicUrl());
236 GetA1PolicyOutput getOutput = getA1(getPolicyTypeInputBuilder.build());
238 GetA1PolicyTypeOutputBuilder getPolicyTypeOutputBuilder = new GetA1PolicyTypeOutputBuilder();
239 getPolicyTypeOutputBuilder.setBody(getOutput.getBody());
240 getPolicyTypeOutputBuilder.setHttpStatus(getOutput.getHttpStatus());
242 return Futures.immediateFuture(RpcResultBuilder.<GetA1PolicyTypeOutput>status(true) //
243 .withResult(getPolicyTypeOutputBuilder.build()) //