Jian Li | 1c10cf2 | 2021-03-05 01:32:04 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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 | */ |
| 16 | package org.onosproject.kubevirtnetworking.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule; |
| 23 | |
| 24 | /** |
| 25 | * Hamcrest matcher for kubevirt port. |
| 26 | */ |
| 27 | public final class KubevirtSecurityGroupRuleJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 28 | |
| 29 | private final KubevirtSecurityGroupRule rule; |
| 30 | |
| 31 | private static final String ID = "id"; |
| 32 | private static final String SECURITY_GROUP_ID = "securityGroupId"; |
| 33 | private static final String DIRECTION = "direction"; |
| 34 | private static final String ETHER_TYPE = "etherType"; |
| 35 | private static final String PORT_RANGE_MAX = "portRangeMax"; |
| 36 | private static final String PORT_RANGE_MIN = "portRangeMin"; |
| 37 | private static final String PROTOCOL = "protocol"; |
| 38 | private static final String REMOTE_IP_PREFIX = "remoteIpPrefix"; |
| 39 | private static final String REMOTE_GROUP_ID = "remoteGroupId"; |
| 40 | |
| 41 | private KubevirtSecurityGroupRuleJsonMatcher(KubevirtSecurityGroupRule rule) { |
| 42 | this.rule = rule; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 47 | // check rule ID |
| 48 | String jsonId = jsonNode.get(ID).asText(); |
| 49 | String id = rule.id(); |
| 50 | if (!jsonId.equals(id)) { |
| 51 | description.appendText("Rule ID was " + jsonId); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | // check security group ID |
| 56 | String jsonSecurityGroupId = jsonNode.get(SECURITY_GROUP_ID).asText(); |
| 57 | String securityGroupId = rule.securityGroupId(); |
| 58 | if (!jsonSecurityGroupId.equals(securityGroupId)) { |
| 59 | description.appendText("Security group ID was " + jsonSecurityGroupId); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // check direction |
| 64 | String jsonDirection = jsonNode.get(DIRECTION).asText(); |
| 65 | String direction = rule.direction(); |
| 66 | if (!jsonDirection.equals(direction)) { |
| 67 | description.appendText("Direction was " + jsonDirection); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // check ether type |
| 72 | JsonNode jsonEtherType = jsonNode.get(ETHER_TYPE); |
| 73 | if (jsonEtherType != null) { |
| 74 | String etherType = rule.etherType(); |
| 75 | if (!jsonEtherType.asText().equals(etherType)) { |
| 76 | description.appendText("EtherType was " + jsonEtherType); |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // check port range max |
| 82 | JsonNode jsonPortRangeMax = jsonNode.get(PORT_RANGE_MAX); |
| 83 | if (jsonPortRangeMax != null) { |
| 84 | int portRangeMax = rule.portRangeMax(); |
| 85 | if (portRangeMax != jsonPortRangeMax.asInt()) { |
| 86 | description.appendText("PortRangeMax was " + jsonPortRangeMax); |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // check port range min |
| 92 | JsonNode jsonPortRangeMin = jsonNode.get(PORT_RANGE_MIN); |
| 93 | if (jsonPortRangeMin != null) { |
| 94 | int portRangeMin = rule.portRangeMin(); |
| 95 | if (portRangeMin != jsonPortRangeMin.asInt()) { |
| 96 | description.appendText("PortRangeMin was " + jsonPortRangeMin); |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // check protocol |
| 102 | JsonNode jsonProtocol = jsonNode.get(PROTOCOL); |
| 103 | if (jsonProtocol != null) { |
| 104 | String protocol = rule.protocol(); |
| 105 | if (!jsonProtocol.asText().equals(protocol)) { |
| 106 | description.appendText("Protocol was " + jsonProtocol); |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // check remote IP prefix |
| 112 | JsonNode jsonRemoteIpPrefix = jsonNode.get(REMOTE_IP_PREFIX); |
| 113 | if (jsonRemoteIpPrefix != null) { |
| 114 | IpPrefix remoteIpPrefix = rule.remoteIpPrefix(); |
| 115 | if (!jsonRemoteIpPrefix.asText().equals(remoteIpPrefix.toString())) { |
| 116 | description.appendText("Remote IP prefix was " + jsonRemoteIpPrefix); |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // check remote group ID |
| 122 | JsonNode jsonRemoteGroupId = jsonNode.get(REMOTE_GROUP_ID); |
| 123 | if (jsonRemoteGroupId != null) { |
| 124 | String remoteGroupId = rule.remoteGroupId(); |
| 125 | if (!jsonRemoteGroupId.asText().equals(remoteGroupId)) { |
| 126 | description.appendText("Remote group ID was " + jsonRemoteGroupId); |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void describeTo(Description description) { |
| 136 | description.appendText(rule.toString()); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Factory to allocate an kubevirt security group rule matcher. |
| 141 | * |
| 142 | * @param rule kubevirt security group rule object we are looking for |
| 143 | * @return matcher |
| 144 | */ |
| 145 | public static KubevirtSecurityGroupRuleJsonMatcher |
| 146 | matchesKubevirtSecurityGroupRule(KubevirtSecurityGroupRule rule) { |
| 147 | return new KubevirtSecurityGroupRuleJsonMatcher(rule); |
| 148 | } |
| 149 | } |