Bug 6272 - support RESTCONF PATCH for mounted NETCONF nodes
[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
9 package org.opendaylight.restconf;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.Collections;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
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.ServicesWrapperImpl;
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 draft17.
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 TransactionChainHandler transactionChainHandler;
61     private static DOMDataBroker dataBroker;
62     private static DOMMountPointServiceHandler mountPointServiceHandler;
63
64     @Override
65     public void onSessionInitiated(final ProviderSession session) {
66         final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
67
68         final ServicesWrapperImpl wrapperServices = ServicesWrapperImpl.getInstance();
69
70         final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
71         this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
72
73         RestConnectorProvider.mountPointServiceHandler = new DOMMountPointServiceHandler(
74                 session.getService(DOMMountPointService.class));
75
76         RestConnectorProvider.dataBroker = session.getService(DOMDataBroker.class);
77         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(RestConnectorProvider.dataBroker);
78
79         RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(RestConnectorProvider.dataBroker
80                 .createTransactionChain(RestConnectorProvider.transactionListener));
81
82         final DOMRpcService rpcService = session.getService(DOMRpcService.class);
83         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
84
85         wrapperServices.setHandlers(schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
86                 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler);
87     }
88
89     /**
90      * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
91      *
92      * @param chain
93      *            - old {@link TransactionChain}
94      */
95     public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
96         LOG.trace("Resetting TransactionChain({})", chain);
97         chain.close();
98         RestConnectorProvider.transactionChainHandler.update(
99                 Preconditions.checkNotNull(RestConnectorProvider.dataBroker).createTransactionChain(
100                         RestConnectorProvider.transactionListener)
101         );
102     }
103
104     /**
105      * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
106      * @return {@link DOMMountPointService}
107      */
108     public static DOMMountPointService getMountPointService() {
109         return RestConnectorProvider.mountPointServiceHandler.get();
110     }
111
112     @Override
113     public Collection<ProviderFunctionality> getProviderFunctionality() {
114         return Collections.emptySet();
115     }
116
117     @Override
118     public void close() throws Exception {
119         // close registration
120         if (this.listenerRegistration != null) {
121             this.listenerRegistration.close();
122         }
123
124         // close transaction chain
125         if ((transactionChainHandler != null) && (transactionChainHandler.get() != null)) {
126             transactionChainHandler.get().close();
127         }
128     }
129 }