Jian Li | 3defa84 | 2019-02-12 00:31:35 +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 | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 21 | import org.onosproject.k8snode.api.HostNodesInfo; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 22 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 23 | |
| 24 | /** |
| 25 | * Hamcrest matcher for kubernetes API config. |
| 26 | */ |
| 27 | public final class K8sApiConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 28 | |
| 29 | private final K8sApiConfig k8sApiConfig; |
| 30 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 31 | private static final String CLUSTER_NAME = "clusterName"; |
| 32 | private static final String SEGMENT_ID = "segmentId"; |
| 33 | private static final String EXT_NETWORK_CIDR = "extNetworkCidr"; |
| 34 | private static final String MODE = "mode"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 35 | private static final String SCHEME = "scheme"; |
| 36 | private static final String IP_ADDRESS = "ipAddress"; |
| 37 | private static final String PORT = "port"; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 38 | private static final String STATE = "state"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 39 | private static final String TOKEN = "token"; |
| 40 | private static final String CA_CERT_DATA = "caCertData"; |
| 41 | private static final String CLIENT_CERT_DATA = "clientCertData"; |
| 42 | private static final String CLIENT_KEY_DATA = "clientKeyData"; |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 43 | private static final String HOST_NODES_INFO = "hostNodesInfo"; |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame^] | 44 | private static final String DVR = "dvr"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 45 | |
| 46 | private K8sApiConfigJsonMatcher(K8sApiConfig k8sApiConfig) { |
| 47 | this.k8sApiConfig = k8sApiConfig; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 52 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 53 | // check cluster name |
| 54 | String jsonClusterName = jsonNode.get(CLUSTER_NAME).asText(); |
| 55 | String clusterName = k8sApiConfig.clusterName(); |
| 56 | if (!jsonClusterName.equals(clusterName)) { |
| 57 | description.appendText("cluster name was " + jsonClusterName); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // check segment ID |
| 62 | int jsonSegmentId = jsonNode.get(SEGMENT_ID).asInt(); |
| 63 | int segmentId = k8sApiConfig.segmentId(); |
| 64 | if (jsonSegmentId != segmentId) { |
| 65 | description.appendText("Segment ID was " + jsonSegmentId); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // check mode |
| 70 | String jsonMode = jsonNode.get(MODE).asText(); |
| 71 | String mode = k8sApiConfig.mode().name(); |
| 72 | if (!jsonMode.equals(mode)) { |
| 73 | description.appendText("mode was " + jsonMode); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // check external network CIDR |
| 78 | JsonNode jsonCidr = jsonNode.get(EXT_NETWORK_CIDR); |
| 79 | String cidr = k8sApiConfig.extNetworkCidr().toString(); |
| 80 | if (jsonCidr != null) { |
| 81 | if (!jsonCidr.asText().equals(cidr)) { |
| 82 | description.appendText("External network CIDR was " + jsonCidr); |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 87 | // check scheme |
| 88 | String jsonScheme = jsonNode.get(SCHEME).asText(); |
| 89 | String scheme = k8sApiConfig.scheme().name(); |
| 90 | if (!jsonScheme.equals(scheme)) { |
| 91 | description.appendText("scheme was " + jsonScheme); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | // check IP address |
| 96 | String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText(); |
| 97 | String ipAddress = k8sApiConfig.ipAddress().toString(); |
| 98 | if (!jsonIpAddress.equals(ipAddress)) { |
| 99 | description.appendText("ipAddress was " + jsonIpAddress); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // check port |
| 104 | int jsonPort = jsonNode.get(PORT).asInt(); |
| 105 | int port = k8sApiConfig.port(); |
| 106 | if (jsonPort != port) { |
| 107 | description.appendText("port was " + jsonPort); |
| 108 | return false; |
| 109 | } |
| 110 | |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 111 | // check state |
| 112 | JsonNode jsonState = jsonNode.get(STATE); |
| 113 | String state = k8sApiConfig.state().name(); |
| 114 | if (jsonState != null) { |
| 115 | if (!jsonState.asText().equals(state)) { |
| 116 | description.appendText("state was " + jsonState); |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame^] | 121 | // check DVR |
| 122 | JsonNode jsonDvr = jsonNode.get(DVR); |
| 123 | boolean dvr = k8sApiConfig.dvr(); |
| 124 | if (jsonDvr != null) { |
| 125 | if (jsonDvr.asBoolean() != dvr) { |
| 126 | description.appendText("DVR was " + jsonDvr); |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 131 | // check token |
| 132 | JsonNode jsonToken = jsonNode.get(TOKEN); |
| 133 | String token = k8sApiConfig.token(); |
| 134 | if (jsonToken != null) { |
| 135 | if (!jsonToken.asText().equals(token)) { |
| 136 | description.appendText("token was " + jsonToken); |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // check caCertData |
| 142 | JsonNode jsonCaCertData = jsonNode.get(CA_CERT_DATA); |
| 143 | String caCertData = k8sApiConfig.caCertData(); |
| 144 | if (jsonCaCertData != null) { |
| 145 | if (!jsonCaCertData.asText().equals(caCertData)) { |
| 146 | description.appendText("caCertData was " + jsonCaCertData); |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // check clientCertData |
| 152 | JsonNode jsonClientCertData = jsonNode.get(CLIENT_CERT_DATA); |
| 153 | String clientCertData = k8sApiConfig.clientCertData(); |
| 154 | |
| 155 | if (jsonClientCertData != null) { |
| 156 | if (!jsonClientCertData.asText().equals(clientCertData)) { |
| 157 | description.appendText("clientCertData was " + jsonClientCertData); |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // check clientKeyData |
| 163 | JsonNode jsonClientKeyData = jsonNode.get(CLIENT_KEY_DATA); |
| 164 | String clientKeyData = k8sApiConfig.clientKeyData(); |
| 165 | |
| 166 | if (jsonClientKeyData != null) { |
| 167 | if (!jsonClientKeyData.asText().equals(clientKeyData)) { |
| 168 | description.appendText("clientKeyData was " + jsonClientKeyData); |
| 169 | return false; |
| 170 | } |
| 171 | } |
| 172 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 173 | // check hostNodesInfo size |
| 174 | JsonNode jsonInfos = jsonNode.get(HOST_NODES_INFO); |
| 175 | if (jsonInfos.size() != k8sApiConfig.infos().size()) { |
| 176 | description.appendText("Info size was " + jsonInfos.size()); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | // check info |
| 181 | for (HostNodesInfo info : k8sApiConfig.infos()) { |
| 182 | boolean infoFound = false; |
| 183 | for (int infoIndex = 0; infoIndex < jsonInfos.size(); infoIndex++) { |
| 184 | HostNodesInfoJsonMatcher infoMatcher = HostNodesInfoJsonMatcher.matchesHostNodesInfo(info); |
| 185 | if (infoMatcher.matches(jsonInfos.get(infoIndex))) { |
| 186 | infoFound = true; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | if (!infoFound) { |
| 191 | description.appendText("Info not found " + info.toString()); |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 196 | return true; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void describeTo(Description description) { |
| 201 | description.appendText(k8sApiConfig.toString()); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Factory to allocate an k8sApiConfig matcher. |
| 206 | * |
| 207 | * @param config k8sApiConfig object we are looking for |
| 208 | * @return matcher |
| 209 | */ |
| 210 | public static K8sApiConfigJsonMatcher matchesK8sApiConfig(K8sApiConfig config) { |
| 211 | return new K8sApiConfigJsonMatcher(config); |
| 212 | } |
| 213 | } |