blob: 19c9671ed9cb38e22a9de7bf338363ad9adc3091 [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey39616f32015-05-14 15:43:00 -07003 *
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
Jian Liecb3c0f2015-12-15 10:07:49 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableList;
21import org.junit.Before;
Ray Milkey39616f32015-05-14 15:43:00 -070022import org.junit.Test;
Jian Liecb3c0f2015-12-15 10:07:49 -080023import org.onosproject.codec.JsonCodec;
24import org.onosproject.core.CoreService;
Yi Tsengfa394de2017-02-01 11:26:40 -080025import org.onosproject.core.GroupId;
Ray Milkey39616f32015-05-14 15:43:00 -070026import org.onosproject.net.NetTestTools;
Jian Liecb3c0f2015-12-15 10:07:49 -080027import org.onosproject.net.PortNumber;
Ray Milkey39616f32015-05-14 15:43:00 -070028import org.onosproject.net.flow.DefaultTrafficTreatment;
Jian Liecb3c0f2015-12-15 10:07:49 -080029import org.onosproject.net.flow.instructions.Instruction;
30import org.onosproject.net.flow.instructions.Instructions;
Ray Milkey39616f32015-05-14 15:43:00 -070031import org.onosproject.net.group.DefaultGroup;
32import org.onosproject.net.group.DefaultGroupBucket;
Jian Liecb3c0f2015-12-15 10:07:49 -080033import org.onosproject.net.group.Group;
Ray Milkey39616f32015-05-14 15:43:00 -070034import org.onosproject.net.group.GroupBucket;
35import org.onosproject.net.group.GroupBuckets;
Ray Milkey39616f32015-05-14 15:43:00 -070036
Jian Liecb3c0f2015-12-15 10:07:49 -080037import java.io.IOException;
38import java.io.InputStream;
Ray Milkey39616f32015-05-14 15:43:00 -070039
Jian Liecb3c0f2015-12-15 10:07:49 -080040import static org.easymock.EasyMock.createMock;
41import static org.easymock.EasyMock.expect;
42import static org.easymock.EasyMock.replay;
Ray Milkey39616f32015-05-14 15:43:00 -070043import static org.hamcrest.MatcherAssert.assertThat;
Jian Liecb3c0f2015-12-15 10:07:49 -080044import static org.hamcrest.Matchers.is;
45import static org.hamcrest.Matchers.notNullValue;
Charles Chanb3ef1fd2016-05-12 20:49:39 -070046import static org.hamcrest.Matchers.equalTo;
Ray Milkey39616f32015-05-14 15:43:00 -070047import static org.onosproject.codec.impl.GroupJsonMatcher.matchesGroup;
Jian Liecb3c0f2015-12-15 10:07:49 -080048import static org.onosproject.net.NetTestTools.APP_ID;
Jayasree Ghosh2d459852016-07-02 19:06:52 +053049import static org.onosproject.net.group.GroupDescription.Type.*;
Ray Milkey39616f32015-05-14 15:43:00 -070050
51/**
52 * Group codec unit tests.
53 */
54
55public class GroupCodecTest {
56
Jian Liecb3c0f2015-12-15 10:07:49 -080057 MockCodecContext context;
58 JsonCodec<Group> groupCodec;
59 final CoreService mockCoreService = createMock(CoreService.class);
60
61 /**
62 * Sets up for each test. Creates a context and fetches the flow rule
63 * codec.
64 */
65 @Before
66 public void setUp() {
67 context = new MockCodecContext();
68 groupCodec = context.codec(Group.class);
69 assertThat(groupCodec, notNullValue());
70
71 expect(mockCoreService.registerApplication(GroupCodec.REST_APP_ID))
72 .andReturn(APP_ID).anyTimes();
73 replay(mockCoreService);
74 context.registerService(CoreService.class, mockCoreService);
75 }
76
Ray Milkey39616f32015-05-14 15:43:00 -070077 @Test
Jian Liecb3c0f2015-12-15 10:07:49 -080078 public void codecEncodeTest() {
Charles Chanc88ebaa2018-11-01 20:08:47 -070079 GroupBucket bucket1 = DefaultGroupBucket.createAllGroupBucket(DefaultTrafficTreatment.emptyTreatment());
80 GroupBucket bucket2 = DefaultGroupBucket.createAllGroupBucket(DefaultTrafficTreatment.emptyTreatment());
81 GroupBucket bucket3 = DefaultGroupBucket.createIndirectGroupBucket(DefaultTrafficTreatment.emptyTreatment());
82 GroupBuckets allBuckets = new GroupBuckets(ImmutableList.of(bucket1, bucket2));
83 GroupBuckets indirectBuckets = new GroupBuckets(ImmutableList.of(bucket3));
Ray Milkey39616f32015-05-14 15:43:00 -070084
85 DefaultGroup group = new DefaultGroup(
Yi Tsengfa394de2017-02-01 11:26:40 -080086 new GroupId(1),
Ray Milkey39616f32015-05-14 15:43:00 -070087 NetTestTools.did("d1"),
Jayasree Ghosh2d459852016-07-02 19:06:52 +053088 ALL,
Charles Chanc88ebaa2018-11-01 20:08:47 -070089 allBuckets);
Jayasree Ghosh2d459852016-07-02 19:06:52 +053090 DefaultGroup group1 = new DefaultGroup(
Yi Tsengfa394de2017-02-01 11:26:40 -080091 new GroupId(2),
Jayasree Ghosh2d459852016-07-02 19:06:52 +053092 NetTestTools.did("d2"),
93 INDIRECT,
Charles Chanc88ebaa2018-11-01 20:08:47 -070094 indirectBuckets);
Ray Milkey39616f32015-05-14 15:43:00 -070095
96 MockCodecContext context = new MockCodecContext();
97 GroupCodec codec = new GroupCodec();
98 ObjectNode groupJson = codec.encode(group, context);
99
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530100 ObjectNode groupJsonIndirect = codec.encode(group1, context);
101
Ray Milkey39616f32015-05-14 15:43:00 -0700102 assertThat(groupJson, matchesGroup(group));
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530103 assertThat(groupJsonIndirect, matchesGroup(group1));
Ray Milkey39616f32015-05-14 15:43:00 -0700104 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800105
106 @Test
107 public void codecDecodeTest() throws IOException {
108 Group group = getGroup("simple-group.json");
109 checkCommonData(group);
110
111 assertThat(group.buckets().buckets().size(), is(1));
112 GroupBucket groupBucket = group.buckets().buckets().get(0);
113 assertThat(groupBucket.type().toString(), is("ALL"));
114 assertThat(groupBucket.treatment().allInstructions().size(), is(1));
115 Instruction instruction1 = groupBucket.treatment().allInstructions().get(0);
116 assertThat(instruction1.type(), is(Instruction.Type.OUTPUT));
117 assertThat(((Instructions.OutputInstruction) instruction1).port(), is(PortNumber.portNumber(2)));
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530118
Jian Liecb3c0f2015-12-15 10:07:49 -0800119 }
120
Charles Chanb3ef1fd2016-05-12 20:49:39 -0700121 @Test(expected = IllegalArgumentException.class)
122 public void invalidGroupTest() throws IOException {
123 Group group = getGroup("invalid-group.json");
124 }
125
Jian Liecb3c0f2015-12-15 10:07:49 -0800126 /**
127 * Checks that the data shared by all the resource is correct for a given group.
128 *
129 * @param group group to check
130 */
131 private void checkCommonData(Group group) {
132 assertThat(group.appId(), is(APP_ID));
133 assertThat(group.deviceId().toString(), is("of:0000000000000001"));
134 assertThat(group.type().toString(), is("ALL"));
Charles Chanb3ef1fd2016-05-12 20:49:39 -0700135 assertThat(group.appCookie().key(),
136 equalTo(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0xAB, (byte) 0xCD}));
Jian Liecb3c0f2015-12-15 10:07:49 -0800137 assertThat(group.id().id(), is(1));
138 }
139
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530140
Jian Liecb3c0f2015-12-15 10:07:49 -0800141 /**
142 * Reads in a group from the given resource and decodes it.
143 *
144 * @param resourceName resource to use to read the JSON for the rule
145 * @return decoded group
146 * @throws IOException if processing the resource fails
147 */
148 private Group getGroup(String resourceName) throws IOException {
149 InputStream jsonStream = GroupCodecTest.class
150 .getResourceAsStream(resourceName);
151 JsonNode json = context.mapper().readTree(jsonStream);
152 assertThat(json, notNullValue());
153 Group group = groupCodec.decode((ObjectNode) json, context);
154 assertThat(group, notNullValue());
155 return group;
156 }
Ray Milkey39616f32015-05-14 15:43:00 -0700157}