blob: eac21e65b7a2a99204f31dc90d424a96cdad82b8 [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 */
16package org.onosproject.soam.web;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementStat;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Encode and decode to/from JSON to LossMeasurementStat object.
27 */
28public class LossMeasurementStatCodec extends JsonCodec<LossMeasurementStat> {
29
30 @Override
31 public ObjectNode encode(LossMeasurementStat lmStat, CodecContext context) {
32 checkNotNull(lmStat, "LM stat cannot be null");
33 ObjectNode result = context.mapper().createObjectNode()
34 .put("elapsedTime", lmStat.elapsedTime().toString())
35 .put("suspectStatus", String.valueOf(lmStat.suspectStatus()));
36
37 if (lmStat.forwardTransmittedFrames() != null) {
38 result = result.put("forwardTransmittedFrames",
39 lmStat.forwardTransmittedFrames().toString());
40 }
41 if (lmStat.forwardReceivedFrames() != null) {
42 result = result.put("forwardReceivedFrames",
43 lmStat.forwardReceivedFrames().toString());
44 }
45 if (lmStat.forwardMinFrameLossRatio() != null) {
46 result = result.put("forwardMinFrameLossRatio",
47 lmStat.forwardMinFrameLossRatio().toString());
48 }
49 if (lmStat.forwardMaxFrameLossRatio() != null) {
50 result = result.put("forwardMaxFrameLossRatio",
51 lmStat.forwardMaxFrameLossRatio().toString());
52 }
53 if (lmStat.forwardAverageFrameLossRatio() != null) {
54 result = result.put("forwardAverageFrameLossRatio",
55 lmStat.forwardAverageFrameLossRatio().toString());
56 }
57 if (lmStat.backwardTransmittedFrames() != null) {
58 result = result.put("backwardTransmittedFrames",
59 lmStat.backwardTransmittedFrames().toString());
60 }
61 if (lmStat.backwardReceivedFrames() != null) {
62 result = result.put("backwardReceivedFrames",
63 lmStat.backwardReceivedFrames().toString());
64 }
65 if (lmStat.backwardMinFrameLossRatio() != null) {
66 result = result.put("backwardMinFrameLossRatio",
67 lmStat.backwardMinFrameLossRatio().toString());
68 }
69 if (lmStat.backwardMaxFrameLossRatio() != null) {
70 result = result.put("backwardMaxFrameLossRatio",
71 lmStat.backwardMaxFrameLossRatio().toString());
72 }
73 if (lmStat.backwardAverageFrameLossRatio() != null) {
74 result = result.put("backwardAverageFrameLossRatio",
75 lmStat.backwardAverageFrameLossRatio().toString());
76 }
77 if (lmStat.soamPdusSent() != null) {
78 result = result.put("soamPdusSent",
79 lmStat.soamPdusSent().toString());
80 }
81 if (lmStat.soamPdusReceived() != null) {
82 result.put("soamPdusReceived",
83 lmStat.soamPdusReceived().toString());
84 }
85
86 return result;
87 }
88}