Jian Li | 69600e0 | 2018-12-24 13:21:18 +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.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.LinkedList; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | import java.util.Objects; |
| 27 | import java.util.Queue; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | import static com.google.common.collect.ImmutableMap.copyOf; |
| 33 | |
| 34 | /** |
| 35 | * Implementation of TelemetryConfig. |
| 36 | */ |
| 37 | public final class DefaultTelemetryConfig implements TelemetryConfig { |
| 38 | private final String name; |
| 39 | private final ConfigType type; |
| 40 | private final List<TelemetryConfig> parents; |
| 41 | |
| 42 | private final String manufacturer; |
| 43 | private final String swVersion; |
| 44 | private final boolean enabled; |
| 45 | |
| 46 | private final Map<String, String> properties; |
| 47 | |
| 48 | /** |
| 49 | * Creates a configuration with the specified name. |
| 50 | * |
| 51 | * @param name configuration name |
| 52 | * @param type configuration type |
| 53 | * @param parents optional parent configurations |
| 54 | * @param manufacturer off-platform application manufacturer |
| 55 | * @param swVersion off-platform application software version |
| 56 | * @param enabled service enable flag |
| 57 | * @param properties properties for telemetry configuration |
| 58 | */ |
| 59 | public DefaultTelemetryConfig(String name, ConfigType type, |
| 60 | List<TelemetryConfig> parents, |
| 61 | String manufacturer, String swVersion, |
| 62 | boolean enabled, Map<String, String> properties) { |
| 63 | this.name = checkNotNull(name, "Name cannot be null"); |
| 64 | this.type = checkNotNull(type, "type cannot be null"); |
| 65 | this.parents = parents == null ? ImmutableList.of() : ImmutableList.copyOf(parents); |
| 66 | this.manufacturer = checkNotNull(manufacturer, "Manufacturer cannot be null"); |
| 67 | this.swVersion = checkNotNull(swVersion, "SW version cannot be null"); |
| 68 | this.properties = copyOf(checkNotNull(properties, "Properties cannot be null")); |
| 69 | this.enabled = enabled; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public String name() { |
| 74 | return name; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public ConfigType type() { |
| 79 | return type; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public List<TelemetryConfig> parents() { |
| 84 | if (parents == null) { |
| 85 | return ImmutableList.of(); |
| 86 | } else { |
| 87 | return ImmutableList.copyOf(parents); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public String manufacturer() { |
| 93 | return manufacturer; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String swVersion() { |
| 98 | return swVersion; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean enabled() { |
| 103 | return enabled; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public Map<String, String> properties() { |
| 108 | if (properties == null) { |
| 109 | return ImmutableMap.of(); |
| 110 | } else { |
| 111 | return ImmutableMap.copyOf(properties); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String getProperty(String name) { |
| 117 | Queue<TelemetryConfig> queue = new LinkedList<>(); |
| 118 | queue.add(this); |
| 119 | while (!queue.isEmpty()) { |
| 120 | TelemetryConfig config = queue.remove(); |
| 121 | String property = config.properties().get(name); |
| 122 | if (property != null) { |
| 123 | return property; |
| 124 | } else if (config.parents() != null) { |
| 125 | queue.addAll(config.parents()); |
| 126 | } |
| 127 | } |
| 128 | return null; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public TelemetryConfig merge(TelemetryConfig other) { |
| 133 | // merge the properties |
| 134 | ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); |
| 135 | properties.putAll(other.properties()); |
| 136 | |
| 137 | // remove duplicated properties from this configuration and merge |
| 138 | this.properties().entrySet().stream() |
| 139 | .filter(e -> !other.properties().containsKey(e.getKey())) |
| 140 | .forEach(properties::put); |
| 141 | |
| 142 | List<TelemetryConfig> completeParents = new ArrayList<>(); |
| 143 | |
| 144 | if (parents != null) { |
| 145 | parents.forEach(parent -> other.parents().forEach(otherParent -> { |
| 146 | if (otherParent.name().equals(parent.name())) { |
| 147 | completeParents.add(parent.merge(otherParent)); |
| 148 | } else if (!completeParents.contains(otherParent)) { |
| 149 | completeParents.add(otherParent); |
| 150 | } else if (!completeParents.contains(parent)) { |
| 151 | completeParents.add(parent); |
| 152 | } |
| 153 | })); |
| 154 | } |
| 155 | |
| 156 | return new DefaultTelemetryConfig(name, type, |
| 157 | !completeParents.isEmpty() ? completeParents : other.parents(), |
| 158 | manufacturer, swVersion, enabled, properties.build()); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Set<String> keys() { |
| 163 | return properties.keySet(); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public String value(String key) { |
| 168 | return properties.get(key); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public String toString() { |
| 173 | return toStringHelper(this) |
| 174 | .add("name", name) |
| 175 | .add("type", type) |
| 176 | .add("parents", parents) |
| 177 | .add("manufacturer", manufacturer) |
| 178 | .add("swVersion", swVersion) |
| 179 | .add("enabled", enabled) |
| 180 | .add("properties", properties) |
| 181 | .toString(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public boolean equals(Object configToBeCompared) { |
| 186 | if (this == configToBeCompared) { |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | if (configToBeCompared == null || getClass() != configToBeCompared.getClass()) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | DefaultTelemetryConfig telemetryConfig = |
| 195 | (DefaultTelemetryConfig) configToBeCompared; |
| 196 | return name.equals(telemetryConfig.name()); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public int hashCode() { |
| 201 | return Objects.hashCode(name); |
| 202 | } |
| 203 | } |