ORAN CMNotify Karaf Feature Development
[ccsdk/features.git] / sdnr / northbound / CMNotify / provider / src / main / java / org / onap / ccsdk / features / sdnr / northbound / CMNotify / CMNotifyProvider.java
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.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;
40
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;
46
47 /**
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.
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
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;
67
68     public CMNotifyProvider(final DataBroker dataBroker, final NotificationPublishService notificationPublishService,
69             final RpcProviderRegistry rpcProviderRegistry, final CMNotifyClient CMNotifyClient) {
70
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;
77         initialize();
78     }
79
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);
84     }
85
86     protected void initializeChild() {
87         // Override if you have custom initialization intelligence
88     }
89
90     @Override
91     public void close() throws Exception {
92         LOG.info("Closing provider for {}", APPLICATION_NAME);
93         executor.shutdown();
94         rpcRegistration.close();
95         LOG.info("Successfully closed provider for {}", APPLICATION_NAME);
96     }
97
98       // RPC nbrlist-change-notification
99
100     @Override
101     public ListenableFuture<RpcResult<NbrlistChangeNotificationOutput>> nbrlistChangeNotification(NbrlistChangeNotificationInput input) {
102         final String svcOperation = "nbrlist-change-notification";
103
104         Properties parms = new Properties();
105         NbrlistChangeNotificationOutputBuilder serviceDataBuilder = (NbrlistChangeNotificationOutputBuilder) getServiceData(NBRLIST_CHANGE_NOTIFICATION);
106
107         LOG.info("Reached RPC nbrlist-change-notification");
108
109         LOG.info(svcOperation + " called.");
110
111         if (input == null) {
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);
117         }
118
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());
123
124         LOG.info("Printing SLI parameters to be passed");
125
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);
130         }
131
132         // Call SLI sync method
133         try {
134             if (CMNotifyClient.hasGraph("CM-NOTIFY-API", svcOperation, null, "sync")) {
135                 LOG.info("CMNotifyClient has a Directed Graph for '" + svcOperation + "'");
136                 try {
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");
141                 }
142             } else {
143                 LOG.error("No service logic active for CMNotify: '" + svcOperation + "'");
144                 serviceDataBuilder.setResponseCode("503");
145             }
146         } catch (Exception e) {
147             LOG.error("Caught exception looking for service logic", e);
148             serviceDataBuilder.setResponseCode("500");
149         }
150
151         String errorCode = serviceDataBuilder.getResponseCode();
152
153         if (!("0".equals(errorCode) || "200".equals(errorCode))) {
154             LOG.error("Returned FAILED for " + svcOperation + " error code: '" + errorCode + "'");
155         } else {
156             LOG.info("Returned SUCCESS for " + svcOperation + " ");
157             serviceDataBuilder.setResponseMessage("CM Notification Executed and RuntimeDB Updated. ");
158         }
159
160         RpcResult<NbrlistChangeNotificationOutput> rpcResult =
161                 RpcResultBuilder.<NbrlistChangeNotificationOutput>status(true).withResult(serviceDataBuilder.build()).build();
162
163         LOG.info("Successful exit from nbrlist-change-notification ");
164
165         return Futures.immediateFuture(rpcResult);
166     }
167
168
169     protected Builder<?> getServiceData(String svcOperation) {
170         switch (svcOperation) {
171             case NBRLIST_CHANGE_NOTIFICATION:
172                 return new NbrlistChangeNotificationOutputBuilder();
173         }
174         return null;
175     }
176 }