e13a2f691ed2a04769715eacf8144168cf451431
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / RestConnectorProvider.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.restconf.nb.rfc8040;
10
11 import com.google.common.base.Preconditions;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
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.mdsal.dom.api.DOMSchemaService;
18 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
19 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
20 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
21 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
22 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
23 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
24 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServiceWrapper;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Provider for restconf draft18.
30  *
31  */
32 public class RestConnectorProvider<T extends ServiceWrapper> implements RestconfConnector, AutoCloseable {
33
34     private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
35
36     @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
37     private static volatile DOMMountPointServiceHandler mountPointServiceHandler;
38
39     private final DOMRpcService rpcService;
40     private final DOMNotificationService notificationService;
41     private final DOMMountPointService mountPointService;
42     private final DOMSchemaService domSchemaService;
43     private final TransactionChainHandler transactionChainHandler;
44     private final DOMDataBroker dataBroker;
45     private final SchemaContextHandler schemaCtxHandler;
46     private final T wrapperServices;
47
48     public RestConnectorProvider(final DOMDataBroker domDataBroker, final DOMSchemaService domSchemaService,
49             final DOMRpcService rpcService, final DOMNotificationService notificationService,
50             final DOMMountPointService mountPointService, final TransactionChainHandler transactionChainHandler,
51             final SchemaContextHandler schemaCtxHandler, final T wrapperServices) {
52         this.wrapperServices = wrapperServices;
53         this.domSchemaService = Preconditions.checkNotNull(domSchemaService);
54         this.rpcService = Preconditions.checkNotNull(rpcService);
55         this.notificationService = Preconditions.checkNotNull(notificationService);
56         this.mountPointService = Preconditions.checkNotNull(mountPointService);
57         this.transactionChainHandler = Preconditions.checkNotNull(transactionChainHandler);
58         this.dataBroker = Preconditions.checkNotNull(domDataBroker);
59         this.schemaCtxHandler = Preconditions.checkNotNull(schemaCtxHandler);
60     }
61
62     public synchronized void start() {
63         mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
64
65         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
66
67         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
68
69         final NotificationServiceHandler notificationServiceHandler =
70                 new NotificationServiceHandler(notificationService);
71
72         if (wrapperServices != null) {
73             wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
74                     transactionChainHandler, brokerHandler, rpcServiceHandler,
75                     notificationServiceHandler, domSchemaService);
76         }
77     }
78
79     public DOMMountPointServiceHandler getMountPointServiceHandler() {
80         return mountPointServiceHandler;
81     }
82
83     /**
84      * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
85      * @return {@link DOMMountPointService}
86      */
87     public static DOMMountPointService getMountPointService() {
88         return mountPointServiceHandler.get();
89     }
90
91     @Override
92     public void close() {
93         mountPointServiceHandler = null;
94     }
95 }