summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java
blob: 5b6608e87d8606ab0bad3f7cc60dbfdf2470f387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.netconf.sal.restconf.impl;

import static java.util.Objects.requireNonNull;

import java.math.BigInteger;
import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
import org.opendaylight.netconf.sal.rest.api.RestConnector;
import org.opendaylight.netconf.sal.restconf.impl.jmx.Config;
import org.opendaylight.netconf.sal.restconf.impl.jmx.Delete;
import org.opendaylight.netconf.sal.restconf.impl.jmx.Get;
import org.opendaylight.netconf.sal.restconf.impl.jmx.Operational;
import org.opendaylight.netconf.sal.restconf.impl.jmx.Post;
import org.opendaylight.netconf.sal.restconf.impl.jmx.Put;
import org.opendaylight.netconf.sal.restconf.impl.jmx.RestConnectorRuntimeMXBean;
import org.opendaylight.netconf.sal.restconf.impl.jmx.Rpcs;
import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;

public class RestconfProviderImpl extends AbstractMXBean
        implements AutoCloseable, RestConnector, RestConnectorRuntimeMXBean {
    private final IpAddress websocketAddress;
    private final PortNumber websocketPort;
    private final StatisticsRestconfServiceWrapper stats;
    private Thread webSocketServerThread;

    public RestconfProviderImpl(final StatisticsRestconfServiceWrapper stats, final IpAddress websocketAddress,
            final PortNumber websocketPort) {
        super("Draft02ProviderStatistics", "restconf-connector", null);
        this.stats = requireNonNull(stats);
        this.websocketAddress = requireNonNull(websocketAddress);
        this.websocketPort = requireNonNull(websocketPort);
    }

    public void start() {
        this.webSocketServerThread = new Thread(WebSocketServer.createInstance(
                websocketAddress.stringValue(), websocketPort.getValue().toJava()));
        this.webSocketServerThread.setName("Web socket server on port " + websocketPort);
        this.webSocketServerThread.start();

        registerMBean();
    }

    @Override
    public void close() {
        WebSocketServer.destroyInstance();
        if (this.webSocketServerThread != null) {
            this.webSocketServerThread.interrupt();
        }

        unregisterMBean();
    }

    @Override
    public Config getConfig() {
        final Config config = new Config();

        final Get get = new Get();
        get.setReceivedRequests(this.stats.getConfigGet());
        get.setSuccessfulResponses(this.stats.getSuccessGetConfig());
        get.setFailedResponses(this.stats.getFailureGetConfig());
        config.setGet(get);

        final Post post = new Post();
        post.setReceivedRequests(this.stats.getConfigPost());
        post.setSuccessfulResponses(this.stats.getSuccessPost());
        post.setFailedResponses(this.stats.getFailurePost());
        config.setPost(post);

        final Put put = new Put();
        put.setReceivedRequests(this.stats.getConfigPut());
        put.setSuccessfulResponses(this.stats.getSuccessPut());
        put.setFailedResponses(this.stats.getFailurePut());
        config.setPut(put);

        final Delete delete = new Delete();
        delete.setReceivedRequests(this.stats.getConfigDelete());
        delete.setSuccessfulResponses(this.stats.getSuccessDelete());
        delete.setFailedResponses(this.stats.getFailureDelete());
        config.setDelete(delete);

        return config;
    }

    @Override
    public Operational getOperational() {
        final BigInteger opGet = this.stats.getOperationalGet();
        final Operational operational = new Operational();
        final Get get = new Get();
        get.setReceivedRequests(opGet);
        get.setSuccessfulResponses(this.stats.getSuccessGetOperational());
        get.setFailedResponses(this.stats.getFailureGetOperational());
        operational.setGet(get);
        return operational;
    }

    @Override
    public Rpcs getRpcs() {
        final BigInteger rpcInvoke = this.stats.getRpc();
        final Rpcs rpcs = new Rpcs();
        rpcs.setReceivedRequests(rpcInvoke);
        return rpcs;
    }
}