blob: b6204bb4af8759f9d16136dd2baa73eca37629bf [file] [log] [blame]
Seyeon Jeong8188da12020-03-03 12:49:48 -08001/*
2 * Copyright 2015-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.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.flow.DefaultFlowEntry;
27import org.onosproject.net.flow.DefaultTrafficSelector;
28import org.onosproject.net.flow.DefaultTrafficTreatment;
29import org.onosproject.net.flow.FlowEntry;
30import org.onosproject.net.flow.FlowRule;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33
34import java.io.IOException;
35import java.io.InputStream;
36
37import static org.easymock.EasyMock.*;
38import static org.junit.Assert.assertEquals;
39import static org.junit.Assert.assertNotNull;
40import static org.onosproject.codec.impl.JsonCodecUtils.assertJsonEncodable;
41import static org.onosproject.net.NetTestTools.APP_ID;
42
43/**
44 * Unit test for FlowEntryCodec.
45 */
46public class FlowEntryCodecTest {
47
48 private static final FlowEntry.FlowEntryState STATE;
49 private static final long LIFE;
50 private static final FlowEntry.FlowLiveType LIVE_TYPE;
51 private static final long PACKETS;
52 private static final long BYTES;
53 private static final TrafficSelector SELECTOR;
54 private static final TrafficTreatment TREATMENT;
55 private static final FlowRule FLOW_RULE;
56 private static final FlowEntry FLOW_ENTRY;
57
58 private CoreService mockCoreService = createMock(CoreService.class);
59 private MockCodecContext context = new MockCodecContext();
60 private JsonCodec<FlowEntry> flowEntryCodec = context.codec(FlowEntry.class);
61
62 private static final String JSON_FILE = "simple-flow-entry.json";
63
64 static {
65 // make sure these members have same values with the corresponding JSON fields
66 STATE = FlowEntry.FlowEntryState.valueOf("ADDED");
67 LIFE = 1000;
68 LIVE_TYPE = FlowEntry.FlowLiveType.valueOf("UNKNOWN");
69 PACKETS = 123;
70 BYTES = 456;
71
72 SELECTOR = DefaultTrafficSelector.builder()
73 .matchEthType((short) (Integer.decode("0x800") & 0xFFFF))
74 .build();
75 TREATMENT = DefaultTrafficTreatment.builder()
76 .setOutput(PortNumber.fromString("CONTROLLER"))
77 .build();
78 FLOW_RULE = DefaultFlowEntry.builder()
79 .withCookie(1)
80 // isPermanent & timeout
81 .makeTemporary(1)
82 .forDevice(DeviceId.deviceId("of:0000000000000001"))
83 .forTable(Integer.valueOf("1"))
84 .withPriority(Integer.valueOf("1"))
85 .withSelector(SELECTOR)
86 .withTreatment(TREATMENT)
87 .build();
88 FLOW_ENTRY = new DefaultFlowEntry(FLOW_RULE, STATE, LIFE, LIVE_TYPE, PACKETS, BYTES);
89 }
90
91 @Before
92 public void setUp() {
93 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
94 .andReturn(APP_ID).anyTimes();
95 expect(mockCoreService.registerApplication(APP_ID.name()))
96 .andReturn(APP_ID).anyTimes();
97 expect(mockCoreService.getAppId(anyShort())).andReturn(APP_ID).anyTimes();
98 replay(mockCoreService);
99
100 context.registerService(CoreService.class, mockCoreService);
101 }
102
103 @Test
104 public void testCodec() {
105 assertNotNull(flowEntryCodec);
106 assertJsonEncodable(context, flowEntryCodec, FLOW_ENTRY);
107 }
108
109 @Test
110 public void testDecode() throws IOException {
111 InputStream jsonStream = FlowEntryCodec.class.getResourceAsStream(JSON_FILE);
112 JsonNode jsonString = context.mapper().readTree(jsonStream);
113
114 FlowEntry expected = flowEntryCodec.decode((ObjectNode) jsonString, context);
115 assertEquals(expected, FLOW_ENTRY);
116 }
117
118 @Test
119 public void testEncode() throws IOException {
120 InputStream jsonStream = FlowEntryCodec.class.getResourceAsStream(JSON_FILE);
121 ObjectNode jsonString = (ObjectNode) context.mapper().readTree(jsonStream);
122
123 ObjectNode expected = flowEntryCodec.encode(FLOW_ENTRY, context);
124 // only set by the internal FlowRule encoder, so should not appear in the JSON string
125 expected.remove("id");
126 expected.remove("appId");
127 expected.remove("tableName");
128
129 // only set by the FlowEntry encoder but not used for the decoder
130 // so should not appear in the JSON, or a decoding error occurs
131 expected.remove(FlowEntryCodec.GROUP_ID);
132 expected.remove(FlowEntryCodec.LAST_SEEN);
133
134 // assert equality of those values separately. see below
135 assertEquals(expected.get(FlowEntryCodec.LIFE).asLong(), jsonString.get(FlowEntryCodec.LIFE).asLong());
136 assertEquals(expected.get(FlowEntryCodec.PACKETS).asLong(), jsonString.get(FlowEntryCodec.PACKETS).asLong());
137 assertEquals(expected.get(FlowEntryCodec.BYTES).asLong(), jsonString.get(FlowEntryCodec.BYTES).asLong());
138
139 // if those numeric values are included in expected as a result of the encoding,
140 // AssertionError occurs even though both expected and jsonString are semantically identical
141 expected.remove(FlowEntryCodec.LIFE);
142 expected.remove(FlowEntryCodec.PACKETS);
143 expected.remove(FlowEntryCodec.BYTES);
144 jsonString.remove(FlowEntryCodec.LIFE);
145 jsonString.remove(FlowEntryCodec.PACKETS);
146 jsonString.remove(FlowEntryCodec.BYTES);
147
148 assertEquals(expected, jsonString);
149 }
150
151}