Fixed the broken Typed IT tests & included it to run as part of failsafe maven plugin.
[ovsdb.git] / library / src / test / java / org / opendaylight / ovsdb / lib / 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 package org.opendaylight.ovsdb.lib;
11
12 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
13
14 import java.io.IOException;
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.ExecutorService;
20 import java.util.concurrent.Executors;
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.message.OvsdbRPC;
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 import org.opendaylight.ovsdb.lib.schema.temp.Reference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.google.common.collect.ImmutableMap;
42 import com.google.common.collect.Maps;
43 import com.google.common.collect.Sets;
44 import com.google.common.util.concurrent.ListenableFuture;
45
46 public class OvsDBClientTestITTyped extends OvsdbTestBase {
47
48     Logger logger = LoggerFactory.getLogger(OvsDBClientTestITTyped.class);
49     OvsDBClientImpl ovs;
50     DatabaseSchema dbSchema = null;
51     static String testBridgeName = "br-test";
52     static UUID testBridgeUuid = null;
53
54     public static class Bridge extends TableSchema<Bridge> {
55
56         public Bridge (TableSchema<Bridge> tableSchema) {
57             super("Bridge", tableSchema.getColumnSchemas());
58         }
59
60         public Bridge(String name, Map<String, ColumnSchema> columns) {
61             super(name, columns);
62         }
63
64         public ColumnSchema<Bridge, String> name() {
65             return column("name", String.class);
66         }
67
68         public ColumnSchema<Bridge, Integer> floodVlans() {
69             return column("flood_vlans", Integer.class);
70         }
71
72         public ColumnSchema<Bridge, Map> status() {
73             return column("status", Map.class);
74         }
75
76         public ColumnSchema<Bridge, Reference> netflow() {
77             return column("netflow", Reference.class);
78         }
79     }
80
81
82     @Test
83     public void testTypedBridgeCreate() throws IOException, InterruptedException, ExecutionException {
84         Bridge bridge = dbSchema.table("Bridge", Bridge.class);
85         GenericTableSchema ovsTable = dbSchema.table("Open_vSwitch", GenericTableSchema.class);
86         ColumnSchema<GenericTableSchema, Set<UUID>> bridges = ovsTable.multiValuedColumn("bridges", UUID.class);
87
88         GenericTableSchema anytable = null;
89         String namedUuid = "br_test";
90         int insertOperationIndex = 0;
91
92         TransactionBuilder transactionBuilder = ovs.transactBuilder()
93                 .add(op.insert(bridge)
94                         .withId(namedUuid)
95                         .value(bridge.name(), testBridgeName))
96                 .add(op.update(bridge)
97                         .set(bridge.status(), Maps.newHashMap(ImmutableMap.of("key","value")))
98                         .set(bridge.floodVlans(), 34)
99                         .where(bridge.name().opEqual(testBridgeName))
100                         .and(bridge.name().opEqual(testBridgeName)).build())
101                 .add(op.mutate(ovsTable)
102                         .addMutation(bridges, Mutator.INSERT, Sets.newHashSet(new UUID(namedUuid))));
103
104         ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
105         List<OperationResult> operationResults = results.get();
106         Assert.assertFalse(operationResults.isEmpty());
107         // Check if Results matches the number of operations in transaction
108         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
109         System.out.println("Insert & Update operation results = " + operationResults);
110         testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
111     }
112
113     public void testGetDBs() throws ExecutionException, InterruptedException {
114         ListenableFuture<List<String>> databases = ovs.getDatabases();
115         List<String> dbNames = databases.get();
116         Assert.assertNotNull(dbNames);
117         boolean hasOpenVswitchSchema = false;
118         for(String dbName : dbNames) {
119            if (dbName.equals(OPEN_VSWITCH_SCHEMA)) {
120                 hasOpenVswitchSchema = true;
121                 break;
122            }
123         }
124         Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
125     }
126
127     @Before
128     public  void setUp() throws IOException, ExecutionException, InterruptedException {
129         if (ovs != null) {
130             return;
131         }
132         OvsdbRPC rpc = getTestConnection();
133         if (rpc == null) {
134             System.out.println("Unable to Establish Test Connection");
135         }
136         ExecutorService executorService = Executors.newFixedThreadPool(3);
137         ovs = new OvsDBClientImpl(rpc, executorService);
138         testGetDBs();
139         dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get();
140     }
141
142     @After
143     public void tearDown() throws InterruptedException, ExecutionException {
144         TableSchema<GenericTableSchema> bridge = dbSchema.table("Bridge", GenericTableSchema.class);
145         ColumnSchema<GenericTableSchema, String> name = bridge.column("name", String.class);
146         GenericTableSchema ovsTable = dbSchema.table("Open_vSwitch", GenericTableSchema.class);
147         ColumnSchema<GenericTableSchema, Set<UUID>> bridges = ovsTable.multiValuedColumn("bridges", UUID.class);
148
149         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
150                 .add(op.delete(bridge)
151                         .where(name.opEqual(testBridgeName))
152                         .build())
153                 .add(op.mutate(ovsTable)
154                         .addMutation(bridges, Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
155                 .add(op.commit(true))
156                 .execute();
157
158         List<OperationResult> operationResults = results.get();
159         System.out.println("Delete operation results = " + operationResults);
160     }
161
162     @Override
163     public void update(Object node, UpdateNotification upadateNotification) {
164         // TODO Auto-generated method stub
165
166     }
167     @Override
168     public void locked(Object node, List<String> ids) {
169         // TODO Auto-generated method stub
170
171     }
172     @Override
173     public void stolen(Object node, List<String> ids) {
174         // TODO Auto-generated method stub
175
176     }
177
178 }