944f36c58a77380ee214d5e7b050e79372c2924b
[ovsdb.git] / schemas / Open_vSwitch / src / test / java / org / opendaylight / ovsdb / schema / openvswitch / IpfixTestCases.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 : Brent Salisbury, Dave Tucker
9  */
10
11 package org.opendaylight.ovsdb.schema.openvswitch;
12
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Sets;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import junit.framework.Assert;
18 import org.junit.Assume;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
22 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
23 import org.opendaylight.ovsdb.lib.notation.Mutator;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.notation.Version;
26 import org.opendaylight.ovsdb.lib.operations.OperationResult;
27 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.concurrent.ExecutionException;
34 import java.util.concurrent.TimeoutException;
35
36 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
37
38 public class IpfixTestCases extends OpenVswitchSchemaTestBase {
39
40     Logger logger = LoggerFactory.getLogger(IpfixTestCases.class);
41     Version schemaVersion;
42     Version ipfixFromVersion = Version.fromString("7.1.0");
43     Version ipfixCacheFromVersion = Version.fromString("7.3.0");
44
45     @Before
46     public void setUp() throws ExecutionException, InterruptedException, TimeoutException, IOException {
47         super.setUp();
48         schemaVersion = ovs.getDatabaseSchema("Open_vSwitch").getVersion();
49     }
50
51     @Test
52     public void testTableNotSupported() {
53         // Don't run this test if we can run the IPFIX test
54         Assume.assumeTrue(schemaVersion.compareTo(ipfixFromVersion) < 0);
55         boolean isExceptionRaised = false;
56         try {
57             IPFIX ipfix = ovs.createTypedRowWrapper(IPFIX.class);
58         } catch (SchemaVersionMismatchException e) {
59             isExceptionRaised = true;
60         }
61         Assert.assertTrue(isExceptionRaised);
62     }
63
64     @Test
65     public void testCreateTypedIpFix() throws InterruptedException, ExecutionException, IllegalArgumentException{
66         // Don't run this test if the table is not supported
67         Assume.assumeTrue(schemaVersion.compareTo(ipfixFromVersion) >= 0);
68         String ipfixUuidStr = "testIpfix";
69         String ipfixTarget = "172.16.20.1:4739";
70         Long obsDomainId = 112L;
71         Long obsPointId = 358L;
72         Long cacheMax = 132L;
73         Long cacheTimeout = 134L;
74         Long sampling = 558L;
75
76         IPFIX ipfix = ovs.createTypedRowWrapper(IPFIX.class);
77         ipfix.setTargets(ImmutableSet.of(ipfixTarget));
78         ipfix.setObsDomainId(ImmutableSet.of(obsDomainId));
79         ipfix.setObsPointId(ImmutableSet.of(obsPointId));
80         // Only set these rows if the schema version supports it
81         if (schemaVersion.compareTo(ipfixCacheFromVersion) >= 0) {
82             ipfix.setCacheMaxFlows(ImmutableSet.of(cacheMax));
83             ipfix.setCacheActiveTimeout(ImmutableSet.of(cacheTimeout));
84         }
85         ipfix.setSampling(ImmutableSet.of(sampling));
86         ipfix.setExternalIds(ImmutableMap.of("<3", "ovs"));
87         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
88         TransactionBuilder transactionBuilder = ovs.transactBuilder()
89                 .add(op.insert(ipfix)
90                         .withId(ipfixUuidStr))
91                 .add(op.mutate(bridge.getSchema())
92                         .addMutation(bridge.getIpfixColumn().getSchema(), Mutator.INSERT,
93                                 Sets.newHashSet(new UUID(ipfixUuidStr)))
94                         .where(bridge.getNameColumn().getSchema().opEqual(TEST_BRIDGE_NAME))
95                         .build());
96         ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
97         List<OperationResult> operationResults = results.get();
98         Assert.assertFalse(operationResults.isEmpty());
99         // Check if Results matches the number of operations in transaction
100         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
101         for (OperationResult result : operationResults) Assert.assertNull(result.getError());
102         logger.info("Insert & Mutate operation results for IPFIX = {} ", operationResults);
103     }
104
105     @Override
106     public void update(Object context, UpdateNotification upadateNotification) {
107
108     }
109
110     @Override
111     public void locked(Object context, List<String> ids) {
112
113     }
114
115     @Override
116     public void stolen(Object context, List<String> ids) {
117
118     }
119 }