blob: de06f6b9197040da189f47a99aaa7f0a46a6a363 [file] [log] [blame]
Jian Lic704b672018-09-04 18:52:53 +09001/*
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 */
16package org.onosproject.openstacknode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.openstacknode.api.NeutronConfig;
22
23/**
24 * Hamcrest matcher for neutron config.
25 */
26public 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 Li92b6f292018-09-06 10:57:18 +090032 private static final String NOVA_METADATA_IP = "novaMetadataIp";
33 private static final String NOVA_METADATA_PORT = "novaMetadataPort";
Jian Lic704b672018-09-04 18:52:53 +090034
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 Li92b6f292018-09-06 10:57:18 +090062 // 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 Lic704b672018-09-04 18:52:53 +090082 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}