blob: e4e96f559b68da7ffafd2118c7392088a0a701f1 [file] [log] [blame]
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Sho SHIMIZUe4efe452015-08-26 15:06:55 -07003 *
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
andreaed976a42015-10-05 14:38:25 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.google.common.collect.ImmutableList;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Jian Lieaf31032018-05-03 15:54:03 +090023import org.apache.felix.scr.annotations.Modified;
24import org.apache.felix.scr.annotations.Property;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070027import org.apache.felix.scr.annotations.Service;
28import org.onlab.packet.IpAddress;
29import org.onlab.packet.MacAddress;
Hyunsun Moon5fb20a52015-09-25 17:02:33 -070030import org.onlab.packet.TpPort;
Jian Lieaf31032018-05-03 15:54:03 +090031import org.onlab.util.Tools;
32import org.onosproject.cfg.ComponentConfigService;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070033import org.onosproject.ovsdb.controller.DefaultEventSubject;
34import org.onosproject.ovsdb.controller.EventSubject;
35import org.onosproject.ovsdb.controller.OvsdbClientService;
36import org.onosproject.ovsdb.controller.OvsdbConstant;
37import org.onosproject.ovsdb.controller.OvsdbController;
38import org.onosproject.ovsdb.controller.OvsdbDatapathId;
39import org.onosproject.ovsdb.controller.OvsdbEvent;
40import org.onosproject.ovsdb.controller.OvsdbEvent.Type;
41import org.onosproject.ovsdb.controller.OvsdbEventListener;
42import org.onosproject.ovsdb.controller.OvsdbIfaceId;
43import org.onosproject.ovsdb.controller.OvsdbNodeId;
44import org.onosproject.ovsdb.controller.OvsdbNodeListener;
45import org.onosproject.ovsdb.controller.OvsdbPortName;
46import org.onosproject.ovsdb.controller.OvsdbPortNumber;
47import org.onosproject.ovsdb.controller.OvsdbPortType;
48import org.onosproject.ovsdb.controller.driver.OvsdbAgent;
49import org.onosproject.ovsdb.rfc.jsonrpc.Callback;
50import org.onosproject.ovsdb.rfc.message.TableUpdate;
51import org.onosproject.ovsdb.rfc.message.TableUpdates;
52import org.onosproject.ovsdb.rfc.message.UpdateNotification;
53import org.onosproject.ovsdb.rfc.notation.OvsdbMap;
54import org.onosproject.ovsdb.rfc.notation.OvsdbSet;
55import org.onosproject.ovsdb.rfc.notation.Row;
Jonathan Hart51539b82015-10-29 09:53:04 -070056import org.onosproject.ovsdb.rfc.notation.Uuid;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070057import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
58import org.onosproject.ovsdb.rfc.table.Bridge;
59import org.onosproject.ovsdb.rfc.table.Interface;
60import org.onosproject.ovsdb.rfc.table.OvsdbTable;
61import org.onosproject.ovsdb.rfc.table.TableGenerator;
62import org.onosproject.ovsdb.rfc.utils.FromJsonUtil;
63import org.osgi.service.component.ComponentContext;
64import org.slf4j.Logger;
65import org.slf4j.LoggerFactory;
66
andreaed976a42015-10-05 14:38:25 -070067import java.math.BigInteger;
Jian Lieaf31032018-05-03 15:54:03 +090068import java.util.Dictionary;
andreaed976a42015-10-05 14:38:25 -070069import java.util.HashSet;
70import java.util.Iterator;
71import java.util.List;
72import java.util.Map;
73import java.util.Set;
74import java.util.concurrent.ConcurrentHashMap;
75import java.util.concurrent.CopyOnWriteArraySet;
76import java.util.concurrent.ExecutionException;
jiangruibce80652017-10-17 14:35:42 +080077import java.util.concurrent.TimeUnit;
78import java.util.concurrent.TimeoutException;
jaegonkim1af0ae52017-01-01 10:46:55 +090079import java.util.function.Consumer;
andreaed976a42015-10-05 14:38:25 -070080
81import static com.google.common.base.Preconditions.checkNotNull;
Jian Lieaf31032018-05-03 15:54:03 +090082import static org.onosproject.ovsdb.controller.OvsdbConstant.SERVER_MODE;
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070083
84/**
85 * The implementation of OvsdbController.
86 */
87@Component(immediate = true)
88@Service
89public class OvsdbControllerImpl implements OvsdbController {
90
91 public static final Logger log = LoggerFactory
92 .getLogger(OvsdbControllerImpl.class);
jiangruibce80652017-10-17 14:35:42 +080093 private static final long DEFAULT_OVSDB_RPC_TIMEOUT = 3000;
94 private final Controller controller = new Controller();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070095 protected ConcurrentHashMap<OvsdbNodeId, OvsdbClientService> ovsdbClients =
96 new ConcurrentHashMap<OvsdbNodeId, OvsdbClientService>();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070097 protected OvsdbAgent agent = new InternalOvsdbNodeAgent();
98 protected InternalMonitorCallBack updateCallback = new InternalMonitorCallBack();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -070099 protected Set<OvsdbNodeListener> ovsdbNodeListener = new CopyOnWriteArraySet<>();
100 protected Set<OvsdbEventListener> ovsdbEventListener = new CopyOnWriteArraySet<>();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700101 protected ConcurrentHashMap<String, OvsdbClientService> requestNotification =
102 new ConcurrentHashMap<String, OvsdbClientService>();
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700103 protected ConcurrentHashMap<String, String> requestDbName = new ConcurrentHashMap<String, String>();
104
Jian Lieaf31032018-05-03 15:54:03 +0900105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected ComponentConfigService configService;
107
108 @Property(name = "serverMode", boolValue = SERVER_MODE,
109 label = "Run as server mode, listen on 6640 port")
110 private boolean serverMode = SERVER_MODE;
111
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700112 @Activate
113 public void activate(ComponentContext context) {
Jian Lieaf31032018-05-03 15:54:03 +0900114 controller.start(agent, updateCallback, serverMode);
115
116 configService.registerProperties(getClass());
117
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700118 log.info("Started");
119 }
120
121 @Deactivate
122 public void deactivate() {
123 controller.stop();
Jian Lieaf31032018-05-03 15:54:03 +0900124
125 configService.unregisterProperties(getClass(), false);
126
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700127 log.info("Stoped");
128 }
129
Jian Lieaf31032018-05-03 15:54:03 +0900130 @Modified
131 protected void modified(ComponentContext context) {
132 Dictionary<?, ?> properties = context.getProperties();
133 Boolean flag;
134
135 flag = Tools.isPropertyEnabled(properties, "serverMode");
136 if (flag == null) {
137 log.info("OVSDB server mode is not configured, " +
138 "using current value of {}", serverMode);
139 } else {
140 serverMode = flag;
141 log.info("Configured. OVSDB server mode was {}",
142 serverMode ? "enabled" : "disabled");
143 }
144
145 restartController();
146 }
147
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700148 @Override
149 public void addNodeListener(OvsdbNodeListener listener) {
150 if (!ovsdbNodeListener.contains(listener)) {
151 this.ovsdbNodeListener.add(listener);
152 }
153 }
154
155 @Override
156 public void removeNodeListener(OvsdbNodeListener listener) {
157 this.ovsdbNodeListener.remove(listener);
158 }
159
160 @Override
161 public void addOvsdbEventListener(OvsdbEventListener listener) {
162 if (!ovsdbEventListener.contains(listener)) {
163 this.ovsdbEventListener.add(listener);
164 }
165 }
166
167 @Override
168 public void removeOvsdbEventListener(OvsdbEventListener listener) {
169 this.ovsdbEventListener.remove(listener);
170 }
171
172 @Override
173 public List<OvsdbNodeId> getNodeIds() {
andreaed976a42015-10-05 14:38:25 -0700174 return ImmutableList.copyOf(ovsdbClients.keySet());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700175 }
176
177 @Override
178 public OvsdbClientService getOvsdbClient(OvsdbNodeId nodeId) {
179 return ovsdbClients.get(nodeId);
180 }
181
Hyunsun Moon5fb20a52015-09-25 17:02:33 -0700182 @Override
183 public void connect(IpAddress ip, TpPort port) {
184 controller.connect(ip, port);
185 }
186
jaegonkim1af0ae52017-01-01 10:46:55 +0900187 @Override
188 public void connect(IpAddress ip, TpPort port, Consumer<Exception> failhandler) {
189 controller.connect(ip, port, failhandler);
190 }
191
Jian Lieaf31032018-05-03 15:54:03 +0900192 @Override
193 public void setServerMode(boolean serverMode) {
194 this.serverMode = serverMode;
195 restartController();
196 }
197
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700198 /**
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700199 * Processes table updates.
200 *
201 * @param clientService OvsdbClientService instance
andreaed976a42015-10-05 14:38:25 -0700202 * @param updates TableUpdates instance
203 * @param dbName ovsdb database name
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700204 */
205 private void processTableUpdates(OvsdbClientService clientService,
206 TableUpdates updates, String dbName)
207 throws InterruptedException {
208 checkNotNull(clientService, "OvsdbClientService is not null");
209
210 DatabaseSchema dbSchema = clientService.getDatabaseSchema(dbName);
211
212 for (String tableName : updates.result().keySet()) {
213 TableUpdate update = updates.result().get(tableName);
Jonathan Hart51539b82015-10-29 09:53:04 -0700214 for (Uuid uuid : (Set<Uuid>) update.rows().keySet()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700215 log.debug("Begin to process table updates uuid: {}, databaseName: {}, tableName: {}",
216 uuid.value(), dbName, tableName);
217
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700218 Row newRow = update.getNew(uuid);
219 if (newRow != null) {
220 clientService.updateOvsdbStore(dbName, tableName,
221 uuid.value(), newRow);
222
223 if (OvsdbConstant.INTERFACE.equals(tableName)) {
224 dispatchInterfaceEvent(clientService,
CNluciusa66c3972015-09-06 20:31:29 +0800225 newRow,
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700226 OvsdbEvent.Type.PORT_ADDED,
227 dbSchema);
228 }
229 } else if (update.getOld(uuid) != null) {
CNluciusa66c3972015-09-06 20:31:29 +0800230 if (OvsdbConstant.INTERFACE.equals(tableName)) {
231 Row row = clientService.getRow(OvsdbConstant.DATABASENAME, tableName, uuid.value());
232 dispatchInterfaceEvent(clientService,
233 row,
andreaed976a42015-10-05 14:38:25 -0700234 OvsdbEvent.Type.PORT_REMOVED,
235 dbSchema);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700236 }
CNluciusa66c3972015-09-06 20:31:29 +0800237 clientService.removeRow(dbName, tableName, uuid.value());
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700238 }
239 }
240 }
241 }
242
243 /**
244 * Dispatches event to the north.
245 *
246 * @param clientService OvsdbClientService instance
tanbangchenge8d04392017-11-18 18:12:47 +0800247 * @param row a new row
andreaed976a42015-10-05 14:38:25 -0700248 * @param eventType type of event
249 * @param dbSchema ovsdb database schema
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700250 */
251 private void dispatchInterfaceEvent(OvsdbClientService clientService,
CNluciusa66c3972015-09-06 20:31:29 +0800252 Row row,
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700253 Type eventType,
254 DatabaseSchema dbSchema) {
255
256 long dpid = getDataPathid(clientService, dbSchema);
257 Interface intf = (Interface) TableGenerator
CNluciusa66c3972015-09-06 20:31:29 +0800258 .getTable(dbSchema, row, OvsdbTable.INTERFACE);
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700259 if (intf == null) {
260 return;
261 }
262
263 String portType = (String) intf.getTypeColumn().data();
264 long localPort = getOfPort(intf);
265 if (localPort < 0) {
266 return;
267 }
268 String[] macAndIfaceId = getMacAndIfaceid(intf);
269 if (macAndIfaceId == null) {
270 return;
271 }
272
273 EventSubject eventSubject = new DefaultEventSubject(MacAddress.valueOf(
andreaed976a42015-10-05 14:38:25 -0700274 macAndIfaceId[0]),
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700275 new HashSet<IpAddress>(),
276 new OvsdbPortName(intf
andreaed976a42015-10-05 14:38:25 -0700277 .getName()),
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700278 new OvsdbPortNumber(localPort),
279 new OvsdbDatapathId(Long
andreaed976a42015-10-05 14:38:25 -0700280 .toString(dpid)),
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700281 new OvsdbPortType(portType),
282 new OvsdbIfaceId(macAndIfaceId[1]));
283 for (OvsdbEventListener listener : ovsdbEventListener) {
284 listener.handle(new OvsdbEvent<EventSubject>(eventType,
285 eventSubject));
286 }
287 }
288
289 /**
290 * Gets mac and iface from the table Interface.
291 *
292 * @param intf Interface instance
293 * @return attachedMac, ifaceid
294 */
295 private String[] getMacAndIfaceid(Interface intf) {
296 OvsdbMap ovsdbMap = (OvsdbMap) intf.getExternalIdsColumn().data();
297 @SuppressWarnings("unchecked")
298 Map<String, String> externalIds = ovsdbMap.map();
299 if (externalIds == null) {
300 log.warn("The external_ids is null");
301 return null;
302 }
303
304 String attachedMac = externalIds.get(OvsdbConstant.EXTERNAL_ID_VM_MAC);
305 if (attachedMac == null) {
andreaed976a42015-10-05 14:38:25 -0700306 log.debug("The attachedMac is null"); //FIXME why always null?
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700307 return null;
308 }
309 String ifaceid = externalIds
310 .get(OvsdbConstant.EXTERNAL_ID_INTERFACE_ID);
311 if (ifaceid == null) {
312 log.warn("The ifaceid is null");
313 return null;
314 }
andreaed976a42015-10-05 14:38:25 -0700315 return new String[]{attachedMac, ifaceid};
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700316 }
317
318 /**
319 * Gets ofPorts number from table Interface.
320 *
321 * @param intf Interface instance
322 * @return ofport the ofport number
323 */
324 private long getOfPort(Interface intf) {
325 OvsdbSet ofPortSet = (OvsdbSet) intf.getOpenFlowPortColumn().data();
326 @SuppressWarnings("unchecked")
327 Set<Integer> ofPorts = ofPortSet.set();
Jon Hallb1f4e0f2017-02-22 13:36:16 -0800328 if (ofPorts == null || ofPorts.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700329 log.debug("The ofport is null in {}", intf.getName());
330 return -1;
331 }
332 Iterator<Integer> it = ofPorts.iterator();
333 return Long.parseLong(it.next().toString());
334 }
335
336 /**
337 * Gets datapathid from table bridge.
338 *
339 * @param clientService OvsdbClientService instance
andreaed976a42015-10-05 14:38:25 -0700340 * @param dbSchema ovsdb database schema
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700341 * @return datapathid the bridge datapathid
342 */
343 private long getDataPathid(OvsdbClientService clientService,
344 DatabaseSchema dbSchema) {
345 String bridgeUuid = clientService
346 .getBridgeUuid(OvsdbConstant.INTEGRATION_BRIDGE);
347 if (bridgeUuid == null) {
348 log.debug("Unable to spot bridge uuid for {} in {}",
349 OvsdbConstant.INTEGRATION_BRIDGE, clientService);
350 return 0;
351 }
352
353 Row bridgeRow = clientService.getRow(OvsdbConstant.DATABASENAME,
354 "Bridge", bridgeUuid);
355 Bridge bridge = (Bridge) TableGenerator.getTable(dbSchema, bridgeRow,
356 OvsdbTable.BRIDGE);
357 OvsdbSet dpidSet = (OvsdbSet) bridge.getDatapathIdColumn().data();
358 @SuppressWarnings("unchecked")
359 Set<String> dpids = dpidSet.set();
Jon Hallcbd1b392017-01-18 20:15:44 -0800360 if (dpids == null || dpids.isEmpty()) {
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700361 return 0;
362 }
363 return stringToLong((String) dpids.toArray()[0]);
364 }
365
366 private long stringToLong(String values) {
367 long value = (new BigInteger(values.replaceAll(":", ""), 16))
368 .longValue();
369 return value;
370 }
371
Jian Lieaf31032018-05-03 15:54:03 +0900372 private void restartController() {
373 controller.stop();
374 controller.start(agent, updateCallback, serverMode);
375 }
376
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700377 /**
jiangruibce80652017-10-17 14:35:42 +0800378 * Implementation of an Ovsdb Agent which is responsible for keeping track
379 * of connected node and the state in which they are.
380 */
381 private class InternalOvsdbNodeAgent implements OvsdbAgent {
382 @Override
383 public void addConnectedNode(OvsdbNodeId nodeId,
384 OvsdbClientService ovsdbClient) {
385
386 if (ovsdbClients.get(nodeId) != null) {
387 ovsdbClient.disconnect();
388 return;
389 } else {
390
391 try {
392 List<String> dbNames = ovsdbClient.listDbs().get(DEFAULT_OVSDB_RPC_TIMEOUT, TimeUnit.MILLISECONDS);
393 for (String dbName : dbNames) {
394 DatabaseSchema dbSchema;
395 dbSchema = ovsdbClient.getOvsdbSchema(dbName)
396 .get(DEFAULT_OVSDB_RPC_TIMEOUT, TimeUnit.MILLISECONDS);
397
398 log.debug("Begin to monitor tables");
399 String id = java.util.UUID.randomUUID().toString();
400 TableUpdates updates = ovsdbClient
401 .monitorTables(dbName, id).get(DEFAULT_OVSDB_RPC_TIMEOUT, TimeUnit.MILLISECONDS);
402
403 requestDbName.put(id, dbName);
404 requestNotification.put(id, ovsdbClient);
405
406 if (updates != null) {
407 processTableUpdates(ovsdbClient, updates,
408 dbSchema.name());
409 }
410 }
411 } catch (InterruptedException e) {
412 log.warn("Interrupted while waiting to get message from ovsdb");
413 Thread.currentThread().interrupt();
414 return;
415 } catch (ExecutionException e) {
416 log.error("Exception thrown while to get message from ovsdb");
417 ovsdbClient.disconnect();
418 return;
419 } catch (TimeoutException e) {
420 log.error("TimeoutException thrown while to get message from ovsdb");
421 ovsdbClient.disconnect();
422 return;
423 }
424 ovsdbClients.put(nodeId, ovsdbClient);
425
426 log.debug("Add node to north");
427 for (OvsdbNodeListener l : ovsdbNodeListener) {
428 l.nodeAdded(nodeId);
429 }
430 return;
431 }
432 }
433
434 @Override
435 public void removeConnectedNode(OvsdbNodeId nodeId) {
tanbangchenge8d04392017-11-18 18:12:47 +0800436 requestNotification.forEach((k, v) -> {
437 if (v.nodeId().equals(nodeId)) {
438 requestNotification.remove(k);
439 requestDbName.remove(k);
440
441 ovsdbClients.remove(nodeId);
442 log.debug("Node connection is removed");
443 for (OvsdbNodeListener l : ovsdbNodeListener) {
444 l.nodeRemoved(nodeId);
445 }
446 }
447 });
jiangruibce80652017-10-17 14:35:42 +0800448 }
449 }
450
451 /**
Sho SHIMIZUe4efe452015-08-26 15:06:55 -0700452 * Implementation of an Callback which is responsible for receiving request
453 * infomation from ovsdb.
454 */
455 private class InternalMonitorCallBack implements Callback {
456 @Override
457 public void update(UpdateNotification updateNotification) {
458 Object key = updateNotification.jsonValue();
459 OvsdbClientService ovsdbClient = requestNotification.get(key);
460
461 String dbName = requestDbName.get(key);
462 JsonNode updatesJson = updateNotification.tbUpdatesJsonNode();
463 DatabaseSchema dbSchema = ovsdbClient.getDatabaseSchema(dbName);
464 TableUpdates updates = FromJsonUtil
465 .jsonNodeToTableUpdates(updatesJson, dbSchema);
466 try {
467 processTableUpdates(ovsdbClient, updates, dbName);
468 } catch (InterruptedException e) {
469 log.warn("Interrupted while processing table updates");
470 Thread.currentThread().interrupt();
471 }
472 }
473
474 @Override
475 public void locked(List<String> ids) {
476 // TODO Auto-generated method stub
477 }
478
479 @Override
480 public void stolen(List<String> ids) {
481 // TODO Auto-generated method stub
482 }
483
484 }
485
486}