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 com.google.common.util.concurrent.Futures;
29 import com.google.common.util.concurrent.ListenableFuture;
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;
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.
56 public class OpticalServiceProvider implements AutoCloseable, OpticalserviceService {
58 private static final Logger LOG = LoggerFactory.getLogger(OpticalServiceProvider.class);
60 private static final String APPLICATION_NAME = "optical-service";
62 private final ExecutorService executor;
64 protected DataBroker dataBroker;
65 protected NotificationPublishService notificationService;
66 protected RpcProviderService rpcProviderService;
67 protected ObjectRegistration<OpticalserviceService> rpcRegistration;
68 private final OpticalServiceClient opticalServiceClient;
70 public OpticalServiceProvider(final DataBroker dataBroker,
71 final NotificationPublishService notificationPublishService, final RpcProviderService rpcProviderService,
72 final OpticalServiceClient opticalServiceClient) {
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;
83 public void initialize() {
84 LOG.info("Initializing provider for {}", APPLICATION_NAME);
86 if (rpcRegistration == null) {
87 if (rpcProviderService != null) {
88 rpcRegistration = rpcProviderService.registerRpcImplementation(OpticalserviceService.class, this);
91 LOG.info("Initialization complete for {}", APPLICATION_NAME);
94 protected void initializeChild() {
95 // Override if you have custom initialization intelligence
99 public void close() throws Exception {
100 LOG.info("Closing provider for {}", APPLICATION_NAME);
102 rpcRegistration.close();
103 LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
107 public ListenableFuture<RpcResult<OpticalServiceCreateOutput>> opticalServiceCreate(
108 OpticalServiceCreateInput input) {
109 final String svcOperation = "optical-service-create";
111 Properties parms = new Properties();
112 OpticalServiceCreateOutputBuilder serviceDataBuilder = new OpticalServiceCreateOutputBuilder();
114 LOG.info(svcOperation + " called.");
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);
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;
129 // Call SLI sync method
131 if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
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);
143 } catch (Exception e) {
144 LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
145 serviceDataBuilder.setResponseCode("500");
148 LOG.error("No service LOGic active for OpticalService: '" + svcOperation + "'");
149 serviceDataBuilder.setResponseCode("503");
151 } catch (Exception e) {
152 LOG.error("Caught exception looking for service LOGic", e);
153 serviceDataBuilder.setResponseCode("500");
156 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
157 LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
159 LOG.info("Returned SUCCESS for " + svcOperation + " ");
162 RpcResult<OpticalServiceCreateOutput> rpcResult = RpcResultBuilder.<OpticalServiceCreateOutput>status(true)
163 .withResult(serviceDataBuilder.build()).build();
165 return Futures.immediateFuture(rpcResult);
169 public ListenableFuture<RpcResult<OpticalServiceDeleteOutput>> opticalServiceDelete(
170 OpticalServiceDeleteInput input) {
171 final String svcOperation = "optical-service-delete";
173 Properties parms = new Properties();
174 OpticalServiceDeleteOutputBuilder serviceDataBuilder = new OpticalServiceDeleteOutputBuilder();
177 LOG.info(svcOperation + " called.");
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);
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();
193 // Call SLI sync method
196 if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
197 LOG.info("OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
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);
210 } catch (Exception e) {
211 LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
212 serviceDataBuilder.setResponseCode("500");
215 LOG.error("No service LOGic active for Oofpcipoc: '" + svcOperation + "'");
216 serviceDataBuilder.setResponseCode("503");
218 } catch (Exception e) {
219 LOG.error("Caught exception looking for service LOGic", e);
220 serviceDataBuilder.setResponseCode("500");
223 String errorCode = serviceDataBuilder.getResponseCode();
225 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
226 LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
228 LOG.info("Returned SUCCESS for " + svcOperation + " ");
231 RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
232 .withResult(serviceDataBuilder.build()).build();
234 return Futures.immediateFuture(rpcResult);