aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/northbound/CMNotify/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/cmnotify/CMNotifyProvider.java
blob: 452741b3f8b6c7ba9f45ed129f035ea841fde53c (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
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 			reserved.
 * Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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.cmnotify;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
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.rev200224.CMNOTIFYAPIService;
import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationInput;
import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationInputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationOutput;
import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationOutputBuilder;
import org.opendaylight.yangtools.concepts.Builder;
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 CMNotifyProvider implements AutoCloseable, CMNOTIFYAPIService {

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

	private static final String APPLICATION_NAME = "CMNotify-api";
	private static final String NBRLIST_CHANGE_NOTIFICATION = "nbrlist-change-notification";

	private final ExecutorService executor;
	protected DataBroker dataBroker;
	protected RpcProviderService rpcProviderRegistry;
	protected NotificationPublishService notificationService;
	private ObjectRegistration<CMNotifyProvider> rpcReg;
	private CMNotifyClient CMNotifyClient;

	public CMNotifyProvider() {

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

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

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

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

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

	public void init() {
		LOG.info("Initializing provider for {}", APPLICATION_NAME);
		rpcReg = rpcProviderRegistry.registerRpcImplementation(CMNOTIFYAPIService.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 (rpcReg != null)
			rpcReg.close();
		LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
	}

	// RPC nbrlist-change-notification

	@Override
	public ListenableFuture<RpcResult<NbrlistChangeNotificationOutput>> nbrlistChangeNotification(
			NbrlistChangeNotificationInput input) {
		final String svcOperation = "nbrlist-change-notification";

		Properties parms = new Properties();
		NbrlistChangeNotificationOutputBuilder serviceDataBuilder = (NbrlistChangeNotificationOutputBuilder) getServiceData(
				NBRLIST_CHANGE_NOTIFICATION);

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

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

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

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

		LOG.info("Printing SLI parameters to be passed");

		// iterate properties file to get key-value pairs
		for (String key : parms.stringPropertyNames()) {
			String value = parms.getProperty(key);
			LOG.info("The SLI parameter in " + key + " is: " + value);
		}

		// Call SLI sync method
		try {
			if (CMNotifyClient.hasGraph("CM-NOTIFY-API", svcOperation, null, "sync")) {
				LOG.info("CMNotifyClient has a Directed Graph for '" + svcOperation + "'");
				try {
					CMNotifyClient.execute("CM-NOTIFY-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 CMNotify: '" + 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.setResponseMessage("CM Notification Executed and RuntimeDB Updated. ");
		}

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

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

		return Futures.immediateFuture(rpcResult);
	}

	protected Builder<?> getServiceData(String svcOperation) {
		switch (svcOperation) {
		case NBRLIST_CHANGE_NOTIFICATION:
			return new NbrlistChangeNotificationOutputBuilder();
		}
		return null;
	}
}