Jian Li | b45ffda | 2019-10-23 14:05:32 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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.openstackvtap.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.hamcrest.MatcherAssert; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.TpPort; |
| 27 | import org.onosproject.codec.CodecContext; |
| 28 | import org.onosproject.codec.JsonCodec; |
| 29 | import org.onosproject.codec.impl.CodecManager; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.openstackvtap.api.OpenstackVtapCriterion; |
| 32 | import org.onosproject.openstackvtap.impl.DefaultOpenstackVtapCriterion; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.util.HashMap; |
| 37 | import java.util.Map; |
| 38 | |
| 39 | import static org.easymock.EasyMock.createMock; |
| 40 | import static org.easymock.EasyMock.expect; |
| 41 | import static org.easymock.EasyMock.replay; |
| 42 | import static org.hamcrest.MatcherAssert.assertThat; |
| 43 | import static org.hamcrest.Matchers.is; |
| 44 | import static org.hamcrest.Matchers.notNullValue; |
| 45 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 46 | import static org.onosproject.openstackvtap.codec.OpenstackVtapCriterionJsonMatcher.matchVtapCriterion; |
| 47 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getProtocolStringFromType; |
| 48 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getProtocolTypeFromString; |
| 49 | |
| 50 | /** |
| 51 | * Unit tests for OpenstackVtapCriterion codec. |
| 52 | */ |
| 53 | public class OpenstackVtapCriterionCodecTest { |
| 54 | |
| 55 | MockCodecContext context; |
| 56 | JsonCodec<OpenstackVtapCriterion> vtapCriterionCodec; |
| 57 | |
| 58 | final CoreService mockCoreService = createMock(CoreService.class); |
| 59 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 60 | |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | context = new MockCodecContext(); |
| 64 | vtapCriterionCodec = new OpenstackVtapCriterionCodec(); |
| 65 | |
| 66 | assertThat(vtapCriterionCodec, notNullValue()); |
| 67 | |
| 68 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 69 | .andReturn(APP_ID).anyTimes(); |
| 70 | replay(mockCoreService); |
| 71 | context.registerService(CoreService.class, mockCoreService); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests the openstack vtap criterion encoding. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testOpenstackVtapCriterionEncode() { |
| 79 | OpenstackVtapCriterion criterion = DefaultOpenstackVtapCriterion.builder() |
| 80 | .srcIpPrefix(IpPrefix.valueOf(IpAddress.valueOf("10.10.10.10"), 32)) |
| 81 | .dstIpPrefix(IpPrefix.valueOf(IpAddress.valueOf("20.20.20.20"), 32)) |
| 82 | .ipProtocol(getProtocolTypeFromString("tcp")) |
| 83 | .srcTpPort(TpPort.tpPort(8080)) |
| 84 | .dstTpPort(TpPort.tpPort(9090)) |
| 85 | .build(); |
| 86 | |
| 87 | ObjectNode criterionJson = vtapCriterionCodec.encode(criterion, context); |
| 88 | assertThat(criterionJson, matchVtapCriterion(criterion)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Tests the openstack vtap criterion decoding. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testOpenstackVtapCriterionDecode() throws IOException { |
| 96 | OpenstackVtapCriterion criterion = getVtapCriterion("OpenstackVtapCriterion.json"); |
| 97 | |
| 98 | assertThat(criterion.srcIpPrefix().address().toString(), is("10.10.10.10")); |
| 99 | assertThat(criterion.dstIpPrefix().address().toString(), is("20.20.20.20")); |
| 100 | assertThat(getProtocolStringFromType(criterion.ipProtocol()), is("tcp")); |
| 101 | assertThat(criterion.srcTpPort().toInt(), is(8080)); |
| 102 | assertThat(criterion.dstTpPort().toInt(), is(9090)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Reads in an openstack vtap criterion from the given resource and decodes it. |
| 107 | * |
| 108 | * @param resourceName resource to use to read the JSON for the rule |
| 109 | * @return decoded openstack vtap criterion |
| 110 | * @throws IOException if processing the resource fails |
| 111 | */ |
| 112 | private OpenstackVtapCriterion getVtapCriterion(String resourceName) throws IOException { |
| 113 | InputStream jsonStream = OpenstackVtapCriterionCodecTest.class.getResourceAsStream(resourceName); |
| 114 | JsonNode json = context.mapper().readTree(jsonStream); |
| 115 | MatcherAssert.assertThat(json, notNullValue()); |
| 116 | OpenstackVtapCriterion criterion = vtapCriterionCodec.decode((ObjectNode) json, context); |
| 117 | assertThat(criterion, notNullValue()); |
| 118 | return criterion; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Mock codec context for use in codec unit tests. |
| 123 | */ |
| 124 | private class MockCodecContext implements CodecContext { |
| 125 | private final ObjectMapper mapper = new ObjectMapper(); |
| 126 | private final CodecManager manager = new CodecManager(); |
| 127 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 128 | |
| 129 | /** |
| 130 | * Constructs a new mock codec context. |
| 131 | */ |
| 132 | public MockCodecContext() { |
| 133 | manager.activate(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public ObjectMapper mapper() { |
| 138 | return mapper; |
| 139 | } |
| 140 | |
| 141 | @SuppressWarnings("unchecked") |
| 142 | @Override |
| 143 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 144 | if (entityClass == OpenstackVtapCriterion.class) { |
| 145 | return (JsonCodec<T>) vtapCriterionCodec; |
| 146 | } |
| 147 | return manager.getCodec(entityClass); |
| 148 | } |
| 149 | |
| 150 | @SuppressWarnings("unchecked") |
| 151 | @Override |
| 152 | public <T> T getService(Class<T> serviceClass) { |
| 153 | return (T) services.get(serviceClass); |
| 154 | } |
| 155 | |
| 156 | // for registering mock services |
| 157 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 158 | services.put(serviceClass, impl); |
| 159 | } |
| 160 | } |
| 161 | } |