Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.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 com.google.common.collect.ImmutableSet; |
| 22 | import org.hamcrest.MatcherAssert; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.IpPrefix; |
| 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.kubevirtnetworking.api.DefaultKubevirtNetwork; |
| 32 | import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute; |
| 33 | import org.onosproject.kubevirtnetworking.api.KubevirtIpPool; |
| 34 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 35 | |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
| 38 | import java.util.HashMap; |
| 39 | import java.util.Map; |
| 40 | |
| 41 | import static org.easymock.EasyMock.createMock; |
| 42 | import static org.easymock.EasyMock.expect; |
| 43 | import static org.easymock.EasyMock.replay; |
| 44 | import static org.hamcrest.MatcherAssert.assertThat; |
| 45 | import static org.hamcrest.Matchers.is; |
| 46 | import static org.hamcrest.Matchers.notNullValue; |
| 47 | import static org.onosproject.kubevirtnetworking.codec.KubevirtNetworkJsonMatcher.matchesKubevirtNetwork; |
| 48 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 49 | |
| 50 | /** |
| 51 | * Unit tests for KubevirtNetwork codec. |
| 52 | */ |
| 53 | public final class KubevirtNetworkCodecTest { |
| 54 | |
| 55 | MockCodecContext context; |
| 56 | |
| 57 | JsonCodec<KubevirtNetwork> kubevirtNetworkCodec; |
| 58 | JsonCodec<KubevirtHostRoute> kubevirtHostRouteCodec; |
| 59 | JsonCodec<KubevirtIpPool> kubevirtIpPoolCodec; |
| 60 | |
| 61 | final CoreService mockCoreService = createMock(CoreService.class); |
| 62 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 63 | |
| 64 | @Before |
| 65 | public void setUp() { |
| 66 | context = new MockCodecContext(); |
| 67 | kubevirtNetworkCodec = new KubevirtNetworkCodec(); |
| 68 | kubevirtHostRouteCodec = new KubevirtHostRouteCodec(); |
| 69 | kubevirtIpPoolCodec = new KubevirtIpPoolCodec(); |
| 70 | |
| 71 | assertThat(kubevirtNetworkCodec, notNullValue()); |
| 72 | assertThat(kubevirtHostRouteCodec, notNullValue()); |
| 73 | assertThat(kubevirtIpPoolCodec, notNullValue()); |
| 74 | |
| 75 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 76 | .andReturn(APP_ID).anyTimes(); |
| 77 | replay(mockCoreService); |
| 78 | context.registerService(CoreService.class, mockCoreService); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests the kubevirt network encoding. |
| 83 | */ |
| 84 | @Test |
| 85 | public void testKubevirtNetworkEncode() { |
| 86 | KubevirtHostRoute hostRoute1 = new KubevirtHostRoute(IpPrefix.valueOf("10.10.10.0/24"), |
| 87 | IpAddress.valueOf("20.20.20.1")); |
| 88 | KubevirtHostRoute hostRoute2 = new KubevirtHostRoute(IpPrefix.valueOf("20.20.20.0/24"), |
| 89 | IpAddress.valueOf("10.10.10.1")); |
| 90 | |
| 91 | KubevirtIpPool ipPool = new KubevirtIpPool(IpAddress.valueOf("10.10.10.100"), |
| 92 | IpAddress.valueOf("10.10.10.200")); |
| 93 | |
| 94 | KubevirtNetwork network = DefaultKubevirtNetwork.builder() |
| 95 | .networkId("net-1") |
| 96 | .name("net-1") |
| 97 | .type(KubevirtNetwork.Type.FLAT) |
| 98 | .gatewayIp(IpAddress.valueOf("10.10.10.1")) |
| 99 | .mtu(1500) |
| 100 | .cidr("10.10.10.0/24") |
| 101 | .hostRoutes(ImmutableSet.of(hostRoute1, hostRoute2)) |
| 102 | .ipPool(ipPool) |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 103 | .dnses(ImmutableSet.of(IpAddress.valueOf("8.8.8.8"))) |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 104 | .build(); |
| 105 | |
| 106 | ObjectNode networkJson = kubevirtNetworkCodec.encode(network, context); |
| 107 | assertThat(networkJson, matchesKubevirtNetwork(network)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests the kubevirt network decoding. |
| 112 | * |
| 113 | * @throws IOException io exception |
| 114 | */ |
| 115 | @Test |
| 116 | public void testKubevirtNetworkDecode() throws IOException { |
| 117 | KubevirtNetwork network = getKubevirtNetwork("KubevirtNetwork.json"); |
| 118 | |
| 119 | assertThat(network.networkId(), is("network-1")); |
| 120 | assertThat(network.name(), is("network-1")); |
| 121 | assertThat(network.type().name(), is("FLAT")); |
| 122 | assertThat(network.cidr(), is("10.10.0.0/24")); |
| 123 | assertThat(network.gatewayIp().toString(), is("10.10.0.1")); |
| 124 | assertThat(network.ipPool().start().toString(), is("10.10.10.100")); |
| 125 | assertThat(network.ipPool().end().toString(), is("10.10.10.200")); |
Jian Li | 4c35a26 | 2021-02-01 20:36:45 +0900 | [diff] [blame] | 126 | assertThat(network.dnses().size(), is(1)); |
| 127 | KubevirtHostRoute route = network.hostRoutes().stream().findFirst().orElse(null); |
| 128 | assertThat(route, is(new KubevirtHostRoute(IpPrefix.valueOf("10.10.10.0/24"), |
| 129 | IpAddress.valueOf("10.10.10.1")))); |
Jian Li | fc0b890 | 2021-01-12 16:34:49 +0900 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private KubevirtNetwork getKubevirtNetwork(String resourceName) throws IOException { |
| 133 | InputStream jsonStream = KubevirtNetworkCodecTest.class.getResourceAsStream(resourceName); |
| 134 | JsonNode json = context.mapper().readTree(jsonStream); |
| 135 | MatcherAssert.assertThat(json, notNullValue()); |
| 136 | KubevirtNetwork network = kubevirtNetworkCodec.decode((ObjectNode) json, context); |
| 137 | assertThat(network, notNullValue()); |
| 138 | return network; |
| 139 | } |
| 140 | |
| 141 | private class MockCodecContext implements CodecContext { |
| 142 | |
| 143 | private final ObjectMapper mapper = new ObjectMapper(); |
| 144 | private final CodecManager manager = new CodecManager(); |
| 145 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 146 | |
| 147 | /** |
| 148 | * Constructs a new mock codec context. |
| 149 | */ |
| 150 | public MockCodecContext() { |
| 151 | manager.activate(); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public ObjectMapper mapper() { |
| 156 | return mapper; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 161 | if (entityClass == KubevirtHostRoute.class) { |
| 162 | return (JsonCodec<T>) kubevirtHostRouteCodec; |
| 163 | } |
| 164 | |
| 165 | if (entityClass == KubevirtIpPool.class) { |
| 166 | return (JsonCodec<T>) kubevirtIpPoolCodec; |
| 167 | } |
| 168 | |
| 169 | return manager.getCodec(entityClass); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public <T> T getService(Class<T> serviceClass) { |
| 174 | return (T) services.get(serviceClass); |
| 175 | } |
| 176 | |
| 177 | // for registering mock services |
| 178 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 179 | services.put(serviceClass, impl); |
| 180 | } |
| 181 | } |
| 182 | } |