92836631a80c26e2a9cfd6807e77e74d75b5f418
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / forward / DomForwardedBindingBrokerImpl.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.binding.impl.forward;
9
10 import java.util.concurrent.ConcurrentHashMap;
11 import java.util.concurrent.ConcurrentMap;
12
13 import org.opendaylight.controller.sal.binding.api.mount.MountProviderInstance;
14 import org.opendaylight.controller.sal.binding.impl.RootBindingAwareBroker;
15 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingDomConnectorDeployer;
16 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentConnector;
17 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
18 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
19 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
20 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
21 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 public class DomForwardedBindingBrokerImpl extends RootBindingAwareBroker implements DomForwardedBroker {
26
27     private ProviderSession domProviderContext;
28     private BindingIndependentConnector connector;
29
30     private MountProvisionService domMountService;
31
32     private final DomMountPointForwardingManager domForwardingManager = new DomMountPointForwardingManager();
33     private final BindingMountPointForwardingManager bindingForwardingManager = new BindingMountPointForwardingManager();
34
35     private ConcurrentMap<InstanceIdentifier<?>, BindingIndependentConnector> connectors = new ConcurrentHashMap<>();
36     private ConcurrentMap<InstanceIdentifier<?>, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier> forwarded = new ConcurrentHashMap<>();
37     private ListenerRegistration<MountProvisionListener> domListenerRegistration;
38     private ListenerRegistration<org.opendaylight.controller.sal.binding.api.mount.MountProviderService.MountProvisionListener> baListenerRegistration;
39
40
41     public DomForwardedBindingBrokerImpl(String instanceName) {
42         super(instanceName);
43     }
44
45     @Override
46     public BindingIndependentConnector getConnector() {
47         return connector;
48     }
49
50     @Override
51     public ProviderSession getDomProviderContext() {
52         return domProviderContext;
53     }
54
55     @Override
56     public void setConnector(BindingIndependentConnector connector) {
57         this.connector = connector;
58     }
59
60     @Override
61     public void setDomProviderContext(ProviderSession domProviderContext) {
62         this.domProviderContext = domProviderContext;
63     }
64
65     @Override
66     public void startForwarding() {
67         BindingDomConnectorDeployer.startDataForwarding(getConnector(), getDataBroker(), getDomProviderContext());
68         BindingDomConnectorDeployer.startRpcForwarding(getConnector(), getRpcProviderRegistry(),
69                 getDomProviderContext());
70         BindingDomConnectorDeployer.startNotificationForwarding(getConnector(), getNotificationBroker(),
71                 getDomProviderContext());
72         startMountpointForwarding();
73     }
74
75     private void startMountpointForwarding() {
76         domMountService = getDomProviderContext().getService(MountProvisionService.class);
77         if (domMountService != null && getMountManager() != null) {
78             domListenerRegistration = domMountService.registerProvisionListener(domForwardingManager);
79             baListenerRegistration = getMountManager().registerProvisionListener(bindingForwardingManager);
80         }
81     }
82
83     public MountProvisionService getDomMountService() {
84         return domMountService;
85     }
86
87     public void setDomMountService(MountProvisionService domMountService) {
88         this.domMountService = domMountService;
89     }
90
91     private void tryToDeployConnector(InstanceIdentifier<?> baPath,
92             org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier biPath) {
93         org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier previous = forwarded.putIfAbsent(baPath, biPath);
94         if (previous != null) {
95             return;
96         }
97         MountProviderInstance baMountPoint = getMountManager().createOrGetMountPoint(baPath);
98         MountProvisionInstance domMountPoint = domMountService.createOrGetMountPoint(biPath);
99         BindingIndependentConnector connector = createForwarder(baPath, baMountPoint, domMountPoint);
100         connectors.put(baPath, connector);
101     }
102
103     private BindingIndependentConnector createForwarder(InstanceIdentifier<?> path, MountProviderInstance baMountPoint,
104             MountProvisionInstance domMountPoint) {
105         BindingIndependentConnector mountConnector = BindingDomConnectorDeployer.createConnector(getConnector());
106
107         BindingDomConnectorDeployer.startDataForwarding(mountConnector, baMountPoint, domMountPoint);
108         BindingDomConnectorDeployer.startRpcForwarding(mountConnector, baMountPoint, domMountPoint);
109         BindingDomConnectorDeployer.startNotificationForwarding(mountConnector, baMountPoint, domMountPoint);
110         // connector.setDomNotificationBroker(domMountPoint);
111         return mountConnector;
112     }
113
114     public synchronized void tryToDeployDomForwarder(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier domPath) {
115         InstanceIdentifier<?> baPath;
116         try {
117             baPath = connector.getMappingService().fromDataDom(domPath);
118             BindingIndependentConnector potentialConnector = connectors.get(baPath);
119             if (potentialConnector != null) {
120                 return;
121             }
122             tryToDeployConnector(baPath, domPath);
123         } catch (DeserializationException e) {
124
125         }
126     }
127
128     public synchronized void tryToDeployBindingForwarder(InstanceIdentifier<?> baPath) {
129         BindingIndependentConnector potentialConnector = connectors.get(baPath);
130         if (potentialConnector != null) {
131             return;
132         }
133         org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier domPath = connector.getMappingService().toDataDom(baPath);
134         tryToDeployConnector(baPath, domPath);
135     }
136
137     public synchronized void undeployBindingForwarder(InstanceIdentifier<?> baPath) {
138         // FIXME: Implement closeMountPoint
139     }
140
141     public synchronized void undeployDomForwarder(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier biPath) {
142         // FIXME: Implement closeMountPoint
143     }
144
145     private class DomMountPointForwardingManager implements MountProvisionListener {
146
147         @Override
148         public void onMountPointCreated(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier path) {
149             tryToDeployDomForwarder(path);
150         }
151
152         @Override
153         public void onMountPointRemoved(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier path) {
154             undeployDomForwarder(path);
155         }
156     }
157
158     private class BindingMountPointForwardingManager implements
159             org.opendaylight.controller.sal.binding.api.mount.MountProviderService.MountProvisionListener {
160
161         @Override
162         public void onMountPointCreated(InstanceIdentifier<?> path) {
163             tryToDeployBindingForwarder(path);
164         }
165
166         @Override
167         public void onMountPointRemoved(InstanceIdentifier<?> path) {
168             undeployBindingForwarder(path);
169         }
170     }
171
172 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.