blob: f78fb80f20a6e775af6fe4290a466cda77f68d98 [file] [log] [blame]
Wailok Shumee90c132021-03-11 21:00:11 +08001/*
2 * Copyright 2021-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 */
pierventreb37a11a2021-03-18 16:50:04 +010016package org.onosproject.segmentrouting.policy.impl;
Wailok Shumee90c132021-03-11 21:00:11 +080017
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import junit.framework.TestCase;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.Ip4Address;
25import org.onlab.packet.TpPort;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.codec.impl.MockCodecContext;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.TrafficSelector;
pierventreb37a11a2021-03-18 16:50:04 +010031import org.onosproject.segmentrouting.policy.api.PolicyId;
32import org.onosproject.segmentrouting.policy.api.TrafficMatch;
Wailok Shumee90c132021-03-11 21:00:11 +080033
34import java.io.InputStream;
35
36public class TrafficMatchCodecTest extends TestCase {
37 private TrafficMatch trafficMatch;
38 private TrafficSelector trafficSelector;
39 private PolicyId policyId;
40
41 private CodecContext context;
42 private JsonCodec<TrafficMatch> codec;
43
44 @Before
45 public void setUp() throws Exception {
46 context = new MockCodecContext();
47 codec = new TrafficMatchCodec();
48
49 trafficSelector = DefaultTrafficSelector.builder()
50 .matchIPProtocol((byte) 0x06)
51 .matchIPSrc(Ip4Address.valueOf("10.0.0.1").toIpPrefix())
52 .matchIPDst(Ip4Address.valueOf("10.0.0.2").toIpPrefix())
53 .matchTcpSrc(TpPort.tpPort(80))
54 .matchTcpDst(TpPort.tpPort(81))
55 .build();
56 policyId = PolicyId.of("DROP");
57
58 trafficMatch = new TrafficMatch(trafficSelector, policyId);
59 }
60
61 @Test
62 public void testEncode() throws Exception {
63 ObjectMapper mapper = new ObjectMapper();
64
65 InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/trafficmatch.json");
66 JsonNode expected = mapper.readTree(jsonStream1);
67
68 JsonNode actual = codec.encode(trafficMatch, context);
69
70 assertEquals(expected.get(TrafficMatchCodec.TRAFFIC_MATCH_ID), actual.get(TrafficMatchCodec.TRAFFIC_MATCH_ID));
71 assertEquals(expected.get(TrafficMatchCodec.POLICY_ID), actual.get(TrafficMatchCodec.POLICY_ID));
72 for (int i = 0; i < trafficMatch.trafficSelector().criteria().size(); i++) {
73 assertEquals(expected.get(TrafficMatchCodec.TRAFFIC_SELECTOR).get(i),
74 actual.get(TrafficMatchCodec.TRAFFIC_SELECTOR).get(i));
75 }
76 }
77
78 @Test
79 public void testDecode() throws Exception {
80 ObjectMapper mapper = new ObjectMapper();
81
82 InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/trafficmatch.json");
83 ObjectNode json = mapper.readTree(jsonStream1).deepCopy();
84
85 TrafficMatch actual = codec.decode(json, context);
86
87 assertEquals(trafficMatch, actual);
88 }
89}