blob: 5c652634354a45b7316cf2fa1861528daa48b60a [file] [log] [blame]
Jian Lib45ffda2019-10-23 14:05:32 +09001/*
2 * Copyright 2019-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.openstackvtap.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.openstackvtap.api.OpenstackVtapCriterion;
22
23import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getProtocolStringFromType;
24
25/**
26 * Hamcrest matcher for openstack vtap criterion config.
27 */
28public final class OpenstackVtapCriterionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
29
30 private final OpenstackVtapCriterion criterion;
31
32 private static final String SRC_IP = "srcIp";
33 private static final String DST_IP = "dstIp";
34 private static final String IP_PROTOCOL = "ipProto";
35 private static final String SRC_PORT = "srcPort";
36 private static final String DST_PORT = "dstPort";
37
38 private OpenstackVtapCriterionJsonMatcher(OpenstackVtapCriterion criterion) {
39 this.criterion = criterion;
40 }
41
42 @Override
43 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
44
45 // check source IP address
46 JsonNode jsonSrcIp = jsonNode.get(SRC_IP);
47 String srcIp = criterion.srcIpPrefix().address().toString();
48 if (!jsonSrcIp.asText().equals(srcIp)) {
49 description.appendText("Source IP address was " + jsonSrcIp);
50 return false;
51 }
52
53 // check destination IP address
54 JsonNode jsonDstIp = jsonNode.get(DST_IP);
55 String dstIp = criterion.dstIpPrefix().address().toString();
56 if (!jsonDstIp.asText().equals(dstIp)) {
57 description.appendText("Destination IP address was " + jsonDstIp);
58 return false;
59 }
60
61 // check IP protocol
62 JsonNode jsonIpProto = jsonNode.get(IP_PROTOCOL);
63 if (jsonIpProto != null) {
64 String ipProto = getProtocolStringFromType(criterion.ipProtocol());
65 if (!jsonIpProto.asText().equals(ipProto)) {
66 description.appendText("IP protocol was " + jsonIpProto);
67 return false;
68 }
69 }
70
71 // check source port number
72 JsonNode jsonSrcPort = jsonNode.get(SRC_PORT);
73 if (jsonSrcPort != null) {
74 int srcPort = criterion.srcTpPort().toInt();
75 if (jsonSrcPort.asInt() != srcPort) {
76 description.appendText("Source port number was " + jsonSrcPort);
77 return false;
78 }
79 }
80
81 // check destination port number
82 JsonNode jsonDstPort = jsonNode.get(DST_PORT);
83 if (jsonDstPort != null) {
84 int dstPort = criterion.dstTpPort().toInt();
85 if (jsonDstPort.asInt() != dstPort) {
86 description.appendText("Destination port number was " + jsonDstPort);
87 return false;
88 }
89 }
90
91 return true;
92 }
93
94 @Override
95 public void describeTo(Description description) {
96 description.appendText(criterion.toString());
97 }
98
99 /**
100 * Factory to allocate openstack vtap criterion matcher.
101 *
102 * @param criterion vtap criterion object we are looking for
103 * @return matcher
104 */
105 public static OpenstackVtapCriterionJsonMatcher matchVtapCriterion(OpenstackVtapCriterion criterion) {
106 return new OpenstackVtapCriterionJsonMatcher(criterion);
107 }
108}