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.DOMRpcService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
20 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
21 import org.opendaylight.controller.sal.core.api.Provider;
22 import org.opendaylight.controller.sal.core.api.model.SchemaService;
23 import org.opendaylight.netconf.sal.rest.api.RestConnector;
24 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.restconf.common.wrapper.services.Draft15ServicesWrapperImpl;
26 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
27 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
28 import org.opendaylight.restconf.handlers.RpcServiceHandler;
29 import org.opendaylight.restconf.handlers.SchemaContextHandler;
30 import org.opendaylight.restconf.handlers.TransactionChainHandler;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Provider for restconf draft15.
40 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
44 private final TransactionChainListener transactionListener = new TransactionChainListener() {
46 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
47 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
48 LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
50 resetTransactionChainForAdapaters(chain);
51 throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
55 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
56 LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
60 private ListenerRegistration<SchemaContextListener> listenerRegistration;
61 private DOMDataBroker dataBroker;
62 private DOMTransactionChain transactionChain;
65 public void onSessionInitiated(final ProviderSession session) {
66 final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
68 final Draft15ServicesWrapperImpl wrapperServices = Draft15ServicesWrapperImpl.getInstance();
70 final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
71 this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
73 final DOMMountPointServiceHandler domMountPointServiceHandler = new DOMMountPointServiceHandler(
74 session.getService(DOMMountPointService.class));
76 this.dataBroker = session.getService(DOMDataBroker.class);
77 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(this.dataBroker);
79 this.transactionChain = this.dataBroker.createTransactionChain(this.transactionListener);
80 final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(this.transactionChain);
82 final DOMRpcService rpcService = session.getService(DOMRpcService.class);
83 final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
85 wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler, transactionChainHandler,
86 brokerHandler, rpcServiceHandler);
90 * After {@link TransactionChain} failed, this is creating new transaction
94 * - old {@link TransactionChain}
96 private void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
97 LOG.trace("Resetting TransactionChain({}) to {}", chain, this.transactionChain);
98 this.transactionChain = Preconditions.checkNotNull(this.dataBroker)
99 .createTransactionChain(this.transactionListener);
103 public Collection<ProviderFunctionality> getProviderFunctionality() {
104 return Collections.emptySet();
108 public void close() throws Exception {
109 if (this.listenerRegistration != null) {
110 this.listenerRegistration.close();
112 if (this.transactionChain != null) {
113 this.transactionChain.close();