9ed9fbff8cb68492d3debb81622164094ff71139
[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.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;
32
33 /**
34  * Provider for restconf draft11.
35  *
36  */
37 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
38
39     private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
40
41     private final TransactionChainListener transactionListener = new TransactionChainListener() {
42         @Override
43         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
44                 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
45             LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
46             chain.close();
47             resetTransactionChainForAdapaters(chain);
48             throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
49         }
50
51         @Override
52         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
53             LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
54         }
55     };
56
57     private ListenerRegistration<SchemaContextListener> listenerRegistration;
58     private DOMDataBroker dataBroker;
59     private DOMTransactionChain transactionChain;
60
61     @Override
62     public void onSessionInitiated(final ProviderSession session) {
63         final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
64
65         final Draft11ServicesWrapperImpl wrapperServices = Draft11ServicesWrapperImpl.getInstance();
66
67         final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
68         this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
69
70         final DOMMountPointServiceHandler domMountPointServiceHandler = new DOMMountPointServiceHandler(
71                 session.getService(DOMMountPointService.class));
72
73         this.dataBroker = session.getService(DOMDataBroker.class);
74         this.transactionChain = this.dataBroker.createTransactionChain(this.transactionListener);
75         final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(this.transactionChain);
76
77         wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler);
78     }
79
80     /**
81      * After {@link TransactionChain} failed, this is creating new transaction
82      * with listener.
83      *
84      * @param chain
85      *            - old {@link TransactionChain}
86      */
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);
91     }
92
93     @Override
94     public Collection<ProviderFunctionality> getProviderFunctionality() {
95         return Collections.emptySet();
96     }
97
98     @Override
99     public void close() throws Exception {
100         if (this.listenerRegistration != null) {
101             this.listenerRegistration.close();
102         }
103         if (this.transactionChain != null) {
104             this.transactionChain.close();
105         }
106     }
107 }