Upgrade to ODL Aluminum
[sdnc/northbound.git] / optical-service / provider / src / main / java / org / onap / sdnc / northbound / OpticalServiceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2019-2020 Fujitsu Limited Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.sdnc.northbound;
23
24 import java.util.Properties;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27
28 import com.google.common.util.concurrent.Futures;
29 import com.google.common.util.concurrent.ListenableFuture;
30
31 import org.onap.ccsdk.sli.core.sli.provider.MdsalHelper;
32 import org.opendaylight.mdsal.binding.api.DataBroker;
33 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
34 import org.opendaylight.mdsal.binding.api.RpcProviderService;
35 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateInput;
36 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateInputBuilder;
37 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateOutput;
38 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateOutputBuilder;
39 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteInput;
40 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteInputBuilder;
41 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteOutput;
42 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteOutputBuilder;
43 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalserviceService;
44 import org.opendaylight.yangtools.concepts.ObjectRegistration;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 /**
50  * Defines a base implementation for your provider. This class extends from a
51  * helper class which provides storage for the most commonly used components of
52  * the MD-SAL. Additionally the base class provides some basic LOGging and
53  * initialization / clean up methods.
54  *
55  */
56 public class OpticalServiceProvider implements AutoCloseable, OpticalserviceService {
57
58         private static final Logger LOG = LoggerFactory.getLogger(OpticalServiceProvider.class);
59
60         private static final String APPLICATION_NAME = "optical-service";
61
62         private final ExecutorService executor;
63
64         protected DataBroker dataBroker;
65         protected NotificationPublishService notificationService;
66         protected RpcProviderService rpcProviderService;
67         protected ObjectRegistration<OpticalserviceService> rpcRegistration;
68         private final OpticalServiceClient opticalServiceClient;
69
70         public OpticalServiceProvider(final DataBroker dataBroker,
71                         final NotificationPublishService notificationPublishService, final RpcProviderService rpcProviderService,
72                         final OpticalServiceClient opticalServiceClient) {
73
74                 LOG.info("Creating provider for {}", APPLICATION_NAME);
75                 executor = Executors.newFixedThreadPool(1);
76                 this.dataBroker = dataBroker;
77                 this.notificationService = notificationPublishService;
78                 this.rpcProviderService= rpcProviderService;
79                 this.opticalServiceClient = opticalServiceClient;
80                 initialize();
81         }
82
83         public void initialize() {
84                 LOG.info("Initializing provider for {}", APPLICATION_NAME);
85
86         if (rpcRegistration == null) {
87             if (rpcProviderService != null) {
88                 rpcRegistration = rpcProviderService.registerRpcImplementation(OpticalserviceService.class, this);
89             }
90         }
91                 LOG.info("Initialization complete for {}", APPLICATION_NAME);
92         }
93
94         protected void initializeChild() {
95                 // Override if you have custom initialization intelligence
96         }
97
98         @Override
99         public void close() throws Exception {
100                 LOG.info("Closing provider for {}", APPLICATION_NAME);
101                 executor.shutdown();
102                 rpcRegistration.close();
103                 LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
104         }
105
106         @Override
107         public ListenableFuture<RpcResult<OpticalServiceCreateOutput>> opticalServiceCreate(
108                         OpticalServiceCreateInput input) {
109                 final String svcOperation = "optical-service-create";
110
111                 Properties parms = new Properties();
112                 OpticalServiceCreateOutputBuilder serviceDataBuilder = new OpticalServiceCreateOutputBuilder();
113
114                 LOG.info(svcOperation + " called.");
115                 if (input == null) {
116                         LOG.debug("exiting " + svcOperation + " because of invalid input");
117                         serviceDataBuilder.setResponseCode("403");
118                         RpcResult<OpticalServiceCreateOutput> rpcResult = RpcResultBuilder.<OpticalServiceCreateOutput>status(true)
119                                         .withResult(serviceDataBuilder.build()).build();
120                         return Futures.immediateFuture(rpcResult);
121                 }
122                 // add input to parms
123                 LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
124                 OpticalServiceCreateInputBuilder inputBuilder = new OpticalServiceCreateInputBuilder(input);
125                 MdsalHelper.toProperties(parms, inputBuilder.build());
126                 Properties outputParams = new Properties();
127                 String errorCode = null;
128
129                 // Call SLI sync method
130                 try {
131                         if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
132                                 try {
133                                         outputParams = opticalServiceClient.execute(APPLICATION_NAME, svcOperation, null, "sync",
134                                                         serviceDataBuilder, parms);
135                                         errorCode = outputParams.getProperty("error-code");
136                                         LOG.info("Response Code" + errorCode);
137                                         String errorMessage = outputParams.getProperty("error-message");
138                                         String ackFinalIndicator = outputParams.getProperty("ack-final-indicator");
139                                         serviceDataBuilder.setResponseCode(errorCode);
140                                         serviceDataBuilder.setResponseMessage(errorMessage);
141                                         serviceDataBuilder.setAckFinalIndicator(ackFinalIndicator);
142
143                                 } catch (Exception e) {
144                                         LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
145                                         serviceDataBuilder.setResponseCode("500");
146                                 }
147                         } else {
148                                 LOG.error("No service LOGic active for OpticalService: '" + svcOperation + "'");
149                                 serviceDataBuilder.setResponseCode("503");
150                         }
151                 } catch (Exception e) {
152                         LOG.error("Caught exception looking for service LOGic", e);
153                         serviceDataBuilder.setResponseCode("500");
154                 }
155
156                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
157                         LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
158                 } else {
159                         LOG.info("Returned SUCCESS for " + svcOperation + " ");
160                 }
161
162                 RpcResult<OpticalServiceCreateOutput> rpcResult = RpcResultBuilder.<OpticalServiceCreateOutput>status(true)
163                                 .withResult(serviceDataBuilder.build()).build();
164                 // return error
165                 return Futures.immediateFuture(rpcResult);
166         }
167
168         @Override
169         public ListenableFuture<RpcResult<OpticalServiceDeleteOutput>> opticalServiceDelete(
170                         OpticalServiceDeleteInput input) {
171                 final String svcOperation = "optical-service-delete";
172
173                 Properties parms = new Properties();
174                 OpticalServiceDeleteOutputBuilder serviceDataBuilder = new OpticalServiceDeleteOutputBuilder();
175
176
177                 LOG.info(svcOperation + " called.");
178
179                 if (input == null) {
180                         LOG.debug("exiting " + svcOperation + " because of invalid input");
181                         serviceDataBuilder.setResponseCode("Input is null");
182                         RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
183                                         .withResult(serviceDataBuilder.build()).build();
184                         return Futures.immediateFuture(rpcResult);
185                 }
186
187                 // add input to parms
188                 LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
189                 OpticalServiceDeleteInputBuilder inputBuilder = new OpticalServiceDeleteInputBuilder(input);
190                 MdsalHelper.toProperties(parms, inputBuilder.build());
191                 Properties outputParams = new Properties();
192
193                 // Call SLI sync method
194                 try {
195
196                         if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
197                                 LOG.info("OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
198
199                                 try {
200                                         outputParams = opticalServiceClient.execute(APPLICATION_NAME, svcOperation, null, "sync",
201                                                         serviceDataBuilder, parms);
202                                         String errorCode = outputParams.getProperty("error-code");
203                                         LOG.info("Response Code" + errorCode);
204                                         String errorMessage = outputParams.getProperty("error-message");
205                                         String ackFinalIndicator = outputParams.getProperty("ack-final-indicator");
206                                         serviceDataBuilder.setResponseCode(errorCode);
207                                         serviceDataBuilder.setResponseMessage(errorMessage);
208                                         serviceDataBuilder.setAckFinalIndicator(ackFinalIndicator);
209                                         
210                                 } catch (Exception e) {
211                                         LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
212                                         serviceDataBuilder.setResponseCode("500");
213                                 }
214                         } else {
215                                 LOG.error("No service LOGic active for Oofpcipoc: '" + svcOperation + "'");
216                                 serviceDataBuilder.setResponseCode("503");
217                         }
218                 } catch (Exception e) {
219                         LOG.error("Caught exception looking for service LOGic", e);
220                         serviceDataBuilder.setResponseCode("500");
221                 }
222
223                 String errorCode = serviceDataBuilder.getResponseCode();
224
225                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
226                         LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
227                 } else {
228                         LOG.info("Returned SUCCESS for " + svcOperation + " ");
229                 }
230
231                 RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
232                                 .withResult(serviceDataBuilder.build()).build();
233
234                 return Futures.immediateFuture(rpcResult);
235         }
236 }