Jian Li | fc0b890 | 2021-01-12 16:34:49 +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; |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 22 | import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute; |
| 23 | import org.onosproject.kubevirtnetworking.api.KubevirtIpPool; |
| 24 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 25 | |
| 26 | /** |
| 27 | * Hamcrest matcher for kubevirt network. |
| 28 | */ |
| 29 | public final class KubevirtNetworkJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 30 | |
| 31 | private final KubevirtNetwork network; |
| 32 | private static final String NETWORK_ID = "networkId"; |
| 33 | private static final String TYPE = "type"; |
| 34 | private static final String NAME = "name"; |
| 35 | private static final String MTU = "mtu"; |
| 36 | private static final String SEGMENT_ID = "segmentId"; |
| 37 | private static final String GATEWAY_IP = "gatewayIp"; |
| 38 | private static final String CIDR = "cidr"; |
| 39 | private static final String HOST_ROUTES = "hostRoutes"; |
| 40 | private static final String IP_POOL = "ipPool"; |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 41 | private static final String DNSES = "dnses"; |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 42 | |
| 43 | private KubevirtNetworkJsonMatcher(KubevirtNetwork network) { |
| 44 | this.network = network; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 49 | // check network ID |
| 50 | String jsonNetworkId = jsonNode.get(NETWORK_ID).asText(); |
| 51 | String networkId = network.networkId(); |
| 52 | if (!jsonNetworkId.equals(networkId)) { |
| 53 | description.appendText("network ID was " + jsonNetworkId); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | // check type |
| 58 | String jsonType = jsonNode.get(TYPE).asText(); |
| 59 | String type = network.type().name(); |
| 60 | if (!jsonType.equals(type)) { |
| 61 | description.appendText("network type was " + jsonType); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // check name |
| 66 | String jsonName = jsonNode.get(NAME).asText(); |
| 67 | String name = network.name(); |
| 68 | if (!jsonName.equals(name)) { |
| 69 | description.appendText("network name was " + jsonName); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | // check MTU |
| 74 | int jsonMtu = jsonNode.get(MTU).asInt(); |
| 75 | int mtu = network.mtu(); |
| 76 | if (jsonMtu != mtu) { |
| 77 | description.appendText("network MTU was " + jsonMtu); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | // check gateway IP |
| 82 | String jsonGatewayIp = jsonNode.get(GATEWAY_IP).asText(); |
| 83 | String gatewayIp = network.gatewayIp().toString(); |
| 84 | if (!jsonGatewayIp.equals(gatewayIp)) { |
| 85 | description.appendText("gateway IP was " + jsonGatewayIp); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | // check CIDR |
| 90 | String jsonCidr = jsonNode.get(CIDR).asText(); |
| 91 | String cidr = network.cidr(); |
| 92 | if (!jsonCidr.equals(cidr)) { |
| 93 | description.appendText("CIDR was " + jsonCidr); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | // check segment ID |
| 98 | JsonNode jsonSegmentId = jsonNode.get(SEGMENT_ID); |
| 99 | if (jsonSegmentId != null) { |
| 100 | String segmentId = network.segmentId(); |
| 101 | if (!jsonSegmentId.asText().equals(segmentId)) { |
| 102 | description.appendText("segment ID was " + jsonSegmentId.asText()); |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // check ip pool |
| 108 | JsonNode jsonIpPool = jsonNode.get(IP_POOL); |
| 109 | if (jsonIpPool != null) { |
| 110 | KubevirtIpPool ipPool = network.ipPool(); |
| 111 | KubevirtIpPoolJsonMatcher ipPoolMatcher = |
| 112 | KubevirtIpPoolJsonMatcher.matchesKubevirtIpPool(ipPool); |
| 113 | if (ipPoolMatcher.matches(jsonIpPool)) { |
| 114 | return true; |
| 115 | } else { |
| 116 | description.appendText("IP pool was " + jsonIpPool.toString()); |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // check host routes |
| 122 | JsonNode jsonHostRoutes = jsonNode.get(HOST_ROUTES); |
| 123 | if (jsonHostRoutes != null) { |
| 124 | if (jsonHostRoutes.size() != network.hostRoutes().size()) { |
| 125 | description.appendText("host routes size was " + jsonHostRoutes.size()); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | for (KubevirtHostRoute hostRoute : network.hostRoutes()) { |
| 130 | boolean routeFound = false; |
| 131 | for (int routeIndex = 0; routeIndex < jsonHostRoutes.size(); routeIndex++) { |
| 132 | KubevirtHostRouteJsonMatcher routeMatcher = |
| 133 | KubevirtHostRouteJsonMatcher.matchesKubevirtHostRoute(hostRoute); |
| 134 | if (routeMatcher.matches(jsonHostRoutes.get(routeIndex))) { |
| 135 | routeFound = true; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (!routeFound) { |
| 141 | description.appendText("Host route not found " + hostRoute.toString()); |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 147 | // check dnses |
| 148 | JsonNode jsonDnses = jsonNode.get(DNSES); |
| 149 | if (jsonDnses != null) { |
| 150 | if (jsonDnses.size() != network.dnses().size()) { |
| 151 | description.appendText("DNSes size was " + jsonDnses.size()); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | for (IpAddress dns : network.dnses()) { |
| 157 | boolean dnsFound = false; |
| 158 | for (int dnsIndex = 0; dnsIndex < jsonDnses.size(); dnsIndex++) { |
| 159 | String jsonDns = jsonDnses.get(dnsIndex).asText(); |
| 160 | if (jsonDns.equals(dns.toString())) { |
| 161 | dnsFound = true; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (!dnsFound) { |
| 167 | description.appendText("DNS not found " + dns.toString()); |
| 168 | return false; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void describeTo(Description description) { |
| 178 | description.appendText(network.toString()); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Factory to allocate an kubevirt network matcher. |
| 183 | * |
| 184 | * @param network kubevirt network object we are looking for |
| 185 | * @return matcher |
| 186 | */ |
| 187 | public static KubevirtNetworkJsonMatcher |
| 188 | matchesKubevirtNetwork(KubevirtNetwork network) { |
| 189 | return new KubevirtNetworkJsonMatcher(network); |
| 190 | } |
| 191 | } |