blob: 1c51b7e48b592003fb3cc7e0e17e2f5ff922d461 [file] [log] [blame]
Carmelo Cascone5899c132016-04-06 22:09:08 -07001/*
Carmelo Casconeaa8b6292016-04-13 14:27:06 -07002 * Copyright 2016-present Open Networking Laboratory
Carmelo Cascone5899c132016-04-06 22:09:08 -07003 *
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
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070017package org.onosproject.bmv2.api.model;
Carmelo Cascone5899c132016-04-06 22:09:08 -070018
19import com.eclipsesource.json.Json;
20import com.eclipsesource.json.JsonObject;
21import com.google.common.testing.EqualsTester;
22import org.junit.Before;
23import org.junit.Test;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070024import org.onosproject.bmv2.api.runtime.Bmv2MatchParam;
Carmelo Cascone5899c132016-04-06 22:09:08 -070025
26import java.io.BufferedReader;
27import java.io.InputStreamReader;
28
29import static org.hamcrest.CoreMatchers.notNullValue;
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.Matchers.hasSize;
32import static org.hamcrest.Matchers.is;
33import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
34import static org.hamcrest.core.IsEqual.equalTo;
35
36/**
37 * BMv2 model parser test suite.
38 */
39public class Bmv2ModelTest {
40
41 private JsonObject json;
42 private JsonObject json2;
43
44 @Before
45 public void setUp() throws Exception {
46 json = Json.parse(new BufferedReader(new InputStreamReader(
47 this.getClass().getResourceAsStream("/simple_pipeline.json")))).asObject();
48 json2 = Json.parse(new BufferedReader(new InputStreamReader(
49 this.getClass().getResourceAsStream("/simple_pipeline.json")))).asObject();
50 }
51
52 @Test
53 public void testParse() throws Exception {
54 Bmv2Model model = Bmv2Model.parse(json);
Carmelo Casconef8cf2882016-05-04 14:06:17 -070055 Bmv2Model model2 = Bmv2Model.parse(json2);
Carmelo Cascone5899c132016-04-06 22:09:08 -070056
57 new EqualsTester()
58 .addEqualityGroup(model, model2)
59 .testEquals();
60
61 /* Check header types */
62 Bmv2ModelHeaderType stdMetaT = model.headerType("standard_metadata_t");
63 Bmv2ModelHeaderType ethernetT = model.headerType("ethernet_t");
64 Bmv2ModelHeaderType intrinsicMetaT = model.headerType("intrinsic_metadata_t");
65
66 Bmv2ModelHeaderType stdMetaT2 = model2.headerType("standard_metadata_t");
67 Bmv2ModelHeaderType ethernetT2 = model2.headerType("ethernet_t");
68 Bmv2ModelHeaderType intrinsicMetaT2 = model2.headerType("intrinsic_metadata_t");
69
70 new EqualsTester()
71 .addEqualityGroup(stdMetaT, stdMetaT2)
72 .addEqualityGroup(ethernetT, ethernetT2)
73 .addEqualityGroup(intrinsicMetaT, intrinsicMetaT2)
74 .testEquals();
75
76 // existence
77 assertThat("Json parsed value is null", stdMetaT, notNullValue());
78 assertThat("Json parsed value is null", ethernetT, notNullValue());
79 assertThat("Json parsed value is null", intrinsicMetaT, notNullValue());
80
81 // fields size
82 assertThat("Incorrect size for header type fields",
83 stdMetaT.fields(), hasSize(8));
84 assertThat("Incorrect size for header type fields",
85 ethernetT.fields(), hasSize(3));
86 assertThat("Incorrect size for header type fields",
87 intrinsicMetaT.fields(), hasSize(4));
88
89 // check that fields are in order
90 assertThat("Incorrect order for header type fields",
91 stdMetaT.fields(), contains(
92 stdMetaT.field("ingress_port"),
93 stdMetaT.field("packet_length"),
94 stdMetaT.field("egress_spec"),
95 stdMetaT.field("egress_port"),
96 stdMetaT.field("egress_instance"),
97 stdMetaT.field("instance_type"),
98 stdMetaT.field("clone_spec"),
99 stdMetaT.field("_padding")));
100
101 /* Check actions */
102 Bmv2ModelAction floodAction = model.action("flood");
103 Bmv2ModelAction dropAction = model.action("_drop");
104 Bmv2ModelAction fwdAction = model.action("fwd");
105
106 Bmv2ModelAction floodAction2 = model2.action("flood");
107 Bmv2ModelAction dropAction2 = model2.action("_drop");
108 Bmv2ModelAction fwdAction2 = model2.action("fwd");
109
110 new EqualsTester()
111 .addEqualityGroup(floodAction, floodAction2)
112 .addEqualityGroup(dropAction, dropAction2)
113 .addEqualityGroup(fwdAction, fwdAction2)
114 .testEquals();
115
116 // existence
117 assertThat("Json parsed value is null", floodAction, notNullValue());
118 assertThat("Json parsed value is null", dropAction, notNullValue());
119 assertThat("Json parsed value is null", fwdAction, notNullValue());
120
121 // runtime data size
122 assertThat("Incorrect size for action runtime data",
123 floodAction.runtimeDatas().size(), is(equalTo(0)));
124 assertThat("Incorrect size for action runtime data",
125 dropAction.runtimeDatas().size(), is(equalTo(0)));
126 assertThat("Incorrect size for action runtime data",
127 fwdAction.runtimeDatas().size(), is(equalTo(1)));
128
129 // runtime data existence and parsing
130 assertThat("Parsed Json value is null",
131 fwdAction.runtimeData("port"), notNullValue());
132 assertThat("Incorrect value for action runtime data bitwidth",
133 fwdAction.runtimeData("port").bitWidth(), is(equalTo(9)));
134
135 /* Check tables */
136 Bmv2ModelTable table0 = model.table(0);
137 Bmv2ModelTable table02 = model2.table(0);
138
139 new EqualsTester()
140 .addEqualityGroup(table0, table02)
141 .testEquals();
142
143 // existence
144 assertThat("Parsed Json value is null", table0, notNullValue());
145
146 // id and name correspondence
147 assertThat("Incorrect value for table name",
148 table0.name(), is(equalTo("table0")));
149
150 // keys size
151 assertThat("Incorrect size for table keys",
152 table0.keys().size(), is(equalTo(4)));
153
154 // key match type
155 assertThat("Incorrect value for table key match type",
Carmelo Casconeaa8b6292016-04-13 14:27:06 -0700156 table0.keys().get(0).matchType(), is(equalTo(Bmv2MatchParam.Type.TERNARY)));
Carmelo Cascone5899c132016-04-06 22:09:08 -0700157
158 // header type
159 assertThat("Incorrect value for table key header type",
160 table0.keys().get(0).field().header().type(), is(equalTo(stdMetaT)));
161 }
162}