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