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.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
33 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
34 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
35 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.CMNOTIFYAPIService;
36 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationInput;
37 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationInputBuilder;
38 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationOutput;
39 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev200224.NbrlistChangeNotificationOutputBuilder;
41 import org.opendaylight.yangtools.concepts.Builder;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Defines a base implementation for your provider. This class extends from a helper class which
49 * provides storage for the most commonly used components of the MD-SAL. Additionally the base class
50 * provides some basic logging and 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";
61 private final ExecutorService executor;
62 protected DataBroker dataBroker;
63 protected NotificationPublishService notificationService;
64 protected RpcProviderRegistry rpcRegistry;
65 protected BindingAwareBroker.RpcRegistration<CMNOTIFYAPIService> rpcRegistration;
66 private final CMNotifyClient CMNotifyClient;
68 public CMNotifyProvider(final DataBroker dataBroker, final NotificationPublishService notificationPublishService,
69 final RpcProviderRegistry rpcProviderRegistry, final CMNotifyClient CMNotifyClient) {
71 LOG.info("Creating provider for {}", APPLICATION_NAME);
72 executor = Executors.newFixedThreadPool(1);
73 this.dataBroker = dataBroker;
74 this.notificationService = notificationPublishService;
75 this.rpcRegistry = rpcProviderRegistry;
76 this.CMNotifyClient = CMNotifyClient;
80 public void initialize() {
81 LOG.info("Initializing provider for {}", APPLICATION_NAME);
82 rpcRegistration = rpcRegistry.addRpcImplementation(CMNOTIFYAPIService.class, this);
83 LOG.info("Initialization complete for {}", APPLICATION_NAME);
86 protected void initializeChild() {
87 // Override if you have custom initialization intelligence
91 public void close() throws Exception {
92 LOG.info("Closing provider for {}", APPLICATION_NAME);
94 rpcRegistration.close();
95 LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
98 // RPC nbrlist-change-notification
101 public ListenableFuture<RpcResult<NbrlistChangeNotificationOutput>> nbrlistChangeNotification(NbrlistChangeNotificationInput input) {
102 final String svcOperation = "nbrlist-change-notification";
104 Properties parms = new Properties();
105 NbrlistChangeNotificationOutputBuilder serviceDataBuilder = (NbrlistChangeNotificationOutputBuilder) getServiceData(NBRLIST_CHANGE_NOTIFICATION);
107 LOG.info("Reached RPC nbrlist-change-notification");
109 LOG.info(svcOperation + " called.");
112 LOG.debug("exiting " + svcOperation + " because of invalid input");
113 serviceDataBuilder.setResponseCode("Input is null");
114 RpcResult<NbrlistChangeNotificationOutput> rpcResult =
115 RpcResultBuilder.<NbrlistChangeNotificationOutput>status(true).withResult(serviceDataBuilder.build()).build();
116 return Futures.immediateFuture(rpcResult);
119 // add input to parms
120 LOG.info("Adding INPUT data for " + svcOperation + " input: " + input);
121 NbrlistChangeNotificationInputBuilder inputBuilder = new NbrlistChangeNotificationInputBuilder(input);
122 MdsalHelper.toProperties(parms, inputBuilder.build());
124 LOG.info("Printing SLI parameters to be passed");
126 // iterate properties file to get key-value pairs
127 for (String key : parms.stringPropertyNames()) {
128 String value = parms.getProperty(key);
129 LOG.info("The SLI parameter in " + key + " is: " + value);
132 // Call SLI sync method
134 if (CMNotifyClient.hasGraph("CM-NOTIFY-API", svcOperation, null, "sync")) {
135 LOG.info("CMNotifyClient has a Directed Graph for '" + svcOperation + "'");
137 CMNotifyClient.execute("CM-NOTIFY-API", svcOperation, null, "sync", serviceDataBuilder, parms);
138 } catch (Exception e) {
139 LOG.error("Caught exception executing service logic for " + svcOperation, e);
140 serviceDataBuilder.setResponseCode("500");
143 LOG.error("No service logic active for CMNotify: '" + svcOperation + "'");
144 serviceDataBuilder.setResponseCode("503");
146 } catch (Exception e) {
147 LOG.error("Caught exception looking for service logic", e);
148 serviceDataBuilder.setResponseCode("500");
151 String errorCode = serviceDataBuilder.getResponseCode();
153 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
154 LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
156 LOG.info("Returned SUCCESS for " + svcOperation + " ");
157 serviceDataBuilder.setResponseMessage("CM Notification Executed and RuntimeDB Updated. ");
160 RpcResult<NbrlistChangeNotificationOutput> rpcResult =
161 RpcResultBuilder.<NbrlistChangeNotificationOutput>status(true).withResult(serviceDataBuilder.build()).build();
163 LOG.info("Successful exit from nbrlist-change-notification ");
165 return Futures.immediateFuture(rpcResult);
169 protected Builder<?> getServiceData(String svcOperation) {
170 switch (svcOperation) {
171 case NBRLIST_CHANGE_NOTIFICATION:
172 return new NbrlistChangeNotificationOutputBuilder();