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