Clean up logging
[ovsdb.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / ovsdbclient / OvsdbClientTestITTyped.java
1 /*
2  * Copyright (C) 2014 EBay Software Foundation
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 : Ashwin Raveendran, Madhu Venugopal
9  */
10
11 package org.opendaylight.ovsdb.integrationtest.ovsdbclient;
12
13 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
14
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21
22 import junit.framework.Assert;
23
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.ovsdb.lib.OvsdbClient;
28 import org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService;
29 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
30 import org.opendaylight.ovsdb.lib.notation.Mutator;
31 import org.opendaylight.ovsdb.lib.notation.UUID;
32 import org.opendaylight.ovsdb.lib.operations.OperationResult;
33 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
34 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
35 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
36 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
37 import org.opendaylight.ovsdb.lib.schema.TableSchema;
38
39 import com.google.common.collect.ImmutableMap;
40 import com.google.common.collect.Sets;
41 import com.google.common.util.concurrent.ListenableFuture;
42
43 public class OvsdbClientTestITTyped extends OvsdbTestBase {
44
45     OvsdbClient ovs;
46     DatabaseSchema dbSchema = null;
47     static String testBridgeName = "br_test";
48     static UUID testBridgeUuid = null;
49
50     /**
51      * Test creation of statically typed bridge table as defined in
52      * ovs-vswitchd.conf.db with get/set for all relevant columns. The
53      * SETDATA methods for "name", "status" and "flood_vlans" columns
54      * are verified.
55      */
56     @Test
57     public void testTypedBridgeCreate() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
58         TestBridge rBridge = ovs.createTypedRowWrapper(TestBridge.class);
59         rBridge.setName(testBridgeName);
60         rBridge.setStatus(ImmutableMap.of("key","value"));
61         rBridge.setFloodVlans(Sets.newHashSet(34));
62
63         GenericTableSchema ovsTable = dbSchema.table("Open_vSwitch", GenericTableSchema.class);
64         ColumnSchema<GenericTableSchema, Set<UUID>> bridges = ovsTable.multiValuedColumn("bridges", UUID.class);
65
66         String namedUuid = "br_test";
67         int insertOperationIndex = 0;
68
69         TransactionBuilder transactionBuilder = ovs.transactBuilder(dbSchema)
70                 .add(op.insert(rBridge)
71                         .withId(namedUuid))
72                 .add(op.mutate(ovsTable)
73                         .addMutation(bridges, Mutator.INSERT, Sets.newHashSet(new UUID(namedUuid))));
74
75         ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
76         List<OperationResult> operationResults = results.get();
77         Assert.assertFalse(operationResults.isEmpty());
78         // Check if Results matches the number of operations in transaction
79         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
80         System.out.println("Insert & Update operation results = " + operationResults);
81         testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
82     }
83
84     public void testGetDBs() throws ExecutionException, InterruptedException {
85         ListenableFuture<List<String>> databases = ovs.getDatabases();
86         List<String> dbNames = databases.get();
87         Assert.assertNotNull(dbNames);
88         boolean hasOpenVswitchSchema = false;
89         for(String dbName : dbNames) {
90            if (dbName.equals(OPEN_VSWITCH_SCHEMA)) {
91                 hasOpenVswitchSchema = true;
92                 break;
93            }
94         }
95         Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
96     }
97
98     @Before
99     public  void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException {
100         if (ovs != null) {
101             return;
102         }
103         ovs = this.getTestConnection();
104         testGetDBs();
105         dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA).get();
106     }
107
108     @After
109     public void tearDown() throws InterruptedException, ExecutionException {
110         TableSchema<GenericTableSchema> bridge = dbSchema.table("Bridge", GenericTableSchema.class);
111         ColumnSchema<GenericTableSchema, String> name = bridge.column("name", String.class);
112         GenericTableSchema ovsTable = dbSchema.table("Open_vSwitch", GenericTableSchema.class);
113         ColumnSchema<GenericTableSchema, Set<UUID>> bridges = ovsTable.multiValuedColumn("bridges", UUID.class);
114
115         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder(dbSchema)
116                 .add(op.delete(bridge)
117                         .where(name.opEqual(testBridgeName))
118                         .build())
119                 .add(op.mutate(ovsTable)
120                         .addMutation(bridges, Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
121                 .add(op.commit(true))
122                 .execute();
123
124         List<OperationResult> operationResults = results.get();
125         System.out.println("Delete operation results = " + operationResults);
126         OvsdbConnectionService.getService().disconnect(ovs);
127     }
128
129     @Override
130     public void update(Object node, UpdateNotification upadateNotification) {
131         // TODO Auto-generated method stub
132
133     }
134     @Override
135     public void locked(Object node, List<String> ids) {
136         // TODO Auto-generated method stub
137
138     }
139     @Override
140     public void stolen(Object node, List<String> ids) {
141         // TODO Auto-generated method stub
142
143     }
144 }