2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.restconf.nb.rfc8040;
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;
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;
38 * Provider for restconf draft18.
41 public class RestConnectorProvider<T extends ServiceWrapper> implements RestconfConnector, AutoCloseable {
43 private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
45 public static final TransactionChainListener TRANSACTION_CHAIN_LISTENER = new TransactionChainListener() {
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);
55 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
56 LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
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;
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;
73 private ListenerRegistration<SchemaContextListener> listenerRegistration;
74 private SchemaContextHandler schemaCtxHandler;
75 private T wrapperServices;
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);
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);
95 RestConnectorProvider.dataBroker = Preconditions.checkNotNull(domDataBroker);
98 public synchronized void start() {
99 mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
100 servicesProperties.add(mountPointServiceHandler);
102 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
103 servicesProperties.add(brokerHandler);
105 RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(dataBroker
106 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER));
107 servicesProperties.add(transactionChainHandler);
109 this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
110 servicesProperties.add(schemaCtxHandler);
111 this.listenerRegistration = domSchemaService.registerSchemaContextListener(this.schemaCtxHandler);
113 final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
114 servicesProperties.add(rpcServiceHandler);
116 final NotificationServiceHandler notificationServiceHandler =
117 new NotificationServiceHandler(notificationService);
118 servicesProperties.add(notificationServiceHandler);
120 if (wrapperServices != null) {
121 wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
122 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
123 notificationServiceHandler, domSchemaService);
127 public DOMMountPointServiceHandler getMountPointServiceHandler() {
128 return mountPointServiceHandler;
132 * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
135 * old {@link TransactionChain}
137 public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
138 LOG.trace("Resetting TransactionChain({})", chain);
140 RestConnectorProvider.transactionChainHandler.update(
141 Preconditions.checkNotNull(dataBroker).createTransactionChain(
142 RestConnectorProvider.TRANSACTION_CHAIN_LISTENER)
147 * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
148 * @return {@link DOMMountPointService}
150 public static DOMMountPointService getMountPointService() {
151 return mountPointServiceHandler.get();
155 public void close() throws Exception {
156 // close registration
157 if (this.listenerRegistration != null) {
158 this.listenerRegistration.close();
161 // close transaction chain
162 if (transactionChainHandler != null && transactionChainHandler.get() != null) {
163 transactionChainHandler.get().close();
166 transactionChainHandler = null;
167 mountPointServiceHandler = null;
171 public final synchronized Set<Object> getServicesProperties() {
172 return servicesProperties.build();