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.ObjectRegistration;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Defines a base implementation for your provider. This class extends from a
47 * helper class which provides storage for the most commonly used components of
48 * the MD-SAL. Additionally the base class provides some basic logging and
49 * initialization / clean up methods.
52 public class CMNotifyProvider implements AutoCloseable, CMNOTIFYAPIService {
54 private static final Logger LOG = LoggerFactory.getLogger(CMNotifyProvider.class);
56 private static final String APPLICATION_NAME = "CMNotify-api";
57 private static final String NBRLIST_CHANGE_NOTIFICATION = "nbrlist-change-notification";
59 private final ExecutorService executor;
60 protected DataBroker dataBroker;
61 protected RpcProviderService rpcProviderRegistry;
62 protected NotificationPublishService notificationService;
63 private ObjectRegistration<CMNotifyProvider> rpcReg;
64 private CMNotifyClient CMNotifyClient;
66 public CMNotifyProvider() {
68 LOG.info("Creating provider for {}", APPLICATION_NAME);
69 executor = Executors.newFixedThreadPool(1);
71 this.dataBroker = null;
72 this.notificationService = null;
73 this.rpcProviderRegistry = null;
74 this.CMNotifyClient = null;
78 public void setDataBroker(DataBroker dataBroker) {
79 this.dataBroker = dataBroker;
82 public void setRpcProviderRegistry(RpcProviderService rpcProviderRegistry) {
83 this.rpcProviderRegistry = rpcProviderRegistry;
86 public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
87 this.notificationService = notificationPublishService;
90 public void setClient(CMNotifyClient client) {
91 this.CMNotifyClient = client;
95 LOG.info("Initializing provider for {}", APPLICATION_NAME);
96 rpcReg = rpcProviderRegistry.registerRpcImplementation(CMNOTIFYAPIService.class, this);
97 LOG.info("Initialization complete for {}", APPLICATION_NAME);
101 public void close() throws Exception {
102 LOG.info("Closing provider for {}", APPLICATION_NAME);
106 LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
109 // RPC nbrlist-change-notification
112 public ListenableFuture<RpcResult<NbrlistChangeNotificationOutput>> nbrlistChangeNotification(
113 NbrlistChangeNotificationInput input) {
114 final String svcOperation = "nbrlist-change-notification";
116 Properties parms = new Properties();
117 NbrlistChangeNotificationOutputBuilder serviceDataBuilder = (NbrlistChangeNotificationOutputBuilder) getServiceData(
118 NBRLIST_CHANGE_NOTIFICATION);
120 LOG.info("Reached RPC nbrlist-change-notification");
122 LOG.info(svcOperation + " called.");
125 LOG.debug("exiting " + svcOperation + " because of invalid input");
126 serviceDataBuilder.setResponseCode("Input is null");
127 RpcResult<NbrlistChangeNotificationOutput> rpcResult = RpcResultBuilder
128 .<NbrlistChangeNotificationOutput>status(true).withResult(serviceDataBuilder.build()).build();
129 return Futures.immediateFuture(rpcResult);
132 // add input to parms
133 LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
134 NbrlistChangeNotificationInputBuilder inputBuilder = new NbrlistChangeNotificationInputBuilder(input);
135 MdsalHelper.toProperties(parms, inputBuilder.build());
137 LOG.info("Printing SLI parameters to be passed");
139 // iterate properties file to get key-value pairs
140 for (String key : parms.stringPropertyNames()) {
141 String value = parms.getProperty(key);
142 LOG.info("The SLI parameter in " + key + " is: " + value);
145 // Call SLI sync method
147 if (CMNotifyClient.hasGraph("CM-NOTIFY-API", svcOperation, null, "sync")) {
148 LOG.info("CMNotifyClient has a Directed Graph for '" + svcOperation + "'");
150 CMNotifyClient.execute("CM-NOTIFY-API", svcOperation, null, "sync", serviceDataBuilder, parms);
151 } catch (Exception e) {
152 LOG.error("Caught exception executing service logic for " + svcOperation, e);
153 serviceDataBuilder.setResponseCode("500");
156 LOG.error("No service logic active for CMNotify: '" + svcOperation + "'");
157 serviceDataBuilder.setResponseCode("503");
159 } catch (Exception e) {
160 LOG.error("Caught exception looking for service logic", e);
161 serviceDataBuilder.setResponseCode("500");
164 String errorCode = serviceDataBuilder.getResponseCode();
166 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
167 LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
169 LOG.info("Returned SUCCESS for " + svcOperation + " ");
170 serviceDataBuilder.setResponseMessage("CM Notification Executed and RuntimeDB Updated. ");
173 RpcResult<NbrlistChangeNotificationOutput> rpcResult = RpcResultBuilder
174 .<NbrlistChangeNotificationOutput>status(true).withResult(serviceDataBuilder.build()).build();
176 LOG.info("Successful exit from nbrlist-change-notification ");
178 return Futures.immediateFuture(rpcResult);
181 protected NbrlistChangeNotificationOutputBuilder getServiceData(String svcOperation) {
182 switch (svcOperation) {
183 case NBRLIST_CHANGE_NOTIFICATION:
184 return new NbrlistChangeNotificationOutputBuilder();