Jian Li | 810f58c | 2021-02-27 01:10:50 +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.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 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.MacAddress; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.MockCodecContext; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter; |
| 31 | import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter; |
| 32 | import org.onosproject.kubevirtnetworking.api.KubevirtRouter; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | |
| 37 | import static junit.framework.TestCase.assertEquals; |
| 38 | import static junit.framework.TestCase.assertTrue; |
| 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.notNullValue; |
| 44 | import static org.onosproject.kubevirtnetworking.codec.KubevirtRouterJsonMatcher.matchesKubevirtRouter; |
| 45 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 46 | |
| 47 | /** |
| 48 | * Unit tests for KubevirtRouter codec. |
| 49 | */ |
| 50 | public final class KubevirtRouterCodecTest { |
| 51 | |
| 52 | MockCodecContext context; |
| 53 | |
| 54 | JsonCodec<KubevirtRouter> kubevirtRouterCodec; |
| 55 | |
| 56 | final CoreService mockCoreService = createMock(CoreService.class); |
| 57 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 58 | |
| 59 | @Before |
| 60 | public void setUp() { |
| 61 | context = new MockCodecContext(); |
| 62 | kubevirtRouterCodec = new KubevirtRouterCodec(); |
| 63 | |
| 64 | assertThat(kubevirtRouterCodec, notNullValue()); |
| 65 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 66 | .andReturn(APP_ID).anyTimes(); |
| 67 | replay(mockCoreService); |
| 68 | context.registerService(CoreService.class, mockCoreService); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Tests the kubevirt router encoding. |
| 73 | */ |
| 74 | @Test |
| 75 | public void testKubevirtRouterEncode() { |
| 76 | KubevirtPeerRouter peerRouter = new KubevirtPeerRouter(IpAddress.valueOf("10.10.10.10"), |
| 77 | MacAddress.valueOf("11:22:33:44:55:66")); |
| 78 | |
| 79 | KubevirtRouter router = DefaultKubevirtRouter.builder() |
| 80 | .name("router-1") |
| 81 | .enableSnat(true) |
Jian Li | 9756cec | 2021-03-16 16:06:33 +0900 | [diff] [blame] | 82 | .mac(MacAddress.valueOf("11:22:33:44:55:66")) |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 83 | .description("router-1") |
| 84 | .internal(ImmutableSet.of("vlan-1")) |
| 85 | .external(ImmutableMap.of("10.10.10.20", "flat-1")) |
| 86 | .peerRouter(peerRouter) |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 87 | .electedGateway("gatewayNode") |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 88 | .build(); |
| 89 | |
| 90 | ObjectNode routerJson = kubevirtRouterCodec.encode(router, context); |
| 91 | assertThat(routerJson, matchesKubevirtRouter(router)); |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testKubevirtRouterDecode() throws IOException { |
| 96 | KubevirtRouter router = getKubevirtRouter("KubevirtRouter.json"); |
| 97 | |
| 98 | assertEquals("router-1", router.name()); |
| 99 | assertEquals("Example Virtual Router", router.description()); |
| 100 | assertTrue(router.enableSnat()); |
Jian Li | 9756cec | 2021-03-16 16:06:33 +0900 | [diff] [blame] | 101 | assertEquals("11:22:33:44:55:66", router.mac().toString()); |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 102 | assertEquals("192.168.10.5", |
| 103 | router.external().keySet().stream().findAny().orElse(null)); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 104 | assertEquals("external-network", router.external().get("192.168.10.5")); |
| 105 | assertTrue(router.internal().contains("vxlan-network-1")); |
| 106 | assertTrue(router.internal().contains("vxlan-network-2")); |
| 107 | assertEquals("192.168.10.1", router.peerRouter().ipAddress().toString()); |
Daniel Park | 2884b23 | 2021-03-04 18:58:47 +0900 | [diff] [blame] | 108 | assertEquals("gatewayNode", router.electedGateway()); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | private KubevirtRouter getKubevirtRouter(String resourceName) throws IOException { |
| 112 | InputStream jsonStream = KubevirtRouterCodecTest.class.getResourceAsStream(resourceName); |
| 113 | JsonNode json = context.mapper().readTree(jsonStream); |
| 114 | MatcherAssert.assertThat(json, notNullValue()); |
| 115 | KubevirtRouter router = kubevirtRouterCodec.decode((ObjectNode) json, context); |
| 116 | assertThat(router, notNullValue()); |
| 117 | return router; |
| 118 | } |
| 119 | } |