aboutsummaryrefslogtreecommitdiffstats
path: root/optical-service/provider/src/main/java/org/onap/sdnc/northbound/OpticalServiceProvider.java
blob: c8a80d5b4e38dac86c9e700e203203a3f83ff515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*-
 * ============LICENSE_START=======================================================
 * ONAP : SDN-C
 * ================================================================================
 * Copyright (C) 2019-2020 Fujitsu Limited Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdnc.northbound;

import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import org.onap.ccsdk.sli.core.sli.provider.MdsalHelper;
import org.opendaylight.mdsal.binding.api.DataBroker;
import org.opendaylight.mdsal.binding.api.NotificationPublishService;
import org.opendaylight.mdsal.binding.api.RpcProviderService;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateInput;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateInputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateOutput;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceCreateOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteInput;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteInputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteOutput;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalServiceDeleteOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.optical.service.rev191206.OpticalserviceService;
import org.opendaylight.yangtools.concepts.ObjectRegistration;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * Defines a base implementation for your provider. This class extends from a
 * helper class which provides storage for the most commonly used components of
 * the MD-SAL. Additionally the base class provides some basic LOGging and
 * initialization / clean up methods.
 *
 */
public class OpticalServiceProvider implements AutoCloseable, OpticalserviceService {

	private static final Logger LOG = LoggerFactory.getLogger(OpticalServiceProvider.class);

	private static final String APPLICATION_NAME = "optical-service";

	private final ExecutorService executor;

	protected DataBroker dataBroker;
	protected NotificationPublishService notificationService;
	protected RpcProviderService rpcProviderService;
	protected ObjectRegistration<OpticalserviceService> rpcRegistration;
	private final OpticalServiceClient opticalServiceClient;

	public OpticalServiceProvider(final DataBroker dataBroker,
			final NotificationPublishService notificationPublishService, final RpcProviderService rpcProviderService,
			final OpticalServiceClient opticalServiceClient) {

		LOG.info("Creating provider for {}", APPLICATION_NAME);
		executor = Executors.newFixedThreadPool(1);
		this.dataBroker = dataBroker;
		this.notificationService = notificationPublishService;
		this.rpcProviderService= rpcProviderService;
		this.opticalServiceClient = opticalServiceClient;
		initialize();
	}

	public void initialize() {
		LOG.info("Initializing provider for {}", APPLICATION_NAME);

        if (rpcRegistration == null) {
            if (rpcProviderService != null) {
                rpcRegistration = rpcProviderService.registerRpcImplementation(OpticalserviceService.class, this);
            }
        }
		LOG.info("Initialization complete for {}", APPLICATION_NAME);
	}

	protected void initializeChild() {
		// Override if you have custom initialization intelligence
	}

	@Override
	public void close() throws Exception {
		LOG.info("Closing provider for {}", APPLICATION_NAME);
		executor.shutdown();
		rpcRegistration.close();
		LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
	}

	@Override
	public ListenableFuture<RpcResult<OpticalServiceCreateOutput>> opticalServiceCreate(
			OpticalServiceCreateInput input) {
		final String svcOperation = "optical-service-create";

		Properties parms = new Properties();
		OpticalServiceCreateOutputBuilder serviceDataBuilder = new OpticalServiceCreateOutputBuilder();

		LOG.info(svcOperation + " called.");
		if (input == null) {
			LOG.debug("exiting " + svcOperation + " because of invalid input");
			serviceDataBuilder.setResponseCode("403");
			RpcResult<OpticalServiceCreateOutput> rpcResult = RpcResultBuilder.<OpticalServiceCreateOutput>status(true)
					.withResult(serviceDataBuilder.build()).build();
			return Futures.immediateFuture(rpcResult);
		}
		// add input to parms
		LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
		OpticalServiceCreateInputBuilder inputBuilder = new OpticalServiceCreateInputBuilder(input);
		MdsalHelper.toProperties(parms, inputBuilder.build());
		Properties outputParams = new Properties();
		String errorCode = null;

		// Call SLI sync method
		try {
			if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
				try {
					outputParams = opticalServiceClient.execute(APPLICATION_NAME, svcOperation, null, "sync",
							serviceDataBuilder, parms);
					errorCode = outputParams.getProperty("error-code");
					LOG.info("Response Code" + errorCode);
					String errorMessage = outputParams.getProperty("error-message");
                                        String ackFinalIndicator = outputParams.getProperty("ack-final-indicator");
					serviceDataBuilder.setResponseCode(errorCode);
					serviceDataBuilder.setResponseMessage(errorMessage);
                                        serviceDataBuilder.setAckFinalIndicator(ackFinalIndicator);

				} catch (Exception e) {
					LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
					serviceDataBuilder.setResponseCode("500");
				}
			} else {
				LOG.error("No service LOGic active for OpticalService: '" + svcOperation + "'");
				serviceDataBuilder.setResponseCode("503");
			}
		} catch (Exception e) {
			LOG.error("Caught exception looking for service LOGic", e);
			serviceDataBuilder.setResponseCode("500");
		}

		if (!("0".equals(errorCode) || "200".equals(errorCode))) {
			LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
		} else {
			LOG.info("Returned SUCCESS for " + svcOperation + " ");
		}

		RpcResult<OpticalServiceCreateOutput> rpcResult = RpcResultBuilder.<OpticalServiceCreateOutput>status(true)
				.withResult(serviceDataBuilder.build()).build();
		// return error
		return Futures.immediateFuture(rpcResult);
	}

	@Override
	public ListenableFuture<RpcResult<OpticalServiceDeleteOutput>> opticalServiceDelete(
			OpticalServiceDeleteInput input) {
		final String svcOperation = "optical-service-delete";

		Properties parms = new Properties();
		OpticalServiceDeleteOutputBuilder serviceDataBuilder = new OpticalServiceDeleteOutputBuilder();


		LOG.info(svcOperation + " called.");

		if (input == null) {
			LOG.debug("exiting " + svcOperation + " because of invalid input");
			serviceDataBuilder.setResponseCode("Input is null");
			RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
					.withResult(serviceDataBuilder.build()).build();
			return Futures.immediateFuture(rpcResult);
		}

		// add input to parms
		LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
		OpticalServiceDeleteInputBuilder inputBuilder = new OpticalServiceDeleteInputBuilder(input);
		MdsalHelper.toProperties(parms, inputBuilder.build());
		Properties outputParams = new Properties();

		// Call SLI sync method
		try {

			if (opticalServiceClient.hasGraph(APPLICATION_NAME, svcOperation, null, "sync")) {
				LOG.info("OofpcipocClient has a Directed Graph for '" + svcOperation + "'");

				try {
					outputParams = opticalServiceClient.execute(APPLICATION_NAME, svcOperation, null, "sync",
							serviceDataBuilder, parms);
					String errorCode = outputParams.getProperty("error-code");
					LOG.info("Response Code" + errorCode);
                                        String errorMessage = outputParams.getProperty("error-message");
                                        String ackFinalIndicator = outputParams.getProperty("ack-final-indicator");
                                        serviceDataBuilder.setResponseCode(errorCode);
                                        serviceDataBuilder.setResponseMessage(errorMessage);
                                        serviceDataBuilder.setAckFinalIndicator(ackFinalIndicator);
					
				} catch (Exception e) {
					LOG.error("Caught exception executing service LOGic for " + svcOperation, e);
					serviceDataBuilder.setResponseCode("500");
				}
			} else {
				LOG.error("No service LOGic active for Oofpcipoc: '" + svcOperation + "'");
				serviceDataBuilder.setResponseCode("503");
			}
		} catch (Exception e) {
			LOG.error("Caught exception looking for service LOGic", e);
			serviceDataBuilder.setResponseCode("500");
		}

		String errorCode = serviceDataBuilder.getResponseCode();

		if (!("0".equals(errorCode) || "200".equals(errorCode))) {
			LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
		} else {
			LOG.info("Returned SUCCESS for " + svcOperation + " ");
		}

		RpcResult<OpticalServiceDeleteOutput> rpcResult = RpcResultBuilder.<OpticalServiceDeleteOutput>status(true)
				.withResult(serviceDataBuilder.build()).build();

		return Futures.immediateFuture(rpcResult);
	}
}