Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.kubevirtnode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 22 | |
| 23 | /** |
| 24 | * Hamcrest matcher for KubeVirt API config. |
| 25 | */ |
| 26 | public final class KubevirtApiConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 27 | |
| 28 | private final KubevirtApiConfig kubevirtApiConfig; |
| 29 | |
| 30 | private static final String SCHEME = "scheme"; |
| 31 | private static final String IP_ADDRESS = "ipAddress"; |
| 32 | private static final String PORT = "port"; |
| 33 | private static final String STATE = "state"; |
| 34 | private static final String TOKEN = "token"; |
| 35 | private static final String CA_CERT_DATA = "caCertData"; |
| 36 | private static final String CLIENT_CERT_DATA = "clientCertData"; |
| 37 | private static final String CLIENT_KEY_DATA = "clientKeyData"; |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 38 | private static final String SERVICE_FQDN = "serviceFqdn"; |
Jian Li | 3c3b163 | 2021-04-28 17:24:56 +0900 | [diff] [blame^] | 39 | private static final String API_SERVER_FQDN = "apiServerFqdn"; |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 40 | |
| 41 | private KubevirtApiConfigJsonMatcher(KubevirtApiConfig kubevirtApiConfig) { |
| 42 | this.kubevirtApiConfig = kubevirtApiConfig; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 47 | // check scheme |
| 48 | String jsonScheme = jsonNode.get(SCHEME).asText(); |
| 49 | String scheme = kubevirtApiConfig.scheme().name(); |
| 50 | if (!jsonScheme.equals(scheme)) { |
| 51 | description.appendText("scheme was " + jsonScheme); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | // check IP address |
| 56 | String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText(); |
| 57 | String ipAddress = kubevirtApiConfig.ipAddress().toString(); |
| 58 | if (!jsonIpAddress.equals(ipAddress)) { |
| 59 | description.appendText("ipAddress was " + jsonIpAddress); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // check port |
| 64 | int jsonPort = jsonNode.get(PORT).asInt(); |
| 65 | int port = kubevirtApiConfig.port(); |
| 66 | if (jsonPort != port) { |
| 67 | description.appendText("port was " + jsonPort); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // check state |
| 72 | JsonNode jsonState = jsonNode.get(STATE); |
| 73 | String state = kubevirtApiConfig.state().name(); |
| 74 | if (jsonState != null) { |
| 75 | if (!jsonState.asText().equals(state)) { |
| 76 | description.appendText("state was " + jsonState); |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // check token |
| 82 | JsonNode jsonToken = jsonNode.get(TOKEN); |
| 83 | String token = kubevirtApiConfig.token(); |
| 84 | if (jsonToken != null) { |
| 85 | if (!jsonToken.asText().equals(token)) { |
| 86 | description.appendText("token was " + jsonToken); |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // check caCertData |
| 92 | JsonNode jsonCaCertData = jsonNode.get(CA_CERT_DATA); |
| 93 | String caCertData = kubevirtApiConfig.caCertData(); |
| 94 | if (jsonCaCertData != null) { |
| 95 | if (!jsonCaCertData.asText().equals(caCertData)) { |
| 96 | description.appendText("caCertData was " + jsonCaCertData); |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // check clientCertData |
| 102 | JsonNode jsonClientCertData = jsonNode.get(CLIENT_CERT_DATA); |
| 103 | String clientCertData = kubevirtApiConfig.clientCertData(); |
| 104 | |
| 105 | if (jsonClientCertData != null) { |
| 106 | if (!jsonClientCertData.asText().equals(clientCertData)) { |
| 107 | description.appendText("clientCertData was " + jsonClientCertData); |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // check clientKeyData |
| 113 | JsonNode jsonClientKeyData = jsonNode.get(CLIENT_KEY_DATA); |
| 114 | String clientKeyData = kubevirtApiConfig.clientKeyData(); |
| 115 | |
| 116 | if (jsonClientKeyData != null) { |
| 117 | if (!jsonClientKeyData.asText().equals(clientKeyData)) { |
| 118 | description.appendText("clientKeyData was " + jsonClientKeyData); |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | |
Jian Li | 94b6d16 | 2021-04-15 17:09:11 +0900 | [diff] [blame] | 123 | // service FQDN |
| 124 | JsonNode jsonServiceFqdn = jsonNode.get(SERVICE_FQDN); |
| 125 | String serviceFqdn = kubevirtApiConfig.serviceFqdn(); |
| 126 | |
| 127 | if (jsonServiceFqdn != null) { |
| 128 | if (!jsonServiceFqdn.asText().equals(serviceFqdn)) { |
| 129 | description.appendText("serviceFqdn was " + jsonServiceFqdn); |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | |
Jian Li | 3c3b163 | 2021-04-28 17:24:56 +0900 | [diff] [blame^] | 134 | // API server FQDN |
| 135 | JsonNode jsonApiServerFqdn = jsonNode.get(API_SERVER_FQDN); |
| 136 | String apiServerFqdn = kubevirtApiConfig.apiServerFqdn(); |
| 137 | |
| 138 | if (jsonApiServerFqdn != null) { |
| 139 | if (!jsonApiServerFqdn.asText().equals(apiServerFqdn)) { |
| 140 | description.appendText("apiServerFqdn was " + jsonApiServerFqdn); |
| 141 | return false; |
| 142 | } |
| 143 | } |
| 144 | |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void describeTo(Description description) { |
| 150 | description.appendText(kubevirtApiConfig.toString()); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Factory to allocate an kubevirtApiConfig matcher. |
| 155 | * |
| 156 | * @param config kubevirtApiConfig object we are looking for |
| 157 | * @return matcher |
| 158 | */ |
| 159 | public static KubevirtApiConfigJsonMatcher matchesKubevirtApiConfig(KubevirtApiConfig config) { |
| 160 | return new KubevirtApiConfigJsonMatcher(config); |
| 161 | } |
| 162 | } |