2 * Copyright (C) 2014 Red Hat, Inc.
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
8 * Authors : Madhu Venugopal, Dave Tucker
11 package org.opendaylight.ovsdb.schema.openvswitch;
13 import java.io.IOException;
14 import java.util.HashMap;
15 import java.util.List;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeoutException;
21 import junit.framework.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.ovsdb.lib.MonitorCallBack;
26 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
27 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
28 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
29 import org.opendaylight.ovsdb.lib.message.TableUpdate;
30 import org.opendaylight.ovsdb.lib.message.TableUpdates;
31 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
32 import org.opendaylight.ovsdb.lib.notation.Row;
33 import org.opendaylight.ovsdb.lib.notation.UUID;
34 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
35 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
36 import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.collect.Lists;
42 public class MonitorTestCases extends OpenVswitchSchemaTestBase {
44 Logger logger = LoggerFactory.getLogger(PortAndInterfaceTestCases.class);
48 public void setUp() throws ExecutionException, InterruptedException, TimeoutException, IOException {
50 OpenVswitchSchemaSuiteIT.dbSchema = this.ovs.getSchema(OPEN_VSWITCH_SCHEMA).get();
54 public void monitorTables() throws ExecutionException, InterruptedException, IOException {
55 Assert.assertNotNull(OpenVswitchSchemaSuiteIT.dbSchema);
57 List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
58 monitorRequests.add(this.getAllColumnsMonitorRequest(Bridge.class));
59 monitorRequests.add(this.getAllColumnsMonitorRequest(OpenVSwitch.class));
61 TableUpdates updates = ovs.monitor(OpenVswitchSchemaSuiteIT.dbSchema, monitorRequests, new UpdateMonitor());
62 Assert.assertNotNull(updates);
63 this.updateTableCache(updates);
67 * As per RFC 7047, section 4.1.5, if a Monitor request is sent without any columns, the update response will not include
69 * ----------------------------------------------------------------------------------------------------------------------------------
70 * Each <monitor-request> specifies one or more columns and the manner in which the columns (or the entire table) are to be monitored.
71 * The "columns" member specifies the columns whose values are monitored. It MUST NOT contain duplicates.
72 * If "columns" is omitted, all columns in the table, except for "_uuid", are monitored.
73 * ----------------------------------------------------------------------------------------------------------------------------------
74 * In order to overcome this limitation, this method
76 * @return MonitorRequest that includes all the Bridge Columns including _uuid
78 public <T extends TypedBaseTable<GenericTableSchema>> MonitorRequest<GenericTableSchema> getAllColumnsMonitorRequest (Class <T> klazz) {
79 TypedBaseTable<GenericTableSchema> table = ovs.createTypedRowWrapper(klazz);
80 GenericTableSchema bridgeSchema = table.getSchema();
81 Set<String> columns = bridgeSchema.getColumns();
82 MonitorRequestBuilder<GenericTableSchema> bridgeBuilder = MonitorRequestBuilder.builder(table.getSchema());
83 for (String column : columns) {
84 bridgeBuilder.addColumn(column);
86 return bridgeBuilder.with(new MonitorSelect(true, true, true, true)).build();
92 public void update(Object context, UpdateNotification upadateNotification) {
97 public void locked(Object context, List<String> ids) {
102 public void stolen(Object context, List<String> ids) {
106 private void updateTableCache(TableUpdates updates) {
107 for (String tableName : updates.getUpdates().keySet()) {
108 Map<UUID, Row> tUpdate = OpenVswitchSchemaSuiteIT.getTableCache().get(tableName);
109 TableUpdate update = updates.getUpdates().get(tableName);
110 for (UUID uuid : (Set<UUID>)update.getRows().keySet()) {
111 if (update.getNew(uuid) != null) {
112 if (tUpdate == null) {
113 tUpdate = new HashMap<>();
114 OpenVswitchSchemaSuiteIT.getTableCache().put(tableName, tUpdate);
116 tUpdate.put(uuid, update.getNew(uuid));
118 tUpdate.remove(uuid);
124 private class UpdateMonitor implements MonitorCallBack {
126 public void update(TableUpdates result, DatabaseSchema dbSchema) {
127 updateTableCache(result);
131 public void exception(Throwable t) {
132 System.out.println("Exception t = " + t);