Bug 5679 - implement ietf-restconf-monitoring - capabilities
[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
63     private SchemaContextHandler schemaCtxHandler;
64     private static TransactionChainHandler transactionChainHandler;
65     private static DOMDataBroker dataBroker;
66     private static DOMMountPointServiceHandler mountPointServiceHandler;
67
68     @Override
69     public void onSessionInitiated(final ProviderSession session) {
70         final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
71
72         final ServicesWrapperImpl wrapperServices = ServicesWrapperImpl.getInstance();
73
74         RestConnectorProvider.mountPointServiceHandler = new DOMMountPointServiceHandler(
75                 session.getService(DOMMountPointService.class));
76
77         RestConnectorProvider.dataBroker = session.getService(DOMDataBroker.class);
78         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(RestConnectorProvider.dataBroker);
79
80         RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(RestConnectorProvider.dataBroker
81                 .createTransactionChain(RestConnectorProvider.transactionListener));
82
83         this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
84         this.listenerRegistration = schemaService.registerSchemaContextListener(this.schemaCtxHandler);
85
86         final DOMRpcService rpcService = session.getService(DOMRpcService.class);
87         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
88
89         final DOMNotificationService notificationService = session.getService(DOMNotificationService.class);
90         final NotificationServiceHandler notificationServiceHandler =
91                 new NotificationServiceHandler(notificationService);
92
93         wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
94                 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
95                 notificationServiceHandler);
96     }
97
98     /**
99      * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
100      *
101      * @param chain
102      *            - old {@link TransactionChain}
103      */
104     public static void resetTransactionChainForAdapaters(final TransactionChain<?, ?> chain) {
105         LOG.trace("Resetting TransactionChain({})", chain);
106         chain.close();
107         RestConnectorProvider.transactionChainHandler.update(
108                 Preconditions.checkNotNull(RestConnectorProvider.dataBroker).createTransactionChain(
109                         RestConnectorProvider.transactionListener)
110         );
111     }
112
113     /**
114      * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
115      * @return {@link DOMMountPointService}
116      */
117     public static DOMMountPointService getMountPointService() {
118         return RestConnectorProvider.mountPointServiceHandler.get();
119     }
120
121     @Override
122     public Collection<ProviderFunctionality> getProviderFunctionality() {
123         return Collections.emptySet();
124     }
125
126     @Override
127     public void close() throws Exception {
128         // close registration
129         if (this.listenerRegistration != null) {
130             this.listenerRegistration.close();
131         }
132
133         // close transaction chain
134         if ((transactionChainHandler != null) && (transactionChainHandler.get() != null)) {
135             transactionChainHandler.get().close();
136         }
137     }
138 }