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