7c7ae377e17b9ed85020ad46e6b429a9e1e02868
[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     public static 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             resetTransactionChainForAdapaters(chain);
50             throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
51         }
52
53         @Override
54         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
55             LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
56         }
57     };
58
59     private ListenerRegistration<SchemaContextListener> listenerRegistration;
60     private static DOMDataBroker dataBroker;
61     private static DOMTransactionChain transactionChain;
62
63     @Override
64     public void onSessionInitiated(final ProviderSession session) {
65         final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
66
67         final Draft15ServicesWrapperImpl wrapperServices = Draft15ServicesWrapperImpl.getInstance();
68
69         final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
70         this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
71
72         final DOMMountPointServiceHandler domMountPointServiceHandler = new DOMMountPointServiceHandler(
73                 session.getService(DOMMountPointService.class));
74
75         dataBroker = session.getService(DOMDataBroker.class);
76         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
77
78         transactionChain = dataBroker.createTransactionChain(transactionListener);
79         final TransactionChainHandler transactionChainHandler = new TransactionChainHandler(transactionChain);
80
81         final DOMRpcService rpcService = session.getService(DOMRpcService.class);
82         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
83
84         wrapperServices.setHandlers(schemaCtxHandler, domMountPointServiceHandler, transactionChainHandler,
85                 brokerHandler, rpcServiceHandler);
86     }
87
88     /**
89      * After {@link TransactionChain} failed, this is creating new transaction
90      * with listener.
91      *
92      * @param chain
93      *            - old {@link TransactionChain}
94      */
95     public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
96         LOG.trace("Resetting TransactionChain({}) to {}", chain, transactionChain);
97         chain.close();
98         transactionChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionListener);
99     }
100
101     @Override
102     public Collection<ProviderFunctionality> getProviderFunctionality() {
103         return Collections.emptySet();
104     }
105
106     @Override
107     public void close() throws Exception {
108         if (this.listenerRegistration != null) {
109             this.listenerRegistration.close();
110         }
111         if (transactionChain != null) {
112             transactionChain.close();
113         }
114     }
115 }