Remove static TransactionChainHandler instance
[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.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Provider for restconf draft18.
32  *
33  */
34 public class RestConnectorProvider<T extends ServiceWrapper> implements RestconfConnector, AutoCloseable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
37
38     @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
39     private static volatile DOMMountPointServiceHandler mountPointServiceHandler;
40
41     private final DOMRpcService rpcService;
42     private final DOMNotificationService notificationService;
43     private final DOMMountPointService mountPointService;
44     private final DOMSchemaService domSchemaService;
45     private final TransactionChainHandler transactionChainHandler;
46     private final DOMDataBroker dataBroker;
47
48     private ListenerRegistration<SchemaContextListener> listenerRegistration;
49     private SchemaContextHandler schemaCtxHandler;
50     private final T wrapperServices;
51
52     public RestConnectorProvider(final DOMDataBroker domDataBroker, final DOMSchemaService domSchemaService,
53             final DOMRpcService rpcService, final DOMNotificationService notificationService,
54             final DOMMountPointService mountPointService, final TransactionChainHandler transactionChainHandler,
55             final T wrapperServices) {
56         this.wrapperServices = wrapperServices;
57         this.domSchemaService = Preconditions.checkNotNull(domSchemaService);
58         this.rpcService = Preconditions.checkNotNull(rpcService);
59         this.notificationService = Preconditions.checkNotNull(notificationService);
60         this.mountPointService = Preconditions.checkNotNull(mountPointService);
61         this.transactionChainHandler = Preconditions.checkNotNull(transactionChainHandler);
62         this.dataBroker = Preconditions.checkNotNull(domDataBroker);
63     }
64
65     public synchronized void start() {
66         mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
67
68         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
69
70         this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
71         this.listenerRegistration = domSchemaService.registerSchemaContextListener(this.schemaCtxHandler);
72
73         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
74
75         final NotificationServiceHandler notificationServiceHandler =
76                 new NotificationServiceHandler(notificationService);
77
78         if (wrapperServices != null) {
79             wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
80                     transactionChainHandler, brokerHandler, rpcServiceHandler,
81                     notificationServiceHandler, domSchemaService);
82         }
83     }
84
85     public DOMMountPointServiceHandler getMountPointServiceHandler() {
86         return mountPointServiceHandler;
87     }
88
89     /**
90      * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
91      * @return {@link DOMMountPointService}
92      */
93     public static DOMMountPointService getMountPointService() {
94         return mountPointServiceHandler.get();
95     }
96
97     @Override
98     public void close() throws Exception {
99         // close registration
100         if (this.listenerRegistration != null) {
101             this.listenerRegistration.close();
102         }
103
104         mountPointServiceHandler = null;
105     }
106 }