896f010d8e4fba5f01d35d54b0c01c9ac5e088c0
[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  * 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.netconfnodestateservice.impl.rpc;
19
20 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.VesNotificationListener;
21 import org.opendaylight.mdsal.binding.api.RpcProviderService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.AttributeChangeNotificationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.FaultNotificationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.GetStatusInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.GetStatusOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.GetStatusOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.NetconfnodeStateService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushAttributeChangeNotificationInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushAttributeChangeNotificationOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushFaultNotificationInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushFaultNotificationOutput;
32 import org.opendaylight.yangtools.concepts.ObjectRegistration;
33 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.google.common.util.concurrent.ListenableFuture;
40 import java.util.List;
41
42 public class NetconfnodeStateServiceRpcApiImpl implements NetconfnodeStateService, AutoCloseable {
43
44     private static final Logger LOG = LoggerFactory.getLogger(NetconfnodeStateServiceRpcApiImpl.class);
45
46     private final ObjectRegistration<NetconfnodeStateServiceRpcApiImpl> rpcReg;
47     private RpcApigetStateCallback getStatusCallback;
48     private final List<VesNotificationListener> vesNotificationListenerList;
49
50     public NetconfnodeStateServiceRpcApiImpl(final RpcProviderService rpcProviderRegistry,
51             List<VesNotificationListener> vesNotificationListenerList) {
52
53         this.vesNotificationListenerList = vesNotificationListenerList;
54
55         // Register ourselves as the REST API RPC implementation
56         LOG.info("Register RPC Service " + NetconfnodeStateService.class.getSimpleName());
57         rpcReg = rpcProviderRegistry.registerRpcImplementation(NetconfnodeStateService.class, this);
58         this.getStatusCallback = null;
59     }
60
61     public NetconfnodeStateServiceRpcApiImpl setStatusCallback(RpcApigetStateCallback getStatusCallback) {
62         this.getStatusCallback = getStatusCallback;
63         return this;
64     }
65
66     @Override
67     public void close() throws Exception {
68         LOG.info("Close RPC Service");
69         if (rpcReg != null) {
70             rpcReg.close();
71         }
72     }
73
74     /*-------------------------------
75      * Interfaces for getting information
76      */
77     @Override
78     public ListenableFuture<RpcResult<GetStatusOutput>> getStatus(GetStatusInput input) {
79
80         LOG.info("RPC Request: getStatus input: {}", input);
81         RpcResultBuilder<GetStatusOutput> result;
82
83         try {
84             GetStatusOutputBuilder outputBuilder = new GetStatusOutputBuilder();
85             getStatusCallback.getStatus(input);
86             result = RpcResultBuilder.success(outputBuilder);
87         } catch (Exception e) {
88             result = RpcResultBuilder.failed();
89             result.withError(ErrorType.APPLICATION, "Exception", e);
90         }
91         return result.buildFuture();
92     }
93
94     @Override
95     public ListenableFuture<RpcResult<PushFaultNotificationOutput>> pushFaultNotification(
96             PushFaultNotificationInput input) {
97
98         RpcResultBuilder<PushFaultNotificationOutput> result;
99         try {
100             FaultNotificationBuilder faultNotificationBuilder = new FaultNotificationBuilder();
101             faultNotificationBuilder.fieldsFrom(input);
102             vesNotificationListenerList.forEach(item -> item.onNotification(faultNotificationBuilder.build()));
103             result = RpcResultBuilder.success();
104         } catch (Exception e) {
105             result = RpcResultBuilder.failed();
106             result.withError(ErrorType.APPLICATION, "Exception", e);
107         }
108         return result.buildFuture();
109     }
110
111     @Override
112     public ListenableFuture<RpcResult<PushAttributeChangeNotificationOutput>> pushAttributeChangeNotification(
113             PushAttributeChangeNotificationInput input) {
114         RpcResultBuilder<PushAttributeChangeNotificationOutput> result;
115         try {
116             AttributeChangeNotificationBuilder attributeChangeNotificationBuilder =
117                     new AttributeChangeNotificationBuilder();
118             attributeChangeNotificationBuilder.fieldsFrom(input);
119             vesNotificationListenerList
120                     .forEach(item -> item.onNotification(attributeChangeNotificationBuilder.build()));
121             result = RpcResultBuilder.success();
122         } catch (Exception e) {
123             result = RpcResultBuilder.failed();
124             result.withError(ErrorType.APPLICATION, "Exception", e);
125         }
126         return result.buildFuture();
127     }
128 }