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