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.DOMMountPointServiceHandler;
25 import org.opendaylight.restconf.handlers.SchemaContextHandler;
26 import org.opendaylight.restconf.handlers.TransactionChainHandler;
27 import org.opendaylight.restconf.rest.services.impl.Draft11ServicesWrapperImpl;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Provider for restconf draft11.
37 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
39 private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
41 private final TransactionChainListener transactionListener = new TransactionChainListener() {
43 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
44 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
45 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 ListenerRegistration<SchemaContextListener> listenerRegistration;
58 private DOMDataBroker dataBroker;
59 private DOMTransactionChain transactionChain;
62 public void onSessionInitiated(final ProviderSession session) {
63 final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
65 final Draft11ServicesWrapperImpl wrapperServices = Draft11ServicesWrapperImpl.getInstance();
67 final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
68 this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
70 final DOMMountPointServiceHandler domMountPointServiceHandler = new DOMMountPointServiceHandler(
71 session.getService(DOMMountPointService.class));
73 this.dataBroker = session.getService(DOMDataBroker.class);
74 this.transactionChain = this.dataBroker.createTransactionChain(this.transactionListener);
75 final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(this.transactionChain);
77 wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler);
81 * After {@link TransactionChain} failed, this is creating new transaction
85 * - old {@link TransactionChain}
87 private void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
88 LOG.trace("Resetting TransactionChain({}) to {}", chain, this.transactionChain);
89 this.transactionChain = Preconditions.checkNotNull(this.dataBroker)
90 .createTransactionChain(this.transactionListener);
94 public Collection<ProviderFunctionality> getProviderFunctionality() {
95 return Collections.emptySet();
99 public void close() throws Exception {
100 if (this.listenerRegistration != null) {
101 this.listenerRegistration.close();
103 if (this.transactionChain != null) {
104 this.transactionChain.close();