blob: ce851d6fd3eb92e50717e230b4111e2a6e39b2cd [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 static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Iterator;
21import java.util.Map.Entry;
22
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementEntry;
26
27import com.fasterxml.jackson.databind.JsonNode;
28import com.fasterxml.jackson.databind.node.ArrayNode;
29import com.fasterxml.jackson.databind.node.ObjectNode;
30
31/**
32 * Encode and decode to/from JSON to DelayMeasurementEntry object.
33 */
34public class DmEntryCodec extends JsonCodec<DelayMeasurementEntry> {
35
36 private static final String DM_ID = "dmId";
37 private static final String SESSION_STATUS = "sessionStatus";
38 private static final String FRAME_DELAY_TWO_WAY = "frameDelayTwoWay";
39 private static final String FRAME_DELAY_FORWARD = "frameDelayForward";
40 private static final String FRAME_DELAY_BACKWARD = "frameDelayBackward";
41 private static final String INTER_FRAME_DELAY_VARIATION_TWO_WAY = "interFrameDelayVariationTwoWay";
42 private static final String INTER_FRAME_DELAY_VARIATION_FORWARD = "interFrameDelayVariationForward";
43 private static final String INTER_FRAME_DELAY_VARIATION_BACKWARD = "interFrameDelayVariationBackward";
44 private static final String CURRENT = "current";
45 private static final String HISTORIC = "historic";
46
47 @Override
48 public ObjectNode encode(DelayMeasurementEntry dm, CodecContext context) {
49 checkNotNull(dm, "DM cannot be null");
50 ObjectNode result = context.mapper().createObjectNode()
51 .put(DM_ID, dm.dmId().toString());
52
53 if (dm.sessionStatus() != null) {
54 result.put(SESSION_STATUS, dm.sessionStatus().name());
55 }
56 if (dm.frameDelayTwoWay() != null) {
57 result.put(FRAME_DELAY_TWO_WAY, dm.frameDelayTwoWay().toString());
58 }
59 if (dm.frameDelayForward() != null) {
60 result.put(FRAME_DELAY_FORWARD, dm.frameDelayForward().toString());
61 }
62 if (dm.frameDelayBackward() != null) {
63 result.put(FRAME_DELAY_BACKWARD, dm.frameDelayBackward().toString());
64 }
65 if (dm.interFrameDelayVariationTwoWay() != null) {
66 result.put(INTER_FRAME_DELAY_VARIATION_TWO_WAY,
67 dm.interFrameDelayVariationTwoWay().toString());
68 }
69 if (dm.interFrameDelayVariationForward() != null) {
70 result.put(INTER_FRAME_DELAY_VARIATION_FORWARD,
71 dm.interFrameDelayVariationForward().toString());
72 }
73 if (dm.interFrameDelayVariationBackward() != null) {
74 result.put(INTER_FRAME_DELAY_VARIATION_BACKWARD,
75 dm.interFrameDelayVariationBackward().toString());
76 }
77
78 ObjectNode dmAttrs = new DmCreateCodec().encode(dm, context);
79 Iterator<Entry<String, JsonNode>> elements = dmAttrs.fields();
80 while (elements.hasNext()) {
81 Entry<String, JsonNode> element = elements.next();
82 result.set(element.getKey(), element.getValue());
83 }
84
85 if (dm.currentResult() != null) {
86 result.set(CURRENT, new DelayMeasurementStatCurrentCodec()
87 .encode(dm.currentResult(), context));
88 }
89
90 if (dm.historicalResults() != null) {
91 result.set(HISTORIC, new DelayMeasurementStatHistoryCodec()
92 .encode(dm.historicalResults(), context));
93 }
94
95 return result;
96 }
97
98 @Override
99 public ArrayNode encode(Iterable<DelayMeasurementEntry> dmEntities, CodecContext context) {
100 ArrayNode an = context.mapper().createArrayNode();
101 dmEntities.forEach(dm -> an.add(encode(dm, context)));
102 return an;
103 }
104}