blob: 9037dcc0236ed3da44a94cab084a3821b1154b48 [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Ray Milkey39616f32015-05-14 15:43:00 -070025import org.onosproject.core.DefaultGroupId;
26import 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() {
Ray Milkey39616f32015-05-14 15:43:00 -070079 GroupBucket bucket1 = DefaultGroupBucket
80 .createSelectGroupBucket(DefaultTrafficTreatment.emptyTreatment());
81 GroupBucket bucket2 = DefaultGroupBucket
82 .createIndirectGroupBucket(DefaultTrafficTreatment.emptyTreatment());
83 GroupBuckets buckets = new GroupBuckets(ImmutableList.of(bucket1, bucket2));
Jayasree Ghosh2d459852016-07-02 19:06:52 +053084 GroupBuckets bucketsIndirect = new GroupBuckets(ImmutableList.of(bucket2));
Ray Milkey39616f32015-05-14 15:43:00 -070085
86 DefaultGroup group = new DefaultGroup(
87 new DefaultGroupId(1),
88 NetTestTools.did("d1"),
Jayasree Ghosh2d459852016-07-02 19:06:52 +053089 ALL,
Ray Milkey39616f32015-05-14 15:43:00 -070090 buckets);
Jayasree Ghosh2d459852016-07-02 19:06:52 +053091 DefaultGroup group1 = new DefaultGroup(
92 new DefaultGroupId(2),
93 NetTestTools.did("d2"),
94 INDIRECT,
95 bucketsIndirect);
Ray Milkey39616f32015-05-14 15:43:00 -070096
97 MockCodecContext context = new MockCodecContext();
98 GroupCodec codec = new GroupCodec();
99 ObjectNode groupJson = codec.encode(group, context);
100
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530101 ObjectNode groupJsonIndirect = codec.encode(group1, context);
102
Ray Milkey39616f32015-05-14 15:43:00 -0700103 assertThat(groupJson, matchesGroup(group));
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530104 assertThat(groupJsonIndirect, matchesGroup(group1));
Ray Milkey39616f32015-05-14 15:43:00 -0700105 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800106
107 @Test
108 public void codecDecodeTest() throws IOException {
109 Group group = getGroup("simple-group.json");
110 checkCommonData(group);
111
112 assertThat(group.buckets().buckets().size(), is(1));
113 GroupBucket groupBucket = group.buckets().buckets().get(0);
114 assertThat(groupBucket.type().toString(), is("ALL"));
115 assertThat(groupBucket.treatment().allInstructions().size(), is(1));
116 Instruction instruction1 = groupBucket.treatment().allInstructions().get(0);
117 assertThat(instruction1.type(), is(Instruction.Type.OUTPUT));
118 assertThat(((Instructions.OutputInstruction) instruction1).port(), is(PortNumber.portNumber(2)));
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530119
Jian Liecb3c0f2015-12-15 10:07:49 -0800120 }
121
Charles Chanb3ef1fd2016-05-12 20:49:39 -0700122 @Test(expected = IllegalArgumentException.class)
123 public void invalidGroupTest() throws IOException {
124 Group group = getGroup("invalid-group.json");
125 }
126
Jian Liecb3c0f2015-12-15 10:07:49 -0800127 /**
128 * Checks that the data shared by all the resource is correct for a given group.
129 *
130 * @param group group to check
131 */
132 private void checkCommonData(Group group) {
133 assertThat(group.appId(), is(APP_ID));
134 assertThat(group.deviceId().toString(), is("of:0000000000000001"));
135 assertThat(group.type().toString(), is("ALL"));
Charles Chanb3ef1fd2016-05-12 20:49:39 -0700136 assertThat(group.appCookie().key(),
137 equalTo(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0xAB, (byte) 0xCD}));
Jian Liecb3c0f2015-12-15 10:07:49 -0800138 assertThat(group.id().id(), is(1));
139 }
140
Jayasree Ghosh2d459852016-07-02 19:06:52 +0530141
Jian Liecb3c0f2015-12-15 10:07:49 -0800142 /**
143 * Reads in a group from the given resource and decodes it.
144 *
145 * @param resourceName resource to use to read the JSON for the rule
146 * @return decoded group
147 * @throws IOException if processing the resource fails
148 */
149 private Group getGroup(String resourceName) throws IOException {
150 InputStream jsonStream = GroupCodecTest.class
151 .getResourceAsStream(resourceName);
152 JsonNode json = context.mapper().readTree(jsonStream);
153 assertThat(json, notNullValue());
154 Group group = groupCodec.decode((ObjectNode) json, context);
155 assertThat(group, notNullValue());
156 return group;
157 }
Ray Milkey39616f32015-05-14 15:43:00 -0700158}