Fix incorrect OVSDB class names
[ovsdb.git] / schemas / hardwarevtep / src / test / java / org / opendaylight / ovsdb / schema / hardwarevtep / MonitorTestCases.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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  *  Authors : Madhu Venugopal, Dave Tucker, Matt Oswalt
9  */
10
11 package org.opendaylight.ovsdb.schema.hardwarevtep;
12
13 import java.io.IOException;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeoutException;
20
21 import junit.framework.Assert;
22
23 import org.junit.Assume;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.ovsdb.lib.MonitorCallBack;
27 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
28 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
29 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
30 import org.opendaylight.ovsdb.lib.message.TableUpdate;
31 import org.opendaylight.ovsdb.lib.message.TableUpdates;
32 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
33 import org.opendaylight.ovsdb.lib.notation.Row;
34 import org.opendaylight.ovsdb.lib.notation.UUID;
35 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
36 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
37 import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.google.common.collect.Lists;
42
43 public class MonitorTestCases extends HardwareVtepSchemaTestBase {
44
45     Logger logger = LoggerFactory.getLogger(MonitorTestCases.class);
46
47     @Override
48     @Before
49     public void setUp() throws ExecutionException, InterruptedException, TimeoutException, IOException {
50         super.setUp();
51     }
52
53     @Test
54     public void monitorTables() throws ExecutionException, InterruptedException, IOException, TimeoutException {
55         //Ensure test only proceeds if HW VTEP is supported
56         Assume.assumeTrue(super.checkSchema());
57         HardwareVtepSchemaSuiteIT.dbSchema = this.ovs.getSchema(HARDWARE_VTEP_SCHEMA).get();
58         Assert.assertNotNull(HardwareVtepSchemaSuiteIT.dbSchema);
59
60         List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
61         monitorRequests.add(this.getAllColumnsMonitorRequest(Manager.class));
62         monitorRequests.add(this.getAllColumnsMonitorRequest(Global.class));
63
64         TableUpdates updates = ovs.monitor(HardwareVtepSchemaSuiteIT.dbSchema, monitorRequests, new UpdateMonitor());
65         Assert.assertNotNull(updates);
66         this.updateTableCache(updates);
67     }
68
69     /**
70      * As per RFC 7047, section 4.1.5, if a Monitor request is sent without any columns, the update response will not include
71      * the _uuid column.
72      * ----------------------------------------------------------------------------------------------------------------------------------
73      * Each <monitor-request> specifies one or more columns and the manner in which the columns (or the entire table) are to be monitored.
74      * The "columns" member specifies the columns whose values are monitored. It MUST NOT contain duplicates.
75      * If "columns" is omitted, all columns in the table, except for "_uuid", are monitored.
76      * ----------------------------------------------------------------------------------------------------------------------------------
77      * In order to overcome this limitation, this method
78      *
79      * @return MonitorRequest that includes all the Bridge Columns including _uuid
80      */
81     public <T extends TypedBaseTable<GenericTableSchema>> MonitorRequest<GenericTableSchema> getAllColumnsMonitorRequest (Class <T> klazz) { //TODO: Needs updated for HW VTEP
82         TypedBaseTable<GenericTableSchema> table = ovs.createTypedRowWrapper(klazz);
83         GenericTableSchema bridgeSchema = table.getSchema();
84         Set<String> columns = bridgeSchema.getColumns();
85         MonitorRequestBuilder<GenericTableSchema> bridgeBuilder = MonitorRequestBuilder.builder(table.getSchema());
86         for (String column : columns) {
87             bridgeBuilder.addColumn(column);
88         }
89         return bridgeBuilder.with(new MonitorSelect(true, true, true, true)).build();
90     }
91
92
93
94     @Override
95     public void update(Object context, UpdateNotification upadateNotification) {
96
97     }
98
99     @Override
100     public void locked(Object context, List<String> ids) {
101
102     }
103
104     @Override
105     public void stolen(Object context, List<String> ids) {
106
107     }
108
109     private void updateTableCache(TableUpdates updates) {
110         for (String tableName : updates.getUpdates().keySet()) {
111             Map<UUID, Row> tUpdate = HardwareVtepSchemaSuiteIT.getTableCache().get(tableName);
112             TableUpdate update = updates.getUpdates().get(tableName);
113             for (UUID uuid : (Set<UUID>)update.getRows().keySet()) {
114                 if (update.getNew(uuid) != null) {
115                     if (tUpdate == null) {
116                         tUpdate = new HashMap<>();
117                         HardwareVtepSchemaSuiteIT.getTableCache().put(tableName, tUpdate);
118                     }
119                     tUpdate.put(uuid, update.getNew(uuid));
120                 } else {
121                     tUpdate.remove(uuid);
122                 }
123             }
124         }
125     }
126
127     private class UpdateMonitor implements MonitorCallBack {
128         @Override
129         public void update(TableUpdates result, DatabaseSchema dbSchema) {
130             updateTableCache(result);
131         }
132
133         @Override
134         public void exception(Throwable t) {
135             System.out.println("Exception t = " + t);
136         }
137     }
138 }