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 com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.onosproject.incubator.net.l2monitoring.soam.loss.LossAvailabilityStat; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
| 26 | * Encode and decode to/from JSON to LossAvailabilityStat object. |
| 27 | */ |
| 28 | public class LossAvailabilityStatCodec extends JsonCodec<LossAvailabilityStat> { |
| 29 | |
| 30 | @Override |
| 31 | public ObjectNode encode(LossAvailabilityStat laStat, CodecContext context) { |
| 32 | checkNotNull(laStat, "LA stat cannot be null"); |
| 33 | ObjectNode result = context.mapper().createObjectNode() |
| 34 | .put("elapsedTime", laStat.elapsedTime().toString()) |
| 35 | .put("suspectStatus", String.valueOf(laStat.suspectStatus())); |
| 36 | |
| 37 | if (laStat.forwardHighLoss() != null) { |
| 38 | result = result.put("forwardHighLoss", |
| 39 | laStat.forwardHighLoss().toString()); |
| 40 | } |
| 41 | if (laStat.backwardHighLoss() != null) { |
| 42 | result = result.put("backwardHighLoss", |
| 43 | laStat.backwardHighLoss().toString()); |
| 44 | } |
| 45 | if (laStat.forwardConsecutiveHighLoss() != null) { |
| 46 | result = result.put("forwardConsecutiveHighLoss", |
| 47 | laStat.forwardConsecutiveHighLoss().toString()); |
| 48 | } |
| 49 | if (laStat.backwardConsecutiveHighLoss() != null) { |
| 50 | result = result.put("backwardConsecutiveHighLoss", |
| 51 | laStat.backwardConsecutiveHighLoss().toString()); |
| 52 | } |
| 53 | if (laStat.forwardAvailable() != null) { |
| 54 | result = result.put("forwardAvailable", |
| 55 | laStat.forwardAvailable().toString()); |
| 56 | } |
| 57 | if (laStat.backwardAvailable() != null) { |
| 58 | result = result.put("backwardAvailable", |
| 59 | laStat.backwardAvailable().toString()); |
| 60 | } |
| 61 | if (laStat.forwardUnavailable() != null) { |
| 62 | result = result.put("forwardUnavailable", |
| 63 | laStat.forwardUnavailable().toString()); |
| 64 | } |
| 65 | if (laStat.backwardUnavailable() != null) { |
| 66 | result = result.put("backwardUnavailable", |
| 67 | laStat.backwardUnavailable().toString()); |
| 68 | } |
| 69 | if (laStat.backwardMinFrameLossRatio() != null) { |
| 70 | result = result.put("backwardMinFrameLossRatio", |
| 71 | laStat.backwardMinFrameLossRatio().toString()); |
| 72 | } |
| 73 | if (laStat.backwardMaxFrameLossRatio() != null) { |
| 74 | result = result.put("backwardMaxFrameLossRatio", |
| 75 | laStat.backwardMaxFrameLossRatio().toString()); |
| 76 | } |
| 77 | if (laStat.backwardAverageFrameLossRatio() != null) { |
| 78 | result = result.put("backwardAverageFrameLossRatio", |
| 79 | laStat.backwardAverageFrameLossRatio().toString()); |
| 80 | } |
| 81 | if (laStat.forwardMinFrameLossRatio() != null) { |
| 82 | result = result.put("forwardMinFrameLossRatio", |
| 83 | laStat.forwardMinFrameLossRatio().toString()); |
| 84 | } |
| 85 | if (laStat.forwardMaxFrameLossRatio() != null) { |
| 86 | result = result.put("forwardMaxFrameLossRatio", |
| 87 | laStat.forwardMaxFrameLossRatio().toString()); |
| 88 | } |
| 89 | if (laStat.forwardAverageFrameLossRatio() != null) { |
| 90 | result = result.put("forwardAverageFrameLossRatio", |
| 91 | laStat.forwardAverageFrameLossRatio().toString()); |
| 92 | } |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | } |