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 public static 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);
49 resetTransactionChainForAdapaters(chain);
50 throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
54 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
55 LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
59 private ListenerRegistration<SchemaContextListener> listenerRegistration;
60 private static DOMDataBroker dataBroker;
61 private static DOMTransactionChain transactionChain;
64 public void onSessionInitiated(final ProviderSession session) {
65 final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
67 final Draft15ServicesWrapperImpl wrapperServices = Draft15ServicesWrapperImpl.getInstance();
69 final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
70 this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
72 final DOMMountPointServiceHandler domMountPointServiceHandler = new DOMMountPointServiceHandler(
73 session.getService(DOMMountPointService.class));
75 dataBroker = session.getService(DOMDataBroker.class);
76 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
78 transactionChain = dataBroker.createTransactionChain(transactionListener);
79 final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(transactionChain);
81 final DOMRpcService rpcService = session.getService(DOMRpcService.class);
82 final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
84 wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler, transactionChainHandler,
85 brokerHandler, rpcServiceHandler);
89 * After {@link TransactionChain} failed, this is creating new transaction
93 * - old {@link TransactionChain}
95 public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
96 LOG.trace("Resetting TransactionChain({}) to {}", chain, transactionChain);
98 transactionChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionListener);
102 public Collection<ProviderFunctionality> getProviderFunctionality() {
103 return Collections.emptySet();
107 public void close() throws Exception {
108 if (this.listenerRegistration != null) {
109 this.listenerRegistration.close();
111 if (transactionChain != null) {
112 transactionChain.close();