2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.restconf.nb.rfc8040;
11 import com.google.common.base.Preconditions;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
14 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
15 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
17 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
18 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
19 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
20 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
21 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
22 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
23 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
24 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServiceWrapper;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Provider for restconf draft18.
34 public class RestConnectorProvider<T extends ServiceWrapper> implements RestconfConnector, AutoCloseable {
36 private static final Logger LOG = LoggerFactory.getLogger(RestConnectorProvider.class);
38 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
39 private static volatile DOMMountPointServiceHandler mountPointServiceHandler;
41 private final DOMRpcService rpcService;
42 private final DOMNotificationService notificationService;
43 private final DOMMountPointService mountPointService;
44 private final DOMSchemaService domSchemaService;
45 private final TransactionChainHandler transactionChainHandler;
46 private final DOMDataBroker dataBroker;
48 private ListenerRegistration<SchemaContextListener> listenerRegistration;
49 private SchemaContextHandler schemaCtxHandler;
50 private final T wrapperServices;
52 public RestConnectorProvider(final DOMDataBroker domDataBroker, final DOMSchemaService domSchemaService,
53 final DOMRpcService rpcService, final DOMNotificationService notificationService,
54 final DOMMountPointService mountPointService, final TransactionChainHandler transactionChainHandler,
55 final T wrapperServices) {
56 this.wrapperServices = wrapperServices;
57 this.domSchemaService = Preconditions.checkNotNull(domSchemaService);
58 this.rpcService = Preconditions.checkNotNull(rpcService);
59 this.notificationService = Preconditions.checkNotNull(notificationService);
60 this.mountPointService = Preconditions.checkNotNull(mountPointService);
61 this.transactionChainHandler = Preconditions.checkNotNull(transactionChainHandler);
62 this.dataBroker = Preconditions.checkNotNull(domDataBroker);
65 public synchronized void start() {
66 mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
68 final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
70 this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
71 this.listenerRegistration = domSchemaService.registerSchemaContextListener(this.schemaCtxHandler);
73 final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
75 final NotificationServiceHandler notificationServiceHandler =
76 new NotificationServiceHandler(notificationService);
78 if (wrapperServices != null) {
79 wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
80 transactionChainHandler, brokerHandler, rpcServiceHandler,
81 notificationServiceHandler, domSchemaService);
85 public DOMMountPointServiceHandler getMountPointServiceHandler() {
86 return mountPointServiceHandler;
90 * Get current {@link DOMMountPointService} from {@link DOMMountPointServiceHandler}.
91 * @return {@link DOMMountPointService}
93 public static DOMMountPointService getMountPointService() {
94 return mountPointServiceHandler.get();
98 public void close() throws Exception {
100 if (this.listenerRegistration != null) {
101 this.listenerRegistration.close();
104 mountPointServiceHandler = null;