blob: 7574054b3038df3c08a33d0afaeb61758249e081 [file] [log] [blame]
Jian Li4a3fffa2018-06-10 23:12:40 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.openstacktelemetry.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.openstacktelemetry.api.FlowInfo;
22import org.onosproject.openstacktelemetry.api.StatsInfo;
23
24/**
25 * Hamcrest matcher for flow info.
26 */
27public final class FlowInfoJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
28
29 private final FlowInfo flowInfo;
30
31 private static final String FLOW_TYPE = "flowType";
32 private static final String DEVICE_ID = "deviceId";
33 private static final String INPUT_INTERFACE_ID = "inputInterfaceId";
34 private static final String OUTPUT_INTERFACE_ID = "outputInterfaceId";
35
36 private static final String VLAN_ID = "vlanId";
37 private static final String VXLAN_ID = "vxlanId";
38 private static final String SRC_IP = "srcIp";
39 private static final String SRC_IP_PREFIX_LEN = "srcIpPrefixLength";
40 private static final String DST_IP = "dstIp";
41 private static final String DST_IP_PREFIX_LEN = "dstIpPrefixLength";
42 private static final String SRC_PORT = "srcPort";
43 private static final String DST_PORT = "dstPort";
44 private static final String PROTOCOL = "protocol";
45 private static final String SRC_MAC = "srcMac";
46 private static final String DST_MAC = "dstMac";
47 private static final String STATS_INFO = "statsInfo";
48
49 private FlowInfoJsonMatcher(FlowInfo flowInfo) {
50 this.flowInfo = flowInfo;
51 }
52
53 @Override
54 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
55
56 // check flow type
57 String jsonFlowType = jsonNode.get(FLOW_TYPE).asText();
58 String flowType = String.valueOf(flowInfo.flowType());
59 if (!jsonFlowType.equals(flowType)) {
60 description.appendText("flow type was " + jsonFlowType);
61 return false;
62 }
63
64 // check device id
65 String jsonDeviceId = jsonNode.get(DEVICE_ID).asText();
66 String deviceId = flowInfo.deviceId().toString();
67 if (!jsonDeviceId.equals(deviceId)) {
68 description.appendText("device id was " + jsonDeviceId);
69 return false;
70 }
71
72 // check input interface id
73 int jsonInputInterfaceId = jsonNode.get(INPUT_INTERFACE_ID).asInt();
74 int inputInterfaceId = flowInfo.inputInterfaceId();
75 if (jsonInputInterfaceId != inputInterfaceId) {
76 description.appendText("input interface id was " + jsonInputInterfaceId);
77 return false;
78 }
79
80 // check output interface id
81 int jsonOutputInterfaceId = jsonNode.get(OUTPUT_INTERFACE_ID).asInt();
82 int outputInterfaceId = flowInfo.outputInterfaceId();
83 if (jsonOutputInterfaceId != outputInterfaceId) {
84 description.appendText("output interface id was " + jsonInputInterfaceId);
85 return false;
86 }
87
88 // check vlan id
boyoung2a8549d22018-11-23 20:42:37 +090089 try {
90 if (!(jsonNode.get(VLAN_ID).isNull())) {
91 String jsonVlanId = jsonNode.get(VLAN_ID).asText();
92 String vlanId = flowInfo.vlanId().toString();
93 if (!jsonVlanId.equals(vlanId)) {
94 description.appendText("VLAN id was " + jsonVlanId);
95 return false;
96 }
97 }
98 } catch (NullPointerException ex) {
99 description.appendText("VLAN id was null");
Jian Li4a3fffa2018-06-10 23:12:40 +0900100 }
101
102 // check source IP
103 String jsonSrcIp = jsonNode.get(SRC_IP).asText();
104 String srcIp = flowInfo.srcIp().address().toString();
105 if (!jsonSrcIp.equals(srcIp)) {
106 description.appendText("Source IP was " + jsonSrcIp);
107 return false;
108 }
109
110 // check destination IP
111 String jsonDstIp = jsonNode.get(DST_IP).asText();
112 String dstIp = flowInfo.dstIp().address().toString();
113 if (!jsonDstIp.equals(dstIp)) {
114 description.appendText("Destination IP was " + jsonDstIp);
115 return false;
116 }
117
118 // check source IP prefix length
119 int jsonSrcPrefixLength = jsonNode.get(SRC_IP_PREFIX_LEN).asInt();
120 int srcPrefixLength = flowInfo.srcIp().prefixLength();
121 if (jsonSrcPrefixLength != srcPrefixLength) {
122 description.appendText("Source IP prefix length was " + jsonSrcPrefixLength);
123 return false;
124 }
125
126 // check destination IP prefix length
127 int jsonDstPrefixLength = jsonNode.get(DST_IP_PREFIX_LEN).asInt();
128 int dstPrefixLength = flowInfo.dstIp().prefixLength();
129 if (jsonDstPrefixLength != dstPrefixLength) {
130 description.appendText("Destination IP prefix length was " + jsonDstPrefixLength);
131 return false;
132 }
133
134 // check source port
135 int jsonSrcPort = jsonNode.get(SRC_PORT).asInt();
136 int srcPort = flowInfo.srcPort().toInt();
137 if (jsonSrcPort != srcPort) {
138 description.appendText("Source port was " + jsonSrcPort);
139 return false;
140 }
141
142 // check destination port
143 int jsonDstPort = jsonNode.get(DST_PORT).asInt();
144 int dstPort = flowInfo.dstPort().toInt();
145 if (jsonDstPort != dstPort) {
146 description.appendText("Destination port was " + jsonDstPort);
147 return false;
148 }
149
150 // check protocol
151 String jsonProtocol = jsonNode.get(PROTOCOL).asText();
152 String protocol = String.valueOf(flowInfo.protocol());
153 if (!jsonProtocol.equals(protocol)) {
154 description.appendText("Protocol was " + jsonProtocol);
155 return false;
156 }
157
158 // check source mac
159 String jsonSrcMac = jsonNode.get(SRC_MAC).asText();
160 String srcMac = flowInfo.srcMac().toString();
161 if (!jsonSrcMac.equals(srcMac)) {
162 description.appendText("Source MAC was " + jsonSrcMac);
163 return false;
164 }
165
166 // check destination mac
167 String jsonDstMac = jsonNode.get(DST_MAC).asText();
168 String dstMac = flowInfo.dstMac().toString();
169 if (!jsonDstMac.equals(dstMac)) {
170 description.appendText("Destination MAC was " + jsonDstMac);
171 return false;
172 }
173
174 // check stats info
175 JsonNode jsonStatsInfo = jsonNode.get(STATS_INFO);
176 if (jsonStatsInfo != null) {
177 StatsInfo statsInfo = flowInfo.statsInfo();
178 StatsInfoJsonMatcher infoMatcher =
179 StatsInfoJsonMatcher.matchStatsInfo(statsInfo);
180 return infoMatcher.matches(jsonStatsInfo);
181 }
182
183 return true;
184 }
185
186 @Override
187 public void describeTo(Description description) {
188 description.appendText(flowInfo.toString());
189 }
190
191 /**
192 * Factory to allocate an flow info matcher.
193 *
194 * @param flowInfo flow info object we are looking for
195 * @return matcher
196 */
197 public static FlowInfoJsonMatcher matchesFlowInfo(FlowInfo flowInfo) {
198 return new FlowInfoJsonMatcher(flowInfo);
199 }
200}