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