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 java.util.Collection;
13 import java.util.Collections;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
21 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
22 import org.opendaylight.controller.sal.core.api.Provider;
23 import org.opendaylight.controller.sal.core.api.model.SchemaService;
24 import org.opendaylight.netconf.sal.rest.api.RestConnector;
25 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
26 import org.opendaylight.restconf.common.wrapper.services.ServicesWrapperImpl;
27 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
28 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
29 import org.opendaylight.restconf.handlers.NotificationServiceHandler;
30 import org.opendaylight.restconf.handlers.RpcServiceHandler;
31 import org.opendaylight.restconf.handlers.SchemaContextHandler;
32 import org.opendaylight.restconf.handlers.TransactionChainHandler;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Provider for restconf draft18.
42 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
44 private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
46 public static final TransactionChainListener transactionListener = new TransactionChainListener() {
48 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
49 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
50 LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
51 resetTransactionChainForAdapaters(chain);
52 throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
56 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
57 LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
61 private ListenerRegistration<SchemaContextListener> listenerRegistration;
62 private static TransactionChainHandler transactionChainHandler;
63 private static DOMDataBroker dataBroker;
64 private static DOMMountPointServiceHandler mountPointServiceHandler;
67 public void onSessionInitiated(final ProviderSession session) {
68 final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
70 final ServicesWrapperImpl wrapperServices = ServicesWrapperImpl.getInstance();
72 RestConnectorProvider.mountPointServiceHandler = new DOMMountPointServiceHandler(
73 session.getService(DOMMountPointService.class));
75 RestConnectorProvider.dataBroker = session.getService(DOMDataBroker.class);
76 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(RestConnectorProvider.dataBroker);
78 RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(RestConnectorProvider.dataBroker
79 .createTransactionChain(RestConnectorProvider.transactionListener));
81 final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
82 this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
84 final DOMRpcService rpcService = session.getService(DOMRpcService.class);
85 final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
87 final DOMNotificationService notificationService = session.getService(DOMNotificationService.class);
88 final NotificationServiceHandler notificationServiceHandler =
89 new NotificationServiceHandler(notificationService);
91 wrapperServices.setHandlers(schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
92 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
93 notificationServiceHandler);
97 * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
100 * - old {@link TransactionChain}
102 public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
103 LOG.trace("Resetting TransactionChain({})", chain);
105 RestConnectorProvider.transactionChainHandler.update(
106 Preconditions.checkNotNull(RestConnectorProvider.dataBroker).createTransactionChain(
107 RestConnectorProvider.transactionListener)
112 * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
113 * @return {@link DOMMountPointService}
115 public static DOMMountPointService getMountPointService() {
116 return RestConnectorProvider.mountPointServiceHandler.get();
120 public Collection<ProviderFunctionality> getProviderFunctionality() {
121 return Collections.emptySet();
125 public void close() throws Exception {
126 // close registration
127 if (this.listenerRegistration != null) {
128 this.listenerRegistration.close();
131 // close transaction chain
132 if ((transactionChainHandler != null) && (transactionChainHandler.get() != null)) {
133 transactionChainHandler.get().close();