blob: 3a17f9466bf6bd299683abc3390dea0160e67978 [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.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.incubator.net.l2monitoring.soam.MilliPct;
24import org.onosproject.incubator.net.l2monitoring.soam.SoamId;
25import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementThreshold;
26
27import java.util.ArrayList;
28import java.util.List;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static org.onlab.util.Tools.nullIsIllegal;
32import static org.onosproject.incubator.net.l2monitoring.soam.loss.DefaultLmThreshold.*;
33
34/**
35 * Encode and decode to/from JSON to LossMeasurementThreshold object.
36 */
37public class LossMeasurementThresholdCodec extends JsonCodec<LossMeasurementThreshold> {
38 static final String MEASUREDFLRFORWARD = "measuredFlrForward";
39 static final String MAXFLRFORWARD = "maxFlrForward";
40 static final String AVERAGEFLRFORWARD = "averageFlrForward";
41 static final String MEASUREDFLRBACKWARD = "measuredFlrBackward";
42 static final String MAXFLRBACKWARD = "maxFlrBackward";
43 static final String AVERAGEFLRBACKWARD = "averageFlrBackward";
44 static final String FORWARDHIGHLOSS = "forwardHighLoss";
45 static final String FORWARDCONSECUTIVEHIGHLOSS = "forwardConsecutiveHighLoss";
46 static final String BACKWARDHIGHLOSS = "backwardHighLoss";
47 static final String BACKWARDCONSECUTIVEHIGHLOSS = "backwardConsecutiveHighLoss";
48 static final String FORWARDUNAVAILABLECOUNT = "forwardUnavailableCount";
49 static final String FORWARDAVAILABLERATIO = "forwardAvailableRatio";
50 static final String BACKWARDUNAVAILABLECOUNT = "backwardUnavailableCount";
51 static final String BACKWARDAVAILABLERATIO = "backwardAvailableRatio";
52 static final String THRESHOLDOPTIONS = "thresholdOptions";
53
54 @Override
55 public ObjectNode encode(LossMeasurementThreshold lmt, CodecContext context) {
56 checkNotNull(lmt, "LM thresholds cannot be null");
57 ObjectNode result = context.mapper().createObjectNode()
58 .put("id", lmt.thresholdId().value());
59
60 if (lmt.thresholds() != null) {
61 result.set(THRESHOLDOPTIONS, new LmThresholdOptionCodec()
62 .encode(lmt.thresholds(), context));
63 }
64
65 if (lmt.measuredFlrForward() != null) {
66 result.put(MEASUREDFLRFORWARD, lmt.measuredFlrForward().percentValue());
67 }
68 if (lmt.maxFlrForward() != null) {
69 result.put(MAXFLRFORWARD, lmt.maxFlrForward().percentValue());
70 }
71 if (lmt.averageFlrForward() != null) {
72 result.put(AVERAGEFLRFORWARD, lmt.averageFlrForward().percentValue());
73 }
74 if (lmt.measuredFlrBackward() != null) {
75 result.put(MEASUREDFLRBACKWARD, lmt.measuredFlrBackward().percentValue());
76 }
77 if (lmt.maxFlrBackward() != null) {
78 result.put(MAXFLRBACKWARD, lmt.maxFlrBackward().percentValue());
79 }
80 if (lmt.averageFlrBackward() != null) {
81 result.put(AVERAGEFLRBACKWARD, lmt.averageFlrBackward().percentValue());
82 }
83 if (lmt.forwardHighLoss() != null) {
84 result.put(FORWARDHIGHLOSS, lmt.forwardHighLoss().longValue());
85 }
86 if (lmt.forwardConsecutiveHighLoss() != null) {
87 result.put(FORWARDCONSECUTIVEHIGHLOSS, lmt.measuredFlrForward().longValue());
88 }
89 if (lmt.backwardHighLoss() != null) {
90 result.put(BACKWARDHIGHLOSS, lmt.backwardHighLoss().longValue());
91 }
92 if (lmt.backwardConsecutiveHighLoss() != null) {
93 result.put(BACKWARDCONSECUTIVEHIGHLOSS, lmt.backwardConsecutiveHighLoss().longValue());
94 }
95 if (lmt.forwardUnavailableCount() != null) {
96 result.put(FORWARDUNAVAILABLECOUNT, lmt.forwardUnavailableCount().longValue());
97 }
98 if (lmt.forwardAvailableRatio() != null) {
99 result.put(FORWARDAVAILABLERATIO, lmt.forwardAvailableRatio().percentValue());
100 }
101 if (lmt.backwardUnavailableCount() != null) {
102 result.put(BACKWARDUNAVAILABLECOUNT, lmt.backwardUnavailableCount().longValue());
103 }
104 if (lmt.backwardAvailableRatio() != null) {
105 result.put(BACKWARDAVAILABLERATIO, lmt.backwardAvailableRatio().percentValue());
106 }
107
108 return result;
109 }
110
111 @Override
112 public List<LossMeasurementThreshold> decode(ArrayNode json, CodecContext context) {
113 if (json == null) {
114 return null;
115 }
116 List<LossMeasurementThreshold> thrList = new ArrayList<>();
117 json.forEach(node -> thrList.add(decode((ObjectNode) node, context)));
118 return thrList;
119 }
120
121 @Override
122 public LossMeasurementThreshold decode(ObjectNode json, CodecContext context) {
123 if (json == null || !json.isObject()) {
124 return null;
125 }
126
127 JsonNode thrNode = json.get("threshold");
128
129 SoamId thresholdId = SoamId.valueOf(nullIsIllegal(thrNode.get("id"),
130 "thresholdId must not be null").asInt());
131 LossMeasurementThreshold.LmThresholdBuilder builder = builder(thresholdId);
132
133 if (thrNode.get("thresholds") != null) {
134 context.codec(ThresholdOption.class)
135 .decode((ArrayNode) (thrNode.get("thresholds")), context)
136 .forEach(builder::addToThreshold);
137 }
138
139 if (thrNode.get(MEASUREDFLRFORWARD) != null) {
140 builder.measuredFlrForward(MilliPct.ofPercent(
141 (float) thrNode.get(MEASUREDFLRFORWARD).asDouble()));
142 }
143 if (thrNode.get(MAXFLRFORWARD) != null) {
144 builder.maxFlrForward(MilliPct.ofPercent(
145 (float) thrNode.get(MAXFLRFORWARD).asDouble()));
146 }
147 if (thrNode.get(AVERAGEFLRFORWARD) != null) {
148 builder.averageFlrForward(MilliPct.ofPercent(
149 (float) thrNode.get(AVERAGEFLRFORWARD).asDouble()));
150 }
151 if (thrNode.get(MEASUREDFLRBACKWARD) != null) {
152 builder.measuredFlrBackward(MilliPct.ofPercent(
153 (float) thrNode.get(MEASUREDFLRBACKWARD).asDouble()));
154 }
155 if (thrNode.get(MAXFLRBACKWARD) != null) {
156 builder.maxFlrBackward(MilliPct.ofPercent(
157 (float) thrNode.get(MAXFLRBACKWARD).asDouble()));
158 }
159 if (thrNode.get(AVERAGEFLRBACKWARD) != null) {
160 builder.averageFlrBackward(MilliPct.ofPercent(
161 (float) thrNode.get(AVERAGEFLRBACKWARD).asDouble()));
162 }
163 if (thrNode.get(FORWARDHIGHLOSS) != null) {
164 builder.forwardHighLoss(thrNode.get(FORWARDHIGHLOSS).asLong());
165 }
166 if (thrNode.get(FORWARDCONSECUTIVEHIGHLOSS) != null) {
167 builder.forwardConsecutiveHighLoss(thrNode.get(FORWARDCONSECUTIVEHIGHLOSS).asLong());
168 }
169 if (thrNode.get(BACKWARDHIGHLOSS) != null) {
170 builder.backwardHighLoss(thrNode.get(BACKWARDHIGHLOSS).asLong());
171 }
172 if (thrNode.get(BACKWARDCONSECUTIVEHIGHLOSS) != null) {
173 builder.backwardConsecutiveHighLoss(thrNode.get(BACKWARDCONSECUTIVEHIGHLOSS).asLong());
174 }
175 if (thrNode.get(FORWARDUNAVAILABLECOUNT) != null) {
176 builder.forwardUnavailableCount(thrNode.get(FORWARDUNAVAILABLECOUNT).asLong());
177 }
178 if (thrNode.get(FORWARDAVAILABLERATIO) != null) {
179 builder.forwardAvailableRatio(MilliPct.ofPercent(
180 (float) thrNode.get(FORWARDAVAILABLERATIO).asDouble()));
181 }
182 if (thrNode.get(BACKWARDUNAVAILABLECOUNT) != null) {
183 builder.backwardUnavailableCount(thrNode.get(BACKWARDUNAVAILABLECOUNT).asLong());
184 }
185 if (thrNode.get(BACKWARDAVAILABLERATIO) != null) {
186 builder.backwardAvailableRatio(MilliPct.ofPercent(
187 (float) thrNode.get(BACKWARDAVAILABLERATIO).asDouble()));
188 }
189
190 return builder.build();
191 }
192}