19d34af0704c14e782ff5ba977ef54fd2711c81b
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / restconf / impl / RestconfProviderImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.sal.restconf.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.math.BigInteger;
12 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
13 import org.opendaylight.netconf.sal.rest.api.RestConnector;
14 import org.opendaylight.netconf.sal.restconf.impl.jmx.Config;
15 import org.opendaylight.netconf.sal.restconf.impl.jmx.Delete;
16 import org.opendaylight.netconf.sal.restconf.impl.jmx.Get;
17 import org.opendaylight.netconf.sal.restconf.impl.jmx.Operational;
18 import org.opendaylight.netconf.sal.restconf.impl.jmx.Post;
19 import org.opendaylight.netconf.sal.restconf.impl.jmx.Put;
20 import org.opendaylight.netconf.sal.restconf.impl.jmx.RestConnectorRuntimeMXBean;
21 import org.opendaylight.netconf.sal.restconf.impl.jmx.Rpcs;
22 import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
25
26 public class RestconfProviderImpl extends AbstractMXBean
27         implements AutoCloseable, RestConnector, RestConnectorRuntimeMXBean {
28     private final IpAddress websocketAddress;
29     private final PortNumber websocketPort;
30     private final StatisticsRestconfServiceWrapper stats;
31     private Thread webSocketServerThread;
32
33     public RestconfProviderImpl(StatisticsRestconfServiceWrapper stats, IpAddress websocketAddress,
34             PortNumber websocketPort) {
35         super("Draft02ProviderStatistics", "restconf-connector", null);
36         this.stats = Preconditions.checkNotNull(stats);
37         this.websocketAddress = Preconditions.checkNotNull(websocketAddress);
38         this.websocketPort = Preconditions.checkNotNull(websocketPort);
39     }
40
41     public void start() {
42         this.webSocketServerThread = new Thread(WebSocketServer.createInstance(
43                 new String(websocketAddress.getValue()), websocketPort.getValue()));
44         this.webSocketServerThread.setName("Web socket server on port " + websocketPort);
45         this.webSocketServerThread.start();
46
47         registerMBean();
48     }
49
50     @Override
51     public void close() {
52         WebSocketServer.destroyInstance();
53         if (this.webSocketServerThread != null) {
54             this.webSocketServerThread.interrupt();
55         }
56
57         unregisterMBean();
58     }
59
60     @Override
61     public Config getConfig() {
62         final Config config = new Config();
63
64         final Get get = new Get();
65         get.setReceivedRequests(this.stats.getConfigGet());
66         get.setSuccessfulResponses(this.stats.getSuccessGetConfig());
67         get.setFailedResponses(this.stats.getFailureGetConfig());
68         config.setGet(get);
69
70         final Post post = new Post();
71         post.setReceivedRequests(this.stats.getConfigPost());
72         post.setSuccessfulResponses(this.stats.getSuccessPost());
73         post.setFailedResponses(this.stats.getFailurePost());
74         config.setPost(post);
75
76         final Put put = new Put();
77         put.setReceivedRequests(this.stats.getConfigPut());
78         put.setSuccessfulResponses(this.stats.getSuccessPut());
79         put.setFailedResponses(this.stats.getFailurePut());
80         config.setPut(put);
81
82         final Delete delete = new Delete();
83         delete.setReceivedRequests(this.stats.getConfigDelete());
84         delete.setSuccessfulResponses(this.stats.getSuccessDelete());
85         delete.setFailedResponses(this.stats.getFailureDelete());
86         config.setDelete(delete);
87
88         return config;
89     }
90
91     @Override
92     public Operational getOperational() {
93         final BigInteger opGet = this.stats.getOperationalGet();
94         final Operational operational = new Operational();
95         final Get get = new Get();
96         get.setReceivedRequests(opGet);
97         get.setSuccessfulResponses(this.stats.getSuccessGetOperational());
98         get.setFailedResponses(this.stats.getFailureGetOperational());
99         operational.setGet(get);
100         return operational;
101     }
102
103     @Override
104     public Rpcs getRpcs() {
105         final BigInteger rpcInvoke = this.stats.getRpc();
106         final Rpcs rpcs = new Rpcs();
107         rpcs.setReceivedRequests(rpcInvoke);
108         return rpcs;
109     }
110 }