blob: 93473a516e87c7927c7eaec9816a7a45fefcb6c3 [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;
36import org.onosproject.net.group.GroupDescription;
37
Jian Liecb3c0f2015-12-15 10:07:49 -080038import java.io.IOException;
39import java.io.InputStream;
Ray Milkey39616f32015-05-14 15:43:00 -070040
Jian Liecb3c0f2015-12-15 10:07:49 -080041import static org.easymock.EasyMock.createMock;
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.replay;
Ray Milkey39616f32015-05-14 15:43:00 -070044import static org.hamcrest.MatcherAssert.assertThat;
Jian Liecb3c0f2015-12-15 10:07:49 -080045import static org.hamcrest.Matchers.is;
46import static org.hamcrest.Matchers.notNullValue;
Charles Chanb3ef1fd2016-05-12 20:49:39 -070047import static org.hamcrest.Matchers.equalTo;
Ray Milkey39616f32015-05-14 15:43:00 -070048import static org.onosproject.codec.impl.GroupJsonMatcher.matchesGroup;
Jian Liecb3c0f2015-12-15 10:07:49 -080049import static org.onosproject.net.NetTestTools.APP_ID;
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));
84
85
86 DefaultGroup group = new DefaultGroup(
87 new DefaultGroupId(1),
88 NetTestTools.did("d1"),
89 GroupDescription.Type.INDIRECT,
90 buckets);
91
92 MockCodecContext context = new MockCodecContext();
93 GroupCodec codec = new GroupCodec();
94 ObjectNode groupJson = codec.encode(group, context);
95
96 assertThat(groupJson, matchesGroup(group));
97 }
Jian Liecb3c0f2015-12-15 10:07:49 -080098
99 @Test
100 public void codecDecodeTest() throws IOException {
101 Group group = getGroup("simple-group.json");
102 checkCommonData(group);
103
104 assertThat(group.buckets().buckets().size(), is(1));
105 GroupBucket groupBucket = group.buckets().buckets().get(0);
106 assertThat(groupBucket.type().toString(), is("ALL"));
107 assertThat(groupBucket.treatment().allInstructions().size(), is(1));
108 Instruction instruction1 = groupBucket.treatment().allInstructions().get(0);
109 assertThat(instruction1.type(), is(Instruction.Type.OUTPUT));
110 assertThat(((Instructions.OutputInstruction) instruction1).port(), is(PortNumber.portNumber(2)));
111 }
112
Charles Chanb3ef1fd2016-05-12 20:49:39 -0700113 @Test(expected = IllegalArgumentException.class)
114 public void invalidGroupTest() throws IOException {
115 Group group = getGroup("invalid-group.json");
116 }
117
Jian Liecb3c0f2015-12-15 10:07:49 -0800118 /**
119 * Checks that the data shared by all the resource is correct for a given group.
120 *
121 * @param group group to check
122 */
123 private void checkCommonData(Group group) {
124 assertThat(group.appId(), is(APP_ID));
125 assertThat(group.deviceId().toString(), is("of:0000000000000001"));
126 assertThat(group.type().toString(), is("ALL"));
Charles Chanb3ef1fd2016-05-12 20:49:39 -0700127 assertThat(group.appCookie().key(),
128 equalTo(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0xAB, (byte) 0xCD}));
Jian Liecb3c0f2015-12-15 10:07:49 -0800129 assertThat(group.id().id(), is(1));
130 }
131
132 /**
133 * Reads in a group from the given resource and decodes it.
134 *
135 * @param resourceName resource to use to read the JSON for the rule
136 * @return decoded group
137 * @throws IOException if processing the resource fails
138 */
139 private Group getGroup(String resourceName) throws IOException {
140 InputStream jsonStream = GroupCodecTest.class
141 .getResourceAsStream(resourceName);
142 JsonNode json = context.mapper().readTree(jsonStream);
143 assertThat(json, notNullValue());
144 Group group = groupCodec.decode((ObjectNode) json, context);
145 assertThat(group, notNullValue());
146 return group;
147 }
Ray Milkey39616f32015-05-14 15:43:00 -0700148}