Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.soam.web; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.time.Duration; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementStat; |
| 26 | |
| 27 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 28 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 29 | |
| 30 | /** |
| 31 | * Encode and decode to/from JSON to DelayMeasurementStat object. |
| 32 | */ |
| 33 | public class DelayMeasurementStatCodec extends JsonCodec<DelayMeasurementStat> { |
| 34 | |
| 35 | private static final String LOWER_LIMIT = "lowerLimit"; |
| 36 | private static final String COUNT = "count"; |
| 37 | private static final String BINS = "bins"; |
| 38 | private static final String SOAM_PDUS_SENT = "soamPdusSent"; |
| 39 | private static final String SOAM_PDUS_RECEIVED = "soamPdusReceived"; |
| 40 | |
| 41 | @Override |
| 42 | public ObjectNode encode(DelayMeasurementStat dmStat, CodecContext context) { |
| 43 | checkNotNull(dmStat, "DM stat cannot be null"); |
| 44 | ObjectNode result = context.mapper().createObjectNode() |
| 45 | .put("elapsedTime", dmStat.elapsedTime().toString()) |
| 46 | .put("suspectStatus", String.valueOf(dmStat.suspectStatus())); |
| 47 | |
| 48 | if (dmStat.frameDelayTwoWayMin() != null) { |
| 49 | result = result.put("frameDelayTwoWayMin", |
| 50 | dmStat.frameDelayTwoWayMin().toString()); |
| 51 | } |
| 52 | if (dmStat.frameDelayTwoWayMax() != null) { |
| 53 | result = result.put("frameDelayTwoWayMax", |
| 54 | dmStat.frameDelayTwoWayMax().toString()); |
| 55 | } |
| 56 | if (dmStat.frameDelayTwoWayAvg() != null) { |
| 57 | result = result.put("frameDelayTwoWayAvg", |
| 58 | dmStat.frameDelayTwoWayAvg().toString()); |
| 59 | } |
| 60 | if (dmStat.frameDelayTwoWayBins() != null) { |
| 61 | result = (ObjectNode) result.set("frameDelayTwoWayBins", |
| 62 | encode(dmStat.frameDelayTwoWayBins(), context)); |
| 63 | } |
| 64 | if (dmStat.frameDelayForwardMin() != null) { |
| 65 | result = result.put("frameDelayForwardMin", |
| 66 | dmStat.frameDelayForwardMin().toString()); |
| 67 | } |
| 68 | if (dmStat.frameDelayForwardMax() != null) { |
| 69 | result = result.put("frameDelayForwardMax", |
| 70 | dmStat.frameDelayForwardMax().toString()); |
| 71 | } |
| 72 | if (dmStat.frameDelayForwardAvg() != null) { |
| 73 | result = result.put("frameDelayForwardAvg", |
| 74 | dmStat.frameDelayForwardAvg().toString()); |
| 75 | } |
| 76 | if (dmStat.frameDelayForwardBins() != null) { |
| 77 | result = (ObjectNode) result.set("frameDelayForwardBins", |
| 78 | encode(dmStat.frameDelayForwardBins(), context)); |
| 79 | } |
| 80 | if (dmStat.frameDelayBackwardMin() != null) { |
| 81 | result = result.put("frameDelayBackwardMin", |
| 82 | dmStat.frameDelayBackwardMin().toString()); |
| 83 | } |
| 84 | if (dmStat.frameDelayBackwardMax() != null) { |
| 85 | result = result.put("frameDelayBackwardMax", |
| 86 | dmStat.frameDelayBackwardMax().toString()); |
| 87 | } |
| 88 | if (dmStat.frameDelayBackwardAvg() != null) { |
| 89 | result = result.put("frameDelayBackwardAvg", |
| 90 | dmStat.frameDelayBackwardAvg().toString()); |
| 91 | } |
| 92 | if (dmStat.frameDelayBackwardBins() != null) { |
| 93 | result = (ObjectNode) result.set("frameDelayBackwardBins", |
| 94 | encode(dmStat.frameDelayBackwardBins(), context)); |
| 95 | } |
| 96 | if (dmStat.interFrameDelayVariationTwoWayMin() != null) { |
| 97 | result = result.put("interFrameDelayVariationTwoWayMin", |
| 98 | dmStat.interFrameDelayVariationTwoWayMin().toString()); |
| 99 | } |
| 100 | if (dmStat.interFrameDelayVariationTwoWayMax() != null) { |
| 101 | result.put("interFrameDelayVariationTwoWayMax", |
| 102 | dmStat.interFrameDelayVariationTwoWayMax().toString()); |
| 103 | } |
| 104 | if (dmStat.interFrameDelayVariationTwoWayAvg() != null) { |
| 105 | result.put("interFrameDelayVariationTwoWayAvg", |
| 106 | dmStat.interFrameDelayVariationTwoWayAvg().toString()); |
| 107 | } |
| 108 | if (dmStat.interFrameDelayVariationTwoWayBins() != null) { |
| 109 | result = (ObjectNode) result.set("interFrameDelayVariationTwoWayBins", |
| 110 | encode(dmStat.interFrameDelayVariationTwoWayBins(), context)); |
| 111 | } |
| 112 | if (dmStat.interFrameDelayVariationForwardMin() != null) { |
| 113 | result = result.put("interFrameDelayVariationForwardMin", |
| 114 | dmStat.interFrameDelayVariationForwardMin().toString()); |
| 115 | } |
| 116 | if (dmStat.interFrameDelayVariationForwardMax() != null) { |
| 117 | result = result.put("interFrameDelayVariationForwardMax", |
| 118 | dmStat.interFrameDelayVariationForwardMax().toString()); |
| 119 | } |
| 120 | if (dmStat.interFrameDelayVariationForwardAvg() != null) { |
| 121 | result = result.put("interFrameDelayVariationForwardAvg", |
| 122 | dmStat.interFrameDelayVariationForwardAvg().toString()); |
| 123 | } |
| 124 | if (dmStat.interFrameDelayVariationForwardBins() != null) { |
| 125 | result = (ObjectNode) result.set("interFrameDelayVariationForwardBins", |
| 126 | encode(dmStat.interFrameDelayVariationForwardBins(), context)); |
| 127 | } |
| 128 | if (dmStat.interFrameDelayVariationBackwardMin() != null) { |
| 129 | result = result.put("interFrameDelayVariationBackwardMin", |
| 130 | dmStat.interFrameDelayVariationBackwardMin().toString()); |
| 131 | } |
| 132 | if (dmStat.interFrameDelayVariationBackwardMax() != null) { |
| 133 | result = result.put("interFrameDelayVariationBackwardMax", |
| 134 | dmStat.interFrameDelayVariationBackwardMax().toString()); |
| 135 | } |
| 136 | if (dmStat.interFrameDelayVariationBackwardAvg() != null) { |
| 137 | result = result.put("interFrameDelayVariationBackwardAvg", |
| 138 | dmStat.interFrameDelayVariationBackwardAvg().toString()); |
| 139 | } |
| 140 | if (dmStat.interFrameDelayVariationBackwardBins() != null) { |
| 141 | result = (ObjectNode) result.set("interFrameDelayVariationBackwardBins", |
| 142 | encode(dmStat.interFrameDelayVariationBackwardBins(), context)); |
| 143 | } |
| 144 | if (dmStat.frameDelayRangeTwoWayMax() != null) { |
| 145 | result = result.put("frameDelayRangeTwoWayMax", |
| 146 | dmStat.frameDelayRangeTwoWayMax().toString()); |
| 147 | } |
| 148 | if (dmStat.frameDelayRangeTwoWayAvg() != null) { |
| 149 | result = result.put("frameDelayRangeTwoWayAvg", |
| 150 | dmStat.frameDelayRangeTwoWayAvg().toString()); |
| 151 | } |
| 152 | if (dmStat.frameDelayRangeTwoWayBins() != null) { |
| 153 | result = (ObjectNode) result.set("frameDelayRangeTwoWayBins", |
| 154 | encode(dmStat.frameDelayRangeTwoWayBins(), context)); |
| 155 | } |
| 156 | if (dmStat.frameDelayRangeForwardMax() != null) { |
| 157 | result = result.put("frameDelayRangeForwardMax", |
| 158 | dmStat.frameDelayRangeForwardMax().toString()); |
| 159 | } |
| 160 | if (dmStat.frameDelayRangeForwardAvg() != null) { |
| 161 | result = result.put("frameDelayRangeForwardAvg", |
| 162 | dmStat.frameDelayRangeForwardAvg().toString()); |
| 163 | } |
| 164 | if (dmStat.frameDelayRangeForwardBins() != null) { |
| 165 | result = (ObjectNode) result.set("frameDelayRangeForwardBins", |
| 166 | encode(dmStat.frameDelayRangeForwardBins(), context)); |
| 167 | } |
| 168 | if (dmStat.frameDelayRangeBackwardMax() != null) { |
| 169 | result = result.put("frameDelayRangeBackwardMax", |
| 170 | dmStat.frameDelayRangeBackwardMax().toString()); |
| 171 | } |
| 172 | if (dmStat.frameDelayRangeBackwardAvg() != null) { |
| 173 | result = result.put("frameDelayRangeBackwardAvg", |
| 174 | dmStat.frameDelayRangeBackwardAvg().toString()); |
| 175 | } |
| 176 | if (dmStat.frameDelayRangeBackwardBins() != null) { |
| 177 | result = (ObjectNode) result.set("frameDelayRangeBackwardBins", |
| 178 | encode(dmStat.frameDelayRangeBackwardBins(), context)); |
| 179 | } |
| 180 | |
| 181 | if (dmStat.soamPdusReceived() != null) { |
| 182 | result = result.put(SOAM_PDUS_RECEIVED, dmStat.soamPdusReceived().toString()); |
| 183 | } |
| 184 | |
| 185 | if (dmStat.soamPdusSent() != null) { |
| 186 | result = result.put(SOAM_PDUS_SENT, dmStat.soamPdusSent().toString()); |
| 187 | } |
| 188 | |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | private ObjectNode encode(Map<Duration, Integer> bins, CodecContext context) { |
| 193 | checkNotNull(bins, "Bins cannot be null"); |
| 194 | ArrayNode binsResult = context.mapper().createArrayNode(); |
| 195 | bins.keySet().forEach(lwrLimit -> binsResult.add(encode(lwrLimit, bins.get(lwrLimit), context))); |
| 196 | |
| 197 | return (ObjectNode) context.mapper().createObjectNode().set(BINS, binsResult); |
| 198 | } |
| 199 | |
| 200 | private ObjectNode encode(Duration duration, Integer count, CodecContext context) { |
| 201 | return context.mapper().createObjectNode() |
| 202 | .put(LOWER_LIMIT, duration.toString()) |
| 203 | .put(COUNT, count); |
| 204 | } |
| 205 | } |