aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/northbound/oofpcipoc/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/oofpcipoc/OofpcipocProvider.java
blob: 6bfa74eeab045fb87ab04422bb2204619633c472 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T 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.ccsdk.features.sdnr.northbound.oofpcipoc;

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

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.ccsdk.rev190308.*;
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;

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

/**
 * 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 OofpcipocProvider implements AutoCloseable, OofpcipocApiService {

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

	private static final String APPLICATION_NAME = "oofpcipoc-api";

	private final ExecutorService executor;

	private DataBroker dataBroker;
	private NotificationPublishService notificationService;
	private RpcProviderService rpcProviderService;
	private ObjectRegistration<OofpcipocProvider> rpcRegistration;
	private OofpcipocClient OofpcipocClient;

	public OofpcipocProvider() {

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

	public void setDataBroker(DataBroker dataBroker) {
		this.dataBroker = dataBroker;
	}

	public void setRpcProviderService(RpcProviderService rpcProviderRegistry) {
		this.rpcProviderService = rpcProviderRegistry;
	}

	public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
		this.notificationService = notificationPublishService;
	}

	public void setClient(OofpcipocClient client) {
		this.OofpcipocClient = client;
	}

	public void init() {
		LOG.info("Initializing provider for {}", APPLICATION_NAME);
		rpcRegistration = rpcProviderService.registerRpcImplementation(OofpcipocApiService.class, this);
		LOG.info("Initialization complete for {}", APPLICATION_NAME);
	}

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

	@Override
	public ListenableFuture<RpcResult<GreetingOutput>> greeting(GreetingInput input) {
		final String svcOperation = "greeting";

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

		LOG.info("Reached RPC greeting");

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

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

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

		// Call SLI sync method
		try {
			if (OofpcipocClient.hasGraph("oofpcipoc-api", svcOperation, null, "sync")) {
				LOG.info("OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
				try {
					OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
				} catch (Exception e) {
					LOG.error("Caught exception executing service logic for " + svcOperation, e);
					serviceDataBuilder.setResponse("500");
				}
			} else {
				LOG.error("No service logic active for Oofpcipoc: '" + svcOperation + "'");
				serviceDataBuilder.setResponse("503");
			}
		} catch (Exception e) {
			LOG.error("Caught exception looking for service logic", e);
			serviceDataBuilder.setResponse("500");
		}

		String errorCode = serviceDataBuilder.getResponse();

		if (!("0".equals(errorCode) || "200".equals(errorCode))) {
			LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
		} else {
			LOG.info("Returned SUCCESS for " + svcOperation + " ");
			serviceDataBuilder.setResponse("Welcome OOF POC " + input.getSalutation());
		}

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

		LOG.info("Successful exit from greeting ");

		return Futures.immediateFuture(rpcResult);
	}

// RPC configuration-phy-cell-id
	@Override
	public ListenableFuture<RpcResult<ConfigurationPhyCellIdOutput>> configurationPhyCellId(
			ConfigurationPhyCellIdInput input) {
		final String svcOperation = "configuration-phy-cell-id";

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

		LOG.info("Reached RPC configurationPhyCellId");

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

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

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

		// Call SLI sync method
		try {

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

				try {
					OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
				} 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 + " ");
			serviceDataBuilder
					.setResponseCode("Welcome OOF POC. Number of FAP entries " + input.getFapServiceNumberOfEntries());
		}

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

		return Futures.immediateFuture(rpcResult);
	}

	// RPC add-neighbor
	@Override
	public ListenableFuture<RpcResult<AddNeighborOutput>> addNeighbor(AddNeighborInput input) {
		final String svcOperation = "add-neighbor";

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

		LOG.info("Reached RPC addNeighbor");

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

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

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

		// Call SLI sync method
		try {

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

				try {
					OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
				} 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 + " ");
			serviceDataBuilder.setResponseCode(
					"Welcome OOF POC. Number of Neighbor entries to be added " + input.getLteCellNumberOfEntries());
		}

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

		return Futures.immediateFuture(rpcResult);
	}

	// RPC delete-neighbor
	@Override
	public ListenableFuture<RpcResult<DeleteNeighborOutput>> deleteNeighbor(DeleteNeighborInput input) {
		final String svcOperation = "delete-neighbor";

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

		LOG.info("Reached RPC deleteNeighbor");

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

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

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

		// Call SLI sync method
		try {

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

				try {
					OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
				} 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 + " ");
			serviceDataBuilder.setResponseCode(
					"Welcome OOF POC. Number of Neighbor entries to be deleted " + input.getLteCellNumberOfEntries());
		}

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

		return Futures.immediateFuture(rpcResult);
	}

	// RPC generic-neighbor-configuration
	@Override
	public ListenableFuture<RpcResult<GenericNeighborConfigurationOutput>> genericNeighborConfiguration(
			GenericNeighborConfigurationInput input) {
		final String svcOperation = "generic-neighbor-configuration";

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

		LOG.info("Reached RPC genericNeighborConfiguration");

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

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

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

		// Call SLI sync method
		try {

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

				try {
					OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
				} 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 + " ");
			serviceDataBuilder.setResponseCode("Welcome OOF POC. Number of Neighbor entries to be configured "
					+ input.getLteCellNumberOfEntries());
		}

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

		return Futures.immediateFuture(rpcResult);
	}

	// RPC handle-nbrlist-change-notif
	@Override
	public ListenableFuture<RpcResult<HandleNbrlistChangeNotifOutput>> handleNbrlistChangeNotif(
			HandleNbrlistChangeNotifInput input) {
		final String svcOperation = "handle-nbrlist-change-notif";

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

		LOG.info("Reached RPC handle-nbrlist-change-notif");

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

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

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

		// Call SLI sync method
		try {
			if (OofpcipocClient.hasGraph("oofpcipoc-api", svcOperation, null, "sync")) {
				LOG.info("OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
				try {
					OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
				} 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 + " ");
			serviceDataBuilder.setResponseCode(
					"Welcome OOF POC. Number of FAP services changed = " + input.getFapServiceNumberOfEntriesChanged());
		}

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

		LOG.info("Successful exit from handle-nbrlist-change-notif ");

		return Futures.immediateFuture(rpcResult);
	}

}