blob: e88fb2f635a5a25a3bcde22e88fa9fce2aa71cbd [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.cfm.Mep.Priority;
24import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
25import org.onosproject.incubator.net.l2monitoring.soam.MilliPct;
26import org.onosproject.incubator.net.l2monitoring.soam.SoamConfigException;
27import org.onosproject.incubator.net.l2monitoring.soam.StartTime;
28import org.onosproject.incubator.net.l2monitoring.soam.StopTime;
29import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.Version;
30import org.onosproject.incubator.net.l2monitoring.soam.loss.DefaultLmCreate;
31import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementCreate;
32import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementThreshold;
33
34import java.time.Duration;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37import static org.onlab.util.Tools.nullIsIllegal;
38import static org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementCreate.CounterOption;
39import static org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementCreate.LmType;
40import static org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementCreate.LmCreateBuilder;
41
42/**
43 * Encode and decode to/from JSON to LossMeasurementCreate object.
44 */
45public class LmCreateCodec extends JsonCodec<LossMeasurementCreate> {
46
47 public static final String LM = "lm";
48 public static final String VERSION = "version";
49 public static final String LM_CFG_TYPE = "lmCfgType";
50 public static final String LMLMM = "LMLMM";
51 public static final String REMOTE_MEP_ID = "remoteMepId";
52 public static final String PRIORITY = "priority";
53 public static final String COUNTERS_ENABLED = "countersEnabled";
54 public static final String THRESHOLDS = "thresholds";
55 public static final String AVAILABILITY_MEASUREMENT_INTERVAL_MINS =
56 "availabilityMeasurementIntervalMins";
57 public static final String AVAILABILITY_NUMBER_CONSECUTIVE_FLR_MEASUREMENTS =
58 "availabilityNumberConsecutiveFlrMeasurements";
59 public static final String AVAILABILITY_FLR_THRESHOLD_PCT =
60 "availabilityFlrThresholdPct";
61 public static final String AVAILABILITY_NUMBER_CONSECUTIVE_INTERVALS =
62 "availabilityNumberConsecutiveIntervals";
63 public static final String AVAILABILITY_NUMBER_CONSECUTIVE_HIGH_FLR =
64 "availabilityNumberConsecutiveHighFlr";
65 public static final String FRAME_SIZE = "frameSize";
66 public static final String MESSAGE_PERIOD_MS = "messagePeriodMs";
67 public static final String MEASUREMENT_INTERVAL_MINS =
68 "measurementIntervalMins";
69 public static final String ALIGN_MEASUREMENT_INTERVALS =
70 "alignMeasurementIntervals";
71 public static final String ALIGN_MEASUREMENT_OFFSET_MINS =
72 "alignMeasurementOffsetMins";
73 public static final String START_TIME = "startTime";
74 public static final String STOP_TIME = "stopTime";
75
76 @Override
77 public LossMeasurementCreate decode(ObjectNode json,
78 CodecContext context) {
79
80 if (json == null || !json.isObject()) {
81 return null;
82 }
83
84 JsonNode lmNode = json.get(LM);
85 Version version = Version.Y17312011;
86 if (lmNode.get(VERSION) != null) {
87 version = Version.valueOf(lmNode.get(VERSION).asText());
88 }
89 LmType lmCfgType = LmType.LMLMM;
90 if (lmNode.get(LM_CFG_TYPE) != null) {
91 lmCfgType = LmType.valueOf(lmNode.get(LM_CFG_TYPE).asText(LMLMM));
92 }
93 MepId remoteMepId = MepId.valueOf(
94 nullIsIllegal(lmNode.get(REMOTE_MEP_ID), REMOTE_MEP_ID + " is required")
95 .shortValue());
96 Priority prio = Priority.valueOf(nullIsIllegal(lmNode.get(PRIORITY),
97 PRIORITY + " is required in the format 'PRIOn'").asText());
98
99 try {
100 LmCreateBuilder builder = DefaultLmCreate
101 .builder(version, remoteMepId, prio, lmCfgType);
102
103 if (lmNode.get(COUNTERS_ENABLED) != null) {
104 context.codec(CounterOption.class)
105 .decode((ArrayNode) (lmNode.get(COUNTERS_ENABLED)), context)
106 .forEach(builder::addToCountersEnabled);
107 }
108
109 if (lmNode.get(THRESHOLDS) != null) {
110 context.codec(LossMeasurementThreshold.class)
111 .decode((ArrayNode) (lmNode.get(THRESHOLDS)), context)
112 .forEach(builder::addToLossMeasurementThreshold);
113 }
114
115 if (lmNode.get(AVAILABILITY_MEASUREMENT_INTERVAL_MINS) != null) {
116 builder = builder.availabilityMeasurementInterval(
117 Duration.ofMinutes(lmNode.get(AVAILABILITY_MEASUREMENT_INTERVAL_MINS).asInt()));
118 }
119 if (lmNode.get(AVAILABILITY_NUMBER_CONSECUTIVE_FLR_MEASUREMENTS) != null) {
120 builder = builder.availabilityNumberConsecutiveFlrMeasurements(
121 lmNode.get(AVAILABILITY_NUMBER_CONSECUTIVE_FLR_MEASUREMENTS).asInt());
122 }
123 if (lmNode.get(AVAILABILITY_FLR_THRESHOLD_PCT) != null) {
124 builder = builder.availabilityFlrThreshold(
125 MilliPct.ofPercent((float) lmNode.get(AVAILABILITY_FLR_THRESHOLD_PCT).asDouble()));
126 }
127 if (lmNode.get(AVAILABILITY_NUMBER_CONSECUTIVE_INTERVALS) != null) {
128 builder = builder.availabilityNumberConsecutiveIntervals(
129 (short) lmNode.get(AVAILABILITY_NUMBER_CONSECUTIVE_INTERVALS).asInt());
130 }
131 if (lmNode.get(AVAILABILITY_NUMBER_CONSECUTIVE_HIGH_FLR) != null) {
132 builder = builder.availabilityNumberConsecutiveHighFlr(
133 (short) lmNode.get(AVAILABILITY_NUMBER_CONSECUTIVE_HIGH_FLR).asInt());
134 }
135 if (lmNode.get(FRAME_SIZE) != null) {
136 builder = (LmCreateBuilder) builder.frameSize(
137 (short) lmNode.get(FRAME_SIZE).asInt());
138 }
139 if (lmNode.get(MESSAGE_PERIOD_MS) != null) {
140 builder = (LmCreateBuilder) builder.messagePeriod(Duration.ofMillis(
141 lmNode.get(MESSAGE_PERIOD_MS).asInt()));
142 }
143 if (lmNode.get(MEASUREMENT_INTERVAL_MINS) != null) {
144 builder = (LmCreateBuilder) builder.measurementInterval(
145 Duration.ofMinutes(
146 lmNode.get(MEASUREMENT_INTERVAL_MINS).asInt()));
147 }
148 if (lmNode.get(ALIGN_MEASUREMENT_INTERVALS) != null) {
149 builder = (LmCreateBuilder) builder.alignMeasurementIntervals(
150 lmNode.get(ALIGN_MEASUREMENT_INTERVALS).asBoolean());
151 }
152 if (lmNode.get(ALIGN_MEASUREMENT_OFFSET_MINS) != null) {
153 builder = (LmCreateBuilder) builder.alignMeasurementOffset(Duration.ofMinutes(
154 lmNode.get(ALIGN_MEASUREMENT_OFFSET_MINS).asInt()));
155 }
156 if (lmNode.get(START_TIME) != null) {
157 builder = (LmCreateBuilder) builder.startTime(context.codec(StartTime.class)
158 .decode((ObjectNode) lmNode.get(START_TIME), context));
159 }
160 if (lmNode.get(STOP_TIME) != null) {
161 builder = (LmCreateBuilder) builder.stopTime(context.codec(StopTime.class)
162 .decode((ObjectNode) lmNode.get(STOP_TIME), context));
163 }
164
165
166 return builder.build();
167 } catch (SoamConfigException e) {
168 throw new IllegalArgumentException(e);
169 }
170 }
171
172 @Override
173 public ObjectNode encode(LossMeasurementCreate lm, CodecContext context) {
174 checkNotNull(lm, "LM cannot be null");
175 ObjectNode result = context.mapper().createObjectNode()
176 .put(LM_CFG_TYPE, lm.lmCfgType().name())
177 .put(VERSION, lm.version().name())
178 .put(REMOTE_MEP_ID, lm.remoteMepId().id())
179 .put(PRIORITY, lm.priority().name());
180
181 if (lm.countersEnabled() != null) {
182 result.set(COUNTERS_ENABLED, new LmCounterOptionCodec()
183 .encode(lm.countersEnabled(), context));
184 }
185
186 if (lm.messagePeriod() != null) {
187 result.put(MESSAGE_PERIOD_MS, lm.messagePeriod().toMillis());
188 }
189 if (lm.frameSize() != null) {
190 result.put(FRAME_SIZE, lm.frameSize());
191 }
192
193
194 return result;
195 }
196}