5b6608e87d8606ab0bad3f7cc60dbfdf2470f387
[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 static java.util.Objects.requireNonNull;
11
12 import java.math.BigInteger;
13 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
14 import org.opendaylight.netconf.sal.rest.api.RestConnector;
15 import org.opendaylight.netconf.sal.restconf.impl.jmx.Config;
16 import org.opendaylight.netconf.sal.restconf.impl.jmx.Delete;
17 import org.opendaylight.netconf.sal.restconf.impl.jmx.Get;
18 import org.opendaylight.netconf.sal.restconf.impl.jmx.Operational;
19 import org.opendaylight.netconf.sal.restconf.impl.jmx.Post;
20 import org.opendaylight.netconf.sal.restconf.impl.jmx.Put;
21 import org.opendaylight.netconf.sal.restconf.impl.jmx.RestConnectorRuntimeMXBean;
22 import org.opendaylight.netconf.sal.restconf.impl.jmx.Rpcs;
23 import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
26
27 public class RestconfProviderImpl extends AbstractMXBean
28         implements AutoCloseable, RestConnector, RestConnectorRuntimeMXBean {
29     private final IpAddress websocketAddress;
30     private final PortNumber websocketPort;
31     private final StatisticsRestconfServiceWrapper stats;
32     private Thread webSocketServerThread;
33
34     public RestconfProviderImpl(final StatisticsRestconfServiceWrapper stats, final IpAddress websocketAddress,
35             final PortNumber websocketPort) {
36         super("Draft02ProviderStatistics", "restconf-connector", null);
37         this.stats = requireNonNull(stats);
38         this.websocketAddress = requireNonNull(websocketAddress);
39         this.websocketPort = requireNonNull(websocketPort);
40     }
41
42     public void start() {
43         this.webSocketServerThread = new Thread(WebSocketServer.createInstance(
44                 websocketAddress.stringValue(), websocketPort.getValue().toJava()));
45         this.webSocketServerThread.setName("Web socket server on port " + websocketPort);
46         this.webSocketServerThread.start();
47
48         registerMBean();
49     }
50
51     @Override
52     public void close() {
53         WebSocketServer.destroyInstance();
54         if (this.webSocketServerThread != null) {
55             this.webSocketServerThread.interrupt();
56         }
57
58         unregisterMBean();
59     }
60
61     @Override
62     public Config getConfig() {
63         final Config config = new Config();
64
65         final Get get = new Get();
66         get.setReceivedRequests(this.stats.getConfigGet());
67         get.setSuccessfulResponses(this.stats.getSuccessGetConfig());
68         get.setFailedResponses(this.stats.getFailureGetConfig());
69         config.setGet(get);
70
71         final Post post = new Post();
72         post.setReceivedRequests(this.stats.getConfigPost());
73         post.setSuccessfulResponses(this.stats.getSuccessPost());
74         post.setFailedResponses(this.stats.getFailurePost());
75         config.setPost(post);
76
77         final Put put = new Put();
78         put.setReceivedRequests(this.stats.getConfigPut());
79         put.setSuccessfulResponses(this.stats.getSuccessPut());
80         put.setFailedResponses(this.stats.getFailurePut());
81         config.setPut(put);
82
83         final Delete delete = new Delete();
84         delete.setReceivedRequests(this.stats.getConfigDelete());
85         delete.setSuccessfulResponses(this.stats.getSuccessDelete());
86         delete.setFailedResponses(this.stats.getFailureDelete());
87         config.setDelete(delete);
88
89         return config;
90     }
91
92     @Override
93     public Operational getOperational() {
94         final BigInteger opGet = this.stats.getOperationalGet();
95         final Operational operational = new Operational();
96         final Get get = new Get();
97         get.setReceivedRequests(opGet);
98         get.setSuccessfulResponses(this.stats.getSuccessGetOperational());
99         get.setFailedResponses(this.stats.getFailureGetOperational());
100         operational.setGet(get);
101         return operational;
102     }
103
104     @Override
105     public Rpcs getRpcs() {
106         final BigInteger rpcInvoke = this.stats.getRpc();
107         final Rpcs rpcs = new Rpcs();
108         rpcs.setReceivedRequests(rpcInvoke);
109         return rpcs;
110     }
111 }