blob: ee84e59f34a37f4dcdf667e400cdec36420948e8 [file] [log] [blame]
Jian Li4a3fffa2018-06-10 23:12:40 +09001/*
2 * Copyright 2018-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.openstacktelemetry.codec;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
boyoung2a8549d22018-11-23 20:42:37 +090020import com.google.common.testing.EqualsTester;
Jian Li4a3fffa2018-06-10 23:12:40 +090021import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.TpPort;
27import org.onlab.packet.VlanId;
28import org.onosproject.codec.CodecContext;
29import org.onosproject.codec.JsonCodec;
30import org.onosproject.codec.impl.CodecManager;
31import org.onosproject.core.CoreService;
32import org.onosproject.net.DeviceId;
33import org.onosproject.openstacktelemetry.api.FlowInfo;
34import org.onosproject.openstacktelemetry.api.StatsInfo;
35import org.onosproject.openstacktelemetry.impl.DefaultFlowInfo;
36import org.onosproject.openstacktelemetry.impl.DefaultStatsInfo;
37
38import java.util.HashMap;
39import java.util.Map;
40
41import static org.easymock.EasyMock.createMock;
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.replay;
44import static org.hamcrest.MatcherAssert.assertThat;
45import static org.hamcrest.Matchers.notNullValue;
46import static org.onosproject.net.NetTestTools.APP_ID;
47import static org.onosproject.openstacktelemetry.codec.FlowInfoJsonMatcher.matchesFlowInfo;
48
49/**
50 * Unit tests for FlowInfo codec.
51 */
52public class FlowInfoJsonCodecTest {
53 MockCodecContext context;
54 JsonCodec<FlowInfo> flowInfoCodec;
55 JsonCodec<StatsInfo> statsInfoCodec;
56 final CoreService mockCoreService = createMock(CoreService.class);
57 private static final String REST_APP_ID = "org.onosproject.rest";
58
59 private static final int INPUT_INTERFACE_ID = 1;
60 private static final int OUTPUT_INTERFACE_ID = 2;
61
62 private static final int VLAN_ID = 1;
boyoung2a8549d22018-11-23 20:42:37 +090063 private static final short VXLAN_ID = 10;
Jian Li4a3fffa2018-06-10 23:12:40 +090064 private static final int PROTOCOL = 1;
65 private static final int FLOW_TYPE = 1;
Jian Li0bbbb1c2018-06-22 22:01:17 +090066 private static final String DEVICE_ID = "of:00000000000000a1";
Jian Li4a3fffa2018-06-10 23:12:40 +090067
68 private static final String SRC_IP_ADDRESS = "10.10.10.1";
69 private static final int SRC_IP_PREFIX = 24;
70 private static final String DST_IP_ADDRESS = "20.20.20.1";
71 private static final int DST_IP_PREFIX = 24;
72 private static final int SRC_PORT = 1000;
73 private static final int DST_PORT = 2000;
74 private static final String SRC_MAC_ADDRESS = "AA:BB:CC:DD:EE:FF";
75 private static final String DST_MAC_ADDRESS = "FF:EE:DD:CC:BB:AA";
76
77 private static final long LONG_VALUE = 1L;
78 private static final int INTEGER_VALUE = 1;
79 private static final short SHORT_VALUE = (short) 1;
80
Jian Liae3fcff2018-07-30 11:55:44 +090081 /**
82 * Initial setup for this unit test.
83 */
Jian Li4a3fffa2018-06-10 23:12:40 +090084 @Before
85 public void setUp() {
86 context = new MockCodecContext();
87 flowInfoCodec = new FlowInfoJsonCodec();
88 statsInfoCodec = new StatsInfoJsonCodec();
89
90 assertThat(flowInfoCodec, notNullValue());
91 assertThat(statsInfoCodec, notNullValue());
92
93 expect(mockCoreService.registerApplication(REST_APP_ID))
94 .andReturn(APP_ID).anyTimes();
95 replay(mockCoreService);
96 context.registerService(CoreService.class, mockCoreService);
97 }
98
99 /**
100 * Tests the flow info encoding.
101 */
102 @Test
103 public void testEncode() {
104 StatsInfo statsInfo = new DefaultStatsInfo.DefaultBuilder()
105 .withStartupTime(LONG_VALUE)
106 .withFstPktArrTime(LONG_VALUE)
107 .withLstPktOffset(INTEGER_VALUE)
108 .withPrevAccBytes(LONG_VALUE)
109 .withPrevAccPkts(INTEGER_VALUE)
110 .withCurrAccBytes(LONG_VALUE)
111 .withCurrAccPkts(INTEGER_VALUE)
112 .withErrorPkts(SHORT_VALUE)
113 .withDropPkts(SHORT_VALUE)
114 .build();
115 FlowInfo flowInfo = new DefaultFlowInfo.DefaultBuilder()
116 .withFlowType((byte) FLOW_TYPE)
117 .withDeviceId(DeviceId.deviceId(DEVICE_ID))
118 .withInputInterfaceId(INPUT_INTERFACE_ID)
119 .withOutputInterfaceId(OUTPUT_INTERFACE_ID)
120 .withVlanId(VlanId.vlanId((short) VLAN_ID))
121 .withSrcIp(IpPrefix.valueOf(
122 IpAddress.valueOf(SRC_IP_ADDRESS), SRC_IP_PREFIX))
123 .withDstIp(IpPrefix.valueOf(
124 IpAddress.valueOf(DST_IP_ADDRESS), DST_IP_PREFIX))
125 .withSrcPort(TpPort.tpPort(SRC_PORT))
126 .withDstPort(TpPort.tpPort(DST_PORT))
127 .withProtocol((byte) PROTOCOL)
128 .withSrcMac(MacAddress.valueOf(SRC_MAC_ADDRESS))
129 .withDstMac(MacAddress.valueOf(DST_MAC_ADDRESS))
130 .withStatsInfo(statsInfo)
131 .build();
132
133 ObjectNode nodeJson = flowInfoCodec.encode(flowInfo, context);
134 assertThat(nodeJson, matchesFlowInfo(flowInfo));
boyoung2a8549d22018-11-23 20:42:37 +0900135
136 FlowInfo flowInfoDecoded = flowInfoCodec.decode(nodeJson, context);
137 new EqualsTester().addEqualityGroup(flowInfo, flowInfoDecoded).testEquals();
Jian Li4a3fffa2018-06-10 23:12:40 +0900138 }
139
140 /**
141 * Mock codec context for use in codec unit tests.
142 */
143 private class MockCodecContext implements CodecContext {
144 private final ObjectMapper mapper = new ObjectMapper();
145 private final CodecManager manager = new CodecManager();
146 private final Map<Class<?>, Object> services = new HashMap<>();
147
148 /**
149 * Constructs a new mock codec context.
150 */
151 public MockCodecContext() {
152 manager.activate();
153 }
154
155 @Override
156 public ObjectMapper mapper() {
157 return mapper;
158 }
159
160 @Override
161 @SuppressWarnings("unchecked")
162 public <T> JsonCodec<T> codec(Class<T> entityClass) {
163 if (entityClass == FlowInfo.class) {
164 return (JsonCodec<T>) flowInfoCodec;
165 }
166 if (entityClass == StatsInfo.class) {
167 return (JsonCodec<T>) statsInfoCodec;
168 }
169 return manager.getCodec(entityClass);
170 }
171
172 @SuppressWarnings("unchecked")
173 @Override
174 public <T> T getService(Class<T> serviceClass) {
175 return (T) services.get(serviceClass);
176 }
177
178 // for registering mock services
179 public <T> void registerService(Class<T> serviceClass, T impl) {
180 services.put(serviceClass, impl);
181 }
182 }
183}