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