CNlucius
Committed by Gerrit Code Review

ONOS-2707

Fix bug of process ovsdb table update

Change-Id: I55a879da1bf8770b68cb13c676f54482f685e60d
1 -/* 1 +/*
2 - * Copyright 2015 Open Networking Laboratory 2 + * Copyright 2015 Open Networking Laboratory
3 - * 3 + *
4 - * Licensed under the Apache License, Version 2.0 (the "License"); 4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License. 5 + * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at 6 + * You may obtain a copy of the License at
7 - * 7 + *
8 - * http://www.apache.org/licenses/LICENSE-2.0 8 + * http://www.apache.org/licenses/LICENSE-2.0
9 - * 9 + *
10 - * Unless required by applicable law or agreed to in writing, software 10 + * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS, 11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and 13 + * See the License for the specific language governing permissions and
14 - * limitations under the License. 14 + * limitations under the License.
15 - */ 15 + */
16 -package org.onosproject.ovsdb.controller.impl; 16 +package org.onosproject.ovsdb.controller.impl;
17 - 17 +
18 -import static com.google.common.base.Preconditions.checkNotNull; 18 +import static com.google.common.base.Preconditions.checkNotNull;
19 - 19 +
20 -import java.math.BigInteger; 20 +import java.math.BigInteger;
21 -import java.util.HashSet; 21 +import java.util.HashSet;
22 -import java.util.List; 22 +import java.util.Iterator;
23 -import java.util.Map; 23 +import java.util.List;
24 -import java.util.Set; 24 +import java.util.Map;
25 -import java.util.concurrent.ConcurrentHashMap; 25 +import java.util.Set;
26 -import java.util.concurrent.CopyOnWriteArraySet; 26 +import java.util.concurrent.ConcurrentHashMap;
27 -import java.util.concurrent.ExecutionException; 27 +import java.util.concurrent.CopyOnWriteArraySet;
28 - 28 +import java.util.concurrent.ExecutionException;
29 -import org.apache.felix.scr.annotations.Activate; 29 +
30 -import org.apache.felix.scr.annotations.Component; 30 +import org.apache.felix.scr.annotations.Activate;
31 -import org.apache.felix.scr.annotations.Deactivate; 31 +import org.apache.felix.scr.annotations.Component;
32 -import org.apache.felix.scr.annotations.Service; 32 +import org.apache.felix.scr.annotations.Deactivate;
33 -import org.onlab.packet.IpAddress; 33 +import org.apache.felix.scr.annotations.Service;
34 -import org.onlab.packet.MacAddress; 34 +import org.onlab.packet.IpAddress;
35 -import org.onosproject.ovsdb.controller.DefaultEventSubject; 35 +import org.onlab.packet.MacAddress;
36 -import org.onosproject.ovsdb.controller.EventSubject; 36 +import org.onosproject.ovsdb.controller.DefaultEventSubject;
37 -import org.onosproject.ovsdb.controller.OvsdbClientService; 37 +import org.onosproject.ovsdb.controller.EventSubject;
38 -import org.onosproject.ovsdb.controller.OvsdbConstant; 38 +import org.onosproject.ovsdb.controller.OvsdbClientService;
39 -import org.onosproject.ovsdb.controller.OvsdbController; 39 +import org.onosproject.ovsdb.controller.OvsdbConstant;
40 -import org.onosproject.ovsdb.controller.OvsdbDatapathId; 40 +import org.onosproject.ovsdb.controller.OvsdbController;
41 -import org.onosproject.ovsdb.controller.OvsdbEvent; 41 +import org.onosproject.ovsdb.controller.OvsdbDatapathId;
42 -import org.onosproject.ovsdb.controller.OvsdbEvent.Type; 42 +import org.onosproject.ovsdb.controller.OvsdbEvent;
43 -import org.onosproject.ovsdb.controller.OvsdbEventListener; 43 +import org.onosproject.ovsdb.controller.OvsdbEvent.Type;
44 -import org.onosproject.ovsdb.controller.OvsdbIfaceId; 44 +import org.onosproject.ovsdb.controller.OvsdbEventListener;
45 -import org.onosproject.ovsdb.controller.OvsdbNodeId; 45 +import org.onosproject.ovsdb.controller.OvsdbIfaceId;
46 -import org.onosproject.ovsdb.controller.OvsdbNodeListener; 46 +import org.onosproject.ovsdb.controller.OvsdbNodeId;
47 -import org.onosproject.ovsdb.controller.OvsdbPortName; 47 +import org.onosproject.ovsdb.controller.OvsdbNodeListener;
48 -import org.onosproject.ovsdb.controller.OvsdbPortNumber; 48 +import org.onosproject.ovsdb.controller.OvsdbPortName;
49 -import org.onosproject.ovsdb.controller.OvsdbPortType; 49 +import org.onosproject.ovsdb.controller.OvsdbPortNumber;
50 -import org.onosproject.ovsdb.controller.driver.OvsdbAgent; 50 +import org.onosproject.ovsdb.controller.OvsdbPortType;
51 -import org.onosproject.ovsdb.rfc.jsonrpc.Callback; 51 +import org.onosproject.ovsdb.controller.driver.OvsdbAgent;
52 -import org.onosproject.ovsdb.rfc.message.TableUpdate; 52 +import org.onosproject.ovsdb.rfc.jsonrpc.Callback;
53 -import org.onosproject.ovsdb.rfc.message.TableUpdates; 53 +import org.onosproject.ovsdb.rfc.message.TableUpdate;
54 -import org.onosproject.ovsdb.rfc.message.UpdateNotification; 54 +import org.onosproject.ovsdb.rfc.message.TableUpdates;
55 -import org.onosproject.ovsdb.rfc.notation.OvsdbMap; 55 +import org.onosproject.ovsdb.rfc.message.UpdateNotification;
56 -import org.onosproject.ovsdb.rfc.notation.OvsdbSet; 56 +import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
57 -import org.onosproject.ovsdb.rfc.notation.Row; 57 +import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
58 -import org.onosproject.ovsdb.rfc.notation.UUID; 58 +import org.onosproject.ovsdb.rfc.notation.Row;
59 -import org.onosproject.ovsdb.rfc.schema.DatabaseSchema; 59 +import org.onosproject.ovsdb.rfc.notation.UUID;
60 -import org.onosproject.ovsdb.rfc.table.Bridge; 60 +import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
61 -import org.onosproject.ovsdb.rfc.table.Interface; 61 +import org.onosproject.ovsdb.rfc.table.Bridge;
62 -import org.onosproject.ovsdb.rfc.table.OvsdbTable; 62 +import org.onosproject.ovsdb.rfc.table.Interface;
63 -import org.onosproject.ovsdb.rfc.table.Port; 63 +import org.onosproject.ovsdb.rfc.table.OvsdbTable;
64 -import org.onosproject.ovsdb.rfc.table.TableGenerator; 64 +import org.onosproject.ovsdb.rfc.table.TableGenerator;
65 -import org.onosproject.ovsdb.rfc.utils.FromJsonUtil; 65 +import org.onosproject.ovsdb.rfc.utils.FromJsonUtil;
66 -import org.osgi.service.component.ComponentContext; 66 +import org.osgi.service.component.ComponentContext;
67 -import org.slf4j.Logger; 67 +import org.slf4j.Logger;
68 -import org.slf4j.LoggerFactory; 68 +import org.slf4j.LoggerFactory;
69 - 69 +
70 -import com.fasterxml.jackson.databind.JsonNode; 70 +import com.fasterxml.jackson.databind.JsonNode;
71 - 71 +
72 -/** 72 +/**
73 - * The implementation of OvsdbController. 73 + * The implementation of OvsdbController.
74 - */ 74 + */
75 -@Component(immediate = true) 75 +@Component(immediate = true)
76 -@Service 76 +@Service
77 -public class OvsdbControllerImpl implements OvsdbController { 77 +public class OvsdbControllerImpl implements OvsdbController {
78 - 78 +
79 - public static final Logger log = LoggerFactory 79 + public static final Logger log = LoggerFactory
80 - .getLogger(OvsdbControllerImpl.class); 80 + .getLogger(OvsdbControllerImpl.class);
81 - 81 +
82 - protected ConcurrentHashMap<OvsdbNodeId, OvsdbClientService> ovsdbClients = 82 + protected ConcurrentHashMap<OvsdbNodeId, OvsdbClientService> ovsdbClients =
83 - new ConcurrentHashMap<OvsdbNodeId, OvsdbClientService>(); 83 + new ConcurrentHashMap<OvsdbNodeId, OvsdbClientService>();
84 - 84 +
85 - protected OvsdbAgent agent = new InternalOvsdbNodeAgent(); 85 + protected OvsdbAgent agent = new InternalOvsdbNodeAgent();
86 - protected InternalMonitorCallBack updateCallback = new InternalMonitorCallBack(); 86 + protected InternalMonitorCallBack updateCallback = new InternalMonitorCallBack();
87 - 87 +
88 - protected Set<OvsdbNodeListener> ovsdbNodeListener = new CopyOnWriteArraySet<>(); 88 + protected Set<OvsdbNodeListener> ovsdbNodeListener = new CopyOnWriteArraySet<>();
89 - protected Set<OvsdbEventListener> ovsdbEventListener = new CopyOnWriteArraySet<>(); 89 + protected Set<OvsdbEventListener> ovsdbEventListener = new CopyOnWriteArraySet<>();
90 - 90 +
91 - protected ConcurrentHashMap<String, OvsdbClientService> requestNotification = 91 + protected ConcurrentHashMap<String, OvsdbClientService> requestNotification =
92 - new ConcurrentHashMap<String, OvsdbClientService>(); 92 + new ConcurrentHashMap<String, OvsdbClientService>();
93 - 93 +
94 - protected ConcurrentHashMap<String, String> requestDbName = new ConcurrentHashMap<String, String>(); 94 + protected ConcurrentHashMap<String, String> requestDbName = new ConcurrentHashMap<String, String>();
95 - 95 +
96 - private final Controller controller = new Controller(); 96 + private final Controller controller = new Controller();
97 - 97 +
98 - @Activate 98 + @Activate
99 - public void activate(ComponentContext context) { 99 + public void activate(ComponentContext context) {
100 - controller.start(agent, updateCallback); 100 + controller.start(agent, updateCallback);
101 - log.info("Started"); 101 + log.info("Started");
102 - } 102 + }
103 - 103 +
104 - @Deactivate 104 + @Deactivate
105 - public void deactivate() { 105 + public void deactivate() {
106 - controller.stop(); 106 + controller.stop();
107 - log.info("Stoped"); 107 + log.info("Stoped");
108 - } 108 + }
109 - 109 +
110 - @Override 110 + @Override
111 - public void addNodeListener(OvsdbNodeListener listener) { 111 + public void addNodeListener(OvsdbNodeListener listener) {
112 - if (!ovsdbNodeListener.contains(listener)) { 112 + if (!ovsdbNodeListener.contains(listener)) {
113 - this.ovsdbNodeListener.add(listener); 113 + this.ovsdbNodeListener.add(listener);
114 - } 114 + }
115 - } 115 + }
116 - 116 +
117 - @Override 117 + @Override
118 - public void removeNodeListener(OvsdbNodeListener listener) { 118 + public void removeNodeListener(OvsdbNodeListener listener) {
119 - this.ovsdbNodeListener.remove(listener); 119 + this.ovsdbNodeListener.remove(listener);
120 - } 120 + }
121 - 121 +
122 - @Override 122 + @Override
123 - public void addOvsdbEventListener(OvsdbEventListener listener) { 123 + public void addOvsdbEventListener(OvsdbEventListener listener) {
124 - if (!ovsdbEventListener.contains(listener)) { 124 + if (!ovsdbEventListener.contains(listener)) {
125 - this.ovsdbEventListener.add(listener); 125 + this.ovsdbEventListener.add(listener);
126 - } 126 + }
127 - } 127 + }
128 - 128 +
129 - @Override 129 + @Override
130 - public void removeOvsdbEventListener(OvsdbEventListener listener) { 130 + public void removeOvsdbEventListener(OvsdbEventListener listener) {
131 - this.ovsdbEventListener.remove(listener); 131 + this.ovsdbEventListener.remove(listener);
132 - } 132 + }
133 - 133 +
134 - @Override 134 + @Override
135 - public List<OvsdbNodeId> getNodeIds() { 135 + public List<OvsdbNodeId> getNodeIds() {
136 - // TODO Auto-generated method stub 136 + // TODO Auto-generated method stub
137 - return null; 137 + return null;
138 - } 138 + }
139 - 139 +
140 - @Override 140 + @Override
141 - public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) { 141 + public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) {
142 - return ovsdbClients.get(nodeId); 142 + return ovsdbClients.get(nodeId);
143 - } 143 + }
144 - 144 +
145 - /** 145 + /**
146 - * Implementation of an Ovsdb Agent which is responsible for keeping track 146 + * Implementation of an Ovsdb Agent which is responsible for keeping track
147 - * of connected node and the state in which they are. 147 + * of connected node and the state in which they are.
148 - */ 148 + */
149 - private class InternalOvsdbNodeAgent implements OvsdbAgent { 149 + private class InternalOvsdbNodeAgent implements OvsdbAgent {
150 - @Override 150 + @Override
151 - public void addConnectedNode(OvsdbNodeId nodeId, 151 + public void addConnectedNode(OvsdbNodeId nodeId,
152 - OvsdbClientService ovsdbClient) { 152 + OvsdbClientService ovsdbClient) {
153 - 153 +
154 - if (ovsdbClients.get(nodeId) != null) { 154 + if (ovsdbClients.get(nodeId) != null) {
155 - return; 155 + return;
156 - } else { 156 + } else {
157 - ovsdbClients.put(nodeId, ovsdbClient); 157 + ovsdbClients.put(nodeId, ovsdbClient);
158 - 158 +
159 - try { 159 + try {
160 - List<String> dbNames = ovsdbClient.listDbs().get(); 160 + List<String> dbNames = ovsdbClient.listDbs().get();
161 - for (String dbName : dbNames) { 161 + for (String dbName : dbNames) {
162 - DatabaseSchema dbSchema; 162 + DatabaseSchema dbSchema;
163 - dbSchema = ovsdbClient.getOvsdbSchema(dbName).get(); 163 + dbSchema = ovsdbClient.getOvsdbSchema(dbName).get();
164 - 164 +
165 - log.debug("Begin to monitor tables"); 165 + log.debug("Begin to monitor tables");
166 - String id = java.util.UUID.randomUUID().toString(); 166 + String id = java.util.UUID.randomUUID().toString();
167 - TableUpdates updates = ovsdbClient 167 + TableUpdates updates = ovsdbClient
168 - .monitorTables(dbName, id).get(); 168 + .monitorTables(dbName, id).get();
169 - 169 +
170 - requestDbName.put(id, dbName); 170 + requestDbName.put(id, dbName);
171 - requestNotification.put(id, ovsdbClient); 171 + requestNotification.put(id, ovsdbClient);
172 - 172 +
173 - if (updates != null) { 173 + if (updates != null) {
174 - processTableUpdates(ovsdbClient, updates, 174 + processTableUpdates(ovsdbClient, updates,
175 - dbSchema.name()); 175 + dbSchema.name());
176 - } 176 + }
177 - } 177 + }
178 - } catch (InterruptedException e) { 178 + } catch (InterruptedException e) {
179 - log.warn("Interrupted while waiting to get message from ovsdb"); 179 + log.warn("Interrupted while waiting to get message from ovsdb");
180 - Thread.currentThread().interrupt(); 180 + Thread.currentThread().interrupt();
181 - } catch (ExecutionException e) { 181 + } catch (ExecutionException e) {
182 - log.error("Exception thrown while to get message from ovsdb"); 182 + log.error("Exception thrown while to get message from ovsdb");
183 - } 183 + }
184 - 184 +
185 - log.debug("Add node to north"); 185 + log.debug("Add node to north");
186 - for (OvsdbNodeListener l : ovsdbNodeListener) { 186 + for (OvsdbNodeListener l : ovsdbNodeListener) {
187 - l.nodeAdded(nodeId); 187 + l.nodeAdded(nodeId);
188 - } 188 + }
189 - return; 189 + return;
190 - } 190 + }
191 - } 191 + }
192 - 192 +
193 - @Override 193 + @Override
194 - public void removeConnectedNode(OvsdbNodeId nodeId) { 194 + public void removeConnectedNode(OvsdbNodeId nodeId) {
195 - ovsdbClients.remove(nodeId); 195 + ovsdbClients.remove(nodeId);
196 - log.debug("Node connection is removed"); 196 + log.debug("Node connection is removed");
197 - for (OvsdbNodeListener l : ovsdbNodeListener) { 197 + for (OvsdbNodeListener l : ovsdbNodeListener) {
198 - l.nodeRemoved(nodeId); 198 + l.nodeRemoved(nodeId);
199 - } 199 + }
200 - } 200 + }
201 - } 201 + }
202 - 202 +
203 - /** 203 + /**
204 - * Processes table updates. 204 + * Processes table updates.
205 - * 205 + *
206 - * @param clientService OvsdbClientService instance 206 + * @param clientService OvsdbClientService instance
207 - * @param updates TableUpdates instance 207 + * @param updates TableUpdates instance
208 - * @param dbName ovsdb database name 208 + * @param dbName ovsdb database name
209 - */ 209 + */
210 - private void processTableUpdates(OvsdbClientService clientService, 210 + private void processTableUpdates(OvsdbClientService clientService,
211 - TableUpdates updates, String dbName) 211 + TableUpdates updates, String dbName)
212 - throws InterruptedException { 212 + throws InterruptedException {
213 - checkNotNull(clientService, "OvsdbClientService is not null"); 213 + checkNotNull(clientService, "OvsdbClientService is not null");
214 - 214 +
215 - DatabaseSchema dbSchema = clientService.getDatabaseSchema(dbName); 215 + DatabaseSchema dbSchema = clientService.getDatabaseSchema(dbName);
216 - 216 +
217 - for (String tableName : updates.result().keySet()) { 217 + for (String tableName : updates.result().keySet()) {
218 - TableUpdate update = updates.result().get(tableName); 218 + TableUpdate update = updates.result().get(tableName);
219 - for (UUID uuid : (Set<UUID>) update.rows().keySet()) { 219 + for (UUID uuid : (Set<UUID>) update.rows().keySet()) {
220 - log.debug("Begin to process table updates uuid: {}, databaseName: {}, tableName: {}", 220 + log.debug("Begin to process table updates uuid: {}, databaseName: {}, tableName: {}",
221 - uuid.value(), dbName, tableName); 221 + uuid.value(), dbName, tableName);
222 - 222 +
223 - Row row = clientService.getRow(dbName, tableName, uuid.value()); 223 + Row oldRow = update.getOld(uuid);
224 - clientService.updateOvsdbStore(dbName, tableName, uuid.value(), 224 + Row newRow = update.getNew(uuid);
225 - update.getNew(uuid)); 225 + if (newRow != null) {
226 - if (update.getNew(uuid) != null) { 226 + clientService.updateOvsdbStore(dbName, tableName,
227 - boolean isNewRow = (row == null) ? true : false; 227 + uuid.value(), newRow);
228 - if (isNewRow) { 228 +
229 - if (OvsdbConstant.PORT.equals(tableName)) { 229 + if (OvsdbConstant.INTERFACE.equals(tableName)) {
230 - dispatchEvent(clientService, update.getNew(uuid), 230 + dispatchInterfaceEvent(clientService,
231 - null, OvsdbEvent.Type.PORT_ADDED, 231 + newRow, null,
232 - dbSchema); 232 + OvsdbEvent.Type.PORT_ADDED,
233 - } 233 + dbSchema);
234 - } 234 + }
235 - } else if (update.getOld(uuid) != null) { 235 + } else if (update.getOld(uuid) != null) {
236 - clientService.removeRow(dbName, tableName, uuid.toString()); 236 + clientService.removeRow(dbName, tableName, uuid.value());
237 - if (update.getOld(uuid) != null) { 237 + if (OvsdbConstant.PORT.equals(tableName)) {
238 - if (OvsdbConstant.PORT.equals(tableName)) { 238 + dispatchInterfaceEvent(clientService, null,
239 - dispatchEvent(clientService, null, 239 + oldRow,
240 - update.getOld(uuid), 240 + OvsdbEvent.Type.PORT_REMOVED,
241 - OvsdbEvent.Type.PORT_REMOVED, 241 + dbSchema);
242 - dbSchema); 242 + }
243 - } 243 + }
244 - } 244 + }
245 - } 245 + }
246 - } 246 + }
247 - } 247 +
248 - } 248 + /**
249 - 249 + * Dispatches event to the north.
250 - /** 250 + *
251 - * Dispatches event to the north. 251 + * @param clientService OvsdbClientService instance
252 - * 252 + * @param newRow a new row
253 - * @param clientService OvsdbClientService instance 253 + * @param oldRow an old row
254 - * @param newRow a new row 254 + * @param eventType type of event
255 - * @param oldRow an old row 255 + * @param dbSchema ovsdb database schema
256 - * @param eventType type of event 256 + */
257 - * @param dbSchema ovsdb database schema 257 + private void dispatchInterfaceEvent(OvsdbClientService clientService,
258 - */ 258 + Row newRow, Row oldRow,
259 - private void dispatchEvent(OvsdbClientService clientService, Row newRow, 259 + Type eventType,
260 - Row oldRow, Type eventType, 260 + DatabaseSchema dbSchema) {
261 - DatabaseSchema dbSchema) { 261 +
262 - Port port = null; 262 + long dpid = getDataPathid(clientService, dbSchema);
263 - if (OvsdbEvent.Type.PORT_ADDED.equals(eventType)) { 263 + Interface intf = (Interface) TableGenerator
264 - port = (Port) TableGenerator.getTable(dbSchema, newRow, 264 + .getTable(dbSchema, newRow, OvsdbTable.INTERFACE);
265 - OvsdbTable.PORT); 265 + if (intf == null) {
266 - } else if (OvsdbEvent.Type.PORT_REMOVED.equals(eventType)) { 266 + return;
267 - port = (Port) TableGenerator.getTable(dbSchema, oldRow, 267 + }
268 - OvsdbTable.PORT); 268 +
269 - } 269 + String portType = (String) intf.getTypeColumn().data();
270 - if (port == null) { 270 + long localPort = getOfPort(intf);
271 - return; 271 + if (localPort < 0) {
272 - } 272 + return;
273 - 273 + }
274 - long dpid = getDataPathid(clientService, dbSchema); 274 + String[] macAndIfaceId = getMacAndIfaceid(intf);
275 - OvsdbSet intfUuidSet = (OvsdbSet) port.getInterfacesColumn().data(); 275 + if (macAndIfaceId == null) {
276 - @SuppressWarnings({ "unchecked" }) 276 + return;
277 - Set<UUID> intfUuids = intfUuidSet.set(); 277 + }
278 - for (UUID intfUuid : intfUuids) { 278 +
279 - Row intfRow = clientService 279 + EventSubject eventSubject = new DefaultEventSubject(MacAddress.valueOf(
280 - .getRow(OvsdbConstant.DATABASENAME, "Interface", 280 + macAndIfaceId[0]),
281 - intfUuid.toString()); 281 + new HashSet<IpAddress>(),
282 - if (intfRow == null) { 282 + new OvsdbPortName(intf
283 - continue; 283 + .getName()),
284 - } 284 + new OvsdbPortNumber(localPort),
285 - Interface intf = (Interface) TableGenerator 285 + new OvsdbDatapathId(Long
286 - .getTable(dbSchema, intfRow, OvsdbTable.INTERFACE); 286 + .toString(dpid)),
287 - 287 + new OvsdbPortType(portType),
288 - String portType = (String) intf.getTypeColumn().data(); 288 + new OvsdbIfaceId(macAndIfaceId[1]));
289 - long localPort = getOfPort(intf); 289 + for (OvsdbEventListener listener : ovsdbEventListener) {
290 - String[] macAndIfaceId = getMacAndIfaceid(intf); 290 + listener.handle(new OvsdbEvent<EventSubject>(eventType,
291 - if (macAndIfaceId == null) { 291 + eventSubject));
292 - return; 292 + }
293 - } 293 + }
294 - EventSubject eventSubject = new DefaultEventSubject( 294 +
295 - MacAddress 295 + /**
296 - .valueOf(macAndIfaceId[0]), 296 + * Gets mac and iface from the table Interface.
297 - new HashSet<IpAddress>(), 297 + *
298 - new OvsdbPortName(port.getName()), 298 + * @param intf Interface instance
299 - new OvsdbPortNumber(localPort), 299 + * @return attachedMac, ifaceid
300 - new OvsdbDatapathId(Long.toString(dpid)), 300 + */
301 - new OvsdbPortType(portType), 301 + private String[] getMacAndIfaceid(Interface intf) {
302 - new OvsdbIfaceId(macAndIfaceId[1])); 302 + OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
303 - for (OvsdbEventListener listener : ovsdbEventListener) { 303 + @SuppressWarnings("unchecked")
304 - listener.handle(new OvsdbEvent<EventSubject>(eventType, 304 + Map<String, String> externalIds = ovsdbMap.map();
305 - eventSubject)); 305 + if (externalIds == null) {
306 - } 306 + log.warn("The external_ids is null");
307 - 307 + return null;
308 - } 308 + }
309 - 309 +
310 - } 310 + String attachedMac = externalIds.get(OvsdbConstant.EXTERNAL_ID_VM_MAC);
311 - 311 + if (attachedMac == null) {
312 - /** 312 + log.warn("The attachedMac is null");
313 - * Gets mac and iface from the table Interface. 313 + return null;
314 - * 314 + }
315 - * @param intf Interface instance 315 + String ifaceid = externalIds
316 - * @return attachedMac, ifaceid 316 + .get(OvsdbConstant.EXTERNAL_ID_INTERFACE_ID);
317 - */ 317 + if (ifaceid == null) {
318 - private String[] getMacAndIfaceid(Interface intf) { 318 + log.warn("The ifaceid is null");
319 - OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data(); 319 + return null;
320 - @SuppressWarnings("unchecked") 320 + }
321 - Map<String, String> externalIds = ovsdbMap.map(); 321 + return new String[] {attachedMac, ifaceid};
322 - if (externalIds == null) { 322 + }
323 - log.warn("The external_ids is null"); 323 +
324 - return null; 324 + /**
325 - } 325 + * Gets ofPorts number from table Interface.
326 - 326 + *
327 - String attachedMac = externalIds.get(OvsdbConstant.EXTERNAL_ID_VM_MAC); 327 + * @param intf Interface instance
328 - if (attachedMac == null) { 328 + * @return ofport the ofport number
329 - log.warn("The attachedMac is null"); 329 + */
330 - return null; 330 + private long getOfPort(Interface intf) {
331 - } 331 + OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
332 - String ifaceid = externalIds 332 + @SuppressWarnings("unchecked")
333 - .get(OvsdbConstant.EXTERNAL_ID_INTERFACE_ID); 333 + Set<Integer> ofPorts = ofPortSet.set();
334 - if (ifaceid == null) { 334 + while (ofPorts == null || ofPorts.size() <= 0) {
335 - log.warn("The ifaceid is null"); 335 + log.debug("The ofport is null in {}", intf.getName());
336 - return null; 336 + return -1;
337 - } 337 + }
338 - return new String[] {attachedMac, ifaceid}; 338 + Iterator<Integer> it = ofPorts.iterator();
339 - } 339 + return Long.parseLong(it.next().toString());
340 - 340 + }
341 - /** 341 +
342 - * Gets ofPorts number from table Interface. 342 + /**
343 - * 343 + * Gets datapathid from table bridge.
344 - * @param intf Interface instance 344 + *
345 - * @return ofport the ofport number 345 + * @param clientService OvsdbClientService instance
346 - */ 346 + * @param dbSchema ovsdb database schema
347 - private long getOfPort(Interface intf) { 347 + * @return datapathid the bridge datapathid
348 - OvsdbSet ovsdbSet = (OvsdbSet) intf.getOpenFlowPortColumn().data(); 348 + */
349 - @SuppressWarnings("unchecked") 349 + private long getDataPathid(OvsdbClientService clientService,
350 - Set<Long> ofPorts = ovsdbSet.set(); 350 + DatabaseSchema dbSchema) {
351 - while (ofPorts == null || ofPorts.size() <= 0) { 351 + String bridgeUuid = clientService
352 - log.debug("The ofport is null in {}", intf.getName()); 352 + .getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE);
353 - return 0; 353 + if (bridgeUuid == null) {
354 - } 354 + log.debug("Unable to spot bridge uuid for {} in {}",
355 - return (long) ofPorts.toArray()[0]; 355 + OvsdbConstant.INTEGRATION_BRIDGE, clientService);
356 - } 356 + return 0;
357 - 357 + }
358 - /** 358 +
359 - * Gets datapathid from table bridge. 359 + Row bridgeRow = clientService.getRow(OvsdbConstant.DATABASENAME,
360 - * 360 + "Bridge", bridgeUuid);
361 - * @param clientService OvsdbClientService instance 361 + Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow,
362 - * @param dbSchema ovsdb database schema 362 + OvsdbTable.BRIDGE);
363 - * @return datapathid the bridge datapathid 363 + OvsdbSet dpidSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
364 - */ 364 + @SuppressWarnings("unchecked")
365 - private long getDataPathid(OvsdbClientService clientService, 365 + Set<String> dpids = dpidSet.set();
366 - DatabaseSchema dbSchema) { 366 + if (dpids == null || dpids.size() == 0) {
367 - String bridgeUuid = clientService 367 + return 0;
368 - .getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE); 368 + }
369 - if (bridgeUuid == null) { 369 + return stringToLong((String) dpids.toArray()[0]);
370 - log.debug("Unable to spot bridge uuid for {} in {}", 370 + }
371 - OvsdbConstant.INTEGRATION_BRIDGE, clientService); 371 +
372 - return 0; 372 + private long stringToLong(String values) {
373 - } 373 + long value = (new BigInteger(values.replaceAll(":", ""), 16))
374 - 374 + .longValue();
375 - Row bridgeRow = clientService.getRow(OvsdbConstant.DATABASENAME, 375 + return value;
376 - "Bridge", bridgeUuid); 376 + }
377 - Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow, 377 +
378 - OvsdbTable.BRIDGE); 378 + /**
379 - OvsdbSet ovsdbSet = (OvsdbSet) bridge.getDatapathIdColumn().data(); 379 + * Implementation of an Callback which is responsible for receiving request
380 - @SuppressWarnings("unchecked") 380 + * infomation from ovsdb.
381 - Set<String> dpids = ovsdbSet.set(); 381 + */
382 - if (dpids == null || dpids.size() == 0) { 382 + private class InternalMonitorCallBack implements Callback {
383 - return 0; 383 + @Override
384 - } 384 + public void update(UpdateNotification updateNotification) {
385 - return stringToLong((String) dpids.toArray()[0]); 385 + Object key = updateNotification.jsonValue();
386 - } 386 + OvsdbClientService ovsdbClient = requestNotification.get(key);
387 - 387 +
388 - private long stringToLong(String values) { 388 + String dbName = requestDbName.get(key);
389 - long value = (new BigInteger(values.replaceAll(":", ""), 16)) 389 + JsonNode updatesJson = updateNotification.tbUpdatesJsonNode();
390 - .longValue(); 390 + DatabaseSchema dbSchema = ovsdbClient.getDatabaseSchema(dbName);
391 - return value; 391 + TableUpdates updates = FromJsonUtil
392 - } 392 + .jsonNodeToTableUpdates(updatesJson, dbSchema);
393 - 393 + try {
394 - /** 394 + processTableUpdates(ovsdbClient, updates, dbName);
395 - * Implementation of an Callback which is responsible for receiving request 395 + } catch (InterruptedException e) {
396 - * infomation from ovsdb. 396 + log.warn("Interrupted while processing table updates");
397 - */ 397 + Thread.currentThread().interrupt();
398 - private class InternalMonitorCallBack implements Callback { 398 + }
399 - @Override 399 + }
400 - public void update(UpdateNotification updateNotification) { 400 +
401 - Object key = updateNotification.jsonValue(); 401 + @Override
402 - OvsdbClientService ovsdbClient = requestNotification.get(key); 402 + public void locked(List<String> ids) {
403 - 403 + // TODO Auto-generated method stub
404 - String dbName = requestDbName.get(key); 404 + }
405 - JsonNode updatesJson = updateNotification.tbUpdatesJsonNode(); 405 +
406 - DatabaseSchema dbSchema = ovsdbClient.getDatabaseSchema(dbName); 406 + @Override
407 - TableUpdates updates = FromJsonUtil 407 + public void stolen(List<String> ids) {
408 - .jsonNodeToTableUpdates(updatesJson, dbSchema); 408 + // TODO Auto-generated method stub
409 - try { 409 + }
410 - processTableUpdates(ovsdbClient, updates, dbName); 410 +
411 - } catch (InterruptedException e) { 411 + }
412 - log.warn("Interrupted while processing table updates"); 412 +
413 - Thread.currentThread().interrupt(); 413 +}
414 - }
415 - }
416 -
417 - @Override
418 - public void locked(List<String> ids) {
419 - // TODO Auto-generated method stub
420 - }
421 -
422 - @Override
423 - public void stolen(List<String> ids) {
424 - // TODO Auto-generated method stub
425 - }
426 -
427 - }
428 -
429 -}
......