blob: 0abd0ef6057ead95c8a8542177ccf34ac656687b [file] [log] [blame]
Jian Lib54d14b2017-03-28 21:34:34 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Lib54d14b2017-03-28 21:34:34 +09003 *
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 */
Jian Li2e818b02017-04-12 19:28:30 +090016package org.onosproject.mapping.codec;
Jian Lib54d14b2017-03-28 21:34:34 +090017
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.mapping.instructions.MappingInstruction;
22import org.onosproject.mapping.instructions.MulticastMappingInstruction;
23import org.onosproject.mapping.instructions.UnicastMappingInstruction;
24
25/**
26 * Hamcrest matcher for mapping instructions.
27 */
28public final class MappingInstructionJsonMatcher
29 extends TypeSafeDiagnosingMatcher<JsonNode> {
30
31 private final MappingInstruction instruction;
32
33 /**
34 * A default constructor.
35 *
36 * @param instruction mapping instruction
37 */
38 private MappingInstructionJsonMatcher(MappingInstruction instruction) {
39 this.instruction = instruction;
40 }
41
42 /**
43 * Matches the contents of an unicast weight mapping instruction.
44 *
45 * @param node JSON instruction to match
46 * @param description object used for recording errors
47 * @return true if contents match, false otherwise
48 */
49 private boolean matchUnicastWeightInstruction(JsonNode node,
50 Description description) {
51 UnicastMappingInstruction.WeightMappingInstruction instructionToMatch =
52 (UnicastMappingInstruction.WeightMappingInstruction) instruction;
53 final String jsonSubtype = node.get(MappingInstructionCodec.SUBTYPE).textValue();
54 if (!instructionToMatch.subtype().name().equals(jsonSubtype)) {
55 description.appendText("subtype was " + jsonSubtype);
56 return false;
57 }
58
59 final String jsonType = node.get(MappingInstructionCodec.TYPE).textValue();
60 if (!instructionToMatch.type().name().equals(jsonType)) {
61 description.appendText("type was " + jsonType);
62 return false;
63 }
64
65 final int jsonWeight = node.get(MappingInstructionCodec.UNICAST_WEIGHT).intValue();
66 final int weight = instructionToMatch.weight();
67 if (jsonWeight != weight) {
68 description.appendText("Unicast weight was " + jsonWeight);
69 return false;
70 }
71
72 return true;
73 }
74
75 /**
76 * Matches the contents of an unicast priority mapping instruction.
77 *
78 * @param node JSON instruction to match
79 * @param description object used for recording errors
80 * @return true if contents match, false otherwise
81 */
82 private boolean matchUnicastPriorityInstruction(JsonNode node,
83 Description description) {
84 UnicastMappingInstruction.PriorityMappingInstruction instructionToMatch =
85 (UnicastMappingInstruction.PriorityMappingInstruction) instruction;
86 final String jsonSubtype = node.get(MappingInstructionCodec.SUBTYPE).textValue();
87 if (!instructionToMatch.subtype().name().equals(jsonSubtype)) {
88 description.appendText("subtype was " + jsonSubtype);
89 return false;
90 }
91
92 final String jsonType = node.get(MappingInstructionCodec.TYPE).textValue();
93 if (!instructionToMatch.type().name().equals(jsonType)) {
94 description.appendText("type was " + jsonType);
95 return false;
96 }
97
98 final int jsonPriority = node.get(MappingInstructionCodec.UNICAST_PRIORITY).intValue();
99 final int priority = instructionToMatch.priority();
100 if (jsonPriority != priority) {
101 description.appendText("Unicast priority was " + jsonPriority);
102 return false;
103 }
104
105 return true;
106 }
107
108 /**
109 * Matches the contents of a multicast weight mapping instruction.
110 *
111 * @param node JSON instruction to match
112 * @param description object used for recording errors
113 * @return true if contents match, false otherwise
114 */
115 private boolean matchMulticastWeightInstruction(JsonNode node,
116 Description description) {
117 MulticastMappingInstruction.WeightMappingInstruction instructionToMatch =
118 (MulticastMappingInstruction.WeightMappingInstruction) instruction;
119 final String jsonSubtype = node.get(MappingInstructionCodec.SUBTYPE).textValue();
120 if (!instructionToMatch.subtype().name().equals(jsonSubtype)) {
121 description.appendText("subtype was " + jsonSubtype);
122 return false;
123 }
124
125 final String jsonType = node.get(MappingInstructionCodec.TYPE).textValue();
126 if (!instructionToMatch.type().name().equals(jsonType)) {
127 description.appendText("type was " + jsonType);
128 return false;
129 }
130
131 final int jsonWeight = node.get(MappingInstructionCodec.MULTICAST_WEIGHT).intValue();
132 final int weight = instructionToMatch.weight();
133 if (jsonWeight != weight) {
134 description.appendText("Multicast weight was " + jsonWeight);
135 return false;
136 }
137
138 return true;
139 }
140
141 /**
142 * Matches the contents of a multicast priority mapping instruction.
143 *
144 * @param node JSON instruction to match
145 * @param description object used for recording errors
146 * @return true if contents match, false otherwise
147 */
148 private boolean matchMulticastPriorityInstruction(JsonNode node,
149 Description description) {
150 MulticastMappingInstruction.PriorityMappingInstruction instructionToMatch =
151 (MulticastMappingInstruction.PriorityMappingInstruction) instruction;
152 final String jsonSubtype = node.get(MappingInstructionCodec.SUBTYPE).textValue();
153 if (!instructionToMatch.subtype().name().equals(jsonSubtype)) {
154 description.appendText("subtype was " + jsonSubtype);
155 return false;
156 }
157
158 final String jsonType = node.get(MappingInstructionCodec.TYPE).textValue();
159 if (!instructionToMatch.type().name().equals(jsonType)) {
160 description.appendText("type was " + jsonType);
161 return false;
162 }
163
164 final int jsonPriority = node.get(MappingInstructionCodec.MULTICAST_PRIORITY).intValue();
165 final int priority = instructionToMatch.priority();
166 if (jsonPriority != priority) {
167 description.appendText("Multicast priority was " + jsonPriority);
168 return false;
169 }
170
171 return true;
172 }
173
174 @Override
175 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
176
177 // check type
178 final JsonNode jsonTypeNode = jsonNode.get(MappingInstructionCodec.TYPE);
179 final String jsonType = jsonTypeNode.textValue();
180 final String type = instruction.type().name();
181 if (!jsonType.equals(type)) {
182 description.appendText("type was " + type);
183 return false;
184 }
185
186 if (instruction instanceof UnicastMappingInstruction.WeightMappingInstruction) {
187 return matchUnicastWeightInstruction(jsonNode, description);
188 } else if (instruction instanceof UnicastMappingInstruction.PriorityMappingInstruction) {
189 return matchUnicastPriorityInstruction(jsonNode, description);
190 } else if (instruction instanceof MulticastMappingInstruction.WeightMappingInstruction) {
191 return matchMulticastWeightInstruction(jsonNode, description);
192 } else if (instruction instanceof MulticastMappingInstruction.PriorityMappingInstruction) {
193 return matchMulticastPriorityInstruction(jsonNode, description);
194 }
195
196 return false;
197 }
198
199 @Override
200 public void describeTo(Description description) {
201 description.appendText(instruction.toString());
202 }
203
204 /**
205 * Factory to allocate a mapping instruction matcher.
206 *
207 * @param instruction instruction object we are looking for
208 * @return matcher
209 */
210 public static MappingInstructionJsonMatcher matchesInstruction(
211 MappingInstruction instruction) {
212 return new MappingInstructionJsonMatcher(instruction);
213 }
214}