Jian Li | ca20b71 | 2021-01-18 00:19:31 +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.IpAddress; |
| 22 | import org.onosproject.kubevirtnetworking.api.KubevirtPort; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | |
| 26 | /** |
| 27 | * Hamcrest matcher for kubevirt port. |
| 28 | */ |
| 29 | public final class KubevirtPortJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 30 | |
| 31 | private final KubevirtPort port; |
| 32 | |
| 33 | private static final String NETWORK_ID = "networkId"; |
| 34 | private static final String MAC_ADDRESS = "macAddress"; |
| 35 | private static final String IP_ADDRESS = "ipAddress"; |
| 36 | private static final String DEVICE_ID = "deviceId"; |
| 37 | private static final String PORT_NUMBER = "portNumber"; |
| 38 | |
| 39 | private KubevirtPortJsonMatcher(KubevirtPort port) { |
| 40 | this.port = port; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 45 | // check network ID |
| 46 | String jsonNetworkId = jsonNode.get(NETWORK_ID).asText(); |
| 47 | String networkId = port.networkId(); |
| 48 | if (!jsonNetworkId.equals(networkId)) { |
| 49 | description.appendText("network ID was " + jsonNetworkId); |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | // check MAC address |
| 54 | String jsonMacAddress = jsonNode.get(MAC_ADDRESS).asText(); |
| 55 | String macAddress = port.macAddress().toString(); |
| 56 | if (!jsonMacAddress.equals(macAddress)) { |
| 57 | description.appendText("MAC address was " + jsonMacAddress); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // check IP address |
| 62 | JsonNode jsonIpAddress = jsonNode.get(IP_ADDRESS); |
| 63 | if (jsonIpAddress != null) { |
| 64 | IpAddress ipAddress = port.ipAddress(); |
| 65 | if (!jsonIpAddress.asText().equals(ipAddress.toString())) { |
| 66 | description.appendText("IP address was " + jsonIpAddress.asText()); |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // check device ID |
| 72 | JsonNode jsonDeviceId = jsonNode.get(DEVICE_ID); |
| 73 | if (jsonDeviceId != null) { |
| 74 | DeviceId deviceId = port.deviceId(); |
| 75 | if (!jsonDeviceId.asText().equals(deviceId.toString())) { |
| 76 | description.appendText("Device ID was " + jsonDeviceId.asText()); |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // check port number |
| 82 | JsonNode jsonPortNumber = jsonNode.get(PORT_NUMBER); |
| 83 | if (jsonPortNumber != null) { |
| 84 | PortNumber portNUmber = port.portNumber(); |
| 85 | if (!jsonPortNumber.asText().equals(portNUmber.toString())) { |
| 86 | description.appendText("Port number was " + jsonPortNumber.asText()); |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void describeTo(Description description) { |
| 96 | description.appendText(port.toString()); |
| 97 | } |
| 98 | /** |
| 99 | * Factory to allocate an kubevirt port matcher. |
| 100 | * |
| 101 | * @param port kubevirt port object we are looking for |
| 102 | * @return matcher |
| 103 | */ |
| 104 | public static KubevirtPortJsonMatcher matchesKubevirtPort(KubevirtPort port) { |
| 105 | return new KubevirtPortJsonMatcher(port); |
| 106 | } |
| 107 | } |