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