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 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.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.TimeZone;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
34 import org.onap.sdnc.northbound.restadapter.NearRicUrlProvider;
35 import org.onap.sdnc.northbound.restadapter.RestAdapter;
36 import org.onap.sdnc.northbound.restadapter.RestAdapterImpl;
37 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
38 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
39 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
40 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
41 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
42 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
43 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.A1ADAPTERAPIService;
44 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeletePolicyInput;
45 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeletePolicyOutput;
46 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.DeletePolicyOutputBuilder;
47 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesInput;
48 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesOutput;
49 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesOutputBuilder;
50 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesInput;
51 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutput;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutputBuilder;
53 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeInput;
54 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutput;
55 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutputBuilder;
56 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyInput;
57 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutput;
58 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutputBuilder;
59 import org.opendaylight.yangtools.yang.common.RpcResult;
60 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63 import org.springframework.http.ResponseEntity;
66 * Defines a base implementation for your provider. This class overrides the generated interface
67 * from the YANG model and implements the request model for the A1 interface. This class identifies
68 * the Near-RIC throught the IP passed over the payload and calls the corresponding Near-RIC over
73 * @author lathishbabu.ganesan@est.tech
77 public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService {
79 protected static final String APP_NAME = "nonrt-ric-api";
80 protected static final String NO_SERVICE_LOGIC_ACTIVE = "No service logic active for ";
81 private static final String NON_NULL_PARAM = "non-null";
82 private static final String NULL_PARAM = "null";
84 private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class);
85 private final ExecutorService executor;
87 protected DataBroker dataBroker;
88 protected NotificationPublishService notificationService;
89 protected RpcProviderRegistry rpcRegistry;
90 protected BindingAwareBroker.RpcRegistration<?> rpcRegistration;
91 private RestAdapter restAdapter;
92 private NearRicUrlProvider nearRicUrlProvider;
94 public NonrtRicApiProvider(DataBroker dataBroker,
95 NotificationPublishService notificationPublishService,
96 RpcProviderRegistry rpcProviderRegistry) {
97 log.info("Creating provider for {}", APP_NAME);
98 executor = Executors.newFixedThreadPool(1);
99 setDataBroker(dataBroker);
100 setNotificationService(notificationPublishService);
101 setRpcRegistry(rpcProviderRegistry);
106 public void initialize() {
107 log.info("Initializing provider for {}", APP_NAME);
109 restAdapter = new RestAdapterImpl();
110 nearRicUrlProvider = new NearRicUrlProvider();
111 log.info("Initialization complete for {}", APP_NAME);
114 protected void initializeChild() {
115 // Override if you have custom initialization intelligence
119 public void close() throws Exception {
120 log.info("Closing provider for {}", APP_NAME);
122 rpcRegistration.close();
123 log.info("Successfully closed provider for {}", APP_NAME);
126 private static class Iso8601Util {
128 private static TimeZone timeZone = TimeZone.getTimeZone("UTC");
129 private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
131 private Iso8601Util() {}
134 dateFormat.setTimeZone(timeZone);
137 private static String now() {
138 return dateFormat.format(new Date());
142 public void setDataBroker(DataBroker dataBroker) {
143 this.dataBroker = dataBroker;
144 if (log.isDebugEnabled()) {
145 log.debug("DataBroker set to {}", dataBroker == null ? NULL_PARAM : NON_NULL_PARAM);
149 public void setNotificationService(NotificationPublishService notificationService) {
150 this.notificationService = notificationService;
151 if (log.isDebugEnabled()) {
152 log.debug("Notification Service set to {}",
153 notificationService == null ? NULL_PARAM : NON_NULL_PARAM);
157 public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
158 this.rpcRegistry = rpcRegistry;
159 if (log.isDebugEnabled()) {
160 log.debug("RpcRegistry set to {}", rpcRegistry == null ? NULL_PARAM : NON_NULL_PARAM);
164 private void createContainers() {
166 final WriteTransaction t = dataBroker.newReadWriteTransaction();
169 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
171 log.info("Create containers succeeded!");
173 } catch (InterruptedException | ExecutionException e) {
174 log.error("Create containers failed: ", e);
179 public ListenableFuture<RpcResult<GetPolicyTypeIdentitiesOutput>> getPolicyTypeIdentities(
180 GetPolicyTypeIdentitiesInput input) {
181 log.info("Start of getPolicyTypeIdentities");
182 GetPolicyTypeIdentitiesOutputBuilder responseBuilder = new GetPolicyTypeIdentitiesOutputBuilder();
183 String uri = nearRicUrlProvider.policyTypesUrl(String.valueOf(input.getNearRtRicUrl()));
184 ResponseEntity<List<String>> response = restAdapter.get(uri, List.class);
185 if (response.hasBody()) {
186 log.info("Response getPolicyTypeIdentities : {} ", response.getBody());
187 responseBuilder.setPolicyTypeIdList(response.getBody());
189 log.info("End of getPolicyTypeIdentities");
190 RpcResult<GetPolicyTypeIdentitiesOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeIdentitiesOutput>status(true)
191 .withResult(responseBuilder.build()).build();
192 return Futures.immediateFuture(rpcResult);
196 public ListenableFuture<RpcResult<GetPolicyIdentitiesOutput>> getPolicyIdentities(GetPolicyIdentitiesInput input) {
197 log.info("Start of getPolicyIdentities");
198 GetPolicyIdentitiesOutputBuilder responseBuilder = new GetPolicyIdentitiesOutputBuilder();
199 String uri = nearRicUrlProvider.policiesUrl(String.valueOf(input.getNearRtRicUrl()));
200 ResponseEntity<List<String>> response = restAdapter.get(uri, List.class);
201 if (response.hasBody()) {
202 log.info("Response getPolicyIdentities : {} ", response.getBody());
203 responseBuilder.setPolicyIdList(response.getBody());
205 log.info("End of getPolicyIdentities");
206 RpcResult<GetPolicyIdentitiesOutput> rpcResult = RpcResultBuilder
207 .<GetPolicyIdentitiesOutput>status(true).withResult(responseBuilder.build()).build();
208 return Futures.immediateFuture(rpcResult);
212 public ListenableFuture<RpcResult<GetPolicyTypeOutput>> getPolicyType(GetPolicyTypeInput input) {
213 log.info("Start of getPolicyType");
214 log.info("Policy Type Id : {} ", input.getPolicyTypeId());
215 GetPolicyTypeOutputBuilder responseBuilder = new GetPolicyTypeOutputBuilder();
216 String uri = nearRicUrlProvider.getPolicyTypeUrl(String.valueOf(input.getNearRtRicUrl()),
217 String.valueOf(input.getPolicyTypeId()));
218 ResponseEntity<String> response = restAdapter.get(uri, String.class);
219 if (response.hasBody()) {
220 log.info("Response getPolicyType : {} ", response.getBody());
221 responseBuilder.setPolicyType(response.getBody());
223 log.info("End of getPolicyType");
224 RpcResult<GetPolicyTypeOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeOutput>status(true)
225 .withResult(responseBuilder.build()).build();
226 return Futures.immediateFuture(rpcResult);
230 public ListenableFuture<RpcResult<PutPolicyOutput>> putPolicy(PutPolicyInput input) {
231 log.info("Start of putPolicy");
232 PutPolicyOutputBuilder responseBuilder = new PutPolicyOutputBuilder();
233 String uri = nearRicUrlProvider.putPolicyUrl(String.valueOf(input.getNearRtRicUrl()),
234 String.valueOf(input.getPolicyId()), String.valueOf(input.getPolicyTypeId()));
235 log.info("PUT Request input.getPolicy() : {} ", input.getPolicy());
236 ResponseEntity<String> response = restAdapter.put(uri, input.getPolicy(), String.class);
237 if (response.hasBody()) {
238 log.info("Response putPolicy : {} ", response.getBody());
239 responseBuilder.setReturnedPolicy(response.getBody());
241 log.info("End of putPolicy");
242 RpcResult<PutPolicyOutput> rpcResult = RpcResultBuilder
243 .<PutPolicyOutput>status(true).withResult(responseBuilder.build()).build();
244 return Futures.immediateFuture(rpcResult);
248 public ListenableFuture<RpcResult<DeletePolicyOutput>> deletePolicy(DeletePolicyInput input) {
249 log.info("Start of deletePolicy");
250 DeletePolicyOutputBuilder responseBuilder = new DeletePolicyOutputBuilder();
251 String uri = nearRicUrlProvider.deletePolicyUrl(String.valueOf(input.getNearRtRicUrl()),
252 String.valueOf(input.getPolicyId()));
253 ResponseEntity<Void> response = restAdapter.delete(uri);
254 log.info("End of deletePolicy");
255 RpcResult<DeletePolicyOutput> rpcResult = RpcResultBuilder
256 .<DeletePolicyOutput>status(true).withResult(responseBuilder.build()).build();
257 return Futures.immediateFuture(rpcResult);