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;
63 private SchemaContextHandler schemaCtxHandler;
64 private static TransactionChainHandler transactionChainHandler;
65 private static DOMDataBroker dataBroker;
66 private static DOMMountPointServiceHandler mountPointServiceHandler;
69 public void onSessionInitiated(final ProviderSession session) {
70 final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
72 final ServicesWrapperImpl wrapperServices = ServicesWrapperImpl.getInstance();
74 RestConnectorProvider.mountPointServiceHandler = new DOMMountPointServiceHandler(
75 session.getService(DOMMountPointService.class));
77 RestConnectorProvider.dataBroker = session.getService(DOMDataBroker.class);
78 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(RestConnectorProvider.dataBroker);
80 RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(RestConnectorProvider.dataBroker
81 .createTransactionChain(RestConnectorProvider.transactionListener));
83 this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
84 this.listenerRegistration = schemaService.registerSchemaContextListener(this.schemaCtxHandler);
86 final DOMRpcService rpcService = session.getService(DOMRpcService.class);
87 final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
89 final DOMNotificationService notificationService = session.getService(DOMNotificationService.class);
90 final NotificationServiceHandler notificationServiceHandler =
91 new NotificationServiceHandler(notificationService);
93 wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
94 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
95 notificationServiceHandler);
99 * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
102 * - old {@link TransactionChain}
104 public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
105 LOG.trace("Resetting TransactionChain({})", chain);
107 RestConnectorProvider.transactionChainHandler.update(
108 Preconditions.checkNotNull(RestConnectorProvider.dataBroker).createTransactionChain(
109 RestConnectorProvider.transactionListener)
114 * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
115 * @return {@link DOMMountPointService}
117 public static DOMMountPointService getMountPointService() {
118 return RestConnectorProvider.mountPointServiceHandler.get();
122 public Collection<ProviderFunctionality> getProviderFunctionality() {
123 return Collections.emptySet();
127 public void close() throws Exception {
128 // close registration
129 if (this.listenerRegistration != null) {
130 this.listenerRegistration.close();
133 // close transaction chain
134 if ((transactionChainHandler != null) && (transactionChainHandler.get() != null)) {
135 transactionChainHandler.get().close();