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