blob: aa44c091dd4d4aca4d0394cd6fe6b912cc7666e5 [file] [log] [blame]
Yi Tsengf33c0772017-06-06 14:56:18 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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.bmv2.model;
18
19import com.eclipsesource.json.Json;
20import com.eclipsesource.json.JsonObject;
Yi Tsengf33c0772017-06-06 14:56:18 -070021import com.google.common.collect.Sets;
22import com.google.common.testing.EqualsTester;
23import org.hamcrest.collection.IsIterableContainingInOrder;
24import org.junit.Before;
25import org.junit.Test;
26import org.onosproject.net.pi.model.PiHeaderFieldModel;
27import org.onosproject.net.pi.model.PiHeaderModel;
28import org.onosproject.net.pi.model.PiHeaderTypeModel;
29import org.onosproject.net.pi.model.PiMatchType;
30import org.onosproject.net.pi.model.PiTableMatchFieldModel;
31
32import java.io.BufferedReader;
33import java.io.InputStreamReader;
Yi Tsengf33c0772017-06-06 14:56:18 -070034import java.util.Set;
35import java.util.stream.Collectors;
36
37import static org.hamcrest.CoreMatchers.notNullValue;
38import static org.hamcrest.MatcherAssert.assertThat;
39import static org.hamcrest.Matchers.*;
40import static org.hamcrest.core.IsEqual.equalTo;
41
42/**
43 * BMv2 JSON configuration parser test.
44 */
45public class Bmv2PipelineModelParserTest {
46
47 private JsonObject json;
48 private JsonObject json2;
49
50 @Before
51 public void setUp() throws Exception {
52 json = Json.parse(new BufferedReader(new InputStreamReader(
53 this.getClass().getResourceAsStream("/default.json")))).asObject();
54 json2 = Json.parse(new BufferedReader(new InputStreamReader(
55 this.getClass().getResourceAsStream("/default.json")))).asObject();
56 }
57
58 @Test
59 public void testParse() throws Exception {
60 Bmv2PipelineModel config = Bmv2PipelineModelParser.parse(json);
61 Bmv2PipelineModel config2 = Bmv2PipelineModelParser.parse(json2);
62
63 new EqualsTester()
64 .addEqualityGroup(config, config2)
65 .testEquals();
66
67 /* Check header types */
68 Bmv2HeaderTypeModel stdMetaT =
69 (Bmv2HeaderTypeModel) config.headerType("standard_metadata").orElse(null);
70 Bmv2HeaderTypeModel ethernetT =
71 (Bmv2HeaderTypeModel) config.headerType("ethernet_t").orElse(null);
72 Bmv2HeaderTypeModel intrinsicMetaT =
73 (Bmv2HeaderTypeModel) config.headerType("intrinsic_metadata_t").orElse(null);
74
75 Bmv2HeaderTypeModel stdMetaT2 =
76 (Bmv2HeaderTypeModel) config2.headerType("standard_metadata").orElse(null);
77 Bmv2HeaderTypeModel ethernetT2 =
78 (Bmv2HeaderTypeModel) config2.headerType("ethernet_t").orElse(null);
79 Bmv2HeaderTypeModel intrinsicMetaT2 =
80 (Bmv2HeaderTypeModel) config2.headerType("intrinsic_metadata_t").orElse(null);
81
82 new EqualsTester()
83 .addEqualityGroup(stdMetaT, stdMetaT2)
84 .addEqualityGroup(ethernetT, ethernetT2)
85 .addEqualityGroup(intrinsicMetaT, intrinsicMetaT2)
86 .testEquals();
87
88 // existence
89 assertThat("Json parsed value is null", stdMetaT, notNullValue());
90 assertThat("Json parsed value is null", ethernetT, notNullValue());
91 assertThat("Json parsed value is null", intrinsicMetaT, notNullValue());
92
93 // fields size
94 assertThat("Incorrect size for header type fields",
95 stdMetaT.fields(), hasSize(18));
96 assertThat("Incorrect size for header type fields",
97 ethernetT.fields(), hasSize(3));
98 assertThat("Incorrect size for header type fields",
99 intrinsicMetaT.fields(), hasSize(4));
100
101 // check that fields are in order
102 assertThat("Incorrect order for header type fields",
103 stdMetaT.fields(), IsIterableContainingInOrder.contains(
104 stdMetaT.field("ingress_port").get(),
105 stdMetaT.field("egress_spec").get(),
106 stdMetaT.field("egress_port").get(),
107 stdMetaT.field("clone_spec").get(),
108 stdMetaT.field("instance_type").get(),
109 stdMetaT.field("drop").get(),
110 stdMetaT.field("recirculate_port").get(),
111 stdMetaT.field("packet_length").get(),
112 stdMetaT.field("enq_timestamp").get(),
113 stdMetaT.field("enq_qdepth").get(),
114 stdMetaT.field("deq_timedelta").get(),
115 stdMetaT.field("deq_qdepth").get(),
116 stdMetaT.field("ingress_global_timestamp").get(),
117 stdMetaT.field("lf_field_list").get(),
118 stdMetaT.field("mcast_grp").get(),
119 stdMetaT.field("resubmit_flag").get(),
120 stdMetaT.field("egress_rid").get(),
121 stdMetaT.field("_padding").get()
122 ));
123
124 /* Check actions */
125 Bmv2ActionModel noAction =
126 (Bmv2ActionModel) config.action("NoAction").orElse(null);
127 Bmv2ActionModel setEgressPortAction =
128 (Bmv2ActionModel) config.action("set_egress_port_0").orElse(null);
129 Bmv2ActionModel sendToCpuAction =
130 (Bmv2ActionModel) config.action("send_to_cpu_0").orElse(null);
131 Bmv2ActionModel dropAction =
132 (Bmv2ActionModel) config.action("_drop_0").orElse(null);
133 Bmv2ActionModel processPortCountersCountPacketAction =
134 (Bmv2ActionModel) config.action("process_port_counters_0.count_packet").orElse(null);
135
136
137 Bmv2ActionModel noAction2 =
138 (Bmv2ActionModel) config.action("NoAction").orElse(null);
139 Bmv2ActionModel setEgressPortAction2 =
140 (Bmv2ActionModel) config.action("set_egress_port_0").orElse(null);
141 Bmv2ActionModel sendToCpuAction2 =
142 (Bmv2ActionModel) config.action("send_to_cpu_0").orElse(null);
143 Bmv2ActionModel dropAction2 =
144 (Bmv2ActionModel) config.action("_drop_0").orElse(null);
145 Bmv2ActionModel processPortCountersCountPacketAction2 =
146 (Bmv2ActionModel) config.action("process_port_counters_0.count_packet").orElse(null);
147
148 new EqualsTester()
149 .addEqualityGroup(noAction, noAction2)
150 .addEqualityGroup(setEgressPortAction, setEgressPortAction2)
151 .addEqualityGroup(sendToCpuAction, sendToCpuAction2)
152 .addEqualityGroup(dropAction, dropAction2)
153 .addEqualityGroup(processPortCountersCountPacketAction, processPortCountersCountPacketAction2)
154 .testEquals();
155
156 // existence
157 assertThat("Json parsed value is null", noAction, notNullValue());
158 assertThat("Json parsed value is null", setEgressPortAction, notNullValue());
159 assertThat("Json parsed value is null", sendToCpuAction, notNullValue());
160 assertThat("Json parsed value is null", dropAction, notNullValue());
161 assertThat("Json parsed value is null", processPortCountersCountPacketAction, notNullValue());
162
163 // runtime data size
164 assertThat("Incorrect size for action runtime data",
165 noAction.params().size(), is(equalTo(0)));
166 assertThat("Incorrect size for action runtime data",
167 setEgressPortAction.params().size(), is(equalTo(1)));
168 assertThat("Incorrect size for action runtime data",
169 sendToCpuAction.params().size(), is(equalTo(0)));
170 assertThat("Incorrect size for action runtime data",
171 dropAction.params().size(), is(equalTo(0)));
172 assertThat("Incorrect size for action runtime data",
173 processPortCountersCountPacketAction.params().size(), is(equalTo(0)));
174
175 // runtime data existence and parsing
176 assertThat("Parsed Json value is null",
177 setEgressPortAction.param("port").orElse(null), notNullValue());
178 assertThat("Incorrect value for action runtime data bitwidth",
179 setEgressPortAction.param("port").get().bitWidth(), is(equalTo(9)));
180
181
182 /* Check tables */
183 Bmv2TableModel table0 =
184 (Bmv2TableModel) config.table("table0").orElse(null);
185 Bmv2TableModel table02 =
186 (Bmv2TableModel) config2.table("table0").orElse(null);
187
188 new EqualsTester()
189 .addEqualityGroup(table0, table02)
190 .testEquals();
191
192 // existence
193 assertThat("Parsed Json value is null", table0, notNullValue());
194
195 // id and name correspondence
196 assertThat("Incorrect value for table name",
197 table0.name(), is(equalTo("table0")));
198
199 Set<PiTableMatchFieldModel> matchFields = Sets.newHashSet(table0.matchFields());
200
201 // keys size
202 assertThat("Incorrect size for table keys",
203 matchFields.size(), is(equalTo(4)));
204
205 Set<PiMatchType> matchTypes = matchFields.stream()
206 .map(PiTableMatchFieldModel::matchType)
207 .collect(Collectors.toSet());
208
209 // key match type
210 assertThat("Incorrect value for table key match type",
211 matchTypes, containsInAnyOrder(PiMatchType.TERNARY));
212
213 Set<PiHeaderTypeModel> headerTypeModels = matchFields.stream()
214 .map(PiTableMatchFieldModel::field)
215 .map(PiHeaderFieldModel::header)
216 .map(PiHeaderModel::type)
217 .collect(Collectors.toSet());
218
219 // header type
220 assertThat("Incorrect value for table key header type",
221 headerTypeModels, containsInAnyOrder(ethernetT, stdMetaT));
222
223 }
224}