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