d4dba02b05ad072d6c0e4b27bd1da3f4aa030d3f
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepDeviceInfo.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.ovsdb.hwvtepsouthbound;
10
11 import org.opendaylight.ovsdb.lib.notation.UUID;
12 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
13 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
14 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
15 import org.opendaylight.yangtools.yang.binding.Identifiable;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 /*
25  * HwvtepDeviceInfo is used to store some of the table entries received
26  * in updates from a Hwvtep device. There will be one instance of this per
27  * Hwvtep device connected. Table entries are stored in a map keyed by
28  * uuids of respective rows.
29  *
30  * Purpose of this class is to provide data present in tables which
31  * were updated in a previous transaction and are not available in
32  * current updatedRows. This allows us to handle updates for Tables
33  * which reference other tables and need information in those tables
34  * to add data to Operational data store.
35  *
36  * e.g. Mac-entries in data store use logical-switch-ref as one of the
37  * keys. Mac-entry updates from switch rarely contain Logical_Switch
38  * table entries. To add mac-entries we need table entries from
39  * Logical_Switch table which were created in an earlier update.
40  *
41  */
42 public class HwvtepDeviceInfo {
43
44     private static final Logger LOG = LoggerFactory.getLogger(HwvtepDeviceInfo.class);
45
46     public enum DeviceDataStatus {
47         IN_TRANSIT,
48         UNAVAILABLE,
49         AVAILABLE
50     }
51
52     public static class DeviceData {
53         private final InstanceIdentifier key;
54         private final UUID uuid;
55         private final Object data;
56         private final DeviceDataStatus status;
57
58         public DeviceData(InstanceIdentifier key, UUID uuid, Object data, DeviceDataStatus status) {
59             this.data = data;
60             this.key = key;
61             this.status = status;
62             this.uuid = uuid;
63         }
64
65         public Object getData() {
66             return data;
67         }
68
69         public DeviceDataStatus getStatus() {
70             return status;
71         }
72
73         public UUID getUuid() {
74             return uuid;
75         }
76     }
77
78     //TODO remove this
79     private Map<UUID, LogicalSwitch> logicalSwitches = null;
80     private Map<UUID, PhysicalSwitch> physicalSwitches = null;
81     private Map<UUID, PhysicalLocator> physicalLocators = null;
82     private Map<UUID, UUID> mapTunnelToPhysicalSwitch = null;
83
84     private HwvtepConnectionInstance connectionInstance;
85
86     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> configKeyVsData = new ConcurrentHashMap<>();
87     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, DeviceData>> opKeyVsData = new ConcurrentHashMap<>();
88     private Map<Class<? extends Identifiable>, Map<UUID, Object>> uuidVsData = new ConcurrentHashMap<>();
89
90     public HwvtepDeviceInfo(HwvtepConnectionInstance hwvtepConnectionInstance) {
91         this.connectionInstance = hwvtepConnectionInstance;
92         this.logicalSwitches = new HashMap<>();
93         this.physicalSwitches = new HashMap<>();
94         this.physicalLocators = new HashMap<>();
95         this.mapTunnelToPhysicalSwitch = new HashMap<>();
96     }
97
98     public void putLogicalSwitch(UUID uuid, LogicalSwitch lSwitch) {
99         logicalSwitches.put(uuid, lSwitch);
100     }
101
102     public LogicalSwitch getLogicalSwitch(UUID uuid) {
103         return logicalSwitches.get(uuid);
104     }
105
106     public LogicalSwitch removeLogicalSwitch(UUID uuid) {
107         return logicalSwitches.remove(uuid);
108     }
109
110     public Map<UUID, LogicalSwitch> getLogicalSwitches() {
111         return logicalSwitches;
112     }
113
114     public void putPhysicalSwitch(UUID uuid, PhysicalSwitch pSwitch) {
115         physicalSwitches.put(uuid, pSwitch);
116     }
117
118     public PhysicalSwitch getPhysicalSwitch(UUID uuid) {
119         return physicalSwitches.get(uuid);
120     }
121
122     public PhysicalSwitch removePhysicalSwitch(UUID uuid) {
123         return physicalSwitches.remove(uuid);
124     }
125
126     public Map<UUID, PhysicalSwitch> getPhysicalSwitches() {
127         return physicalSwitches;
128     }
129
130     public void putPhysicalLocator(UUID uuid, PhysicalLocator pLocator) {
131         physicalLocators.put(uuid, pLocator);
132     }
133
134     public PhysicalLocator getPhysicalLocator(UUID uuid) {
135         return physicalLocators.get(uuid);
136     }
137
138     public PhysicalLocator removePhysicalLocator(UUID uuid) {
139         return physicalLocators.remove(uuid);
140     }
141
142     public Map<UUID, PhysicalLocator> getPhysicalLocators() {
143         return physicalLocators;
144     }
145
146     public void putPhysicalSwitchForTunnel(UUID uuid, UUID psUUID) {
147         mapTunnelToPhysicalSwitch.put(uuid, psUUID);
148     }
149
150     public PhysicalSwitch getPhysicalSwitchForTunnel(UUID uuid) {
151         return physicalSwitches.get(mapTunnelToPhysicalSwitch.get(uuid));
152     }
153
154     public void removePhysicalSwitchForTunnel(UUID uuid) {
155         mapTunnelToPhysicalSwitch.remove(uuid);
156     }
157
158     public Map<UUID, UUID> getPhysicalSwitchesForTunnels() {
159         return mapTunnelToPhysicalSwitch;
160     }
161
162     public boolean isKeyInTransit(Class<? extends Identifiable> cls, InstanceIdentifier key) {
163         DeviceData deviceData = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
164         return deviceData != null && DeviceDataStatus.IN_TRANSIT == deviceData.status;
165     }
166
167     public boolean isConfigDataAvailable(Class<? extends Identifiable> cls, InstanceIdentifier key) {
168         return HwvtepSouthboundUtil.getData(configKeyVsData, cls, key) != null;
169     }
170
171     public void updateConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key, Object data) {
172         HwvtepSouthboundUtil.updateData(configKeyVsData, cls, key,
173                 new DeviceData(key, null, data, DeviceDataStatus.AVAILABLE));
174     }
175
176     public void clearConfigData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
177         HwvtepSouthboundUtil.clearData(configKeyVsData, cls, key);
178     }
179
180     public void markKeyAsInTransit(Class<? extends Identifiable> cls, InstanceIdentifier key) {
181         HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
182                 new DeviceData(key, null, null, DeviceDataStatus.IN_TRANSIT));
183     }
184
185     public void updateDeviceOpData(Class<? extends Identifiable> cls, InstanceIdentifier key, UUID uuid, Object data) {
186         HwvtepSouthboundUtil.updateData(opKeyVsData, cls, key,
187                 new DeviceData(key, uuid, data, DeviceDataStatus.AVAILABLE));
188         HwvtepSouthboundUtil.updateData(uuidVsData, cls, uuid, data);
189     }
190
191     public void clearDeviceOpData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
192         DeviceData deviceData = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
193         if (deviceData != null && deviceData.uuid != null) {
194             HwvtepSouthboundUtil.clearData(uuidVsData, cls, deviceData.uuid);
195         }
196         HwvtepSouthboundUtil.clearData(opKeyVsData, cls, key);
197     }
198
199     public Object getDeviceOpData(Class<? extends Identifiable> cls, UUID uuid) {
200         return HwvtepSouthboundUtil.getData(uuidVsData, cls, uuid);
201     }
202
203     public DeviceData getDeviceOpData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
204         return HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
205     }
206
207     public UUID getUUID(Class<? extends Identifiable> cls, InstanceIdentifier key) {
208         DeviceData data = HwvtepSouthboundUtil.getData(opKeyVsData, cls, key);
209         if (data != null) {
210             return data.uuid;
211         }
212         return null;
213     }
214 }