e31b79aeac781bacd0bde86de0446a78d017c68d
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Update Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved.
8  * =================================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
10  * in compliance with the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software distributed under the License
15  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
16  * or implied. See the License for the specific language governing permissions and limitations under
17  * the License.
18  * ============LICENSE_END==========================================================================
19  */
20 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl;
21
22 import com.google.common.util.concurrent.ListenableFuture;
23 import java.util.List;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.housekeeping.ResyncNetworkElementsListener;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.MaintenanceRPCServiceAPI;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.impl.MaintenanceServiceImpl;
28 import org.opendaylight.mdsal.binding.api.RpcProviderService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ClearCurrentFaultByNodenameInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ClearCurrentFaultByNodenameOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ClearCurrentFaultByNodenameOutputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.DevicemanagerService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetMaintenanceModeInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetMaintenanceModeOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetMaintenanceModeOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetRequiredNetworkElementKeysInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetRequiredNetworkElementKeysOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetRequiredNetworkElementKeysOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushAttributeChangeNotificationInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushAttributeChangeNotificationOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushCmNotificationInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushCmNotificationOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushFaultNotificationInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushFaultNotificationOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.SetMaintenanceModeInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.SetMaintenanceModeOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.SetMaintenanceModeOutputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ShowRequiredNetworkElementInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ShowRequiredNetworkElementOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ShowRequiredNetworkElementOutputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.TestMaintenanceModeInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.TestMaintenanceModeOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.TestMaintenanceModeOutputBuilder;
54 import org.opendaylight.yangtools.concepts.ObjectRegistration;
55 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
56 import org.opendaylight.yangtools.yang.common.RpcResult;
57 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 public class DeviceManagerApiServiceImpl implements DevicemanagerService, AutoCloseable {
62
63     private static final Logger LOG = LoggerFactory.getLogger(DevicemanagerService.class);
64
65     private final ObjectRegistration<DevicemanagerService> rpcReg;
66     private @Nullable final MaintenanceRPCServiceAPI maintenanceService;
67     private @Nullable final PushNotifications pushNotificationsListener;
68     private @Nullable final ResyncNetworkElementsListener resyncCallbackListener;
69
70     public DeviceManagerApiServiceImpl(final RpcProviderService rpcProviderRegistry,
71             MaintenanceServiceImpl maintenanceService, ResyncNetworkElementsListener listener,
72             PushNotifications pushNotificationsListener) {
73         this.maintenanceService = maintenanceService;
74         this.pushNotificationsListener = pushNotificationsListener;
75         this.resyncCallbackListener = listener;
76
77         // Register ourselves as the REST API RPC implementation
78         LOG.info("Register RPC Service " + DevicemanagerService.class.getSimpleName());
79         this.rpcReg = rpcProviderRegistry.registerRpcImplementation(DevicemanagerService.class, this);
80     }
81
82     @Override
83     public void close() throws Exception {
84         LOG.info("Close RPC Service");
85         if (rpcReg != null) {
86             rpcReg.close();
87         }
88     }
89
90     /*-------------------------------
91      * Interfaces for MaintenanceService
92      */
93
94     @Override
95     public ListenableFuture<RpcResult<GetRequiredNetworkElementKeysOutput>> getRequiredNetworkElementKeys(
96             GetRequiredNetworkElementKeysInput input) {
97         return getRequiredNetworkElementKeys();
98     }
99
100     // For casablanca version no input was generated.
101     public ListenableFuture<RpcResult<GetRequiredNetworkElementKeysOutput>> getRequiredNetworkElementKeys() {
102
103         LOG.info("RPC Request: getRequiredNetworkElementKeys");
104         RpcResultBuilder<GetRequiredNetworkElementKeysOutput> result;
105         try {
106             GetRequiredNetworkElementKeysOutputBuilder outputBuilder =
107                     maintenanceService.getRequiredNetworkElementKeys();
108             result = RpcResultBuilder.success(outputBuilder);
109         } catch (Exception e) {
110             result = RpcResultBuilder.failed();
111             result.withError(ErrorType.APPLICATION, "Exception", e);
112         }
113         return result.buildFuture();
114     }
115
116     @Override
117     public ListenableFuture<RpcResult<ShowRequiredNetworkElementOutput>> showRequiredNetworkElement(
118             ShowRequiredNetworkElementInput input) {
119
120         LOG.info("RPC Request: showRequiredNetworkElement input: {}", input.getMountpointName());
121         RpcResultBuilder<ShowRequiredNetworkElementOutput> result;
122
123         try {
124             ShowRequiredNetworkElementOutputBuilder outputBuilder =
125                     maintenanceService.showRequiredNetworkElement(input);
126             result = RpcResultBuilder.success(outputBuilder);
127         } catch (Exception e) {
128             result = RpcResultBuilder.failed();
129             result.withError(ErrorType.APPLICATION, "Exception", e);
130         }
131         return result.buildFuture();
132     }
133
134     @Override
135     public ListenableFuture<RpcResult<SetMaintenanceModeOutput>> setMaintenanceMode(SetMaintenanceModeInput input) {
136
137         LOG.info("RPC Request: setMaintenanceMode input: {}", input.getNodeId());
138         RpcResultBuilder<SetMaintenanceModeOutput> result;
139
140         try {
141             SetMaintenanceModeOutputBuilder outputBuilder = maintenanceService.setMaintenanceMode(input);
142             result = RpcResultBuilder.success(outputBuilder);
143         } catch (Exception e) {
144             result = RpcResultBuilder.failed();
145             result.withError(ErrorType.APPLICATION, "Exception", e);
146         }
147         return result.buildFuture();
148
149     }
150
151
152
153     @Override
154     public ListenableFuture<RpcResult<GetMaintenanceModeOutput>> getMaintenanceMode(GetMaintenanceModeInput input) {
155
156         LOG.info("RPC Request: getMaintenanceMode input: {}", input.getMountpointName());
157         RpcResultBuilder<GetMaintenanceModeOutput> result;
158
159         try {
160             GetMaintenanceModeOutputBuilder outputBuilder = maintenanceService.getMaintenanceMode(input);
161             result = RpcResultBuilder.success(outputBuilder);
162         } catch (Exception e) {
163             result = RpcResultBuilder.failed();
164             result.withError(ErrorType.APPLICATION, "Exception", e);
165         }
166         return result.buildFuture();
167
168     }
169
170     @Override
171     public ListenableFuture<RpcResult<TestMaintenanceModeOutput>> testMaintenanceMode(TestMaintenanceModeInput input) {
172         LOG.info("RPC Request: getMaintenanceMode input: {}", input.getMountpointName());
173         RpcResultBuilder<TestMaintenanceModeOutput> result;
174
175         try {
176             TestMaintenanceModeOutputBuilder outputBuilder = maintenanceService.testMaintenanceMode(input);
177             result = RpcResultBuilder.success(outputBuilder);
178         } catch (Exception e) {
179             result = RpcResultBuilder.failed();
180             result.withError(ErrorType.APPLICATION, "Exception", e);
181         }
182         return result.buildFuture();
183
184     }
185
186
187     @Override
188     public ListenableFuture<RpcResult<ClearCurrentFaultByNodenameOutput>> clearCurrentFaultByNodename(
189             ClearCurrentFaultByNodenameInput input) {
190         LOG.info("RPC Request: clearNetworkElementAlarms input: {}", input.getNodenames());
191         RpcResultBuilder<ClearCurrentFaultByNodenameOutput> result;
192         try {
193             if (this.resyncCallbackListener != null) {
194                 List<String> nodeNames =
195                         this.resyncCallbackListener.doClearCurrentFaultByNodename(input.getNodenames());
196                 ClearCurrentFaultByNodenameOutputBuilder outputBuilder = new ClearCurrentFaultByNodenameOutputBuilder();
197                 outputBuilder.setNodenames(nodeNames);
198                 result = RpcResultBuilder.success(outputBuilder);
199             } else {
200                 result = RpcResultBuilder.failed();
201                 result.withError(ErrorType.APPLICATION, "Startup running");
202             }
203         } catch (Exception e) {
204             result = RpcResultBuilder.failed();
205             result.withError(ErrorType.APPLICATION, "Exception", e);
206         }
207         return result.buildFuture();
208     }
209
210     @Override
211     public ListenableFuture<RpcResult<PushFaultNotificationOutput>> pushFaultNotification(
212             PushFaultNotificationInput input) {
213         LOG.info("RPC Received fault notification {}", input);
214         RpcResultBuilder<PushFaultNotificationOutput> result;
215         try {
216             pushNotificationsListener.pushFaultNotification(input);
217             result = RpcResultBuilder.success();
218         } catch (Exception e) {
219             result = RpcResultBuilder.failed();
220             result.withError(ErrorType.APPLICATION, "Exception", e);
221         }
222         return result.buildFuture();
223     }
224
225     @Override
226     public ListenableFuture<RpcResult<PushCmNotificationOutput>> pushCmNotification(PushCmNotificationInput input) {
227         LOG.info("RPC Received CM notification {}", input);
228         RpcResultBuilder<PushCmNotificationOutput> result;
229         try {
230             pushNotificationsListener.pushCMNotification(input);
231             result = RpcResultBuilder.success();
232         } catch (Exception e) {
233             result = RpcResultBuilder.failed();
234             result.withError(ErrorType.APPLICATION, "Exception", e);
235         }
236         return result.buildFuture();
237     }
238
239     @Override
240     public ListenableFuture<RpcResult<PushAttributeChangeNotificationOutput>> pushAttributeChangeNotification(
241             PushAttributeChangeNotificationInput input) {
242         LOG.info("RPC Received change notification {}", input);
243         RpcResultBuilder<PushAttributeChangeNotificationOutput> result;
244         try {
245             pushNotificationsListener.pushAttributeChangeNotification(input);
246             result = RpcResultBuilder.success();
247         } catch (Exception e) {
248             result = RpcResultBuilder.failed();
249             result.withError(ErrorType.APPLICATION, "Exception", e);
250         }
251         return result.buildFuture();
252     }
253
254
255
256 }