Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.openstacknode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.openstacknode.api.NeutronConfig; |
| 22 | |
| 23 | /** |
| 24 | * Hamcrest matcher for neutron config. |
| 25 | */ |
| 26 | public final class NeutronConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 27 | |
| 28 | private final NeutronConfig neutronConfig; |
| 29 | |
| 30 | private static final String USE_METADATA_PROXY = "useMetadataProxy"; |
| 31 | private static final String METADATA_PROXY_SECRET = "metadataProxySecret"; |
Jian Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame^] | 32 | private static final String NOVA_METADATA_IP = "novaMetadataIp"; |
| 33 | private static final String NOVA_METADATA_PORT = "novaMetadataPort"; |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 34 | |
| 35 | private NeutronConfigJsonMatcher(NeutronConfig neutronConfig) { |
| 36 | this.neutronConfig = neutronConfig; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 41 | |
| 42 | // check useMetaDataProxy |
| 43 | JsonNode jsonUseMetadataProxy = jsonNode.get(USE_METADATA_PROXY); |
| 44 | if (jsonUseMetadataProxy != null) { |
| 45 | boolean useMetadataProxy = neutronConfig.useMetadataProxy(); |
| 46 | if (jsonUseMetadataProxy.asBoolean() != useMetadataProxy) { |
| 47 | description.appendText("useMetadataProxy was " + jsonUseMetadataProxy); |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // check metadataProxySecret |
| 53 | JsonNode jsonMetadataProxySecret = jsonNode.get(METADATA_PROXY_SECRET); |
| 54 | if (jsonMetadataProxySecret != null) { |
| 55 | String metadataProxySecret = neutronConfig.metadataProxySecret(); |
| 56 | if (!jsonMetadataProxySecret.asText().equals(metadataProxySecret)) { |
| 57 | description.appendText("metadataProxySecret was " + jsonUseMetadataProxy); |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |
Jian Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame^] | 62 | // check NOVA metadata IP |
| 63 | JsonNode jsonNovaMetadataIp = jsonNode.get(NOVA_METADATA_IP); |
| 64 | if (jsonNovaMetadataIp != null) { |
| 65 | String novaMetadataIp = neutronConfig.novaMetadataIp(); |
| 66 | if (!jsonNovaMetadataIp.asText().equals(novaMetadataIp)) { |
| 67 | description.appendText("novaMetadataIp was " + jsonNovaMetadataIp); |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // check NOVA metadata port |
| 73 | JsonNode jsonNovaMetadataPort = jsonNode.get(NOVA_METADATA_PORT); |
| 74 | if (jsonNovaMetadataPort != null) { |
| 75 | Integer novaMetadataPort = neutronConfig.novaMetadataPort(); |
| 76 | if (jsonNovaMetadataPort.asInt() != novaMetadataPort) { |
| 77 | description.appendText("novaMetadataPort was " + jsonNovaMetadataIp); |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void describeTo(Description description) { |
| 87 | description.appendText(neutronConfig.toString()); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Factory to allocate neutron config matcher. |
| 92 | * |
| 93 | * @param config neutron config object we are looking for |
| 94 | * @return matcher |
| 95 | */ |
| 96 | public static NeutronConfigJsonMatcher matchNeutronConfig(NeutronConfig config) { |
| 97 | return new NeutronConfigJsonMatcher(config); |
| 98 | } |
| 99 | } |