blob: 087eb77be1fb9fb7105aac2c549397c249eee55a [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;
Carmelo Cascone87892e22017-11-13 16:01:29 -080024import org.onosproject.net.pi.model.PiActionId;
25import org.onosproject.net.pi.model.PiMatchFieldId;
26import org.onosproject.net.pi.model.PiTableId;
Frank Wange33e4ed2017-06-28 10:01:07 +080027
28import java.util.Map;
29
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.Matchers.is;
32import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Carmelo Cascone87892e22017-11-13 16:01:29 -080033import static org.onosproject.net.pi.runtime.PiConstantsTest.DOT;
34import static org.onosproject.net.pi.runtime.PiConstantsTest.DROP;
35import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR;
36import static org.onosproject.net.pi.runtime.PiConstantsTest.IPV4_HEADER_NAME;
Frank Wange33e4ed2017-06-28 10:01:07 +080037
38/**
39 * Unit tests for PiTableEntry class.
40 */
41public class PiTableEntryTest {
Carmelo Cascone7f75be42017-09-07 14:37:02 +020042 private final PiTableEntry piTableEntry1 = PiTableEntry.builder()
Frank Wange33e4ed2017-06-28 10:01:07 +080043 .forTable(PiTableId.of("Table10"))
44 .withCookie(0xac)
45 .withPriority(10)
46 .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
47 .withTimeout(100)
48 .build();
Carmelo Cascone7f75be42017-09-07 14:37:02 +020049 private final PiTableEntry sameAsPiTableEntry1 = PiTableEntry.builder()
Frank Wange33e4ed2017-06-28 10:01:07 +080050 .forTable(PiTableId.of("Table10"))
51 .withCookie(0xac)
52 .withPriority(10)
53 .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
54 .withTimeout(100)
55 .build();
Carmelo Cascone7f75be42017-09-07 14:37:02 +020056 private final PiTableEntry piTableEntry2 = PiTableEntry.builder()
Frank Wange33e4ed2017-06-28 10:01:07 +080057 .forTable(PiTableId.of("Table20"))
58 .withCookie(0xac)
59 .withPriority(10)
60 .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
61 .withTimeout(1000)
62 .build();
63
64 /**
65 * Checks that the PiTableEntry class is immutable.
66 */
67 @Test
68 public void testImmutability() {
69 assertThatClassIsImmutable(PiTableEntry.class);
70 }
71
72 /**
73 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
74 */
75 @Test
76 public void testEquals() {
77 new EqualsTester()
78 .addEqualityGroup(piTableEntry1, sameAsPiTableEntry1)
79 .addEqualityGroup(piTableEntry2)
80 .testEquals();
81 }
82
83 /**
84 * Tests creation of a DefaultFlowRule using a FlowRule constructor.
85 */
86 @Test
87 public void testBuilder() {
88
89 PiTableId piTableId = PiTableId.of("table10");
90 long cookie = 0xfff0323;
91 int priority = 100;
92 double timeout = 1000;
Carmelo Cascone87892e22017-11-13 16:01:29 -080093 PiMatchFieldId piMatchFieldId = PiMatchFieldId.of(IPV4_HEADER_NAME + DOT + DST_ADDR);
94 PiFieldMatch piFieldMatch = new PiExactFieldMatch(piMatchFieldId, ImmutableByteSequence.copyFrom(0x0a010101));
Frank Wange33e4ed2017-06-28 10:01:07 +080095 PiAction piAction = PiAction.builder().withId(PiActionId.of(DROP)).build();
Carmelo Cascone87892e22017-11-13 16:01:29 -080096 final Map<PiMatchFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap();
97 fieldMatches.put(piMatchFieldId, piFieldMatch);
Frank Wange33e4ed2017-06-28 10:01:07 +080098 final PiTableEntry piTableEntry = PiTableEntry.builder()
99 .forTable(piTableId)
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200100 .withMatchKey(PiMatchKey.builder()
101 .addFieldMatches(fieldMatches.values())
102 .build())
Frank Wange33e4ed2017-06-28 10:01:07 +0800103 .withAction(piAction)
104 .withCookie(cookie)
105 .withPriority(priority)
106 .withTimeout(timeout)
107 .build();
108
109 assertThat(piTableEntry.table(), is(piTableId));
110 assertThat(piTableEntry.cookie(), is(cookie));
Carmelo Cascone87892e22017-11-13 16:01:29 -0800111 assertThat("Priority must be set", piTableEntry.priority().isPresent());
112 assertThat("Timeout must be set", piTableEntry.timeout().isPresent());
Frank Wange33e4ed2017-06-28 10:01:07 +0800113 assertThat(piTableEntry.priority().get(), is(priority));
114 assertThat(piTableEntry.timeout().get(), is(timeout));
115 assertThat("Incorrect match param value",
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200116 CollectionUtils.isEqualCollection(piTableEntry.matchKey().fieldMatches(), fieldMatches.values()));
Frank Wange33e4ed2017-06-28 10:01:07 +0800117 assertThat(piTableEntry.action(), is(piAction));
118 }
119}