Merge "Remove deprecated registerDataChangeListener method"
[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 com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.ImmutableSet.Builder;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.Set;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
20 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
26 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
27 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
28 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
29 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
30 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
31 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServiceWrapper;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Provider for restconf draft18.
39  *
40  */
41 public class RestConnectorProvider<T extends ServiceWrapper> implements RestconfConnector, AutoCloseable {
42
43     private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
44
45     public static final TransactionChainListener TRANSACTION_CHAIN_LISTENER = new TransactionChainListener() {
46         @Override
47         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
48                 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
49             LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
50             resetTransactionChainForAdapaters(chain);
51             throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
52         }
53
54         @Override
55         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
56             LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
57         }
58     };
59
60     @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
61     private static volatile TransactionChainHandler transactionChainHandler;
62     @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
63     private static volatile DOMDataBroker dataBroker;
64     @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
65     private static volatile DOMMountPointServiceHandler mountPointServiceHandler;
66
67     private final DOMRpcService rpcService;
68     private final DOMNotificationService notificationService;
69     private final DOMMountPointService mountPointService;
70     private final DOMSchemaService domSchemaService;
71     private final Builder<Object> servicesProperties;
72
73     private ListenerRegistration<SchemaContextListener> listenerRegistration;
74     private SchemaContextHandler schemaCtxHandler;
75     private T wrapperServices;
76
77     // FIXME: refactor this class and its users to interact via builder pattern, where individual
78     // services are injected and then the provider is created
79     public RestConnectorProvider(final DOMDataBroker domDataBroker,
80             final DOMSchemaService domSchemaService, final DOMRpcService rpcService,
81             final DOMNotificationService notificationService, final DOMMountPointService mountPointService) {
82         this(domDataBroker, domSchemaService, rpcService, notificationService, mountPointService, null);
83     }
84
85     public RestConnectorProvider(final DOMDataBroker domDataBroker, final DOMSchemaService domSchemaService,
86             final DOMRpcService rpcService, final DOMNotificationService notificationService,
87             final DOMMountPointService mountPointService, final T wrapperServices) {
88         this.servicesProperties = ImmutableSet.<Object>builder();
89         this.wrapperServices = wrapperServices;
90         this.domSchemaService = Preconditions.checkNotNull(domSchemaService);
91         this.rpcService = Preconditions.checkNotNull(rpcService);
92         this.notificationService = Preconditions.checkNotNull(notificationService);
93         this.mountPointService = Preconditions.checkNotNull(mountPointService);
94
95         RestConnectorProvider.dataBroker = Preconditions.checkNotNull(domDataBroker);
96     }
97
98     public synchronized void start() {
99         mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
100         servicesProperties.add(mountPointServiceHandler);
101
102         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
103         servicesProperties.add(brokerHandler);
104
105         RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(dataBroker
106                 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER));
107         servicesProperties.add(transactionChainHandler);
108
109         this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
110         servicesProperties.add(schemaCtxHandler);
111         this.listenerRegistration = domSchemaService.registerSchemaContextListener(this.schemaCtxHandler);
112
113         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
114         servicesProperties.add(rpcServiceHandler);
115
116         final NotificationServiceHandler notificationServiceHandler =
117                 new NotificationServiceHandler(notificationService);
118         servicesProperties.add(notificationServiceHandler);
119
120         if (wrapperServices != null) {
121             wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
122                     RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
123                     notificationServiceHandler, domSchemaService);
124         }
125     }
126
127     public DOMMountPointServiceHandler getMountPointServiceHandler() {
128         return mountPointServiceHandler;
129     }
130
131     /**
132      * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
133      *
134      * @param chain
135      *             old {@link TransactionChain}
136      */
137     public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
138         LOG.trace("Resetting TransactionChain({})", chain);
139         chain.close();
140         RestConnectorProvider.transactionChainHandler.update(
141                 Preconditions.checkNotNull(dataBroker).createTransactionChain(
142                         RestConnectorProvider.TRANSACTION_CHAIN_LISTENER)
143         );
144     }
145
146     /**
147      * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
148      * @return {@link DOMMountPointService}
149      */
150     public static DOMMountPointService getMountPointService() {
151         return mountPointServiceHandler.get();
152     }
153
154     @Override
155     public void close() throws Exception {
156         // close registration
157         if (this.listenerRegistration != null) {
158             this.listenerRegistration.close();
159         }
160
161         // close transaction chain
162         if (transactionChainHandler != null && transactionChainHandler.get() != null) {
163             transactionChainHandler.get().close();
164         }
165
166         transactionChainHandler = null;
167         mountPointServiceHandler = null;
168         dataBroker = null;
169     }
170
171     public final synchronized Set<Object> getServicesProperties() {
172         return servicesProperties.build();
173     }
174 }