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