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.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.openstacktelemetry.api.DefaultTelemetryConfig; |
| 26 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 27 | |
| 28 | import java.util.Map; |
| 29 | import java.util.stream.IntStream; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | import static org.onlab.util.Tools.nullIsIllegal; |
| 33 | |
| 34 | /** |
| 35 | * Openstack telemetry config codec used for serializing and de-serializing JSON string. |
| 36 | */ |
| 37 | public final class TelemetryConfigJsonCodec extends JsonCodec<TelemetryConfig> { |
| 38 | |
| 39 | private static final String NAME = "name"; |
| 40 | private static final String TYPE = "type"; |
| 41 | private static final String MANUFACTURER = "manufacturer"; |
| 42 | private static final String SW_VERSION = "swVersion"; |
| 43 | private static final String ENABLED = "enabled"; |
| 44 | private static final String PROPS = "props"; |
| 45 | private static final String KEY = "key"; |
| 46 | private static final String VALUE = "value"; |
| 47 | |
| 48 | private static final String MISSING_MESSAGE = " is required in TelemetryConfig"; |
| 49 | |
| 50 | @Override |
| 51 | public ObjectNode encode(TelemetryConfig config, CodecContext context) { |
| 52 | checkNotNull(config, "TelemetryConfig cannot be null"); |
| 53 | |
| 54 | ObjectNode node = context.mapper().createObjectNode() |
| 55 | .put(NAME, config.name()) |
| 56 | .put(TYPE, config.type().name()) |
| 57 | .put(MANUFACTURER, config.manufacturer()) |
| 58 | .put(SW_VERSION, config.swVersion()) |
| 59 | .put(ENABLED, config.enabled()); |
| 60 | |
| 61 | Map<String, String> props = config.properties(); |
| 62 | ArrayNode propsJson = context.mapper().createArrayNode(); |
| 63 | props.forEach((k, v) -> { |
| 64 | ObjectNode propNode = context.mapper().createObjectNode(); |
| 65 | propNode.put(KEY, k); |
| 66 | propNode.put(VALUE, v); |
| 67 | propsJson.add(propNode); |
| 68 | }); |
| 69 | node.set(PROPS, propsJson); |
| 70 | return node; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public TelemetryConfig decode(ObjectNode json, CodecContext context) { |
| 75 | if (json == null || !json.isObject()) { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | // parse name |
| 80 | String name = nullIsIllegal(json.get(NAME), |
| 81 | NAME + MISSING_MESSAGE).asText(); |
| 82 | |
| 83 | // parse type |
| 84 | String type = nullIsIllegal(json.get(TYPE), |
| 85 | TYPE + MISSING_MESSAGE).asText(); |
| 86 | |
| 87 | TelemetryConfig.ConfigType configType = configType(type); |
| 88 | |
| 89 | // parse manufacturer |
| 90 | String manufacturer = nullIsIllegal(json.get(MANUFACTURER).asText(), |
| 91 | MANUFACTURER + MISSING_MESSAGE); |
| 92 | |
| 93 | // parse software version |
| 94 | String swVersion = nullIsIllegal(json.get(SW_VERSION), |
| 95 | SW_VERSION + MISSING_MESSAGE).asText(); |
| 96 | |
| 97 | // parse enabled flag |
| 98 | boolean enabled = nullIsIllegal(json.get(ENABLED), |
| 99 | ENABLED + MISSING_MESSAGE).asBoolean(); |
| 100 | |
| 101 | JsonNode propertiesJson = json.get(PROPS); |
| 102 | Map<String, String> properties = Maps.newConcurrentMap(); |
| 103 | if (propertiesJson != null) { |
| 104 | IntStream.range(0, propertiesJson.size()).forEach(i -> { |
| 105 | ObjectNode propertyJson = get(propertiesJson, i); |
| 106 | properties.put(propertyJson.get(KEY).asText(), |
| 107 | propertyJson.get(VALUE).asText()); |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | return new DefaultTelemetryConfig(name, configType, |
| 112 | ImmutableList.of(), manufacturer, swVersion, enabled, properties); |
| 113 | } |
| 114 | |
| 115 | private TelemetryConfig.ConfigType configType(String type) { |
| 116 | switch (type.toUpperCase()) { |
| 117 | case "KAFKA" : |
| 118 | return TelemetryConfig.ConfigType.KAFKA; |
| 119 | case "GRPC" : |
| 120 | return TelemetryConfig.ConfigType.GRPC; |
| 121 | case "INFLUXDB" : |
| 122 | return TelemetryConfig.ConfigType.INFLUXDB; |
| 123 | case "PROMETHEUS" : |
| 124 | return TelemetryConfig.ConfigType.PROMETHEUS; |
| 125 | case "REST" : |
| 126 | return TelemetryConfig.ConfigType.REST; |
| 127 | default: |
| 128 | return TelemetryConfig.ConfigType.UNKNOWN; |
| 129 | } |
| 130 | } |
| 131 | } |