blob: 10e1d079b3ec9c92c29645c7d8779bdcbbbbb8d1 [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 Shum37dd29a2021-04-27 18:13:55 +080033import org.onosproject.segmentrouting.policy.api.TrafficMatchPriority;
Wailok Shumee90c132021-03-11 21:00:11 +080034
35import java.io.InputStream;
36
37public class TrafficMatchCodecTest extends TestCase {
38 private TrafficMatch trafficMatch;
39 private TrafficSelector trafficSelector;
40 private PolicyId policyId;
41
42 private CodecContext context;
43 private JsonCodec<TrafficMatch> codec;
44
45 @Before
46 public void setUp() throws Exception {
47 context = new MockCodecContext();
48 codec = new TrafficMatchCodec();
49
50 trafficSelector = DefaultTrafficSelector.builder()
51 .matchIPProtocol((byte) 0x06)
52 .matchIPSrc(Ip4Address.valueOf("10.0.0.1").toIpPrefix())
53 .matchIPDst(Ip4Address.valueOf("10.0.0.2").toIpPrefix())
54 .matchTcpSrc(TpPort.tpPort(80))
55 .matchTcpDst(TpPort.tpPort(81))
56 .build();
57 policyId = PolicyId.of("DROP");
Wailok Shum37dd29a2021-04-27 18:13:55 +080058 TrafficMatchPriority trafficMatchPriority = new TrafficMatchPriority(60000);
59 trafficMatch = new TrafficMatch(trafficSelector, policyId, trafficMatchPriority);
Wailok Shumee90c132021-03-11 21:00:11 +080060 }
61
62 @Test
63 public void testEncode() throws Exception {
64 ObjectMapper mapper = new ObjectMapper();
65
66 InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/trafficmatch.json");
67 JsonNode expected = mapper.readTree(jsonStream1);
68
69 JsonNode actual = codec.encode(trafficMatch, context);
70
71 assertEquals(expected.get(TrafficMatchCodec.TRAFFIC_MATCH_ID), actual.get(TrafficMatchCodec.TRAFFIC_MATCH_ID));
72 assertEquals(expected.get(TrafficMatchCodec.POLICY_ID), actual.get(TrafficMatchCodec.POLICY_ID));
73 for (int i = 0; i < trafficMatch.trafficSelector().criteria().size(); i++) {
74 assertEquals(expected.get(TrafficMatchCodec.TRAFFIC_SELECTOR).get(i),
75 actual.get(TrafficMatchCodec.TRAFFIC_SELECTOR).get(i));
76 }
77 }
78
79 @Test
80 public void testDecode() throws Exception {
81 ObjectMapper mapper = new ObjectMapper();
82
83 InputStream jsonStream1 = RedirectPolicyCodecTest.class.getResourceAsStream("/trafficmatch.json");
84 ObjectNode json = mapper.readTree(jsonStream1).deepCopy();
85
86 TrafficMatch actual = codec.decode(json, context);
87
88 assertEquals(trafficMatch, actual);
89 }
90}