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
8 package org.opendaylight.restconf;
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Collections;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
17 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
19 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
20 import org.opendaylight.controller.sal.core.api.Provider;
21 import org.opendaylight.controller.sal.core.api.model.SchemaService;
22 import org.opendaylight.netconf.sal.rest.api.RestConnector;
23 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
24 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
25 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
26 import org.opendaylight.restconf.handlers.SchemaContextHandler;
27 import org.opendaylight.restconf.handlers.TransactionChainHandler;
28 import org.opendaylight.restconf.rest.services.impl.Draft11ServicesWrapperImpl;
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 draft11.
38 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
42 private final TransactionChainListener transactionListener = 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);
48 resetTransactionChainForAdapaters(chain);
49 throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
53 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
54 LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
58 private ListenerRegistration<SchemaContextListener> listenerRegistration;
59 private DOMDataBroker dataBroker;
60 private DOMTransactionChain transactionChain;
63 public void onSessionInitiated(final ProviderSession session) {
64 final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
66 final Draft11ServicesWrapperImpl wrapperServices = Draft11ServicesWrapperImpl.getInstance();
68 final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
69 this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
71 final DOMMountPointServiceHandler domMountPointServiceHandler = new DOMMountPointServiceHandler(
72 session.getService(DOMMountPointService.class));
74 this.dataBroker = session.getService(DOMDataBroker.class);
75 this.transactionChain = this.dataBroker.createTransactionChain(this.transactionListener);
76 final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(this.transactionChain);
78 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(this.dataBroker);
80 wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler);
84 * After {@link TransactionChain} failed, this is creating new transaction
88 * - old {@link TransactionChain}
90 private void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
91 LOG.trace("Resetting TransactionChain({}) to {}", chain, this.transactionChain);
92 this.transactionChain = Preconditions.checkNotNull(this.dataBroker)
93 .createTransactionChain(this.transactionListener);
97 public Collection<ProviderFunctionality> getProviderFunctionality() {
98 return Collections.emptySet();
102 public void close() throws Exception {
103 if (this.listenerRegistration != null) {
104 this.listenerRegistration.close();
106 if (this.transactionChain != null) {
107 this.transactionChain.close();