Jian Li | 7fe7eaf | 2018-12-31 17:00:33 +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.openstacktelemetry.codec.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 22 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 23 | |
| 24 | /** |
| 25 | * Hamcrest matcher for TelemetryConfig. |
| 26 | */ |
| 27 | public final class TelemetryConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 28 | |
| 29 | private final TelemetryConfig telemetryConfig; |
| 30 | |
| 31 | private static final String NAME = "name"; |
| 32 | private static final String TYPE = "type"; |
| 33 | private static final String MANUFACTURER = "manufacturer"; |
| 34 | private static final String SW_VERSION = "swVersion"; |
| 35 | private static final String ENABLED = "enabled"; |
| 36 | private static final String PROPS = "props"; |
| 37 | private static final String KEY = "key"; |
| 38 | private static final String VALUE = "value"; |
| 39 | |
| 40 | private TelemetryConfigJsonMatcher(TelemetryConfig telemetryConfig) { |
| 41 | this.telemetryConfig = telemetryConfig; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 46 | |
| 47 | // check name |
| 48 | String jsonName = jsonNode.get(NAME).asText(); |
| 49 | String name = telemetryConfig.name(); |
| 50 | if (!jsonName.equals(name)) { |
| 51 | description.appendText("name was " + jsonName); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | // check type |
| 56 | String jsonType = jsonNode.get(TYPE).asText(); |
| 57 | String type = telemetryConfig.type().name(); |
| 58 | if (!jsonType.equalsIgnoreCase(type)) { |
| 59 | description.appendText("type was " + jsonType); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // check manufacturer |
| 64 | String jsonManufacturer = jsonNode.get(MANUFACTURER).asText(); |
| 65 | String manufacturer = telemetryConfig.manufacturer(); |
| 66 | if (!jsonManufacturer.equals(manufacturer)) { |
| 67 | description.appendText("manufacturer was " + jsonManufacturer); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // check software version |
| 72 | String jsonSwVersion = jsonNode.get(SW_VERSION).asText(); |
| 73 | String swVersion = telemetryConfig.swVersion(); |
| 74 | if (!jsonSwVersion.equals(swVersion)) { |
| 75 | description.appendText("SW version was " + jsonSwVersion); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // check enabled |
| 80 | JsonNode jsonEnabled = jsonNode.get(ENABLED); |
| 81 | boolean enabled = telemetryConfig.enabled(); |
| 82 | if (jsonEnabled == null || jsonEnabled.asBoolean() != enabled) { |
| 83 | description.appendText("Enabled was " + jsonEnabled); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // check properties |
| 88 | JsonNode jsonProperties = jsonNode.get(PROPS); |
| 89 | if (jsonProperties != null) { |
| 90 | if (jsonProperties.size() != telemetryConfig.properties().size()) { |
| 91 | description.appendText("properties size was " + jsonProperties.size()); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | for (String key : telemetryConfig.properties().keySet()) { |
| 96 | boolean keyFound = false; |
| 97 | boolean valueFound = false; |
| 98 | String value = telemetryConfig.properties().get(key); |
| 99 | for (int keyIndex = 0; keyIndex < jsonProperties.size(); keyIndex++) { |
| 100 | ObjectNode jsonProperty = get(jsonProperties, keyIndex); |
| 101 | JsonNode jsonKey = jsonProperty.get(KEY); |
| 102 | JsonNode jsonValue = jsonProperty.get(VALUE); |
| 103 | |
| 104 | if (jsonKey != null && jsonValue != null) { |
| 105 | if (jsonKey.asText().equals(key)) { |
| 106 | keyFound = true; |
| 107 | } |
| 108 | |
| 109 | if (jsonValue.asText().equals(value)) { |
| 110 | valueFound = true; |
| 111 | } |
| 112 | |
| 113 | if (keyFound && valueFound) { |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (!keyFound) { |
| 120 | description.appendText("Property key not found " + key); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | if (!valueFound) { |
| 125 | description.appendText("Property value not found " + value); |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void describeTo(Description description) { |
| 136 | description.appendText(telemetryConfig.toString()); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Factory to allocate an flow info matcher. |
| 141 | * |
| 142 | * @param telemetryConfig telemetry config object we are looking for |
| 143 | * @return matcher |
| 144 | */ |
| 145 | public static TelemetryConfigJsonMatcher |
| 146 | matchesTelemetryConfig(TelemetryConfig telemetryConfig) { |
| 147 | return new TelemetryConfigJsonMatcher(telemetryConfig); |
| 148 | } |
| 149 | |
| 150 | private static ObjectNode get(JsonNode parent, int childIndex) { |
| 151 | JsonNode node = parent.path(childIndex); |
| 152 | return node.isObject() && !node.isNull() ? (ObjectNode) node : null; |
| 153 | } |
| 154 | } |