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;
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;
37 * Provider for restconf draft18.
40 public class RestConnectorProvider<T extends ServiceWrapper> implements RestconfConnector, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
44 public static final TransactionChainListener TRANSACTION_CHAIN_LISTENER = new TransactionChainListener() {
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);
54 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
55 LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
59 private static TransactionChainHandler transactionChainHandler;
60 private static DOMDataBroker dataBroker;
61 private static DOMMountPointServiceHandler mountPointServiceHandler;
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;
69 private ListenerRegistration<SchemaContextListener> listenerRegistration;
70 private SchemaContextHandler schemaCtxHandler;
71 private T wrapperServices;
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);
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);
91 RestConnectorProvider.dataBroker = Preconditions.checkNotNull(domDataBroker);
94 public synchronized void start() {
95 mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
96 servicesProperties.add(mountPointServiceHandler);
98 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
99 servicesProperties.add(brokerHandler);
101 RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(dataBroker
102 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER));
103 servicesProperties.add(transactionChainHandler);
105 this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
106 servicesProperties.add(schemaCtxHandler);
107 this.listenerRegistration = domSchemaService.registerSchemaContextListener(this.schemaCtxHandler);
109 final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
110 servicesProperties.add(rpcServiceHandler);
112 final NotificationServiceHandler notificationServiceHandler =
113 new NotificationServiceHandler(notificationService);
114 servicesProperties.add(notificationServiceHandler);
116 if (wrapperServices != null) {
117 wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
118 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
119 notificationServiceHandler, domSchemaService);
123 public DOMMountPointServiceHandler getMountPointServiceHandler() {
124 return mountPointServiceHandler;
128 * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
131 * old {@link TransactionChain}
133 public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
134 LOG.trace("Resetting TransactionChain({})", chain);
136 RestConnectorProvider.transactionChainHandler.update(
137 Preconditions.checkNotNull(dataBroker).createTransactionChain(
138 RestConnectorProvider.TRANSACTION_CHAIN_LISTENER)
143 * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
144 * @return {@link DOMMountPointService}
146 public static DOMMountPointService getMountPointService() {
147 return mountPointServiceHandler.get();
151 public void close() throws Exception {
152 // close registration
153 if (this.listenerRegistration != null) {
154 this.listenerRegistration.close();
157 // close transaction chain
158 if (transactionChainHandler != null && transactionChainHandler.get() != null) {
159 transactionChainHandler.get().close();
162 transactionChainHandler = null;
163 mountPointServiceHandler = null;
167 public final synchronized Set<Object> getServicesProperties() {
168 return servicesProperties.build();