Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import org.junit.Before; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 22 | import org.junit.Test; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.core.CoreService; |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 25 | import org.onosproject.core.GroupId; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 26 | import org.onosproject.net.NetTestTools; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 27 | import org.onosproject.net.PortNumber; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.instructions.Instruction; |
| 30 | import org.onosproject.net.flow.instructions.Instructions; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 31 | import org.onosproject.net.group.DefaultGroup; |
| 32 | import org.onosproject.net.group.DefaultGroupBucket; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 33 | import org.onosproject.net.group.Group; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 34 | import org.onosproject.net.group.GroupBucket; |
| 35 | import org.onosproject.net.group.GroupBuckets; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 36 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 39 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 40 | import static org.easymock.EasyMock.createMock; |
| 41 | import static org.easymock.EasyMock.expect; |
| 42 | import static org.easymock.EasyMock.replay; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 43 | import static org.hamcrest.MatcherAssert.assertThat; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 44 | import static org.hamcrest.Matchers.is; |
| 45 | import static org.hamcrest.Matchers.notNullValue; |
Charles Chan | b3ef1fd | 2016-05-12 20:49:39 -0700 | [diff] [blame] | 46 | import static org.hamcrest.Matchers.equalTo; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 47 | import static org.onosproject.codec.impl.GroupJsonMatcher.matchesGroup; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 48 | import static org.onosproject.net.NetTestTools.APP_ID; |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 49 | import static org.onosproject.net.group.GroupDescription.Type.*; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Group codec unit tests. |
| 53 | */ |
| 54 | |
| 55 | public class GroupCodecTest { |
| 56 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 57 | 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 Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 77 | @Test |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 78 | public void codecEncodeTest() { |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 79 | 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 Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 84 | GroupBuckets bucketsIndirect = new GroupBuckets(ImmutableList.of(bucket2)); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 85 | |
| 86 | DefaultGroup group = new DefaultGroup( |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 87 | new GroupId(1), |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 88 | NetTestTools.did("d1"), |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 89 | ALL, |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 90 | buckets); |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 91 | DefaultGroup group1 = new DefaultGroup( |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 92 | new GroupId(2), |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 93 | NetTestTools.did("d2"), |
| 94 | INDIRECT, |
| 95 | bucketsIndirect); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 96 | |
| 97 | MockCodecContext context = new MockCodecContext(); |
| 98 | GroupCodec codec = new GroupCodec(); |
| 99 | ObjectNode groupJson = codec.encode(group, context); |
| 100 | |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 101 | ObjectNode groupJsonIndirect = codec.encode(group1, context); |
| 102 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 103 | assertThat(groupJson, matchesGroup(group)); |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 104 | assertThat(groupJsonIndirect, matchesGroup(group1)); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 105 | } |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 106 | |
| 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 Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 119 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Charles Chan | b3ef1fd | 2016-05-12 20:49:39 -0700 | [diff] [blame] | 122 | @Test(expected = IllegalArgumentException.class) |
| 123 | public void invalidGroupTest() throws IOException { |
| 124 | Group group = getGroup("invalid-group.json"); |
| 125 | } |
| 126 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 127 | /** |
| 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 Chan | b3ef1fd | 2016-05-12 20:49:39 -0700 | [diff] [blame] | 136 | assertThat(group.appCookie().key(), |
| 137 | equalTo(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0xAB, (byte) 0xCD})); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 138 | assertThat(group.id().id(), is(1)); |
| 139 | } |
| 140 | |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 141 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 142 | /** |
| 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 Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 158 | } |