Merge "Bug 7172 - Correct error-info for missing-attribute errors"
[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.DOMNotificationService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
21 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
22 import org.opendaylight.controller.sal.core.api.Provider;
23 import org.opendaylight.controller.sal.core.api.model.SchemaService;
24 import org.opendaylight.netconf.sal.rest.api.RestConnector;
25 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
26 import org.opendaylight.restconf.common.wrapper.services.ServicesWrapperImpl;
27 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
28 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
29 import org.opendaylight.restconf.handlers.NotificationServiceHandler;
30 import org.opendaylight.restconf.handlers.RpcServiceHandler;
31 import org.opendaylight.restconf.handlers.SchemaContextHandler;
32 import org.opendaylight.restconf.handlers.TransactionChainHandler;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Provider for restconf draft18.
40  *
41  */
42 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
43
44     private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
45
46     public static final TransactionChainListener transactionListener = new TransactionChainListener() {
47         @Override
48         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
49                 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
50             LOG.warn("TransactionChain({}) {} FAILED!", chain, transaction.getIdentifier(), cause);
51             resetTransactionChainForAdapaters(chain);
52             throw new RestconfDocumentedException("TransactionChain(" + chain + ") not committed correctly", cause);
53         }
54
55         @Override
56         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
57             LOG.trace("TransactionChain({}) {} SUCCESSFUL", chain);
58         }
59     };
60
61     private ListenerRegistration<SchemaContextListener> listenerRegistration;
62     private static TransactionChainHandler transactionChainHandler;
63     private static DOMDataBroker dataBroker;
64     private static DOMMountPointServiceHandler mountPointServiceHandler;
65
66     @Override
67     public void onSessionInitiated(final ProviderSession session) {
68         final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
69
70         final ServicesWrapperImpl wrapperServices = ServicesWrapperImpl.getInstance();
71
72         final SchemaContextHandler schemaCtxHandler = new SchemaContextHandler();
73         this.listenerRegistration = schemaService.registerSchemaContextListener(schemaCtxHandler);
74
75         RestConnectorProvider.mountPointServiceHandler = new DOMMountPointServiceHandler(
76                 session.getService(DOMMountPointService.class));
77
78         RestConnectorProvider.dataBroker = session.getService(DOMDataBroker.class);
79         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(RestConnectorProvider.dataBroker);
80
81         RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(RestConnectorProvider.dataBroker
82                 .createTransactionChain(RestConnectorProvider.transactionListener));
83
84         final DOMRpcService rpcService = session.getService(DOMRpcService.class);
85         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
86
87         final DOMNotificationService notificationService = session.getService(DOMNotificationService.class);
88         final NotificationServiceHandler notificationServiceHandler =
89                 new NotificationServiceHandler(notificationService);
90
91         wrapperServices.setHandlers(schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
92                 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
93                 notificationServiceHandler);
94     }
95
96     /**
97      * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
98      *
99      * @param chain
100      *            - old {@link TransactionChain}
101      */
102     public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
103         LOG.trace("Resetting TransactionChain({})", chain);
104         chain.close();
105         RestConnectorProvider.transactionChainHandler.update(
106                 Preconditions.checkNotNull(RestConnectorProvider.dataBroker).createTransactionChain(
107                         RestConnectorProvider.transactionListener)
108         );
109     }
110
111     /**
112      * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
113      * @return {@link DOMMountPointService}
114      */
115     public static DOMMountPointService getMountPointService() {
116         return RestConnectorProvider.mountPointServiceHandler.get();
117     }
118
119     @Override
120     public Collection<ProviderFunctionality> getProviderFunctionality() {
121         return Collections.emptySet();
122     }
123
124     @Override
125     public void close() throws Exception {
126         // close registration
127         if (this.listenerRegistration != null) {
128             this.listenerRegistration.close();
129         }
130
131         // close transaction chain
132         if ((transactionChainHandler != null) && (transactionChainHandler.get() != null)) {
133             transactionChainHandler.get().close();
134         }
135     }
136 }