2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * Modifications Copyright (C) 2020 Nordix Foundation.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.features.sdnr.northbound.cmnotify;
25 import com.google.common.util.concurrent.Futures;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import java.util.Properties;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import org.onap.ccsdk.sli.core.sli.provider.MdsalHelper;
31 import org.opendaylight.mdsal.binding.api.DataBroker;
32 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
33 import org.opendaylight.mdsal.binding.api.RpcProviderService;
34 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.CMNOTIFYAPIService;
35 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationInput;
36 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationInputBuilder;
37 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationOutput;
38 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationOutputBuilder;
39 import org.opendaylight.yangtools.concepts.Builder;
40 import org.opendaylight.yangtools.concepts.ObjectRegistration;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Defines a base implementation for your provider. This class extends from a
48 * helper class which provides storage for the most commonly used components of
49 * the MD-SAL. Additionally the base class provides some basic logging and
50 * initialization / clean up methods.
53 public class CMNotifyProvider implements AutoCloseable, CMNOTIFYAPIService {
55 private static final Logger LOG = LoggerFactory.getLogger(CMNotifyProvider.class);
57 private static final String APPLICATION_NAME = "CMNotify-api";
58 private static final String NBRLIST_CHANGE_NOTIFICATION = "nbrlist-change-notification";
60 private final ExecutorService executor;
61 protected DataBroker dataBroker;
62 protected RpcProviderService rpcProviderRegistry;
63 protected NotificationPublishService notificationService;
64 private ObjectRegistration<CMNotifyProvider> rpcReg;
65 private CMNotifyClient CMNotifyClient;
67 public CMNotifyProvider() {
69 LOG.info("Creating provider for {}", APPLICATION_NAME);
70 executor = Executors.newFixedThreadPool(1);
72 this.dataBroker = null;
73 this.notificationService = null;
74 this.rpcProviderRegistry = null;
75 this.CMNotifyClient = null;
79 public void setDataBroker(DataBroker dataBroker) {
80 this.dataBroker = dataBroker;
83 public void setRpcProviderRegistry(RpcProviderService rpcProviderRegistry) {
84 this.rpcProviderRegistry = rpcProviderRegistry;
87 public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
88 this.notificationService = notificationPublishService;
91 public void setClient(CMNotifyClient client) {
92 this.CMNotifyClient = client;
96 LOG.info("Initializing provider for {}", APPLICATION_NAME);
97 rpcReg = rpcProviderRegistry.registerRpcImplementation(CMNOTIFYAPIService.class, this);
98 LOG.info("Initialization complete for {}", APPLICATION_NAME);
102 public void close() throws Exception {
103 LOG.info("Closing provider for {}", APPLICATION_NAME);
107 LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
110 // RPC nbrlist-change-notification
113 public ListenableFuture<RpcResult<NbrlistChangeNotificationOutput>> nbrlistChangeNotification(
114 NbrlistChangeNotificationInput input) {
115 final String svcOperation = "nbrlist-change-notification";
117 Properties parms = new Properties();
118 NbrlistChangeNotificationOutputBuilder serviceDataBuilder = (NbrlistChangeNotificationOutputBuilder) getServiceData(
119 NBRLIST_CHANGE_NOTIFICATION);
121 LOG.info("Reached RPC nbrlist-change-notification");
123 LOG.info(svcOperation + " called.");
126 LOG.debug("exiting " + svcOperation + " because of invalid input");
127 serviceDataBuilder.setResponseCode("Input is null");
128 RpcResult<NbrlistChangeNotificationOutput> rpcResult = RpcResultBuilder
129 .<NbrlistChangeNotificationOutput>status(true).withResult(serviceDataBuilder.build()).build();
130 return Futures.immediateFuture(rpcResult);
133 // add input to parms
134 LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
135 NbrlistChangeNotificationInputBuilder inputBuilder = new NbrlistChangeNotificationInputBuilder(input);
136 MdsalHelper.toProperties(parms, inputBuilder.build());
138 LOG.info("Printing SLI parameters to be passed");
140 // iterate properties file to get key-value pairs
141 for (String key : parms.stringPropertyNames()) {
142 String value = parms.getProperty(key);
143 LOG.info("The SLI parameter in " + key + " is: " + value);
146 // Call SLI sync method
148 if (CMNotifyClient.hasGraph("CM-NOTIFY-API", svcOperation, null, "sync")) {
149 LOG.info("CMNotifyClient has a Directed Graph for '" + svcOperation + "'");
151 CMNotifyClient.execute("CM-NOTIFY-API", svcOperation, null, "sync", serviceDataBuilder, parms);
152 } catch (Exception e) {
153 LOG.error("Caught exception executing service logic for " + svcOperation, e);
154 serviceDataBuilder.setResponseCode("500");
157 LOG.error("No service logic active for CMNotify: '" + svcOperation + "'");
158 serviceDataBuilder.setResponseCode("503");
160 } catch (Exception e) {
161 LOG.error("Caught exception looking for service logic", e);
162 serviceDataBuilder.setResponseCode("500");
165 String errorCode = serviceDataBuilder.getResponseCode();
167 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
168 LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
170 LOG.info("Returned SUCCESS for " + svcOperation + " ");
171 serviceDataBuilder.setResponseMessage("CM Notification Executed and RuntimeDB Updated. ");
174 RpcResult<NbrlistChangeNotificationOutput> rpcResult = RpcResultBuilder
175 .<NbrlistChangeNotificationOutput>status(true).withResult(serviceDataBuilder.build()).build();
177 LOG.info("Successful exit from nbrlist-change-notification ");
179 return Futures.immediateFuture(rpcResult);
182 protected Builder<?> getServiceData(String svcOperation) {
183 switch (svcOperation) {
184 case NBRLIST_CHANGE_NOTIFICATION:
185 return new NbrlistChangeNotificationOutputBuilder();