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 java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.TimeZone;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.sdnc.northbound.restadpter.NearRicUrlProvider;
33 import org.onap.sdnc.northbound.restadpter.RestAdapter;
34 import org.onap.sdnc.northbound.restadpter.RestAdapterImpl;
35 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
36 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
37 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
38 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
39 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
40 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
41 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.A1ADAPTERAPIService;
42 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyInstanceInput;
43 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyInstanceOutput;
44 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyInstanceOutputBuilder;
45 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyTypeInput;
46 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyTypeOutput;
47 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyTypeOutputBuilder;
48 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyInstanceInput;
49 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyInstanceOutput;
50 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyInstanceOutputBuilder;
51 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyTypeInput;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyTypeOutput;
53 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyTypeOutputBuilder;
54 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetHealthCheckInput;
55 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetHealthCheckOutput;
56 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetHealthCheckOutputBuilder;
57 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetNearRTRICsInput;
58 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetNearRTRICsOutput;
59 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstanceInput;
60 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstanceOutput;
61 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstanceOutputBuilder;
62 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstancesInput;
63 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstancesOutput;
64 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstancesOutputBuilder;
65 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypeInput;
66 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypeOutput;
67 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypeOutputBuilder;
68 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypesInput;
69 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypesOutput;
70 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypesOutputBuilder;
71 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetStatusInput;
72 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetStatusOutput;
73 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetStatusOutputBuilder;
74 import org.opendaylight.yangtools.yang.common.RpcResult;
75 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
76 import org.oransc.ric.a1med.client.model.PolicyTypeSchema;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
79 import com.google.common.base.Optional;
80 import com.google.common.util.concurrent.CheckedFuture;
81 import com.google.common.util.concurrent.Futures;
82 import com.google.common.util.concurrent.ListenableFuture;
85 * Defines a base implementation for your provider. This class overrides the generated interface
86 * from the YANG model and implements the request model for the A1 interface. This class identifies
87 * the Near-RIC throught the IP passed over the payload and calls the corresponding Near-RIC over
92 * @author lathishbabu.ganesan@est.tech
96 public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService {
98 protected static final String APP_NAME = "nonrt-ric-api";
99 protected static final String NO_SERVICE_LOGIC_ACTIVE = "No service logic active for ";
100 private static final String NON_NULL_PARAM = "non-null";
101 private static final String NULL_PARAM = "null";
102 private static final String RESPONSE_SUCCESS = "Success";
103 private static final String RESPONSE_CODE_SUCCESS = "200";
105 private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class);
106 private final ExecutorService executor;
108 protected DataBroker dataBroker;
109 protected NotificationPublishService notificationService;
110 protected RpcProviderRegistry rpcRegistry;
111 protected BindingAwareBroker.RpcRegistration<?> rpcRegistration;
112 private RestAdapter restAdapter;
113 private NearRicUrlProvider nearRicUrlProvider;
115 public NonrtRicApiProvider(DataBroker dataBroker,
116 NotificationPublishService notificationPublishService,
117 RpcProviderRegistry rpcProviderRegistry) {
118 log.info("Creating provider for {}", APP_NAME);
119 executor = Executors.newFixedThreadPool(1);
120 setDataBroker(dataBroker);
121 setNotificationService(notificationPublishService);
122 setRpcRegistry(rpcProviderRegistry);
127 public void initialize() {
128 log.info("Initializing provider for {}", APP_NAME);
130 restAdapter = new RestAdapterImpl();
131 nearRicUrlProvider = new NearRicUrlProvider();
132 log.info("Initialization complete for {}", APP_NAME);
135 protected void initializeChild() {
136 // Override if you have custom initialization intelligence
140 public void close() throws Exception {
141 log.info("Closing provider for {}", APP_NAME);
143 rpcRegistration.close();
144 log.info("Successfully closed provider for {}", APP_NAME);
147 private static class Iso8601Util {
149 private static TimeZone timeZone = TimeZone.getTimeZone("UTC");
150 private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
152 private Iso8601Util() {}
155 dateFormat.setTimeZone(timeZone);
158 private static String now() {
159 return dateFormat.format(new Date());
163 public void setDataBroker(DataBroker dataBroker) {
164 this.dataBroker = dataBroker;
165 if (log.isDebugEnabled()) {
166 log.debug("DataBroker set to {}", dataBroker == null ? NULL_PARAM : NON_NULL_PARAM);
170 public void setNotificationService(NotificationPublishService notificationService) {
171 this.notificationService = notificationService;
172 if (log.isDebugEnabled()) {
173 log.debug("Notification Service set to {}",
174 notificationService == null ? NULL_PARAM : NON_NULL_PARAM);
178 public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
179 this.rpcRegistry = rpcRegistry;
180 if (log.isDebugEnabled()) {
181 log.debug("RpcRegistry set to {}", rpcRegistry == null ? NULL_PARAM : NON_NULL_PARAM);
185 private void createContainers() {
187 final WriteTransaction t = dataBroker.newReadWriteTransaction();
190 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
192 log.info("Create containers succeeded!");
194 } catch (InterruptedException | ExecutionException e) {
195 log.error("Create containers failed: ", e);
200 public ListenableFuture<RpcResult<CreatePolicyInstanceOutput>> createPolicyInstance(
201 CreatePolicyInstanceInput input) {
202 log.debug("Start of createPolicyInstance");
203 String uri = nearRicUrlProvider.getPolicyInstanceId(String.valueOf(input.getPolicyTypeId()),
204 String.valueOf(input.getPolicyInstanceId()));
205 restAdapter.put(uri, input.getPolicyInstance());
206 CreatePolicyInstanceOutputBuilder responseBuilder = new CreatePolicyInstanceOutputBuilder();
207 log.debug("End of createPolicyInstance");
208 RpcResult<CreatePolicyInstanceOutput> rpcResult = RpcResultBuilder
209 .<CreatePolicyInstanceOutput>status(true).withResult(responseBuilder.build()).build();
210 return Futures.immediateFuture(rpcResult);
214 public ListenableFuture<RpcResult<CreatePolicyTypeOutput>> createPolicyType(
215 CreatePolicyTypeInput input) {
216 log.debug("Start of createPolicyType");
217 String uri = nearRicUrlProvider.getPolicyTypeId(String.valueOf(input.getPolicyTypeId()));
218 PolicyTypeSchema policyTypeSchema = new PolicyTypeSchema();
219 restAdapter.put(uri, policyTypeSchema);
220 CreatePolicyTypeOutputBuilder responseBuilder = new CreatePolicyTypeOutputBuilder();
221 responseBuilder.setCode(RESPONSE_CODE_SUCCESS);
222 responseBuilder.setStatus(RESPONSE_SUCCESS);
223 log.debug("End of createPolicyType");
224 RpcResult<CreatePolicyTypeOutput> rpcResult = RpcResultBuilder
225 .<CreatePolicyTypeOutput>status(true).withResult(responseBuilder.build()).build();
226 return Futures.immediateFuture(rpcResult);
230 public ListenableFuture<RpcResult<DeletePolicyInstanceOutput>> deletePolicyInstance(
231 DeletePolicyInstanceInput input) {
232 log.debug("Start of deletePolicyInstance");
233 String uri = nearRicUrlProvider.getPolicyInstanceId(String.valueOf(input.getPolicyTypeId()),
234 String.valueOf(input.getPolicyInstanceId()));
235 restAdapter.delete(uri);
236 DeletePolicyInstanceOutputBuilder responseBuilder = new DeletePolicyInstanceOutputBuilder();
237 log.debug("End of deletePolicyInstance");
238 RpcResult<DeletePolicyInstanceOutput> rpcResult = RpcResultBuilder
239 .<DeletePolicyInstanceOutput>status(true).withResult(responseBuilder.build()).build();
240 return Futures.immediateFuture(rpcResult);
244 public ListenableFuture<RpcResult<DeletePolicyTypeOutput>> deletePolicyType(
245 DeletePolicyTypeInput input) {
246 log.debug("Start of deletePolicyType");
247 String uri = nearRicUrlProvider.getPolicyTypeId(String.valueOf(input.getPolicyTypeId()));
248 restAdapter.delete(uri);
249 DeletePolicyTypeOutputBuilder responseBuilder = new DeletePolicyTypeOutputBuilder();
250 log.debug("End of deletePolicyType");
251 RpcResult<DeletePolicyTypeOutput> rpcResult = RpcResultBuilder
252 .<DeletePolicyTypeOutput>status(true).withResult(responseBuilder.build()).build();
253 return Futures.immediateFuture(rpcResult);
257 public ListenableFuture<RpcResult<GetHealthCheckOutput>> getHealthCheck(
258 GetHealthCheckInput input) {
259 log.debug("Start of getHealthCheck");
260 String uri = nearRicUrlProvider.getHealthCheck();
261 Optional<String> heathCheckStatus = restAdapter.get(uri, String.class);
262 GetHealthCheckOutputBuilder responseBuilder = new GetHealthCheckOutputBuilder();
263 if (heathCheckStatus.get().equals("")) {
264 responseBuilder.setHealthStatus(true);
266 log.debug("End of getHealthCheck");
267 RpcResult<GetHealthCheckOutput> rpcResult = RpcResultBuilder.<GetHealthCheckOutput>status(true)
268 .withResult(responseBuilder.build()).build();
269 return Futures.immediateFuture(rpcResult);
273 public ListenableFuture<RpcResult<GetNearRTRICsOutput>> getNearRTRICs(GetNearRTRICsInput input) {
274 // TODO Auto-generated method stub
279 public ListenableFuture<RpcResult<GetPolicyInstanceOutput>> getPolicyInstance(
280 GetPolicyInstanceInput input) {
281 log.debug("Start of getPolicyInstance");
282 String uri = nearRicUrlProvider.getPolicyInstanceId(String.valueOf(input.getPolicyTypeId()),
283 String.valueOf(input.getPolicyInstanceId()));
284 Optional<String> policyInstance = restAdapter.get(uri, String.class);
285 GetPolicyInstanceOutputBuilder responseBuilder = new GetPolicyInstanceOutputBuilder();
287 .setPolicyInstance(policyInstance.isPresent() ? policyInstance.get() : StringUtils.EMPTY);
288 log.debug("End of getPolicyInstance");
289 RpcResult<GetPolicyInstanceOutput> rpcResult = RpcResultBuilder
290 .<GetPolicyInstanceOutput>status(true).withResult(responseBuilder.build()).build();
291 return Futures.immediateFuture(rpcResult);
295 public ListenableFuture<RpcResult<GetPolicyInstancesOutput>> getPolicyInstances(
296 GetPolicyInstancesInput input) {
297 log.debug("Start of getPolicyInstances");
298 String uri = nearRicUrlProvider.getPolicyInstances(String.valueOf(input.getPolicyTypeId()));
299 Optional<List<String>> policyInstances = restAdapter.get(uri, List.class);
300 GetPolicyInstancesOutputBuilder responseBuilder = new GetPolicyInstancesOutputBuilder();
301 if (policyInstances.isPresent()) {
302 responseBuilder.setPolicyInstanceIdList(policyInstances.get());
304 log.debug("End of getPolicyInstances");
305 RpcResult<GetPolicyInstancesOutput> rpcResult = RpcResultBuilder
306 .<GetPolicyInstancesOutput>status(true).withResult(responseBuilder.build()).build();
307 return Futures.immediateFuture(rpcResult);
311 public ListenableFuture<RpcResult<GetPolicyTypeOutput>> getPolicyType(GetPolicyTypeInput input) {
312 log.debug("Start of getPolicyType");
313 log.debug("Policy Type Id : ", input.getPolicyTypeId());
314 String uri = nearRicUrlProvider.getPolicyTypeId(String.valueOf(input.getPolicyTypeId()));
315 Optional<PolicyTypeSchema> policyTypeSchema = restAdapter.get(uri, PolicyTypeSchema.class);
316 GetPolicyTypeOutputBuilder responseBuilder = new GetPolicyTypeOutputBuilder();
317 if (policyTypeSchema.isPresent()) {
318 responseBuilder.setDescription(policyTypeSchema.get().getDescription());
319 responseBuilder.setName(policyTypeSchema.get().getName());
320 responseBuilder.setPolicyType(policyTypeSchema.get().getCreateSchema().toString());
322 log.debug("End of getPolicyType");
323 RpcResult<GetPolicyTypeOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeOutput>status(true)
324 .withResult(responseBuilder.build()).build();
325 return Futures.immediateFuture(rpcResult);
329 public ListenableFuture<RpcResult<GetPolicyTypesOutput>> getPolicyTypes(
330 GetPolicyTypesInput input) {
331 log.debug("Start of getPolicyTypes");
332 String uri = nearRicUrlProvider.getPolicyTypes();
333 Optional<List<Long>> policyTypes = restAdapter.get(uri, List.class);
334 GetPolicyTypesOutputBuilder responseBuilder = new GetPolicyTypesOutputBuilder();
335 responseBuilder.setPolicyTypeIdList(policyTypes.get());
336 log.debug("End of getPolicyTypes");
337 RpcResult<GetPolicyTypesOutput> rpcResult = RpcResultBuilder.<GetPolicyTypesOutput>status(true)
338 .withResult(responseBuilder.build()).build();
339 return Futures.immediateFuture(rpcResult);
343 public ListenableFuture<RpcResult<GetStatusOutput>> getStatus(GetStatusInput input) {
344 log.debug("Start of getStatus");
345 String uri = nearRicUrlProvider.getPolicyInstanceIdStatus(
346 String.valueOf(input.getPolicyTypeId()), String.valueOf(input.getPolicyInstanceId()));
347 Optional<List<String>> policyTypes = restAdapter.get(uri, List.class);
348 GetStatusOutputBuilder responseBuilder = new GetStatusOutputBuilder();
351 * No Schema defined for the response so Identify a way to parse the schema or create the java
354 responseBuilder.setStatus(RESPONSE_SUCCESS);
355 log.debug("End of getStatus");
356 RpcResult<GetStatusOutput> rpcResult =
357 RpcResultBuilder.<GetStatusOutput>status(true).withResult(responseBuilder.build()).build();
358 return Futures.immediateFuture(rpcResult);