Bug 9256: Add websocket server config knob for ip
[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.controller.md.sal.dom.api.DOMDataBroker;
14 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
15 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
17 import org.opendaylight.controller.sal.core.api.model.SchemaService;
18 import org.opendaylight.netconf.sal.rest.api.RestConnector;
19 import org.opendaylight.netconf.sal.restconf.impl.jmx.Config;
20 import org.opendaylight.netconf.sal.restconf.impl.jmx.Delete;
21 import org.opendaylight.netconf.sal.restconf.impl.jmx.Get;
22 import org.opendaylight.netconf.sal.restconf.impl.jmx.Operational;
23 import org.opendaylight.netconf.sal.restconf.impl.jmx.Post;
24 import org.opendaylight.netconf.sal.restconf.impl.jmx.Put;
25 import org.opendaylight.netconf.sal.restconf.impl.jmx.RestConnectorRuntimeMXBean;
26 import org.opendaylight.netconf.sal.restconf.impl.jmx.Rpcs;
27 import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
32
33 public class RestconfProviderImpl extends AbstractMXBean
34         implements AutoCloseable, RestConnector, RestConnectorRuntimeMXBean {
35     private final DOMDataBroker domDataBroker;
36     private final SchemaService schemaService;
37     private final DOMRpcService rpcService;
38     private final DOMNotificationService notificationService;
39     private final DOMMountPointService mountPointService;
40     private final IpAddress websocketAddress;
41     private final PortNumber websocketPort;
42     private final StatisticsRestconfServiceWrapper stats = StatisticsRestconfServiceWrapper.getInstance();
43     private ListenerRegistration<SchemaContextListener> listenerRegistration;
44     private Thread webSocketServerThread;
45
46     public RestconfProviderImpl(DOMDataBroker domDataBroker, SchemaService schemaService, DOMRpcService rpcService,
47             DOMNotificationService notificationService, DOMMountPointService mountPointService,
48             IpAddress websocketAddress, PortNumber websocketPort) {
49         super("Draft02ProviderStatistics", "restconf-connector", null);
50         this.domDataBroker = Preconditions.checkNotNull(domDataBroker);
51         this.schemaService = Preconditions.checkNotNull(schemaService);
52         this.rpcService = Preconditions.checkNotNull(rpcService);
53         this.notificationService = Preconditions.checkNotNull(notificationService);
54         this.mountPointService = Preconditions.checkNotNull(mountPointService);
55         this.websocketAddress = Preconditions.checkNotNull(websocketAddress);
56         this.websocketPort = Preconditions.checkNotNull(websocketPort);
57     }
58
59     public void start() {
60         this.listenerRegistration = schemaService.registerSchemaContextListener(ControllerContext.getInstance());
61
62         BrokerFacade.getInstance().setDomDataBroker(domDataBroker);
63         BrokerFacade.getInstance().setRpcService(rpcService);
64         BrokerFacade.getInstance().setDomNotificationService(notificationService);
65
66         ControllerContext.getInstance().setSchemas(schemaService.getGlobalContext());
67         ControllerContext.getInstance().setMountService(mountPointService);
68
69         this.webSocketServerThread = new Thread(WebSocketServer.createInstance(
70                 new String(websocketAddress.getValue()), websocketPort.getValue()));
71         this.webSocketServerThread.setName("Web socket server on port " + websocketPort);
72         this.webSocketServerThread.start();
73
74         registerMBean();
75     }
76
77     @Override
78     public void close() {
79         BrokerFacade.getInstance().setDomDataBroker(null);
80
81         if (this.listenerRegistration != null) {
82             this.listenerRegistration.close();
83         }
84
85         WebSocketServer.destroyInstance();
86         if (this.webSocketServerThread != null) {
87             this.webSocketServerThread.interrupt();
88         }
89
90         unregisterMBean();
91     }
92
93     @Override
94     public Config getConfig() {
95         final Config config = new Config();
96
97         final Get get = new Get();
98         get.setReceivedRequests(this.stats.getConfigGet());
99         get.setSuccessfulResponses(this.stats.getSuccessGetConfig());
100         get.setFailedResponses(this.stats.getFailureGetConfig());
101         config.setGet(get);
102
103         final Post post = new Post();
104         post.setReceivedRequests(this.stats.getConfigPost());
105         post.setSuccessfulResponses(this.stats.getSuccessPost());
106         post.setFailedResponses(this.stats.getFailurePost());
107         config.setPost(post);
108
109         final Put put = new Put();
110         put.setReceivedRequests(this.stats.getConfigPut());
111         put.setSuccessfulResponses(this.stats.getSuccessPut());
112         put.setFailedResponses(this.stats.getFailurePut());
113         config.setPut(put);
114
115         final Delete delete = new Delete();
116         delete.setReceivedRequests(this.stats.getConfigDelete());
117         delete.setSuccessfulResponses(this.stats.getSuccessDelete());
118         delete.setFailedResponses(this.stats.getFailureDelete());
119         config.setDelete(delete);
120
121         return config;
122     }
123
124     @Override
125     public Operational getOperational() {
126         final BigInteger opGet = this.stats.getOperationalGet();
127         final Operational operational = new Operational();
128         final Get get = new Get();
129         get.setReceivedRequests(opGet);
130         get.setSuccessfulResponses(this.stats.getSuccessGetOperational());
131         get.setFailedResponses(this.stats.getFailureGetOperational());
132         operational.setGet(get);
133         return operational;
134     }
135
136     @Override
137     public Rpcs getRpcs() {
138         final BigInteger rpcInvoke = this.stats.getRpc();
139         final Rpcs rpcs = new Rpcs();
140         rpcs.setReceivedRequests(rpcInvoke);
141         return rpcs;
142     }
143 }