Make ServicesWrapperImpl as argument in provider
[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 org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
19 import org.opendaylight.controller.sal.core.api.model.SchemaService;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
22 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
23 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
24 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
25 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
26 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
27 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServiceWrapper;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Provider for restconf draft18.
35  *
36  */
37 public class RestConnectorProvider<T extends ServiceWrapper> implements RestconfConnector, AutoCloseable {
38
39     private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
40
41     public static final TransactionChainListener TRANSACTION_CHAIN_LISTENER = new TransactionChainListener() {
42         @Override
43         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
44                 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
45             LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
46             resetTransactionChainForAdapaters(chain);
47             throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
48         }
49
50         @Override
51         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
52             LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
53         }
54     };
55
56     private static TransactionChainHandler transactionChainHandler;
57     private static DOMDataBroker dataBroker;
58     private static DOMMountPointServiceHandler mountPointServiceHandler;
59
60     private final SchemaService schemaService;
61     private final DOMRpcService rpcService;
62     private final DOMNotificationService notificationService;
63     private final DOMMountPointService mountPointService;
64     private final T wrapperServices;
65
66     private ListenerRegistration<SchemaContextListener> listenerRegistration;
67     private SchemaContextHandler schemaCtxHandler;
68
69     public RestConnectorProvider(final DOMDataBroker domDataBroker,
70             final SchemaService schemaService, final DOMRpcService rpcService,
71             final DOMNotificationService notificationService, final DOMMountPointService mountPointService,
72             final T wrapperServices) {
73         this.wrapperServices = Preconditions.checkNotNull(wrapperServices);
74         this.schemaService = Preconditions.checkNotNull(schemaService);
75         this.rpcService = Preconditions.checkNotNull(rpcService);
76         this.notificationService = Preconditions.checkNotNull(notificationService);
77         this.mountPointService = Preconditions.checkNotNull(mountPointService);
78
79         RestConnectorProvider.dataBroker = Preconditions.checkNotNull(domDataBroker);
80     }
81
82     public void start() {
83         mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
84
85         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
86
87         RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(dataBroker
88                 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER));
89
90         this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
91         this.listenerRegistration = schemaService.registerSchemaContextListener(this.schemaCtxHandler);
92
93         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
94
95         final NotificationServiceHandler notificationServiceHandler =
96                 new NotificationServiceHandler(notificationService);
97
98         wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
99                 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
100                 notificationServiceHandler);
101     }
102
103     public DOMMountPointServiceHandler getMountPointServiceHandler() {
104         return mountPointServiceHandler;
105     }
106
107     /**
108      * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
109      *
110      * @param chain
111      *             old {@link TransactionChain}
112      */
113     public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
114         LOG.trace("Resetting TransactionChain({})", chain);
115         chain.close();
116         RestConnectorProvider.transactionChainHandler.update(
117                 Preconditions.checkNotNull(dataBroker).createTransactionChain(
118                         RestConnectorProvider.TRANSACTION_CHAIN_LISTENER)
119         );
120     }
121
122     /**
123      * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
124      * @return {@link DOMMountPointService}
125      */
126     public static DOMMountPointService getMountPointService() {
127         return mountPointServiceHandler.get();
128     }
129
130     @Override
131     public void close() throws Exception {
132         // close registration
133         if (this.listenerRegistration != null) {
134             this.listenerRegistration.close();
135         }
136
137         // close transaction chain
138         if (transactionChainHandler != null && transactionChainHandler.get() != null) {
139             transactionChainHandler.get().close();
140         }
141
142         transactionChainHandler = null;
143         mountPointServiceHandler = null;
144         dataBroker = null;
145     }
146 }