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; |
| 21 | import org.onosproject.k8snode.api.K8sNode; |
| 22 | |
| 23 | /** |
| 24 | * Hamcrest matcher for kubernetes node. |
| 25 | */ |
| 26 | public final class K8sNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 27 | |
| 28 | private final K8sNode node; |
| 29 | |
| 30 | private static final String HOSTNAME = "hostname"; |
| 31 | private static final String TYPE = "type"; |
| 32 | private static final String MANAGEMENT_IP = "managementIp"; |
| 33 | private static final String DATA_IP = "dataIp"; |
| 34 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
| 35 | private static final String STATE = "state"; |
| 36 | |
| 37 | private K8sNodeJsonMatcher(K8sNode node) { |
| 38 | this.node = node; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 43 | |
| 44 | // check hostname |
| 45 | String jsonHostname = jsonNode.get(HOSTNAME).asText(); |
| 46 | String hostname = node.hostname(); |
| 47 | if (!jsonHostname.equals(hostname)) { |
| 48 | description.appendText("hostname was " + jsonHostname); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | // check type |
| 53 | String jsonType = jsonNode.get(TYPE).asText(); |
| 54 | String type = node.type().name(); |
| 55 | if (!jsonType.equals(type)) { |
| 56 | description.appendText("type was " + jsonType); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | // check management IP |
| 61 | String jsonMgmtIp = jsonNode.get(MANAGEMENT_IP).asText(); |
| 62 | String mgmtIp = node.managementIp().toString(); |
| 63 | if (!jsonMgmtIp.equals(mgmtIp)) { |
| 64 | description.appendText("management IP was " + jsonMgmtIp); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | // check integration bridge |
| 69 | JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE); |
| 70 | if (jsonIntgBridge != null) { |
| 71 | String intgBridge = node.intgBridge().toString(); |
| 72 | if (!jsonIntgBridge.asText().equals(intgBridge)) { |
| 73 | description.appendText("integration bridge was " + jsonIntgBridge); |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // check state |
| 79 | String jsonState = jsonNode.get(STATE).asText(); |
| 80 | String state = node.state().name(); |
| 81 | if (!jsonState.equals(state)) { |
| 82 | description.appendText("state was " + jsonState); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // check data IP |
| 87 | JsonNode jsonDataIp = jsonNode.get(DATA_IP); |
| 88 | if (jsonDataIp != null) { |
| 89 | String dataIp = node.dataIp().toString(); |
| 90 | if (!jsonDataIp.asText().equals(dataIp)) { |
| 91 | description.appendText("Data IP was " + jsonDataIp.asText()); |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void describeTo(Description description) { |
| 101 | description.appendText(node.toString()); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Factory to allocate an kubernetes node matcher. |
| 106 | * |
| 107 | * @param node kubernetes node object we are looking for |
| 108 | * @return matcher |
| 109 | */ |
| 110 | public static K8sNodeJsonMatcher matchesK8sNode(K8sNode node) { |
| 111 | return new K8sNodeJsonMatcher(node); |
| 112 | } |
| 113 | } |