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