Merge "Simplifying the User facing APIs in TyperUtils." into topic/schema
[ovsdb.git] / schemas / Open_vSwitch / src / test / java / org / opendaylight / ovsdb / schema / openvswitch / TypedVSwitchdSchemaIT.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
9  */
10 package org.opendaylight.ovsdb.schema.openvswitch;
11
12 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
13
14 import java.io.IOException;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20
21 import junit.framework.Assert;
22
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.ovsdb.lib.OvsDBClientImpl;
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.DatabaseSchema;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.collect.ImmutableMap;
38 import com.google.common.collect.Maps;
39 import com.google.common.collect.Sets;
40 import com.google.common.util.concurrent.ListenableFuture;
41
42 public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
43
44     Logger logger = LoggerFactory.getLogger(TypedVSwitchdSchemaIT.class);
45     OvsDBClientImpl ovs;
46     DatabaseSchema dbSchema = null;
47     static String testBridgeName = "br_test";
48     static UUID testBridgeUuid = null;
49
50     @Test
51     public void testTypedBridgeOperations() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
52         this.createTypedBridge();
53     }
54
55     private void createTypedBridge() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
56         Bridge bridge = ovs.createTypedRowWrapper(Bridge.class);
57         bridge.setName(testBridgeName);
58         bridge.setStatus(Maps.newHashMap(ImmutableMap.of("key","value")));
59         bridge.setFloodVlans(Sets.newHashSet(34));
60
61         OpenVSwitch openVSwitch = ovs.createTypedRowWrapper(OpenVSwitch.class);
62         openVSwitch.setBridges(Sets.newHashSet(new UUID(testBridgeName)));
63
64         int insertOperationIndex = 0;
65
66         TransactionBuilder transactionBuilder = ovs.transactBuilder()
67                 .add(op.insert(bridge.getSchema())
68                         .withId(testBridgeName)
69                         .value(bridge.getNameColumn()))
70                 .add(op.update(bridge.getSchema())
71                         .set(bridge.getStatusColumn())
72                         .set(bridge.getFloodVlansColumn())
73                         .where(bridge.getNameColumn().getSchema().opEqual(bridge.getName()))
74                         .and(bridge.getNameColumn().getSchema().opEqual(bridge.getName())).build())
75                 .add(op.mutate(openVSwitch.getSchema())
76                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.INSERT,
77                                      openVSwitch.getBridgesColumn().getData()));
78
79         ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
80         List<OperationResult> operationResults = results.get();
81         Assert.assertFalse(operationResults.isEmpty());
82         // Check if Results matches the number of operations in transaction
83         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
84         System.out.println("Insert & Update operation results = " + operationResults);
85         testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
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 {
103         if (ovs != null) {
104             return;
105         }
106         OvsdbRPC rpc = getTestConnection();
107         if (rpc == null) {
108             System.out.println("Unable to Establish Test Connection");
109         }
110         ExecutorService executorService = Executors.newFixedThreadPool(3);
111         ovs = new OvsDBClientImpl(rpc, executorService);
112         testGetDBs();
113         dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get();
114     }
115
116     @After
117     public void tearDown() throws InterruptedException, ExecutionException {
118         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
119         OpenVSwitch openVSwitch = ovs.getTypedRowWrapper(OpenVSwitch.class, null);
120
121         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
122                 .add(op.delete(bridge.getSchema())
123                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
124                         .build())
125                 .add(op.mutate(openVSwitch.getSchema())
126                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
127                 .add(op.commit(true))
128                 .execute();
129
130         List<OperationResult> operationResults = results.get();
131         System.out.println("Delete operation results = " + operationResults);
132     }
133
134     @Override
135     public void update(Object node, UpdateNotification upadateNotification) {
136         // TODO Auto-generated method stub
137
138     }
139     @Override
140     public void locked(Object node, List<String> ids) {
141         // TODO Auto-generated method stub
142
143     }
144     @Override
145     public void stolen(Object node, List<String> ids) {
146         // TODO Auto-generated method stub
147
148     }
149 }