2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 Fujitsu Limited Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.sdnc.northbound;
24 import java.util.Properties;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
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;
47 import com.google.common.util.concurrent.Futures;
48 import com.google.common.util.concurrent.ListenableFuture;
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.
57 public class OpticalServiceProvider implements AutoCloseable, OpticalserviceService {
59 private static final Logger LOG = LoggerFactory.getLogger(OpticalServiceProvider.class);
61 private static final String APPLICATION_NAME = "optical-service";
63 private final ExecutorService executor;
65 protected DataBroker dataBroker;
66 protected NotificationPublishService notificationService;
67 protected RpcProviderRegistry rpcRegistry;
68 protected BindingAwareBroker.RpcRegistration<OpticalserviceService> rpcRegistration;
69 private final OpticalServiceClient opticalServiceClient;
71 public OpticalServiceProvider(final DataBroker dataBroker,
72 final NotificationPublishService notificationPublishService, final RpcProviderRegistry rpcProviderRegistry,
73 final OpticalServiceClient opticalServiceClient) {
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;
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);
90 protected void initializeChild() {
91 // Override if you have custom initialization intelligence
95 public void close() throws Exception {
96 LOG.info("Closing provider for {}", APPLICATION_NAME);
98 rpcRegistration.close();
99 LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
103 public ListenableFuture<RpcResult<OpticalServiceCreateOutput>> opticalServiceCreate(
104 OpticalServiceCreateInput input) {
105 final String svcOperation = "optical-service-create";
107 Properties parms = new Properties();
108 OpticalServiceCreateOutputBuilder serviceDataBuilder = new OpticalServiceCreateOutputBuilder();
110 LOG.info(svcOperation + " called.");
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);
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;
125 // Call SLI sync method
127 if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
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);
137 } catch (Exception e) {
138 LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
139 serviceDataBuilder.setResponseCode("500");
142 LOG.error("No service LOGic active for OpticalService: '" + svcOperation + "'");
143 serviceDataBuilder.setResponseCode("503");
145 } catch (Exception e) {
146 LOG.error("Caught exception looking for service LOGic", e);
147 serviceDataBuilder.setResponseCode("500");
150 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
151 LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
153 LOG.info("Returned SUCCESS for " + svcOperation + " ");
156 RpcResult<OpticalServiceCreateOutput> rpcResult = RpcResultBuilder.<OpticalServiceCreateOutput>status(true)
157 .withResult(serviceDataBuilder.build()).build();
159 return Futures.immediateFuture(rpcResult);
163 public ListenableFuture<RpcResult<OpticalServiceDeleteOutput>> opticalServiceDelete(
164 OpticalServiceDeleteInput input) {
165 final String svcOperation = "optical-service-delete";
167 Properties parms = new Properties();
168 OpticalServiceDeleteOutputBuilder serviceDataBuilder = new OpticalServiceDeleteOutputBuilder();
171 LOG.info(svcOperation + " called.");
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);
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();
187 // Call SLI sync method
190 if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
191 LOG.info("OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
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");
206 LOG.error("No service LOGic active for Oofpcipoc: '" + svcOperation + "'");
207 serviceDataBuilder.setResponseCode("503");
209 } catch (Exception e) {
210 LOG.error("Caught exception looking for service LOGic", e);
211 serviceDataBuilder.setResponseCode("500");
214 String errorCode = serviceDataBuilder.getResponseCode();
216 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
217 LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
219 LOG.info("Returned SUCCESS for " + svcOperation + " ");
222 RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
223 .withResult(serviceDataBuilder.build()).build();
225 return Futures.immediateFuture(rpcResult);