Fix incorrect OVSDB class names
[ovsdb.git] / schemas / openvswitch / src / test / java / org / opendaylight / ovsdb / schema / openvswitch / 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
9  */
10
11 package org.opendaylight.ovsdb.schema.openvswitch;
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.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;
39
40 import com.google.common.collect.Lists;
41
42 public class MonitorTestCases extends OpenVswitchSchemaTestBase {
43
44     Logger logger = LoggerFactory.getLogger(PortAndInterfaceTestCases.class);
45
46     @Override
47     @Before
48     public void setUp() throws ExecutionException, InterruptedException, TimeoutException, IOException {
49         super.setUp();
50         OpenVswitchSchemaSuiteIT.dbSchema = this.ovs.getSchema(OPEN_VSWITCH_SCHEMA).get();
51     }
52
53     @Test
54     public void monitorTables() throws ExecutionException, InterruptedException, IOException {
55         Assert.assertNotNull(OpenVswitchSchemaSuiteIT.dbSchema);
56
57         List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
58         monitorRequests.add(this.getAllColumnsMonitorRequest(Bridge.class));
59         monitorRequests.add(this.getAllColumnsMonitorRequest(OpenVSwitch.class));
60
61         TableUpdates updates = ovs.monitor(OpenVswitchSchemaSuiteIT.dbSchema, monitorRequests, new UpdateMonitor());
62         Assert.assertNotNull(updates);
63         this.updateTableCache(updates);
64     }
65
66     /**
67      * As per RFC 7047, section 4.1.5, if a Monitor request is sent without any columns, the update response will not include
68      * the _uuid column.
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
75      *
76      * @return MonitorRequest that includes all the Bridge Columns including _uuid
77      */
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);
85         }
86         return bridgeBuilder.with(new MonitorSelect(true, true, true, true)).build();
87     }
88
89
90
91     @Override
92     public void update(Object context, UpdateNotification upadateNotification) {
93
94     }
95
96     @Override
97     public void locked(Object context, List<String> ids) {
98
99     }
100
101     @Override
102     public void stolen(Object context, List<String> ids) {
103
104     }
105
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);
115                     }
116                     tUpdate.put(uuid, update.getNew(uuid));
117                 } else {
118                     tUpdate.remove(uuid);
119                 }
120             }
121         }
122     }
123
124     private class UpdateMonitor implements MonitorCallBack {
125         @Override
126         public void update(TableUpdates result, DatabaseSchema dbSchema) {
127             updateTableCache(result);
128         }
129
130         @Override
131         public void exception(Throwable t) {
132             System.out.println("Exception t = " + t);
133         }
134     }
135 }