blob: 2a3b7d403e8b830eed921a20eda7e92db7a8a712 [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;
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;
Ray Milkey39616f32015-05-14 15:43:00 -070049
50/**
51 * Group codec unit tests.
52 */
53
54public class GroupCodecTest {
55
Jian Liecb3c0f2015-12-15 10:07:49 -080056 MockCodecContext context;
57 JsonCodec<Group> groupCodec;
58 final CoreService mockCoreService = createMock(CoreService.class);
59
60 /**
61 * Sets up for each test. Creates a context and fetches the flow rule
62 * codec.
63 */
64 @Before
65 public void setUp() {
66 context = new MockCodecContext();
67 groupCodec = context.codec(Group.class);
68 assertThat(groupCodec, notNullValue());
69
70 expect(mockCoreService.registerApplication(GroupCodec.REST_APP_ID))
71 .andReturn(APP_ID).anyTimes();
72 replay(mockCoreService);
73 context.registerService(CoreService.class, mockCoreService);
74 }
75
Ray Milkey39616f32015-05-14 15:43:00 -070076 @Test
Jian Liecb3c0f2015-12-15 10:07:49 -080077 public void codecEncodeTest() {
Ray Milkey39616f32015-05-14 15:43:00 -070078 GroupBucket bucket1 = DefaultGroupBucket
79 .createSelectGroupBucket(DefaultTrafficTreatment.emptyTreatment());
80 GroupBucket bucket2 = DefaultGroupBucket
81 .createIndirectGroupBucket(DefaultTrafficTreatment.emptyTreatment());
82 GroupBuckets buckets = new GroupBuckets(ImmutableList.of(bucket1, bucket2));
83
84
85 DefaultGroup group = new DefaultGroup(
86 new DefaultGroupId(1),
87 NetTestTools.did("d1"),
88 GroupDescription.Type.INDIRECT,
89 buckets);
90
91 MockCodecContext context = new MockCodecContext();
92 GroupCodec codec = new GroupCodec();
93 ObjectNode groupJson = codec.encode(group, context);
94
95 assertThat(groupJson, matchesGroup(group));
96 }
Jian Liecb3c0f2015-12-15 10:07:49 -080097
98 @Test
99 public void codecDecodeTest() throws IOException {
100 Group group = getGroup("simple-group.json");
101 checkCommonData(group);
102
103 assertThat(group.buckets().buckets().size(), is(1));
104 GroupBucket groupBucket = group.buckets().buckets().get(0);
105 assertThat(groupBucket.type().toString(), is("ALL"));
106 assertThat(groupBucket.treatment().allInstructions().size(), is(1));
107 Instruction instruction1 = groupBucket.treatment().allInstructions().get(0);
108 assertThat(instruction1.type(), is(Instruction.Type.OUTPUT));
109 assertThat(((Instructions.OutputInstruction) instruction1).port(), is(PortNumber.portNumber(2)));
110 }
111
112 /**
113 * Checks that the data shared by all the resource is correct for a given group.
114 *
115 * @param group group to check
116 */
117 private void checkCommonData(Group group) {
118 assertThat(group.appId(), is(APP_ID));
119 assertThat(group.deviceId().toString(), is("of:0000000000000001"));
120 assertThat(group.type().toString(), is("ALL"));
121 assertThat(group.appCookie().key(), is("1".getBytes()));
122 assertThat(group.id().id(), is(1));
123 }
124
125 /**
126 * Reads in a group from the given resource and decodes it.
127 *
128 * @param resourceName resource to use to read the JSON for the rule
129 * @return decoded group
130 * @throws IOException if processing the resource fails
131 */
132 private Group getGroup(String resourceName) throws IOException {
133 InputStream jsonStream = GroupCodecTest.class
134 .getResourceAsStream(resourceName);
135 JsonNode json = context.mapper().readTree(jsonStream);
136 assertThat(json, notNullValue());
137 Group group = groupCodec.decode((ObjectNode) json, context);
138 assertThat(group, notNullValue());
139 return group;
140 }
Ray Milkey39616f32015-05-14 15:43:00 -0700141}