dedea9b0a6cd15d3edfdea6e08d9d8462da27f6d
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / RestConnectorProvider.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf;
9
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;
35
36 /**
37  * Provider for restconf draft15.
38  *
39  */
40 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
43
44     private final TransactionChainListener transactionListener = new TransactionChainListener() {
45         @Override
46         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
47                 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
48             LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
49             chain.close();
50             resetTransactionChainForAdapaters(chain);
51             throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
52         }
53
54         @Override
55         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
56             LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
57         }
58     };
59
60     private ListenerRegistration<SchemaContextListener> listenerRegistration;
61     private DOMDataBroker dataBroker;
62     private DOMTransactionChain transactionChain;
63
64     @Override
65     public void onSessionInitiated(final ProviderSession session) {
66         final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
67
68         final Draft15ServicesWrapperImpl wrapperServices = Draft15ServicesWrapperImpl.getInstance();
69
70         final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
71         this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
72
73         final DOMMountPointServiceHandler domMountPointServiceHandler = new DOMMountPointServiceHandler(
74                 session.getService(DOMMountPointService.class));
75
76         this.dataBroker = session.getService(DOMDataBroker.class);
77         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(this.dataBroker);
78
79         this.transactionChain = this.dataBroker.createTransactionChain(this.transactionListener);
80         final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(this.transactionChain);
81
82         final DOMRpcService rpcService = session.getService(DOMRpcService.class);
83         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
84
85         wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler, transactionChainHandler,
86                 brokerHandler, rpcServiceHandler);
87     }
88
89     /**
90      * After {@link TransactionChain} failed, this is creating new transaction
91      * with listener.
92      *
93      * @param chain
94      *            - old {@link TransactionChain}
95      */
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);
100     }
101
102     @Override
103     public Collection<ProviderFunctionality> getProviderFunctionality() {
104         return Collections.emptySet();
105     }
106
107     @Override
108     public void close() throws Exception {
109         if (this.listenerRegistration != null) {
110             this.listenerRegistration.close();
111         }
112         if (this.transactionChain != null) {
113             this.transactionChain.close();
114         }
115     }
116 }