e4af5d2e8321e5a379add3f7e81f0a9b0530f3e8
[ccsdk/features.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.ccsdk.features.sdnr.northbound.cmnotify;
24
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;
44
45 /**
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.
50  *
51  */
52 public class CMNotifyProvider implements AutoCloseable, CMNOTIFYAPIService {
53
54         private static final Logger LOG = LoggerFactory.getLogger(CMNotifyProvider.class);
55
56         private static final String APPLICATION_NAME = "CMNotify-api";
57         private static final String NBRLIST_CHANGE_NOTIFICATION = "nbrlist-change-notification";
58
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;
65
66         public CMNotifyProvider() {
67
68                 LOG.info("Creating provider for {}", APPLICATION_NAME);
69                 executor = Executors.newFixedThreadPool(1);
70                 
71                 this.dataBroker = null; 
72                 this.notificationService = null; 
73                 this.rpcProviderRegistry = null;
74                 this.CMNotifyClient = null;
75                  
76         }
77
78         public void setDataBroker(DataBroker dataBroker) {
79                 this.dataBroker = dataBroker;
80         }
81
82         public void setRpcProviderRegistry(RpcProviderService rpcProviderRegistry) {
83                 this.rpcProviderRegistry = rpcProviderRegistry;
84         }
85
86         public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
87                 this.notificationService = notificationPublishService;
88         }
89
90         public void setClient(CMNotifyClient client) {
91                 this.CMNotifyClient = client;
92         }
93
94         public void init() {
95                 LOG.info("Initializing provider for {}", APPLICATION_NAME);
96                 rpcReg = rpcProviderRegistry.registerRpcImplementation(CMNOTIFYAPIService.class, this);
97                 LOG.info("Initialization complete for {}", APPLICATION_NAME);
98         }
99
100         @Override
101         public void close() throws Exception {
102                 LOG.info("Closing provider for {}", APPLICATION_NAME);
103                 executor.shutdown();
104                 if (rpcReg != null)
105                         rpcReg.close();
106                 LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
107         }
108
109         // RPC nbrlist-change-notification
110
111         @Override
112         public ListenableFuture<RpcResult<NbrlistChangeNotificationOutput>> nbrlistChangeNotification(
113                         NbrlistChangeNotificationInput input) {
114                 final String svcOperation = "nbrlist-change-notification";
115
116                 Properties parms = new Properties();
117                 NbrlistChangeNotificationOutputBuilder serviceDataBuilder = (NbrlistChangeNotificationOutputBuilder) getServiceData(
118                                 NBRLIST_CHANGE_NOTIFICATION);
119
120                 LOG.info("Reached RPC nbrlist-change-notification");
121
122                 LOG.info(svcOperation + " called.");
123
124                 if (input == null) {
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);
130                 }
131
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());
136
137                 LOG.info("Printing SLI parameters to be passed");
138
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);
143                 }
144
145                 // Call SLI sync method
146                 try {
147                         if (CMNotifyClient.hasGraph("CM-NOTIFY-API", svcOperation, null, "sync")) {
148                                 LOG.info("CMNotifyClient has a Directed Graph for '" + svcOperation + "'");
149                                 try {
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");
154                                 }
155                         } else {
156                                 LOG.error("No service logic active for CMNotify: '" + svcOperation + "'");
157                                 serviceDataBuilder.setResponseCode("503");
158                         }
159                 } catch (Exception e) {
160                         LOG.error("Caught exception looking for service logic", e);
161                         serviceDataBuilder.setResponseCode("500");
162                 }
163
164                 String errorCode = serviceDataBuilder.getResponseCode();
165
166                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
167                         LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
168                 } else {
169                         LOG.info("Returned SUCCESS for " + svcOperation + " ");
170                         serviceDataBuilder.setResponseMessage("CM Notification Executed and RuntimeDB Updated. ");
171                 }
172
173                 RpcResult<NbrlistChangeNotificationOutput> rpcResult = RpcResultBuilder
174                                 .<NbrlistChangeNotificationOutput>status(true).withResult(serviceDataBuilder.build()).build();
175
176                 LOG.info("Successful exit from nbrlist-change-notification ");
177
178                 return Futures.immediateFuture(rpcResult);
179         }
180
181         protected NbrlistChangeNotificationOutputBuilder getServiceData(String svcOperation) {
182                 switch (svcOperation) {
183                 case NBRLIST_CHANGE_NOTIFICATION:
184                         return new NbrlistChangeNotificationOutputBuilder();
185                 }
186                 return null;
187         }
188 }