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