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;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
19 import org.opendaylight.controller.sal.core.api.model.SchemaService;
20 import org.opendaylight.netconf.sal.rest.api.RestConnector;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.opendaylight.restconf.common.wrapper.services.ServicesWrapperImpl;
23 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
24 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
25 import org.opendaylight.restconf.handlers.NotificationServiceHandler;
26 import org.opendaylight.restconf.handlers.RpcServiceHandler;
27 import org.opendaylight.restconf.handlers.SchemaContextHandler;
28 import org.opendaylight.restconf.handlers.TransactionChainHandler;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Provider for restconf draft18.
38 public class RestConnectorProvider implements RestConnector, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
42 public static final TransactionChainListener TRANSACTION_CHAIN_LISTENER = new TransactionChainListener() {
44 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
45 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
46 LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
47 resetTransactionChainForAdapaters(chain);
48 throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
52 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
53 LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
57 private static TransactionChainHandler transactionChainHandler;
58 private static DOMDataBroker dataBroker;
59 private static DOMMountPointServiceHandler mountPointServiceHandler;
61 private final SchemaService schemaService;
62 private final DOMRpcService rpcService;
63 private final DOMNotificationService notificationService;
64 private final DOMMountPointService mountPointService;
65 private ListenerRegistration<SchemaContextListener> listenerRegistration;
67 private SchemaContextHandler schemaCtxHandler;
69 public RestConnectorProvider(DOMDataBroker domDataBroker, SchemaService schemaService, DOMRpcService rpcService,
70 DOMNotificationService notificationService, DOMMountPointService mountPointService) {
71 this.schemaService = Preconditions.checkNotNull(schemaService);
72 this.rpcService = Preconditions.checkNotNull(rpcService);
73 this.notificationService = Preconditions.checkNotNull(notificationService);
74 this.mountPointService = Preconditions.checkNotNull(mountPointService);
76 RestConnectorProvider.dataBroker = Preconditions.checkNotNull(domDataBroker);
80 final ServicesWrapperImpl wrapperServices = ServicesWrapperImpl.getInstance();
82 mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
84 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
86 RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(dataBroker
87 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER));
89 this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
90 this.listenerRegistration = schemaService.registerSchemaContextListener(this.schemaCtxHandler);
92 final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
94 final NotificationServiceHandler notificationServiceHandler =
95 new NotificationServiceHandler(notificationService);
97 wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
98 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
99 notificationServiceHandler);
103 * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
106 * old {@link TransactionChain}
108 public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
109 LOG.trace("Resetting TransactionChain({})", chain);
111 RestConnectorProvider.transactionChainHandler.update(
112 Preconditions.checkNotNull(dataBroker).createTransactionChain(
113 RestConnectorProvider.TRANSACTION_CHAIN_LISTENER)
118 * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
119 * @return {@link DOMMountPointService}
121 public static DOMMountPointService getMountPointService() {
122 return mountPointServiceHandler.get();
126 public void close() throws Exception {
127 // close registration
128 if (this.listenerRegistration != null) {
129 this.listenerRegistration.close();
132 // close transaction chain
133 if (transactionChainHandler != null && transactionChainHandler.get() != null) {
134 transactionChainHandler.get().close();
137 transactionChainHandler = null;
138 mountPointServiceHandler = null;