SDN-R add updated devicemanager
[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
64     final MaintenanceRPCServiceAPI maintenanceService;
65     private @Nullable
66     final PushNotifications pushNotificationsListener;
67     private @Nullable
68     final ResyncNetworkElementsListener resyncCallbackListener;
69
70     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 = maintenanceService.getRequiredNetworkElementKeys();
107             result =  RpcResultBuilder.success(outputBuilder);
108         } catch (Exception e) {
109             result = RpcResultBuilder.failed();
110             result.withError(ErrorType.APPLICATION, "Exception", e);
111         }
112         return result.buildFuture();
113     }
114
115     @Override
116     public ListenableFuture<RpcResult<ShowRequiredNetworkElementOutput>> showRequiredNetworkElement(
117             ShowRequiredNetworkElementInput input) {
118
119         LOG.info("RPC Request: showRequiredNetworkElement input: {}", input.getMountpointName());
120         RpcResultBuilder<ShowRequiredNetworkElementOutput> result;
121
122         try {
123             ShowRequiredNetworkElementOutputBuilder outputBuilder = maintenanceService.showRequiredNetworkElement(input);
124             result =  RpcResultBuilder.success(outputBuilder);
125         } catch (Exception e) {
126             result = RpcResultBuilder.failed();
127             result.withError(ErrorType.APPLICATION, "Exception", e);
128         }
129         return result.buildFuture();
130     }
131
132     @Override
133     public ListenableFuture<RpcResult<SetMaintenanceModeOutput>> setMaintenanceMode(SetMaintenanceModeInput input) {
134
135         LOG.info("RPC Request: setMaintenanceMode input: {}", input.getNodeId());
136         RpcResultBuilder<SetMaintenanceModeOutput> result;
137
138         try {
139             SetMaintenanceModeOutputBuilder outputBuilder = maintenanceService.setMaintenanceMode(input);
140             result =  RpcResultBuilder.success(outputBuilder);
141         } catch (Exception e) {
142             result = RpcResultBuilder.failed();
143             result.withError(ErrorType.APPLICATION, "Exception", e);
144         }
145         return result.buildFuture();
146
147     }
148
149
150
151     @Override
152     public ListenableFuture<RpcResult<GetMaintenanceModeOutput>> getMaintenanceMode(GetMaintenanceModeInput input) {
153
154         LOG.info("RPC Request: getMaintenanceMode input: {}", input.getMountpointName());
155         RpcResultBuilder<GetMaintenanceModeOutput> result;
156
157         try {
158             GetMaintenanceModeOutputBuilder outputBuilder = maintenanceService.getMaintenanceMode(input);
159             result =  RpcResultBuilder.success(outputBuilder);
160         } catch (Exception e) {
161             result = RpcResultBuilder.failed();
162             result.withError(ErrorType.APPLICATION, "Exception", e);
163         }
164         return result.buildFuture();
165
166     }
167
168     @Override
169     public ListenableFuture<RpcResult<TestMaintenanceModeOutput>> testMaintenanceMode(TestMaintenanceModeInput input) {
170         LOG.info("RPC Request: getMaintenanceMode input: {}", input.getMountpointName());
171         RpcResultBuilder<TestMaintenanceModeOutput> result;
172
173         try {
174             TestMaintenanceModeOutputBuilder outputBuilder = maintenanceService.testMaintenanceMode(input);
175             result =  RpcResultBuilder.success(outputBuilder);
176         } catch (Exception e) {
177             result = RpcResultBuilder.failed();
178             result.withError(ErrorType.APPLICATION, "Exception", e);
179         }
180         return result.buildFuture();
181
182     }
183
184
185     @Override
186     public ListenableFuture<RpcResult<ClearCurrentFaultByNodenameOutput>> clearCurrentFaultByNodename(
187             ClearCurrentFaultByNodenameInput input) {
188         LOG.info("RPC Request: clearNetworkElementAlarms input: {}", input.getNodenames());
189         RpcResultBuilder<ClearCurrentFaultByNodenameOutput> result;
190         try {
191             if(this.resyncCallbackListener!=null) {
192                 List<String> nodeNames= 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 }