Jian Li | 49109b5 | 2019-01-22 00:17:28 +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.k8snode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
Jian Li | 7709eb4 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Jian Li | b121844 | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 23 | import org.onosproject.k8snode.api.K8sNode; |
| 24 | |
| 25 | /** |
| 26 | * Hamcrest matcher for kubernetes node. |
| 27 | */ |
| 28 | public final class K8sNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 29 | |
| 30 | private final K8sNode node; |
| 31 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 32 | private static final String CLUSTER_NAME = "clusterName"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 33 | private static final String HOSTNAME = "hostname"; |
| 34 | private static final String TYPE = "type"; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 35 | private static final String SEGMENT_ID = "segmentId"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 36 | private static final String MANAGEMENT_IP = "managementIp"; |
| 37 | private static final String DATA_IP = "dataIp"; |
Jian Li | 9bc6777 | 2020-10-07 02:12:33 +0900 | [diff] [blame^] | 38 | private static final String NODE_IP = "nodeIp"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 39 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
| 40 | private static final String STATE = "state"; |
Jian Li | 7709eb4 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 41 | private static final String EXTERNAL_INTF = "externalInterface"; |
| 42 | private static final String EXTERNAL_BRIDGE_IP = "externalBridgeIp"; |
| 43 | private static final String EXTERNAL_GATEWAY_IP = "externalGatewayIp"; |
Jian Li | b121844 | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 44 | private static final String EXTERNAL_GATEWAY_MAC = "externalGatewayMac"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 45 | |
| 46 | private K8sNodeJsonMatcher(K8sNode node) { |
| 47 | this.node = node; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 52 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 53 | // check cluster name |
| 54 | String jsonClusterName = jsonNode.get(CLUSTER_NAME).asText(); |
| 55 | String clusterName = node.clusterName(); |
| 56 | if (!jsonClusterName.equals(clusterName)) { |
| 57 | description.appendText("cluster name was " + jsonClusterName); |
| 58 | return false; |
| 59 | } |
| 60 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 61 | // check hostname |
| 62 | String jsonHostname = jsonNode.get(HOSTNAME).asText(); |
| 63 | String hostname = node.hostname(); |
| 64 | if (!jsonHostname.equals(hostname)) { |
| 65 | description.appendText("hostname was " + jsonHostname); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // check type |
| 70 | String jsonType = jsonNode.get(TYPE).asText(); |
| 71 | String type = node.type().name(); |
| 72 | if (!jsonType.equals(type)) { |
| 73 | description.appendText("type was " + jsonType); |
| 74 | return false; |
| 75 | } |
| 76 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 77 | // check segment ID |
| 78 | JsonNode jsonSegmentId = jsonNode.get(SEGMENT_ID); |
| 79 | if (jsonSegmentId != null) { |
| 80 | int segmentId = jsonSegmentId.asInt(); |
| 81 | if (segmentId != node.segmentId()) { |
| 82 | description.appendText("segment ID was " + segmentId); |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 87 | // check management IP |
| 88 | String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText(); |
| 89 | String mgmtIp = node.managementIp().toString(); |
| 90 | if (!jsonMgmtIp.equals(mgmtIp)) { |
| 91 | description.appendText("management IP was " + jsonMgmtIp); |
| 92 | return false; |
| 93 | } |
| 94 | |
Jian Li | 9bc6777 | 2020-10-07 02:12:33 +0900 | [diff] [blame^] | 95 | // check node IP |
| 96 | String jsonNodeIp = jsonNode.get(NODE_IP).asText(); |
| 97 | String nodeIp = node.nodeIp().toString(); |
| 98 | if (!jsonNodeIp.equals(nodeIp)) { |
| 99 | description.appendText("node IP was " + jsonNodeIp); |
| 100 | return false; |
| 101 | } |
| 102 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 103 | // check integration bridge |
| 104 | JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE); |
| 105 | if (jsonIntgBridge != null) { |
| 106 | String intgBridge = node.intgBridge().toString(); |
| 107 | if (!jsonIntgBridge.asText().equals(intgBridge)) { |
| 108 | description.appendText("integration bridge was " + jsonIntgBridge); |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // check state |
| 114 | String jsonState = jsonNode.get(STATE).asText(); |
| 115 | String state = node.state().name(); |
| 116 | if (!jsonState.equals(state)) { |
| 117 | description.appendText("state was " + jsonState); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | // check data IP |
| 122 | JsonNode jsonDataIp = jsonNode.get(DATA_IP); |
| 123 | if (jsonDataIp != null) { |
| 124 | String dataIp = node.dataIp().toString(); |
| 125 | if (!jsonDataIp.asText().equals(dataIp)) { |
| 126 | description.appendText("Data IP was " + jsonDataIp.asText()); |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | |
Jian Li | 7709eb4 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 131 | // check external interface |
| 132 | JsonNode jsonExtIntf = jsonNode.get(EXTERNAL_INTF); |
| 133 | if (jsonExtIntf != null) { |
| 134 | String extIntf = node.extIntf(); |
| 135 | if (!jsonExtIntf.asText().equals(extIntf)) { |
| 136 | description.appendText("External interface was " + jsonExtIntf.asText()); |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // check external bridge IP |
| 142 | JsonNode jsonExtBridgeIp = jsonNode.get(EXTERNAL_BRIDGE_IP); |
| 143 | if (jsonExtBridgeIp != null) { |
| 144 | IpAddress extBridgeIp = node.extBridgeIp(); |
| 145 | if (!jsonExtBridgeIp.asText().equals(extBridgeIp.toString())) { |
| 146 | description.appendText("External bridge IP was " + jsonExtBridgeIp.asText()); |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // check external gateway IP |
| 152 | JsonNode jsonExtGatewayIp = jsonNode.get(EXTERNAL_GATEWAY_IP); |
| 153 | if (jsonExtGatewayIp != null) { |
| 154 | IpAddress extGatewayIp = node.extGatewayIp(); |
| 155 | if (!jsonExtGatewayIp.asText().equals(extGatewayIp.toString())) { |
| 156 | description.appendText("External gateway IP was " + jsonExtGatewayIp.asText()); |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
Jian Li | b121844 | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 161 | // check external gateway MAC |
| 162 | JsonNode jsonExtGatewayMac = jsonNode.get(EXTERNAL_GATEWAY_MAC); |
| 163 | if (jsonExtGatewayMac != null) { |
| 164 | MacAddress extGatewayMac = node.extGatewayMac(); |
| 165 | if (!jsonExtGatewayMac.asText().equals(extGatewayMac.toString())) { |
| 166 | description.appendText("External gateway MAC was " + jsonExtGatewayMac.asText()); |
| 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void describeTo(Description description) { |
| 176 | description.appendText(node.toString()); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Factory to allocate an kubernetes node matcher. |
| 181 | * |
| 182 | * @param node kubernetes node object we are looking for |
| 183 | * @return matcher |
| 184 | */ |
| 185 | public static K8sNodeJsonMatcher matchesK8sNode(K8sNode node) { |
| 186 | return new K8sNodeJsonMatcher(node); |
| 187 | } |
| 188 | } |