blob: d795aeef5a9cb6e9a5997e7c42af28b1174d0bab [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
89 String jsonVlanId = jsonNode.get(VLAN_ID).asText();
90 String vlanId = flowInfo.vlanId().toString();
91 if (!jsonVlanId.equals(vlanId)) {
92 description.appendText("VLAN id was " + jsonVlanId);
93 return false;
94 }
95
96 // check source IP
97 String jsonSrcIp = jsonNode.get(SRC_IP).asText();
98 String srcIp = flowInfo.srcIp().address().toString();
99 if (!jsonSrcIp.equals(srcIp)) {
100 description.appendText("Source IP was " + jsonSrcIp);
101 return false;
102 }
103
104 // check destination IP
105 String jsonDstIp = jsonNode.get(DST_IP).asText();
106 String dstIp = flowInfo.dstIp().address().toString();
107 if (!jsonDstIp.equals(dstIp)) {
108 description.appendText("Destination IP was " + jsonDstIp);
109 return false;
110 }
111
112 // check source IP prefix length
113 int jsonSrcPrefixLength = jsonNode.get(SRC_IP_PREFIX_LEN).asInt();
114 int srcPrefixLength = flowInfo.srcIp().prefixLength();
115 if (jsonSrcPrefixLength != srcPrefixLength) {
116 description.appendText("Source IP prefix length was " + jsonSrcPrefixLength);
117 return false;
118 }
119
120 // check destination IP prefix length
121 int jsonDstPrefixLength = jsonNode.get(DST_IP_PREFIX_LEN).asInt();
122 int dstPrefixLength = flowInfo.dstIp().prefixLength();
123 if (jsonDstPrefixLength != dstPrefixLength) {
124 description.appendText("Destination IP prefix length was " + jsonDstPrefixLength);
125 return false;
126 }
127
128 // check source port
129 int jsonSrcPort = jsonNode.get(SRC_PORT).asInt();
130 int srcPort = flowInfo.srcPort().toInt();
131 if (jsonSrcPort != srcPort) {
132 description.appendText("Source port was " + jsonSrcPort);
133 return false;
134 }
135
136 // check destination port
137 int jsonDstPort = jsonNode.get(DST_PORT).asInt();
138 int dstPort = flowInfo.dstPort().toInt();
139 if (jsonDstPort != dstPort) {
140 description.appendText("Destination port was " + jsonDstPort);
141 return false;
142 }
143
144 // check protocol
145 String jsonProtocol = jsonNode.get(PROTOCOL).asText();
146 String protocol = String.valueOf(flowInfo.protocol());
147 if (!jsonProtocol.equals(protocol)) {
148 description.appendText("Protocol was " + jsonProtocol);
149 return false;
150 }
151
152 // check source mac
153 String jsonSrcMac = jsonNode.get(SRC_MAC).asText();
154 String srcMac = flowInfo.srcMac().toString();
155 if (!jsonSrcMac.equals(srcMac)) {
156 description.appendText("Source MAC was " + jsonSrcMac);
157 return false;
158 }
159
160 // check destination mac
161 String jsonDstMac = jsonNode.get(DST_MAC).asText();
162 String dstMac = flowInfo.dstMac().toString();
163 if (!jsonDstMac.equals(dstMac)) {
164 description.appendText("Destination MAC was " + jsonDstMac);
165 return false;
166 }
167
168 // check stats info
169 JsonNode jsonStatsInfo = jsonNode.get(STATS_INFO);
170 if (jsonStatsInfo != null) {
171 StatsInfo statsInfo = flowInfo.statsInfo();
172 StatsInfoJsonMatcher infoMatcher =
173 StatsInfoJsonMatcher.matchStatsInfo(statsInfo);
174 return infoMatcher.matches(jsonStatsInfo);
175 }
176
177 return true;
178 }
179
180 @Override
181 public void describeTo(Description description) {
182 description.appendText(flowInfo.toString());
183 }
184
185 /**
186 * Factory to allocate an flow info matcher.
187 *
188 * @param flowInfo flow info object we are looking for
189 * @return matcher
190 */
191 public static FlowInfoJsonMatcher matchesFlowInfo(FlowInfo flowInfo) {
192 return new FlowInfoJsonMatcher(flowInfo);
193 }
194}