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.controller.sal.core.api.model.SchemaService;
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 SchemaService schemaService;
64 private final DOMRpcService rpcService;
65 private final DOMNotificationService notificationService;
66 private final DOMMountPointService mountPointService;
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 SchemaService schemaService, final DOMRpcService rpcService,
77 final DOMNotificationService notificationService, final DOMMountPointService mountPointService) {
78 this(domDataBroker, schemaService, rpcService, notificationService, mountPointService, null);
81 public RestConnectorProvider(final DOMDataBroker domDataBroker, final SchemaService schemaService,
82 final DOMRpcService rpcService,
83 final DOMNotificationService notificationService, final DOMMountPointService mountPointService,
84 final T wrapperServices) {
85 this.servicesProperties = ImmutableSet.<Object>builder();
86 this.wrapperServices = wrapperServices;
87 this.schemaService = Preconditions.checkNotNull(schemaService);
88 this.rpcService = Preconditions.checkNotNull(rpcService);
89 this.notificationService = Preconditions.checkNotNull(notificationService);
90 this.mountPointService = Preconditions.checkNotNull(mountPointService);
92 RestConnectorProvider.dataBroker = Preconditions.checkNotNull(domDataBroker);
95 public synchronized void start() {
96 mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
97 servicesProperties.add(mountPointServiceHandler);
99 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
100 servicesProperties.add(brokerHandler);
102 RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(dataBroker
103 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER));
104 servicesProperties.add(transactionChainHandler);
106 this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
107 servicesProperties.add(schemaCtxHandler);
108 this.listenerRegistration = schemaService.registerSchemaContextListener(this.schemaCtxHandler);
110 final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
111 servicesProperties.add(rpcServiceHandler);
113 final NotificationServiceHandler notificationServiceHandler =
114 new NotificationServiceHandler(notificationService);
115 servicesProperties.add(notificationServiceHandler);
117 if (wrapperServices != null) {
118 wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
119 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
120 notificationServiceHandler);
124 public DOMMountPointServiceHandler getMountPointServiceHandler() {
125 return mountPointServiceHandler;
129 * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
132 * old {@link TransactionChain}
134 public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
135 LOG.trace("Resetting TransactionChain({})", chain);
137 RestConnectorProvider.transactionChainHandler.update(
138 Preconditions.checkNotNull(dataBroker).createTransactionChain(
139 RestConnectorProvider.TRANSACTION_CHAIN_LISTENER)
144 * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
145 * @return {@link DOMMountPointService}
147 public static DOMMountPointService getMountPointService() {
148 return mountPointServiceHandler.get();
152 public void close() throws Exception {
153 // close registration
154 if (this.listenerRegistration != null) {
155 this.listenerRegistration.close();
158 // close transaction chain
159 if (transactionChainHandler != null && transactionChainHandler.get() != null) {
160 transactionChainHandler.get().close();
163 transactionChainHandler = null;
164 mountPointServiceHandler = null;
168 public final synchronized Set<Object> getServicesProperties() {
169 return servicesProperties.build();