optical-service package added for service create
[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                                         serviceDataBuilder.setResponseCode(errorCode);
135                                         serviceDataBuilder.setResponseMessage(errorMessage);
136
137                                 } catch (Exception e) {
138                                         LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
139                                         serviceDataBuilder.setResponseCode("500");
140                                 }
141                         } else {
142                                 LOG.error("No service LOGic active for OpticalService: '" + svcOperation + "'");
143                                 serviceDataBuilder.setResponseCode("503");
144                         }
145                 } catch (Exception e) {
146                         LOG.error("Caught exception looking for service LOGic", e);
147                         serviceDataBuilder.setResponseCode("500");
148                 }
149
150                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
151                         LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
152                 } else {
153                         LOG.info("Returned SUCCESS for " + svcOperation + " ");
154                 }
155
156                 RpcResult<OpticalServiceCreateOutput> rpcResult = RpcResultBuilder.<OpticalServiceCreateOutput>status(true)
157                                 .withResult(serviceDataBuilder.build()).build();
158                 // return error
159                 return Futures.immediateFuture(rpcResult);
160         }
161
162         @Override
163         public ListenableFuture<RpcResult<OpticalServiceDeleteOutput>> opticalServiceDelete(
164                         OpticalServiceDeleteInput input) {
165                 final String svcOperation = "optical-service-delete";
166
167                 Properties parms = new Properties();
168                 OpticalServiceDeleteOutputBuilder serviceDataBuilder = new OpticalServiceDeleteOutputBuilder();
169
170
171                 LOG.info(svcOperation + " called.");
172
173                 if (input == null) {
174                         LOG.debug("exiting " + svcOperation + " because of invalid input");
175                         serviceDataBuilder.setResponseCode("Input is null");
176                         RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
177                                         .withResult(serviceDataBuilder.build()).build();
178                         return Futures.immediateFuture(rpcResult);
179                 }
180
181                 // add input to parms
182                 LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
183                 OpticalServiceDeleteInputBuilder inputBuilder = new OpticalServiceDeleteInputBuilder(input);
184                 MdsalHelper.toProperties(parms, inputBuilder.build());
185                 Properties outputParams = new Properties();
186
187                 // Call SLI sync method
188                 try {
189
190                         if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
191                                 LOG.info("OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
192
193                                 try {
194                                         outputParams = opticalServiceClient.execute(APPLICATION_NAME, svcOperation, null, "sync",
195                                                         serviceDataBuilder, parms);
196                                         String errorCode = outputParams.getProperty("error-code");
197                                         LOG.info("Response Code" + errorCode);
198                                         String errorMessage = outputParams.getProperty("error-message");
199                                         serviceDataBuilder.setResponseCode(errorCode);
200                                         serviceDataBuilder.setResponseMessage(errorMessage);
201                                 } catch (Exception e) {
202                                         LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
203                                         serviceDataBuilder.setResponseCode("500");
204                                 }
205                         } else {
206                                 LOG.error("No service LOGic active for Oofpcipoc: '" + svcOperation + "'");
207                                 serviceDataBuilder.setResponseCode("503");
208                         }
209                 } catch (Exception e) {
210                         LOG.error("Caught exception looking for service LOGic", e);
211                         serviceDataBuilder.setResponseCode("500");
212                 }
213
214                 String errorCode = serviceDataBuilder.getResponseCode();
215
216                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
217                         LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
218                 } else {
219                         LOG.info("Returned SUCCESS for " + svcOperation + " ");
220                 }
221
222                 RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
223                                 .withResult(serviceDataBuilder.build()).build();
224
225                 return Futures.immediateFuture(rpcResult);
226         }
227 }