/* * Copyright (C) 2014 Red Hat, Inc. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Authors : Madhu Venugopal */ package org.opendaylight.ovsdb.schema.openvswitch; import static org.opendaylight.ovsdb.lib.operations.Operations.op; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.opendaylight.ovsdb.lib.OvsDBClientImpl; import org.opendaylight.ovsdb.lib.message.OvsdbRPC; import org.opendaylight.ovsdb.lib.message.UpdateNotification; import org.opendaylight.ovsdb.lib.notation.Mutator; import org.opendaylight.ovsdb.lib.notation.Row; import org.opendaylight.ovsdb.lib.notation.UUID; import org.opendaylight.ovsdb.lib.operations.OperationResult; import org.opendaylight.ovsdb.lib.operations.TransactionBuilder; import org.opendaylight.ovsdb.lib.schema.DatabaseSchema; import org.opendaylight.ovsdb.lib.schema.GenericTableSchema; import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.common.util.concurrent.ListenableFuture; public class TypedVSwitchdSchemaIT extends OvsdbTestBase { Logger logger = LoggerFactory.getLogger(TypedVSwitchdSchemaIT.class); OvsDBClientImpl ovs; DatabaseSchema dbSchema = null; static String testBridgeName = "br_test"; static UUID testBridgeUuid = null; @Test public void testTypedBridgeOperations() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { this.createTypedBridge(); } private void createTypedBridge() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { GenericTableSchema bridgeSchema = TyperUtils.getTableSchema(dbSchema, Bridge.class); Bridge bridge = TyperUtils.getTypedRowWrapper(dbSchema, Bridge.class, new Row()); bridge.setName(testBridgeName); bridge.setStatus(Maps.newHashMap(ImmutableMap.of("key","value"))); bridge.setFloodVlans(Sets.newHashSet(34)); GenericTableSchema ovsTable = TyperUtils.getTableSchema(dbSchema, OpenVSwitch.class); OpenVSwitch openVSwitch = TyperUtils.getTypedRowWrapper(dbSchema, OpenVSwitch.class, new Row()); openVSwitch.setBridges(Sets.newHashSet(new UUID(testBridgeName))); int insertOperationIndex = 0; TransactionBuilder transactionBuilder = ovs.transactBuilder() .add(op.insert(bridgeSchema) .withId(testBridgeName) .value(bridge.getNameColumn())) .add(op.update(bridgeSchema) .set(bridge.getStatusColumn()) .set(bridge.getFloodVlansColumn()) .where(bridge.getNameColumn().getSchema().opEqual(bridge.getName())) .and(bridge.getNameColumn().getSchema().opEqual(bridge.getName())).build()) .add(op.mutate(ovsTable) .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.INSERT, openVSwitch.getBridgesColumn().getData())); ListenableFuture> results = transactionBuilder.execute(); List operationResults = results.get(); Assert.assertFalse(operationResults.isEmpty()); // Check if Results matches the number of operations in transaction Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size()); System.out.println("Insert & Update operation results = " + operationResults); testBridgeUuid = operationResults.get(insertOperationIndex).getUuid(); } public void testGetDBs() throws ExecutionException, InterruptedException { ListenableFuture> databases = ovs.getDatabases(); List dbNames = databases.get(); Assert.assertNotNull(dbNames); boolean hasOpenVswitchSchema = false; for(String dbName : dbNames) { if (dbName.equals(OPEN_VSWITCH_SCHEMA)) { hasOpenVswitchSchema = true; break; } } Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema); } @Before public void setUp() throws IOException, ExecutionException, InterruptedException { if (ovs != null) { return; } OvsdbRPC rpc = getTestConnection(); if (rpc == null) { System.out.println("Unable to Establish Test Connection"); } ExecutorService executorService = Executors.newFixedThreadPool(3); ovs = new OvsDBClientImpl(rpc, executorService); testGetDBs(); dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get(); } @After public void tearDown() throws InterruptedException, ExecutionException { GenericTableSchema bridgeSchema = TyperUtils.getTableSchema(dbSchema, Bridge.class); Bridge bridge = TyperUtils.getTypedRowWrapper(dbSchema, Bridge.class, null); GenericTableSchema ovsTable = TyperUtils.getTableSchema(dbSchema, OpenVSwitch.class); OpenVSwitch openVSwitch = TyperUtils.getTypedRowWrapper(dbSchema, OpenVSwitch.class, null); ListenableFuture> results = ovs.transactBuilder() .add(op.delete(bridgeSchema) .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName)) .build()) .add(op.mutate(ovsTable) .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid))) .add(op.commit(true)) .execute(); List operationResults = results.get(); System.out.println("Delete operation results = " + operationResults); } @Override public void update(Object node, UpdateNotification upadateNotification) { // TODO Auto-generated method stub } @Override public void locked(Object node, List ids) { // TODO Auto-generated method stub } @Override public void stolen(Object node, List ids) { // TODO Auto-generated method stub } }