c0c46a796558dee9701e53831d7f88a4ebcb74dd
[nonrtric.git] / sdnc-a1-controller / northbound / nonrt-ric-api / provider / src / main / java / org / onap / sdnc / northbound / provider / NonrtRicApiProvider.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdnc.northbound.provider;
22
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;
64
65 /**
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
69  * Rest API
70  *
71  * <pre>
72  *
73  * @author lathishbabu.ganesan@est.tech
74  *
75  */
76
77 public class NonrtRicApiProvider implements AutoCloseable, A1ADAPTERAPIService {
78
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";
83
84   private final Logger log = LoggerFactory.getLogger(NonrtRicApiProvider.class);
85   private final ExecutorService executor;
86
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;
93
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);
102     initialize();
103
104   }
105
106   public void initialize() {
107     log.info("Initializing provider for {}", APP_NAME);
108     createContainers();
109     restAdapter = new RestAdapterImpl();
110     nearRicUrlProvider = new NearRicUrlProvider();
111     log.info("Initialization complete for {}", APP_NAME);
112   }
113
114   protected void initializeChild() {
115     // Override if you have custom initialization intelligence
116   }
117
118   @Override
119   public void close() throws Exception {
120     log.info("Closing provider for {}", APP_NAME);
121     executor.shutdown();
122     rpcRegistration.close();
123     log.info("Successfully closed provider for {}", APP_NAME);
124   }
125
126   private static class Iso8601Util {
127
128     private static TimeZone timeZone = TimeZone.getTimeZone("UTC");
129     private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
130
131     private Iso8601Util() {
132       dateFormat.setTimeZone(timeZone);
133     }
134
135     private String now() {
136       return dateFormat.format(new Date());
137     }
138   }
139
140   public void setDataBroker(DataBroker dataBroker) {
141     this.dataBroker = dataBroker;
142     if (log.isDebugEnabled()) {
143       log.debug("DataBroker set to {}", dataBroker == null ? NULL_PARAM : NON_NULL_PARAM);
144     }
145   }
146
147   public void setNotificationService(NotificationPublishService notificationService) {
148     this.notificationService = notificationService;
149     if (log.isDebugEnabled()) {
150       log.debug("Notification Service set to {}",
151           notificationService == null ? NULL_PARAM : NON_NULL_PARAM);
152     }
153   }
154
155   public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
156     this.rpcRegistry = rpcRegistry;
157     if (log.isDebugEnabled()) {
158       log.debug("RpcRegistry set to {}", rpcRegistry == null ? NULL_PARAM : NON_NULL_PARAM);
159     }
160   }
161
162   private void createContainers() {
163
164     final WriteTransaction t = dataBroker.newReadWriteTransaction();
165
166     try {
167       CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
168       checkedFuture.get();
169       log.info("Create containers succeeded!");
170
171     } catch (InterruptedException | ExecutionException e) {
172       log.error("Create containers failed: ", e);
173       Thread.currentThread().interrupt();
174     }
175   }
176
177   @Override
178   public ListenableFuture<RpcResult<GetPolicyTypeIdentitiesOutput>> getPolicyTypeIdentities(
179           GetPolicyTypeIdentitiesInput input) {
180     log.info("Start of getPolicyTypeIdentities");
181     GetPolicyTypeIdentitiesOutputBuilder responseBuilder = new GetPolicyTypeIdentitiesOutputBuilder();
182     String uri = nearRicUrlProvider.policyTypesUrl(String.valueOf(input.getNearRtRicUrl()));
183     ResponseEntity<List<String>> response = restAdapter.get(uri, List.class);
184     if (response.hasBody()) {
185       log.info("Response getPolicyTypeIdentities : {} ", response.getBody());
186       responseBuilder.setPolicyTypeIdList(response.getBody());
187     }
188     log.info("End of getPolicyTypeIdentities");
189     RpcResult<GetPolicyTypeIdentitiesOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeIdentitiesOutput>status(true)
190         .withResult(responseBuilder.build()).build();
191     return Futures.immediateFuture(rpcResult);
192   }
193
194   @Override
195   public ListenableFuture<RpcResult<GetPolicyIdentitiesOutput>> getPolicyIdentities(GetPolicyIdentitiesInput input) {
196     log.info("Start of getPolicyIdentities");
197     GetPolicyIdentitiesOutputBuilder responseBuilder = new GetPolicyIdentitiesOutputBuilder();
198     String uri = nearRicUrlProvider.policiesUrl(String.valueOf(input.getNearRtRicUrl()));
199     ResponseEntity<List<String>> response = restAdapter.get(uri, List.class);
200     if (response.hasBody()) {
201       log.info("Response getPolicyIdentities : {} ", response.getBody());
202       responseBuilder.setPolicyIdList(response.getBody());
203     }
204     log.info("End of getPolicyIdentities");
205     RpcResult<GetPolicyIdentitiesOutput> rpcResult = RpcResultBuilder
206         .<GetPolicyIdentitiesOutput>status(true).withResult(responseBuilder.build()).build();
207     return Futures.immediateFuture(rpcResult);
208   }
209
210   @Override
211   public ListenableFuture<RpcResult<GetPolicyTypeOutput>> getPolicyType(GetPolicyTypeInput input) {
212     log.info("Start of getPolicyType");
213     log.info("Policy Type Id : {} ", input.getPolicyTypeId());
214     GetPolicyTypeOutputBuilder responseBuilder = new GetPolicyTypeOutputBuilder();
215     String uri = nearRicUrlProvider.getPolicyTypeUrl(String.valueOf(input.getNearRtRicUrl()),
216             String.valueOf(input.getPolicyTypeId()));
217     ResponseEntity<String> response = restAdapter.get(uri, String.class);
218     if (response.hasBody()) {
219       log.info("Response getPolicyType : {} ", response.getBody());
220       responseBuilder.setPolicyType(response.getBody());
221     }
222     log.info("End of getPolicyType");
223     RpcResult<GetPolicyTypeOutput> rpcResult = RpcResultBuilder.<GetPolicyTypeOutput>status(true)
224         .withResult(responseBuilder.build()).build();
225     return Futures.immediateFuture(rpcResult);
226   }
227
228   @Override
229   public ListenableFuture<RpcResult<PutPolicyOutput>> putPolicy(PutPolicyInput input) {
230     log.info("Start of putPolicy");
231     PutPolicyOutputBuilder responseBuilder = new PutPolicyOutputBuilder();
232     String uri = nearRicUrlProvider.putPolicyUrl(String.valueOf(input.getNearRtRicUrl()),
233             String.valueOf(input.getPolicyId()), String.valueOf(input.getPolicyTypeId()));
234     log.info("PUT Request input.getPolicy() : {} ", input.getPolicy());
235     ResponseEntity<String> response = restAdapter.put(uri, input.getPolicy(), String.class);
236     if (response.hasBody()) {
237       log.info("Response putPolicy : {} ", response.getBody());
238       responseBuilder.setReturnedPolicy(response.getBody());
239     }
240     log.info("End of putPolicy");
241     RpcResult<PutPolicyOutput> rpcResult = RpcResultBuilder
242         .<PutPolicyOutput>status(true).withResult(responseBuilder.build()).build();
243     return Futures.immediateFuture(rpcResult);
244   }
245
246   @Override
247   public ListenableFuture<RpcResult<DeletePolicyOutput>> deletePolicy(DeletePolicyInput input) {
248     log.info("Start of deletePolicy");
249     DeletePolicyOutputBuilder responseBuilder = new DeletePolicyOutputBuilder();
250     String uri = nearRicUrlProvider.deletePolicyUrl(String.valueOf(input.getNearRtRicUrl()),
251             String.valueOf(input.getPolicyId()));
252     ResponseEntity<Void> response = restAdapter.delete(uri);
253     log.info("End of deletePolicy");
254     RpcResult<DeletePolicyOutput> rpcResult = RpcResultBuilder
255         .<DeletePolicyOutput>status(true).withResult(responseBuilder.build()).build();
256     return Futures.immediateFuture(rpcResult);
257   }
258 }