Removed unneccessary usage of Maps.newHashMap from the IT files
[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.Sets;
39 import com.google.common.util.concurrent.ListenableFuture;
40
41 public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
42
43     Logger logger = LoggerFactory.getLogger(TypedVSwitchdSchemaIT.class);
44     OvsDBClientImpl ovs;
45     DatabaseSchema dbSchema = null;
46     static String testBridgeName = "br_test";
47     static UUID testBridgeUuid = null;
48
49     @Test
50     public void testTypedBridgeOperations() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
51         this.createTypedBridge();
52     }
53
54     private void createTypedBridge() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
55         Bridge bridge = ovs.createTypedRowWrapper(Bridge.class);
56         bridge.setName(testBridgeName);
57         bridge.setStatus(ImmutableMap.of("key","value"));
58         bridge.setFloodVlans(Sets.newHashSet(34));
59
60         OpenVSwitch openVSwitch = ovs.createTypedRowWrapper(OpenVSwitch.class);
61         openVSwitch.setBridges(Sets.newHashSet(new UUID(testBridgeName)));
62
63         int insertOperationIndex = 0;
64
65         TransactionBuilder transactionBuilder = ovs.transactBuilder()
66                 .add(op.insert(bridge.getSchema())
67                         .withId(testBridgeName)
68                         .value(bridge.getNameColumn()))
69                 .add(op.update(bridge.getSchema())
70                         .set(bridge.getStatusColumn())
71                         .set(bridge.getFloodVlansColumn())
72                         .where(bridge.getNameColumn().getSchema().opEqual(bridge.getName()))
73                         .and(bridge.getNameColumn().getSchema().opEqual(bridge.getName())).build())
74                 .add(op.mutate(openVSwitch.getSchema())
75                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.INSERT,
76                                      openVSwitch.getBridgesColumn().getData()));
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     public void testGetDBs() throws ExecutionException, InterruptedException {
87         ListenableFuture<List<String>> databases = ovs.getDatabases();
88         List<String> dbNames = databases.get();
89         Assert.assertNotNull(dbNames);
90         boolean hasOpenVswitchSchema = false;
91         for(String dbName : dbNames) {
92            if (dbName.equals(OPEN_VSWITCH_SCHEMA)) {
93                 hasOpenVswitchSchema = true;
94                 break;
95            }
96         }
97         Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
98     }
99
100     @Before
101     public  void setUp() throws IOException, ExecutionException, InterruptedException {
102         if (ovs != null) {
103             return;
104         }
105         OvsdbRPC rpc = getTestConnection();
106         if (rpc == null) {
107             System.out.println("Unable to Establish Test Connection");
108         }
109         ExecutorService executorService = Executors.newFixedThreadPool(3);
110         ovs = new OvsDBClientImpl(rpc, executorService);
111         testGetDBs();
112         dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get();
113     }
114
115     @After
116     public void tearDown() throws InterruptedException, ExecutionException {
117         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
118         OpenVSwitch openVSwitch = ovs.getTypedRowWrapper(OpenVSwitch.class, null);
119
120         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
121                 .add(op.delete(bridge.getSchema())
122                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
123                         .build())
124                 .add(op.mutate(openVSwitch.getSchema())
125                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
126                 .add(op.commit(true))
127                 .execute();
128
129         List<OperationResult> operationResults = results.get();
130         System.out.println("Delete operation results = " + operationResults);
131     }
132
133     @Override
134     public void update(Object node, UpdateNotification upadateNotification) {
135         // TODO Auto-generated method stub
136
137     }
138     @Override
139     public void locked(Object node, List<String> ids) {
140         // TODO Auto-generated method stub
141
142     }
143     @Override
144     public void stolen(Object node, List<String> ids) {
145         // TODO Auto-generated method stub
146
147     }
148 }