blob: 028fad822d082f18c5d587e7c97a50138f88afd4 [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 org.junit.Assert.assertEquals;
19
20import java.io.IOException;
21import java.time.Duration;
22import java.util.ArrayList;
23import java.util.Collection;
24
25import org.junit.Before;
26import org.junit.Test;
27import org.onosproject.cfm.CfmCodecContext;
28import org.onosproject.incubator.net.l2monitoring.cfm.Mep.Priority;
29import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
30import org.onosproject.incubator.net.l2monitoring.soam.SoamConfigException;
31import org.onosproject.incubator.net.l2monitoring.soam.SoamId;
32import org.onosproject.incubator.net.l2monitoring.soam.delay.DefaultDelayMeasurementEntry;
33import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.DmType;
34import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.MeasurementOption;
35import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate.Version;
36import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementEntry;
37import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementEntry.DmEntryBuilder;
38import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementEntry.SessionStatus;
39
40import com.fasterxml.jackson.core.JsonProcessingException;
41import com.fasterxml.jackson.databind.ObjectMapper;
42import com.fasterxml.jackson.databind.node.ArrayNode;
43import com.fasterxml.jackson.databind.node.ObjectNode;
44
45public class DmEntryCodecTest {
46 ObjectMapper mapper;
47 CfmCodecContext context;
48 DelayMeasurementEntry dmEntry1;
49
50 @Before
51 public void setUp() throws Exception, SoamConfigException {
52 mapper = new ObjectMapper();
53 context = new CfmCodecContext();
54 DmEntryBuilder builder = DefaultDelayMeasurementEntry
55 .builder(SoamId.valueOf(12), DmType.DM1DMTX,
56 Version.Y17312008, MepId.valueOf((short) 10), Priority.PRIO4);
57 builder = builder.sessionStatus(SessionStatus.NOT_ACTIVE);
58 builder = builder.frameDelayTwoWay(Duration.ofNanos(101 * 1000));
59 builder = builder.frameDelayForward(Duration.ofNanos(102 * 1000));
60 builder = builder.frameDelayBackward(Duration.ofNanos(103 * 1000));
61 builder = builder.interFrameDelayVariationTwoWay(Duration.ofNanos(104 * 1000));
62 builder = builder.interFrameDelayVariationForward(Duration.ofNanos(105 * 1000));
63 builder = builder.interFrameDelayVariationBackward(Duration.ofNanos(106 * 1000));
64 builder.addToMeasurementsEnabled(MeasurementOption.FRAME_DELAY_BACKWARD_MAX);
65 builder.addToMeasurementsEnabled(MeasurementOption.FRAME_DELAY_TWO_WAY_MAX);
66 builder.addToMeasurementsEnabled(MeasurementOption.INTER_FRAME_DELAY_VARIATION_BACKWARD_BINS);
67
68 dmEntry1 = builder.build();
69 }
70
71 @Test
72 public void testEncodeDelayMeasurementEntryCodecContext()
73 throws JsonProcessingException, IOException {
74 ObjectNode node = mapper.createObjectNode();
75 node.set("dm", context.codec(DelayMeasurementEntry.class)
76 .encode(dmEntry1, context));
77
78 assertEquals(12, node.get("dm").get("dmId").asInt());
79 assertEquals(DmType.DM1DMTX.name(), node.get("dm").get("dmCfgType").asText());
80 assertEquals(Version.Y17312008.name(), node.get("dm").get("version").asText());
81 assertEquals(10, node.get("dm").get("remoteMepId").asInt());
82 assertEquals(3, ((ArrayNode) node.get("dm").get("measurementsEnabled")).size());
83
84 assertEquals(SessionStatus.NOT_ACTIVE.name(),
85 node.get("dm").get("sessionStatus").asText());
86 assertEquals("PT0.000101S",
87 node.get("dm").get("frameDelayTwoWay").asText());
88 assertEquals("PT0.000102S",
89 node.get("dm").get("frameDelayForward").asText());
90 assertEquals("PT0.000103S",
91 node.get("dm").get("frameDelayBackward").asText());
92 assertEquals("PT0.000104S",
93 node.get("dm").get("interFrameDelayVariationTwoWay").asText());
94 assertEquals("PT0.000105S",
95 node.get("dm").get("interFrameDelayVariationForward").asText());
96 assertEquals("PT0.000106S",
97 node.get("dm").get("interFrameDelayVariationBackward").asText());
98
99 }
100
101 @Test
102 public void testEncodeIterableOfDelayMeasurementEntryCodecContext()
103 throws SoamConfigException {
104 DmEntryBuilder builder2 = DefaultDelayMeasurementEntry
105 .builder(SoamId.valueOf(14), DmType.DM1DMRX,
106 Version.Y17312011, MepId.valueOf((short) 16), Priority.PRIO5);
107 builder2.addToMeasurementsEnabled(MeasurementOption.FRAME_DELAY_BACKWARD_MIN);
108 builder2.addToMeasurementsEnabled(MeasurementOption.FRAME_DELAY_TWO_WAY_MIN);
109 builder2.addToMeasurementsEnabled(MeasurementOption.INTER_FRAME_DELAY_VARIATION_BACKWARD_MIN);
110
111 Collection<DelayMeasurementEntry> dmEntries = new ArrayList<>();
112 dmEntries.add(dmEntry1);
113 dmEntries.add(builder2.build());
114 ObjectNode node = mapper.createObjectNode();
115 node.set("dm", context.codec(DelayMeasurementEntry.class)
116 .encode(dmEntries, context));
117
118 assertEquals(2, ((ArrayNode) node.get("dm")).size());
119 }
120
121}