931b28ad15b693d85d5ebf6a26dd4477a568dcba
[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 org.onap.ccsdk.sli.core.sli.provider.MdsalHelper;
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.BindingAwareBroker;
32 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
33 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateInput;
34 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateInputBuilder;
35 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateOutput;
36 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateOutputBuilder;
37 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteInput;
38 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteInputBuilder;
39 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteOutput;
40 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteOutputBuilder;
41 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalserviceService;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import com.google.common.util.concurrent.Futures;
48 import com.google.common.util.concurrent.ListenableFuture;
49
50 /**
51  * Defines a base implementation for your provider. This class extends from a
52  * helper class which provides storage for the most commonly used components of
53  * the MD-SAL. Additionally the base class provides some basic LOGging and
54  * initialization / clean up methods.
55  *
56  */
57 public class OpticalServiceProvider implements AutoCloseable, OpticalserviceService {
58
59         private static final Logger LOG = LoggerFactory.getLogger(OpticalServiceProvider.class);
60
61         private static final String APPLICATION_NAME = "optical-service";
62
63         private final ExecutorService executor;
64
65         protected DataBroker dataBroker;
66         protected NotificationPublishService notificationService;
67         protected RpcProviderRegistry rpcRegistry;
68         protected BindingAwareBroker.RpcRegistration<OpticalserviceService> rpcRegistration;
69         private final OpticalServiceClient opticalServiceClient;
70
71         public OpticalServiceProvider(final DataBroker dataBroker,
72                         final NotificationPublishService notificationPublishService, final RpcProviderRegistry rpcProviderRegistry,
73                         final OpticalServiceClient opticalServiceClient) {
74
75                 LOG.info("Creating provider for {}", APPLICATION_NAME);
76                 executor = Executors.newFixedThreadPool(1);
77                 this.dataBroker = dataBroker;
78                 this.notificationService = notificationPublishService;
79                 this.rpcRegistry = rpcProviderRegistry;
80                 this.opticalServiceClient = opticalServiceClient;
81                 initialize();
82         }
83
84         public void initialize() {
85                 LOG.info("Initializing provider for {}", APPLICATION_NAME);
86                 rpcRegistration = rpcRegistry.addRpcImplementation(OpticalserviceService.class, this);
87                 LOG.info("Initialization complete for {}", APPLICATION_NAME);
88         }
89
90         protected void initializeChild() {
91                 // Override if you have custom initialization intelligence
92         }
93
94         @Override
95         public void close() throws Exception {
96                 LOG.info("Closing provider for {}", APPLICATION_NAME);
97                 executor.shutdown();
98                 rpcRegistration.close();
99                 LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
100         }
101
102         @Override
103         public ListenableFuture<RpcResult<OpticalServiceCreateOutput>> opticalServiceCreate(
104                         OpticalServiceCreateInput input) {
105                 final String svcOperation = "optical-service-create";
106
107                 Properties parms = new Properties();
108                 OpticalServiceCreateOutputBuilder serviceDataBuilder = new OpticalServiceCreateOutputBuilder();
109
110                 LOG.info(svcOperation + " called.");
111                 if (input == null) {
112                         LOG.debug("exiting " + svcOperation + " because of invalid input");
113                         serviceDataBuilder.setResponseCode("403");
114                         RpcResult<OpticalServiceCreateOutput> rpcResult = RpcResultBuilder.<OpticalServiceCreateOutput>status(true)
115                                         .withResult(serviceDataBuilder.build()).build();
116                         return Futures.immediateFuture(rpcResult);
117                 }
118                 // add input to parms
119                 LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
120                 OpticalServiceCreateInputBuilder inputBuilder = new OpticalServiceCreateInputBuilder(input);
121                 MdsalHelper.toProperties(parms, inputBuilder.build());
122                 Properties outputParams = new Properties();
123                 String errorCode = null;
124
125                 // Call SLI sync method
126                 try {
127                         if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
128                                 try {
129                                         outputParams = opticalServiceClient.execute(APPLICATION_NAME, svcOperation, null, "sync",
130                                                         serviceDataBuilder, parms);
131                                         errorCode = outputParams.getProperty("error-code");
132                                         LOG.info("Response Code" + errorCode);
133                                         String errorMessage = outputParams.getProperty("error-message");
134                                         String ackFinalIndicator = outputParams.getProperty("ack-final-indicator");
135                                         serviceDataBuilder.setResponseCode(errorCode);
136                                         serviceDataBuilder.setResponseMessage(errorMessage);
137                                         serviceDataBuilder.setAckFinalIndicator(ackFinalIndicator);
138
139                                 } catch (Exception e) {
140                                         LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
141                                         serviceDataBuilder.setResponseCode("500");
142                                 }
143                         } else {
144                                 LOG.error("No service LOGic active for OpticalService: '" + svcOperation + "'");
145                                 serviceDataBuilder.setResponseCode("503");
146                         }
147                 } catch (Exception e) {
148                         LOG.error("Caught exception looking for service LOGic", e);
149                         serviceDataBuilder.setResponseCode("500");
150                 }
151
152                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
153                         LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
154                 } else {
155                         LOG.info("Returned SUCCESS for " + svcOperation + " ");
156                 }
157
158                 RpcResult<OpticalServiceCreateOutput> rpcResult = RpcResultBuilder.<OpticalServiceCreateOutput>status(true)
159                                 .withResult(serviceDataBuilder.build()).build();
160                 // return error
161                 return Futures.immediateFuture(rpcResult);
162         }
163
164         @Override
165         public ListenableFuture<RpcResult<OpticalServiceDeleteOutput>> opticalServiceDelete(
166                         OpticalServiceDeleteInput input) {
167                 final String svcOperation = "optical-service-delete";
168
169                 Properties parms = new Properties();
170                 OpticalServiceDeleteOutputBuilder serviceDataBuilder = new OpticalServiceDeleteOutputBuilder();
171
172
173                 LOG.info(svcOperation + " called.");
174
175                 if (input == null) {
176                         LOG.debug("exiting " + svcOperation + " because of invalid input");
177                         serviceDataBuilder.setResponseCode("Input is null");
178                         RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
179                                         .withResult(serviceDataBuilder.build()).build();
180                         return Futures.immediateFuture(rpcResult);
181                 }
182
183                 // add input to parms
184                 LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
185                 OpticalServiceDeleteInputBuilder inputBuilder = new OpticalServiceDeleteInputBuilder(input);
186                 MdsalHelper.toProperties(parms, inputBuilder.build());
187                 Properties outputParams = new Properties();
188
189                 // Call SLI sync method
190                 try {
191
192                         if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
193                                 LOG.info("OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
194
195                                 try {
196                                         outputParams = opticalServiceClient.execute(APPLICATION_NAME, svcOperation, null, "sync",
197                                                         serviceDataBuilder, parms);
198                                         String errorCode = outputParams.getProperty("error-code");
199                                         LOG.info("Response Code" + errorCode);
200                                         String errorMessage = outputParams.getProperty("error-message");
201                                         String ackFinalIndicator = outputParams.getProperty("ack-final-indicator");
202                                         serviceDataBuilder.setResponseCode(errorCode);
203                                         serviceDataBuilder.setResponseMessage(errorMessage);
204                                         serviceDataBuilder.setAckFinalIndicator(ackFinalIndicator);
205                                         
206                                 } catch (Exception e) {
207                                         LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
208                                         serviceDataBuilder.setResponseCode("500");
209                                 }
210                         } else {
211                                 LOG.error("No service LOGic active for Oofpcipoc: '" + svcOperation + "'");
212                                 serviceDataBuilder.setResponseCode("503");
213                         }
214                 } catch (Exception e) {
215                         LOG.error("Caught exception looking for service LOGic", e);
216                         serviceDataBuilder.setResponseCode("500");
217                 }
218
219                 String errorCode = serviceDataBuilder.getResponseCode();
220
221                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
222                         LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
223                 } else {
224                         LOG.info("Returned SUCCESS for " + svcOperation + " ");
225                 }
226
227                 RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
228                                 .withResult(serviceDataBuilder.build()).build();
229
230                 return Futures.immediateFuture(rpcResult);
231         }
232 }