blob: 6caa63cc2c25bbf40d000a7fa7559aef0e03aa15 [file] [log] [blame]
Frank Wange33e4ed2017-06-28 10:01:07 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wange33e4ed2017-06-28 10:01:07 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.collect.Maps;
20import com.google.common.testing.EqualsTester;
21import org.apache.commons.collections.CollectionUtils;
22import org.junit.Test;
23import org.onlab.util.ImmutableByteSequence;
24
25import java.util.Map;
26
27import static org.hamcrest.MatcherAssert.assertThat;
28import static org.hamcrest.Matchers.is;
29import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Carmelo Cascone7f75be42017-09-07 14:37:02 +020030import static org.onosproject.net.pi.runtime.PiConstantsTest.*;
Frank Wange33e4ed2017-06-28 10:01:07 +080031
32/**
33 * Unit tests for PiTableEntry class.
34 */
35public class PiTableEntryTest {
Carmelo Cascone7f75be42017-09-07 14:37:02 +020036 private final PiTableEntry piTableEntry1 = PiTableEntry.builder()
Frank Wange33e4ed2017-06-28 10:01:07 +080037 .forTable(PiTableId.of("Table10"))
38 .withCookie(0xac)
39 .withPriority(10)
40 .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
41 .withTimeout(100)
42 .build();
Carmelo Cascone7f75be42017-09-07 14:37:02 +020043 private final PiTableEntry sameAsPiTableEntry1 = PiTableEntry.builder()
Frank Wange33e4ed2017-06-28 10:01:07 +080044 .forTable(PiTableId.of("Table10"))
45 .withCookie(0xac)
46 .withPriority(10)
47 .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
48 .withTimeout(100)
49 .build();
Carmelo Cascone7f75be42017-09-07 14:37:02 +020050 private final PiTableEntry piTableEntry2 = PiTableEntry.builder()
Frank Wange33e4ed2017-06-28 10:01:07 +080051 .forTable(PiTableId.of("Table20"))
52 .withCookie(0xac)
53 .withPriority(10)
54 .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
55 .withTimeout(1000)
56 .build();
57
58 /**
59 * Checks that the PiTableEntry class is immutable.
60 */
61 @Test
62 public void testImmutability() {
63 assertThatClassIsImmutable(PiTableEntry.class);
64 }
65
66 /**
67 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
68 */
69 @Test
70 public void testEquals() {
71 new EqualsTester()
72 .addEqualityGroup(piTableEntry1, sameAsPiTableEntry1)
73 .addEqualityGroup(piTableEntry2)
74 .testEquals();
75 }
76
77 /**
Carmelo Cascone7f75be42017-09-07 14:37:02 +020078 * Tests equality of the empty table entry.
79 */
80 @Test
81 public void testEmptyEquals() {
82 new EqualsTester()
83 .addEqualityGroup(PiTableEntry.EMTPY, PiTableEntry.EMTPY)
84 .testEquals();
85 }
86
87 /**
Frank Wange33e4ed2017-06-28 10:01:07 +080088 * Tests creation of a DefaultFlowRule using a FlowRule constructor.
89 */
90 @Test
91 public void testBuilder() {
92
93 PiTableId piTableId = PiTableId.of("table10");
94 long cookie = 0xfff0323;
95 int priority = 100;
96 double timeout = 1000;
97 PiHeaderFieldId piHeaderFieldId = PiHeaderFieldId.of(IPV4_HEADER_NAME, DST_ADDR);
98 PiFieldMatch piFieldMatch = new PiExactFieldMatch(piHeaderFieldId, ImmutableByteSequence.copyFrom(0x0a010101));
99 PiAction piAction = PiAction.builder().withId(PiActionId.of(DROP)).build();
100 final Map<PiHeaderFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap();
101 fieldMatches.put(piHeaderFieldId, piFieldMatch);
102 final PiTableEntry piTableEntry = PiTableEntry.builder()
103 .forTable(piTableId)
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200104 .withMatchKey(PiMatchKey.builder()
105 .addFieldMatches(fieldMatches.values())
106 .build())
Frank Wange33e4ed2017-06-28 10:01:07 +0800107 .withAction(piAction)
108 .withCookie(cookie)
109 .withPriority(priority)
110 .withTimeout(timeout)
111 .build();
112
113 assertThat(piTableEntry.table(), is(piTableId));
114 assertThat(piTableEntry.cookie(), is(cookie));
115 assertThat(piTableEntry.priority().get(), is(priority));
116 assertThat(piTableEntry.timeout().get(), is(timeout));
117 assertThat("Incorrect match param value",
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200118 CollectionUtils.isEqualCollection(piTableEntry.matchKey().fieldMatches(), fieldMatches.values()));
Frank Wange33e4ed2017-06-28 10:01:07 +0800119 assertThat(piTableEntry.action(), is(piAction));
120 }
121}