4d5823a00d34a428e217113fdd900e6b51ee5546
[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.ForwardingListenableFuture;
23 import com.google.common.util.concurrent.ListenableFuture;
24
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.stream.Collectors;
28
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.housekeeping.ResyncNetworkElementsListener;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.MaintenanceRPCServiceAPI;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.impl.MaintenanceServiceImpl;
34 import org.opendaylight.mdsal.binding.api.RpcProviderService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ClearCurrentFaultByNodenameInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ClearCurrentFaultByNodenameOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ClearCurrentFaultByNodenameOutputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.DevicemanagerService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetMaintenanceModeInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetMaintenanceModeOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetMaintenanceModeOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetRequiredNetworkElementKeysInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetRequiredNetworkElementKeysOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.GetRequiredNetworkElementKeysOutputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushAttributeChangeNotificationInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushAttributeChangeNotificationOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushCmNotificationInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushCmNotificationOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushFaultNotificationInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.PushFaultNotificationOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.SetMaintenanceModeInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.SetMaintenanceModeOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.SetMaintenanceModeOutputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ShowRequiredNetworkElementInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ShowRequiredNetworkElementOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ShowRequiredNetworkElementOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.TestMaintenanceModeInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.TestMaintenanceModeOutput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.TestMaintenanceModeOutputBuilder;
60 import org.opendaylight.yangtools.concepts.ObjectRegistration;
61 import org.opendaylight.yangtools.yang.common.ErrorType;
62 import org.opendaylight.yangtools.yang.common.RpcResult;
63 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 public class DeviceManagerApiServiceImpl implements DevicemanagerService, AutoCloseable {
68
69     private static final Logger LOG = LoggerFactory.getLogger(DevicemanagerService.class);
70
71     private final ObjectRegistration<DevicemanagerService> rpcReg;
72     private @Nullable final MaintenanceRPCServiceAPI maintenanceService;
73     private @Nullable final PushNotifications pushNotificationsListener;
74     private @Nullable final ResyncNetworkElementsListener resyncCallbackListener;
75
76     public DeviceManagerApiServiceImpl(final RpcProviderService rpcProviderRegistry,
77             MaintenanceServiceImpl maintenanceService, ResyncNetworkElementsListener listener,
78             PushNotifications pushNotificationsListener) {
79         this.maintenanceService = maintenanceService;
80         this.pushNotificationsListener = pushNotificationsListener;
81         this.resyncCallbackListener = listener;
82
83         // Register ourselves as the REST API RPC implementation
84         LOG.info("Register RPC Service " + DevicemanagerService.class.getSimpleName());
85         this.rpcReg = rpcProviderRegistry.registerRpcImplementation(DevicemanagerService.class, this);
86     }
87
88     @Override
89     public void close() throws Exception {
90         LOG.info("Close RPC Service");
91         if (rpcReg != null) {
92             rpcReg.close();
93         }
94     }
95
96     /*-------------------------------
97      * Interfaces for MaintenanceService
98      */
99
100     @Override
101     public ListenableFuture<RpcResult<GetRequiredNetworkElementKeysOutput>> getRequiredNetworkElementKeys(
102             GetRequiredNetworkElementKeysInput input) {
103         return getRequiredNetworkElementKeys();
104     }
105
106     // For casablanca version no input was generated.
107     public ListenableFuture<RpcResult<GetRequiredNetworkElementKeysOutput>> getRequiredNetworkElementKeys() {
108
109         LOG.info("RPC Request: getRequiredNetworkElementKeys");
110         RpcResultBuilder<GetRequiredNetworkElementKeysOutput> result;
111         try {
112             GetRequiredNetworkElementKeysOutputBuilder outputBuilder =
113                     maintenanceService.getRequiredNetworkElementKeys();
114             result = RpcResultBuilder.success(outputBuilder.build());
115         } catch (Exception e) {
116             result = RpcResultBuilder.failed();
117             result.withError(ErrorType.APPLICATION, "Exception", e);
118         }
119         return result.buildFuture();
120     }
121
122     @Override
123     public ListenableFuture<RpcResult<ShowRequiredNetworkElementOutput>> showRequiredNetworkElement(
124             ShowRequiredNetworkElementInput input) {
125
126         LOG.info("RPC Request: showRequiredNetworkElement input: {}", input.getMountpointName());
127         RpcResultBuilder<ShowRequiredNetworkElementOutput> result;
128
129         try {
130             ShowRequiredNetworkElementOutputBuilder outputBuilder =
131                     maintenanceService.showRequiredNetworkElement(input);
132             result = RpcResultBuilder.success(outputBuilder.build());
133         } catch (Exception e) {
134             result = RpcResultBuilder.failed();
135             result.withError(ErrorType.APPLICATION, "Exception", e);
136         }
137         return result.buildFuture();
138     }
139
140     @Override
141     public ListenableFuture<RpcResult<SetMaintenanceModeOutput>> setMaintenanceMode(SetMaintenanceModeInput input) {
142
143         LOG.info("RPC Request: setMaintenanceMode input: {}", input.getNodeId());
144         RpcResultBuilder<SetMaintenanceModeOutput> result;
145
146         try {
147             SetMaintenanceModeOutputBuilder outputBuilder = maintenanceService.setMaintenanceMode(input);
148             result = RpcResultBuilder.success(outputBuilder.build());
149         } catch (Exception e) {
150             result = RpcResultBuilder.failed();
151             result.withError(ErrorType.APPLICATION, "Exception", e);
152         }
153         return result.buildFuture();
154     }
155
156
157
158     @Override
159     public ListenableFuture<RpcResult<GetMaintenanceModeOutput>> getMaintenanceMode(GetMaintenanceModeInput input) {
160
161         LOG.info("RPC Request: getMaintenanceMode input: {}", input.getMountpointName());
162         @NonNull RpcResultBuilder<GetMaintenanceModeOutput> result;
163
164         try {
165             GetMaintenanceModeOutputBuilder outputBuilder = maintenanceService.getMaintenanceMode(input);
166             result = RpcResultBuilder.success(outputBuilder.build());
167         } catch (Exception e) {
168             result = RpcResultBuilder.failed();
169             result.withError(ErrorType.APPLICATION, "Exception", e);
170         }
171         return result.buildFuture();
172
173     }
174
175     @Override
176     public ListenableFuture<RpcResult<TestMaintenanceModeOutput>> testMaintenanceMode(TestMaintenanceModeInput input) {
177         LOG.info("RPC Request: getMaintenanceMode input: {}", input.getMountpointName());
178         RpcResultBuilder<TestMaintenanceModeOutput> result;
179
180         try {
181             TestMaintenanceModeOutputBuilder outputBuilder = maintenanceService.testMaintenanceMode(input);
182             result = RpcResultBuilder.success(outputBuilder.build());
183         } catch (Exception e) {
184             result = RpcResultBuilder.failed();
185             result.withError(ErrorType.APPLICATION, "Exception", e);
186         }
187         return result.buildFuture();
188
189     }
190
191
192     @Override
193     public ListenableFuture<RpcResult<ClearCurrentFaultByNodenameOutput>> clearCurrentFaultByNodename(
194             ClearCurrentFaultByNodenameInput input) {
195         LOG.info("RPC Request: clearNetworkElementAlarms input: {}", input.getNodenames());
196         RpcResultBuilder<ClearCurrentFaultByNodenameOutput> result;
197         try {
198             if (this.resyncCallbackListener != null) {
199                 List<String> nodeNames =
200                         this.resyncCallbackListener.doClearCurrentFaultByNodename(input.getNodenames().stream().collect(Collectors.toList()));
201                 ClearCurrentFaultByNodenameOutputBuilder outputBuilder = new ClearCurrentFaultByNodenameOutputBuilder();
202                 outputBuilder.setNodenames(new HashSet<>(nodeNames));
203                 result = RpcResultBuilder.success(outputBuilder.build());
204             } else {
205                 result = RpcResultBuilder.failed();
206                 result.withError(ErrorType.APPLICATION, "Startup running");
207             }
208         } catch (Exception e) {
209             result = RpcResultBuilder.failed();
210             result.withError(ErrorType.APPLICATION, "Exception", e);
211         }
212         return result.buildFuture();
213     }
214
215     @Override
216     public ListenableFuture<RpcResult<PushFaultNotificationOutput>> pushFaultNotification(
217             PushFaultNotificationInput input) {
218         LOG.info("RPC Received fault notification {}", input);
219         RpcResultBuilder<PushFaultNotificationOutput> result;
220         try {
221             pushNotificationsListener.pushFaultNotification(input);
222             result = RpcResultBuilder.success();
223         } catch (Exception e) {
224             result = RpcResultBuilder.failed();
225             result.withError(ErrorType.APPLICATION, "Exception", e);
226         }
227         return result.buildFuture();
228     }
229
230     @Override
231     public ListenableFuture<RpcResult<PushCmNotificationOutput>> pushCmNotification(PushCmNotificationInput input) {
232         LOG.info("RPC Received CM notification {}", input);
233         RpcResultBuilder<PushCmNotificationOutput> result;
234         try {
235             pushNotificationsListener.pushCMNotification(input);
236             result = RpcResultBuilder.success();
237         } catch (Exception e) {
238             result = RpcResultBuilder.failed();
239             result.withError(ErrorType.APPLICATION, "Exception", e);
240         }
241         return result.buildFuture();
242     }
243
244     @Override
245     public ListenableFuture<RpcResult<PushAttributeChangeNotificationOutput>> pushAttributeChangeNotification(
246             PushAttributeChangeNotificationInput input) {
247         LOG.info("RPC Received change notification {}", input);
248         RpcResultBuilder<PushAttributeChangeNotificationOutput> result;
249         try {
250             pushNotificationsListener.pushAttributeChangeNotification(input);
251             result = RpcResultBuilder.success();
252         } catch (Exception e) {
253             result = RpcResultBuilder.failed();
254             result.withError(ErrorType.APPLICATION, "Exception", e);
255         }
256         return result.buildFuture();
257     }
258
259 }