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