66f8d5dad2c24e056ca3f230bc310114a3ff8a1a
[ovsdb.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / library / OvsdbLibraryIT.java
1 /*
2  * Copyright (c) 2014 Red Hat, Inc. and others.  All rights reserved.
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.integrationtest.library;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.fail;
17 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
18 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
19 import static org.ops4j.pax.exam.CoreOptions.options;
20 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperty;
21 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
22
23 import java.io.IOException;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.List;
26 import java.util.concurrent.ExecutionException;
27
28 import javax.inject.Inject;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.opendaylight.ovsdb.integrationtest.ConfigurationBundles;
35 import org.opendaylight.ovsdb.integrationtest.OvsdbIntegrationTestBase;
36 import org.opendaylight.ovsdb.lib.OvsdbClient;
37 import org.opendaylight.ovsdb.lib.notation.Mutator;
38 import org.opendaylight.ovsdb.lib.notation.UUID;
39 import org.opendaylight.ovsdb.lib.operations.OperationResult;
40 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
41 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
42 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
43 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
44 import org.ops4j.pax.exam.Configuration;
45 import org.ops4j.pax.exam.Option;
46 import org.ops4j.pax.exam.junit.PaxExam;
47 import org.ops4j.pax.exam.util.PathUtils;
48 import org.osgi.framework.Bundle;
49 import org.osgi.framework.BundleContext;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import com.google.common.collect.ImmutableMap;
54 import com.google.common.collect.Sets;
55 import com.google.common.util.concurrent.ListenableFuture;
56
57 @RunWith(PaxExam.class)
58 public class OvsdbLibraryIT extends OvsdbIntegrationTestBase {
59     private Logger log = LoggerFactory.getLogger(OvsdbLibraryIT.class);
60     @Inject
61     private BundleContext bc;
62     private OvsdbClient client = null;
63
64     @Configuration
65     public Option[] config() {
66         return options(
67             //
68             systemProperty("logback.configurationFile").value(
69                     "file:" + PathUtils.getBaseDir()
70                     + "/src/test/resources/logback.xml"
71             ),
72             // To start OSGi console for inspection remotely
73             systemProperty("osgi.console").value("2401"),
74
75             propagateSystemProperty("ovsdbserver.ipaddress"),
76             propagateSystemProperty("ovsdbserver.port"),
77
78             ConfigurationBundles.controllerBundles(),
79             ConfigurationBundles.ovsdbLibraryBundles(),
80             ConfigurationBundles.ovsdbDefaultSchemaBundles(),
81             junitBundles()
82         );
83     }
84
85     @Before
86     public void areWeReady() throws InterruptedException {
87         assertNotNull(bc);
88         boolean debugit = false;
89         Bundle b[] = bc.getBundles();
90         for (Bundle element : b) {
91             int state = element.getState();
92             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
93                 log.info("Bundle:" + element.getSymbolicName() + " state:"
94                           + stateToString(state));
95                 debugit = true;
96             }
97         }
98         if (debugit) {
99             log.debug("Do some debugging because some bundle is unresolved");
100             Thread.sleep(600000);
101         }
102
103         // Assert if true, if false we are good to go!
104         assertFalse(debugit);
105         try {
106             client = getTestConnection();
107         } catch (Exception e) {
108             fail("Exception : "+e.getMessage());
109         }
110     }
111
112     public boolean isSchemaSupported(String schema) throws ExecutionException, InterruptedException {
113         ListenableFuture<List<String>> databases = client.getDatabases();
114         List<String> dbNames = databases.get();
115         assertNotNull(dbNames);
116         if (dbNames.contains(schema)) return true;
117         return false;
118     }
119
120     static String testBridgeName = "br_test";
121     static UUID testBridgeUuid = null;
122     private void createTypedBridge(DatabaseSchema dbSchema) throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
123         Bridge bridge = client.createTypedRowWrapper(Bridge.class);
124         bridge.setName(testBridgeName);
125         bridge.setStatus(ImmutableMap.of("key","value"));
126         bridge.setFloodVlans(Sets.newHashSet(34L));
127
128         OpenVSwitch openVSwitch = client.createTypedRowWrapper(OpenVSwitch.class);
129         openVSwitch.setBridges(Sets.newHashSet(new UUID(testBridgeName)));
130
131         int insertOperationIndex = 0;
132
133         TransactionBuilder transactionBuilder = client.transactBuilder(dbSchema)
134                 .add(op.insert(bridge.getSchema())
135                         .withId(testBridgeName)
136                         .value(bridge.getNameColumn()))
137                 .add(op.update(bridge.getSchema())
138                         .set(bridge.getStatusColumn())
139                         .set(bridge.getFloodVlansColumn())
140                         .where(bridge.getNameColumn().getSchema().opEqual(bridge.getName()))
141                         .and(bridge.getNameColumn().getSchema().opEqual(bridge.getName())).build())
142                 .add(op.mutate(openVSwitch.getSchema())
143                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.INSERT,
144                                      openVSwitch.getBridgesColumn().getData()));
145
146         ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
147         List<OperationResult> operationResults = results.get();
148         assertFalse(operationResults.isEmpty());
149         // Check if Results matches the number of operations in transaction
150         assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
151         System.out.println("Insert & Update operation results = " + operationResults);
152         for (OperationResult result : operationResults) {
153             assertNull(result.getError());
154         }
155         testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
156         assertNotNull(testBridgeUuid);
157     }
158
159     @Test
160     public void tableTest() throws Exception {
161         assertNotNull("Invalid Client. Check connection params", client);
162         Thread.sleep(3000); // Wait for a few seconds to get the Schema exchange done
163         if (isSchemaSupported(OPEN_VSWITCH_SCHEMA)) {
164             DatabaseSchema dbSchema = client.getSchema(OPEN_VSWITCH_SCHEMA).get();
165             assertNotNull(dbSchema);
166             System.out.println(OPEN_VSWITCH_SCHEMA + " schema in "+ client.getConnectionInfo() +
167                                                      " with Tables : " + dbSchema.getTables());
168
169             // A simple Typed Test to make sure a Typed wrapper bundle can coexist in an OSGi environment
170             createTypedBridge(dbSchema);
171         }
172
173         if (isSchemaSupported(HARDWARE_VTEP)) {
174             DatabaseSchema dbSchema = client.getSchema(HARDWARE_VTEP).get();
175             assertNotNull(dbSchema);
176             System.out.println(HARDWARE_VTEP + " schema in "+ client.getConnectionInfo() +
177                                                " with Tables : " + dbSchema.getTables());
178         }
179     }
180
181     @After
182     public void tearDown() throws InterruptedException, ExecutionException {
183         Bridge bridge = client.getTypedRowWrapper(Bridge.class, null);
184         OpenVSwitch openVSwitch = client.getTypedRowWrapper(OpenVSwitch.class, null);
185         DatabaseSchema dbSchema = client.getSchema(OPEN_VSWITCH_SCHEMA).get();
186         ListenableFuture<List<OperationResult>> results = client.transactBuilder(dbSchema)
187                 .add(op.delete(bridge.getSchema())
188                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
189                         .build())
190                 .add(op.mutate(openVSwitch.getSchema())
191                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
192                 .add(op.commit(true))
193                 .execute();
194
195         List<OperationResult> operationResults = results.get();
196         System.out.println("Delete operation results = " + operationResults);
197     }
198 }