Jian Li | daa7d6a | 2021-04-13 17:22:56 +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 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 22 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule; |
| 23 | |
| 24 | /** |
| 25 | * Hamcrest matcher for kubevirt load balancer. |
| 26 | */ |
| 27 | public final class KubevirtLoadBalancerRuleJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 28 | |
| 29 | private final KubevirtLoadBalancerRule rule; |
| 30 | |
| 31 | private static final String PROTOCOL = "protocol"; |
| 32 | private static final String PORT_RANGE_MAX = "portRangeMax"; |
| 33 | private static final String PORT_RANGE_MIN = "portRangeMin"; |
| 34 | |
| 35 | private KubevirtLoadBalancerRuleJsonMatcher(KubevirtLoadBalancerRule rule) { |
| 36 | this.rule = rule; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 41 | // check protocol |
| 42 | JsonNode jsonProtocol = jsonNode.get(PROTOCOL); |
| 43 | if (jsonProtocol != null) { |
| 44 | String protocol = rule.protocol(); |
| 45 | if (!jsonProtocol.asText().equals(protocol)) { |
| 46 | description.appendText("Protocol was " + jsonProtocol); |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // check port range max |
| 52 | JsonNode jsonPortRangeMax = jsonNode.get(PORT_RANGE_MAX); |
| 53 | if (jsonPortRangeMax != null) { |
| 54 | int portRangeMax = rule.portRangeMax(); |
| 55 | if (portRangeMax != jsonPortRangeMax.asInt()) { |
| 56 | description.appendText("PortRangeMax was " + jsonPortRangeMax); |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // check port range min |
| 62 | JsonNode jsonPortRangeMin = jsonNode.get(PORT_RANGE_MIN); |
| 63 | if (jsonPortRangeMin != null) { |
| 64 | int portRangeMin = rule.portRangeMin(); |
| 65 | if (portRangeMin != jsonPortRangeMin.asInt()) { |
| 66 | description.appendText("PortRangeMin was " + jsonPortRangeMin); |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void describeTo(Description description) { |
| 76 | description.appendText(rule.toString()); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Factory to allocate an kubevirt load balancer rule matcher. |
| 81 | * |
| 82 | * @param rule kubevirt load balancer rule object we are looking for |
| 83 | * @return matcher |
| 84 | */ |
| 85 | public static KubevirtLoadBalancerRuleJsonMatcher |
| 86 | matchesKubevirtLoadBalancerRule(KubevirtLoadBalancerRule rule) { |
| 87 | return new KubevirtLoadBalancerRuleJsonMatcher(rule); |
| 88 | } |
| 89 | } |