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