782a018c5cb87dcba0e185150dd6e80cc71a29d6
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingBrokerWiring.java
1 /*
2  * Copyright (c) 2018 Red Hat, 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.controller.md.sal.binding.impl;
9
10 import javassist.ClassPool;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
13 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
14 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
15 import org.opendaylight.controller.md.sal.binding.compat.HeliumNotificationProviderServiceWithInterestListeners;
16 import org.opendaylight.controller.md.sal.binding.compat.HeliumRpcProviderRegistry;
17 import org.opendaylight.controller.md.sal.binding.spi.AdapterFactory;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
24 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
25 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
26 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
27 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
28 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
29 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
30 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
34
35 /**
36  * Provides the implementations of the APIs.
37  *
38  * <p>Intended to be usable in a standalone environment (non-OSGi/Karaf). Also
39  * internally used by the Blueprint XML to expose the same as OSGi services.
40  * This class does not require (depend on) the Guice dependency injection
41  * framework, but can we used with it.
42  *
43  * @author Michael Vorburger.ch, partially based on refactored code originally by Thomas Pantelis
44  */
45 public class BindingBrokerWiring implements AutoCloseable {
46
47     private static final JavassistUtils JAVASSIST = JavassistUtils.forClassPool(ClassPool.getDefault());
48
49     private final BindingToNormalizedNodeCodec bindingToNormalizedNodeCodec;
50     private final ListenerRegistration<SchemaContextListener> mappingCodecListenerReg;
51     private final RpcProviderRegistry rpcProviderRegistry;
52     private final MountPointService mountPointService;
53     private final NotificationService notificationService;
54     private final NotificationPublishService notificationPublishService;
55     private final HeliumNotificationProviderServiceWithInterestListeners notificationAndProviderService;
56     private final AdapterFactory adapterFactory;
57     private final DataBroker dataBroker;
58     private final DataBroker pingPongDataBroker;
59
60     public BindingBrokerWiring(ClassLoadingStrategy classLoadingStrategy, DOMSchemaService schemaService,
61             DOMRpcService domRpcService, DOMRpcProviderService domRpcProviderService,
62             DOMMountPointService domMountPointService, DOMNotificationService domNotificationService,
63             DOMNotificationPublishService domNotificationPublishService,
64             DOMNotificationSubscriptionListenerRegistry domNotificationListenerRegistry, DOMDataBroker domDataBroker,
65             DOMDataBroker domPingPongDataBroker) {
66         // Runtime binding/normalized mapping service
67         BindingNormalizedNodeCodecRegistry codecRegistry
68             = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(JAVASSIST));
69         bindingToNormalizedNodeCodec = new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry, true);
70
71         // Register the BindingToNormalizedNodeCodec with the SchemaService as a SchemaContextListener
72         mappingCodecListenerReg = schemaService.registerSchemaContextListener(bindingToNormalizedNodeCodec);
73
74         // Binding RPC Registry Service
75         BindingDOMRpcServiceAdapter bindingDOMRpcServiceAdapter
76             = new BindingDOMRpcServiceAdapter(domRpcService, bindingToNormalizedNodeCodec);
77         BindingDOMRpcProviderServiceAdapter bindingDOMRpcProviderServiceAdapter
78             = new BindingDOMRpcProviderServiceAdapter(domRpcProviderService, bindingToNormalizedNodeCodec);
79         rpcProviderRegistry
80             = new HeliumRpcProviderRegistry(bindingDOMRpcServiceAdapter, bindingDOMRpcProviderServiceAdapter);
81
82         // Binding MountPoint Service
83         mountPointService = new BindingDOMMountPointServiceAdapter(domMountPointService, bindingToNormalizedNodeCodec);
84
85         // Binding Notification Service
86         BindingDOMNotificationServiceAdapter notificationServiceImpl = new BindingDOMNotificationServiceAdapter(
87                 bindingToNormalizedNodeCodec.getCodecRegistry(), domNotificationService);
88         notificationService = notificationServiceImpl;
89         BindingDOMNotificationPublishServiceAdapter notificationPublishServiceImpl =
90                 new BindingDOMNotificationPublishServiceAdapter(
91                         bindingToNormalizedNodeCodec, domNotificationPublishService);
92         notificationPublishService = notificationPublishServiceImpl;
93         notificationAndProviderService = new HeliumNotificationProviderServiceWithInterestListeners(
94                 notificationPublishServiceImpl, notificationServiceImpl, domNotificationListenerRegistry);
95
96         adapterFactory = new BindingToDOMAdapterFactory(bindingToNormalizedNodeCodec);
97
98         // Binding DataBroker
99         dataBroker = adapterFactory.createDataBroker(domDataBroker);
100
101         // Binding PingPong DataBroker
102         pingPongDataBroker = adapterFactory.createDataBroker(domPingPongDataBroker);
103     }
104
105     @Override
106     public void close() throws Exception {
107         mappingCodecListenerReg.close();
108     }
109
110     public BindingToNormalizedNodeCodec getBindingToNormalizedNodeCodec() {
111         return bindingToNormalizedNodeCodec;
112     }
113
114     public AdapterFactory getAdapterFactory() {
115         return adapterFactory;
116     }
117
118     public RpcProviderRegistry getRpcProviderRegistry() {
119         return rpcProviderRegistry;
120     }
121
122     public MountPointService getMountPointService() {
123         return mountPointService;
124     }
125
126     public NotificationService getNotificationService() {
127         return notificationService;
128     }
129
130     public NotificationPublishService getNotificationPublishService() {
131         return notificationPublishService;
132     }
133
134     @Deprecated
135     public NotificationProviderService getNotificationProviderService() {
136         return notificationAndProviderService;
137     }
138
139     @Deprecated
140     public org.opendaylight.controller.sal.binding.api.NotificationService getDeprecatedNotificationService() {
141         return notificationAndProviderService;
142     }
143
144     public DataBroker getDataBroker() {
145         return dataBroker;
146     }
147
148     public DataBroker getPingPongDataBroker() {
149         return pingPongDataBroker;
150     }
151
152 }