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