Use BrokerFacade non-statically
[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 = StatisticsRestconfServiceWrapper.getInstance();
31     private Thread webSocketServerThread;
32
33     public RestconfProviderImpl(IpAddress websocketAddress, PortNumber websocketPort) {
34         super("Draft02ProviderStatistics", "restconf-connector", null);
35         this.websocketAddress = Preconditions.checkNotNull(websocketAddress);
36         this.websocketPort = Preconditions.checkNotNull(websocketPort);
37     }
38
39     public void start() {
40         this.webSocketServerThread = new Thread(WebSocketServer.createInstance(
41                 new String(websocketAddress.getValue()), websocketPort.getValue()));
42         this.webSocketServerThread.setName("Web socket server on port " + websocketPort);
43         this.webSocketServerThread.start();
44
45         registerMBean();
46     }
47
48     @Override
49     public void close() {
50         WebSocketServer.destroyInstance();
51         if (this.webSocketServerThread != null) {
52             this.webSocketServerThread.interrupt();
53         }
54
55         unregisterMBean();
56     }
57
58     @Override
59     public Config getConfig() {
60         final Config config = new Config();
61
62         final Get get = new Get();
63         get.setReceivedRequests(this.stats.getConfigGet());
64         get.setSuccessfulResponses(this.stats.getSuccessGetConfig());
65         get.setFailedResponses(this.stats.getFailureGetConfig());
66         config.setGet(get);
67
68         final Post post = new Post();
69         post.setReceivedRequests(this.stats.getConfigPost());
70         post.setSuccessfulResponses(this.stats.getSuccessPost());
71         post.setFailedResponses(this.stats.getFailurePost());
72         config.setPost(post);
73
74         final Put put = new Put();
75         put.setReceivedRequests(this.stats.getConfigPut());
76         put.setSuccessfulResponses(this.stats.getSuccessPut());
77         put.setFailedResponses(this.stats.getFailurePut());
78         config.setPut(put);
79
80         final Delete delete = new Delete();
81         delete.setReceivedRequests(this.stats.getConfigDelete());
82         delete.setSuccessfulResponses(this.stats.getSuccessDelete());
83         delete.setFailedResponses(this.stats.getFailureDelete());
84         config.setDelete(delete);
85
86         return config;
87     }
88
89     @Override
90     public Operational getOperational() {
91         final BigInteger opGet = this.stats.getOperationalGet();
92         final Operational operational = new Operational();
93         final Get get = new Get();
94         get.setReceivedRequests(opGet);
95         get.setSuccessfulResponses(this.stats.getSuccessGetOperational());
96         get.setFailedResponses(this.stats.getFailureGetOperational());
97         operational.setGet(get);
98         return operational;
99     }
100
101     @Override
102     public Rpcs getRpcs() {
103         final BigInteger rpcInvoke = this.stats.getRpc();
104         final Rpcs rpcs = new Rpcs();
105         rpcs.setReceivedRequests(rpcInvoke);
106         return rpcs;
107     }
108 }