Merge "junit tests 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 com.google.common.util.concurrent.ListenableFuture;
21 import java.util.List;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
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.controller.sal.binding.api.RpcProviderRegistry;
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.SetMaintenanceModeInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.SetMaintenanceModeOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.SetMaintenanceModeOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ShowRequiredNetworkElementInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ShowRequiredNetworkElementOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.ShowRequiredNetworkElementOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.TestMaintenanceModeInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.TestMaintenanceModeOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.devicemanager.rev190109.TestMaintenanceModeOutputBuilder;
45 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 @SuppressWarnings("deprecation")
52 public class DeviceManagerApiServiceImpl implements DevicemanagerService, AutoCloseable {
53
54     private static final Logger LOG = LoggerFactory.getLogger(DevicemanagerService.class);
55
56     private final RpcRegistration<DevicemanagerService> rpcReg;
57
58     private MaintenanceRPCServiceAPI maintenanceService;
59     private ResyncNetworkElementsListener resyncCallbackListener;
60
61     DeviceManagerApiServiceImpl(final RpcProviderRegistry rpcProviderRegistry) {
62         // Register ourselves as the REST API RPC implementation
63         LOG.info("Register RPC Service "+DevicemanagerService.class.getSimpleName());
64         this.maintenanceService = null;
65         this.rpcReg = rpcProviderRegistry.addRpcImplementation(DevicemanagerService.class, this);
66     }
67
68     public void setMaintenanceService(MaintenanceServiceImpl maintenanceService2) {
69         this.maintenanceService = maintenanceService2;
70     }
71
72     @Override
73     public void close() throws Exception {
74         LOG.info("Close RPC Service");
75         if (rpcReg != null) {
76             rpcReg.close();
77         }
78     }
79
80     /*-------------------------------
81      * Interfaces for MaintenanceService
82      */
83
84     @Override
85     public ListenableFuture<RpcResult<GetRequiredNetworkElementKeysOutput>> getRequiredNetworkElementKeys(
86             GetRequiredNetworkElementKeysInput input) {
87         return getRequiredNetworkElementKeys();
88     }
89
90     // For casablanca version no input was generated.
91     public ListenableFuture<RpcResult<GetRequiredNetworkElementKeysOutput>> getRequiredNetworkElementKeys() {
92
93         LOG.info("RPC Request: getRequiredNetworkElementKeys");
94         RpcResultBuilder<GetRequiredNetworkElementKeysOutput> result;
95         try {
96             GetRequiredNetworkElementKeysOutputBuilder outputBuilder = maintenanceService.getRequiredNetworkElementKeys();
97             result =  RpcResultBuilder.success(outputBuilder);
98         } catch (Exception e) {
99             result = RpcResultBuilder.failed();
100             result.withError(ErrorType.APPLICATION, "Exception", e);
101         }
102         return result.buildFuture();
103     }
104
105     @Override
106     public ListenableFuture<RpcResult<ShowRequiredNetworkElementOutput>> showRequiredNetworkElement(
107             ShowRequiredNetworkElementInput input) {
108
109         LOG.info("RPC Request: showRequiredNetworkElement input: {}", input.getMountpointName());
110         RpcResultBuilder<ShowRequiredNetworkElementOutput> result;
111
112         try {
113             ShowRequiredNetworkElementOutputBuilder outputBuilder = maintenanceService.showRequiredNetworkElement(input);
114             result =  RpcResultBuilder.success(outputBuilder);
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<SetMaintenanceModeOutput>> setMaintenanceMode(SetMaintenanceModeInput input) {
124
125         LOG.info("RPC Request: setMaintenanceMode input: {}", input.getMountpointName());
126         RpcResultBuilder<SetMaintenanceModeOutput> result;
127
128         try {
129             SetMaintenanceModeOutputBuilder outputBuilder = maintenanceService.setMaintenanceMode(input);
130             result =  RpcResultBuilder.success(outputBuilder);
131         } catch (Exception e) {
132             result = RpcResultBuilder.failed();
133             result.withError(ErrorType.APPLICATION, "Exception", e);
134         }
135         return result.buildFuture();
136
137     }
138
139
140
141     @Override
142     public ListenableFuture<RpcResult<GetMaintenanceModeOutput>> getMaintenanceMode(GetMaintenanceModeInput input) {
143
144         LOG.info("RPC Request: getMaintenanceMode input: {}", input.getMountpointName());
145         RpcResultBuilder<GetMaintenanceModeOutput> result;
146
147         try {
148             GetMaintenanceModeOutputBuilder outputBuilder = maintenanceService.getMaintenanceMode(input);
149             result =  RpcResultBuilder.success(outputBuilder);
150         } catch (Exception e) {
151             result = RpcResultBuilder.failed();
152             result.withError(ErrorType.APPLICATION, "Exception", e);
153         }
154         return result.buildFuture();
155
156     }
157
158     @Override
159     public ListenableFuture<RpcResult<TestMaintenanceModeOutput>> testMaintenanceMode(TestMaintenanceModeInput input) {
160         LOG.info("RPC Request: getMaintenanceMode input: {}", input.getMountpointName());
161         RpcResultBuilder<TestMaintenanceModeOutput> result;
162
163         try {
164             TestMaintenanceModeOutputBuilder outputBuilder = maintenanceService.testMaintenanceMode(input);
165             result =  RpcResultBuilder.success(outputBuilder);
166         } catch (Exception e) {
167             result = RpcResultBuilder.failed();
168             result.withError(ErrorType.APPLICATION, "Exception", e);
169         }
170         return result.buildFuture();
171
172     }
173
174
175     @Override
176     public ListenableFuture<RpcResult<ClearCurrentFaultByNodenameOutput>> clearCurrentFaultByNodename(
177             ClearCurrentFaultByNodenameInput input) {
178         LOG.info("RPC Request: clearNetworkElementAlarms input: {}", input.getNodenames());
179         RpcResultBuilder<ClearCurrentFaultByNodenameOutput> result;
180         try {
181             if(this.resyncCallbackListener!=null) {
182                 List<String> nodeNames= this.resyncCallbackListener.doClearCurrentFaultByNodename(input.getNodenames());
183                 ClearCurrentFaultByNodenameOutputBuilder outputBuilder = new ClearCurrentFaultByNodenameOutputBuilder();
184                 outputBuilder.setNodenames(nodeNames);
185                 result =  RpcResultBuilder.success(outputBuilder);
186             } else {
187                 result = RpcResultBuilder.failed();
188                 result.withError(ErrorType.APPLICATION, "Startup running" );
189             }
190         } catch(Exception e) {
191             result = RpcResultBuilder.failed();
192             result.withError(ErrorType.APPLICATION, "Exception", e);
193         }
194         return result.buildFuture();
195     }
196
197     public void setResyncListener(ResyncNetworkElementsListener listener) {
198         this.resyncCallbackListener = listener;
199     }
200
201 }