Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snetworking.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.k8snetworking.api.K8sPort; |
| 22 | |
| 23 | /** |
| 24 | * Hamcrest matcher for kubernetes port. |
| 25 | */ |
| 26 | public final class K8sPortJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 27 | |
| 28 | private final K8sPort port; |
| 29 | |
| 30 | private static final String NETWORK_ID = "networkId"; |
| 31 | private static final String PORT_ID = "portId"; |
| 32 | private static final String MAC_ADDRESS = "macAddress"; |
| 33 | private static final String IP_ADDRESS = "ipAddress"; |
| 34 | private static final String DEVICE_ID = "deviceId"; |
| 35 | private static final String PORT_NUMBER = "portNumber"; |
| 36 | private static final String STATE = "state"; |
| 37 | |
| 38 | private K8sPortJsonMatcher(K8sPort port) { |
| 39 | this.port = port; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 44 | |
| 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 port ID |
| 54 | String jsonPortId = jsonNode.get(PORT_ID).asText(); |
| 55 | String portId = port.portId(); |
| 56 | if (!jsonPortId.equals(portId)) { |
| 57 | description.appendText("port ID was " + jsonPortId); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // check MAC address |
| 62 | String jsonMacAddress = jsonNode.get(MAC_ADDRESS).asText(); |
| 63 | String macAddress = port.macAddress().toString(); |
| 64 | if (!jsonMacAddress.equals(macAddress)) { |
| 65 | description.appendText("MAC address was " + jsonMacAddress); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // check IP address |
| 70 | String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText(); |
| 71 | String ipAddress = port.ipAddress().toString(); |
| 72 | if (!jsonIpAddress.equals(ipAddress)) { |
| 73 | description.appendText("IP address was " + jsonIpAddress); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // check device ID |
| 78 | String jsonDeviceId = jsonNode.get(DEVICE_ID).asText(); |
| 79 | String deviceId = port.deviceId().toString(); |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 80 | if (jsonDeviceId != null) { |
| 81 | if (!jsonDeviceId.equals(deviceId)) { |
| 82 | description.appendText("device ID was " + jsonDeviceId); |
| 83 | return false; |
| 84 | } |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // check port number |
| 88 | String jsonPortNumber = jsonNode.get(PORT_NUMBER).asText(); |
| 89 | String portNumber = port.portNumber().toString(); |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 90 | if (jsonPortNumber != null) { |
| 91 | if (!jsonPortNumber.equals(portNumber)) { |
| 92 | description.appendText("port number was " + jsonPortNumber); |
| 93 | return false; |
| 94 | } |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // check state |
| 98 | String jsonState = jsonNode.get(STATE).asText(); |
| 99 | String state = port.state().name(); |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 100 | if (jsonState != null) { |
| 101 | if (!jsonState.equals(state)) { |
| 102 | description.appendText("state was " + jsonState); |
| 103 | return false; |
| 104 | } |
Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void describeTo(Description description) { |
| 112 | description.appendText(port.toString()); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Factory to allocate an kubernetes port matcher. |
| 117 | * |
| 118 | * @param port kubernetes port object we are looking for |
| 119 | * @return matcher |
| 120 | */ |
| 121 | public static K8sPortJsonMatcher matchesK8sPort(K8sPort port) { |
| 122 | return new K8sPortJsonMatcher(port); |
| 123 | } |
| 124 | } |