Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.kubevirtnode.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.ImmutableList; |
| 22 | import org.hamcrest.MatcherAssert; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.kubevirtnode.api.DefaultKubevirtNode; |
| 31 | import org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface; |
| 32 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 33 | import org.onosproject.kubevirtnode.api.KubevirtNodeState; |
| 34 | import org.onosproject.kubevirtnode.api.KubevirtPhyInterface; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.util.HashMap; |
| 40 | import java.util.Map; |
| 41 | |
| 42 | import static org.easymock.EasyMock.createMock; |
| 43 | import static org.easymock.EasyMock.expect; |
| 44 | import static org.easymock.EasyMock.replay; |
| 45 | import static org.hamcrest.MatcherAssert.assertThat; |
| 46 | import static org.hamcrest.Matchers.is; |
| 47 | import static org.hamcrest.Matchers.notNullValue; |
| 48 | import static org.onosproject.kubevirtnode.codec.KubevirtNodeJsonMatcher.matchesKubevirtNode; |
| 49 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 50 | |
| 51 | /** |
| 52 | * Unit tests for KubevirtNode codec. |
| 53 | */ |
| 54 | public class KubevirtNodeCodecTest { |
| 55 | MockCodecContext context; |
| 56 | |
| 57 | JsonCodec<KubevirtNode> kubevirtNodeCodec; |
| 58 | JsonCodec<KubevirtPhyInterface> kubevirtPhyInterfaceJsonCodec; |
| 59 | |
| 60 | final CoreService mockCoreService = createMock(CoreService.class); |
| 61 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 62 | |
| 63 | @Before |
| 64 | public void setUp() { |
| 65 | context = new MockCodecContext(); |
| 66 | kubevirtNodeCodec = new KubevirtNodeCodec(); |
| 67 | kubevirtPhyInterfaceJsonCodec = new KubevirtPhyInterfaceCodec(); |
| 68 | |
| 69 | assertThat(kubevirtNodeCodec, notNullValue()); |
| 70 | assertThat(kubevirtPhyInterfaceJsonCodec, notNullValue()); |
| 71 | |
| 72 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 73 | .andReturn(APP_ID).anyTimes(); |
| 74 | replay(mockCoreService); |
| 75 | context.registerService(CoreService.class, mockCoreService); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests the kubevirt compute node encoding. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testKubevirtComputeNodeEncode() { |
| 83 | KubevirtPhyInterface phyIntf1 = DefaultKubevirtPhyInterface.builder() |
| 84 | .network("mgmtnetwork") |
| 85 | .intf("eth3") |
| 86 | .build(); |
| 87 | |
| 88 | KubevirtPhyInterface phyIntf2 = DefaultKubevirtPhyInterface.builder() |
| 89 | .network("oamnetwork") |
| 90 | .intf("eth4") |
| 91 | .build(); |
| 92 | |
| 93 | KubevirtNode node = DefaultKubevirtNode.builder() |
| 94 | .hostname("worker") |
| 95 | .type(KubevirtNode.Type.WORKER) |
| 96 | .state(KubevirtNodeState.INIT) |
| 97 | .managementIp(IpAddress.valueOf("10.10.10.1")) |
| 98 | .intgBridge(DeviceId.deviceId("br-int")) |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 99 | .tunBridge(DeviceId.deviceId("br-tun")) |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 100 | .dataIp(IpAddress.valueOf("20.20.20.2")) |
| 101 | .phyIntfs(ImmutableList.of(phyIntf1, phyIntf2)) |
| 102 | .build(); |
| 103 | |
| 104 | ObjectNode nodeJson = kubevirtNodeCodec.encode(node, context); |
| 105 | assertThat(nodeJson, matchesKubevirtNode(node)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Tests the kubevirt compute node decoding. |
| 110 | * |
| 111 | * @throws IOException io exception |
| 112 | */ |
| 113 | @Test |
| 114 | public void testKubevirtComputeNodeDecode() throws IOException { |
| 115 | KubevirtNode node = getKubevirtNode("KubevirtWorkerNode.json"); |
| 116 | |
| 117 | assertThat(node.hostname(), is("worker-01")); |
| 118 | assertThat(node.type().name(), is("WORKER")); |
| 119 | assertThat(node.managementIp().toString(), is("172.16.130.4")); |
| 120 | assertThat(node.dataIp().toString(), is("172.16.130.4")); |
| 121 | assertThat(node.intgBridge().toString(), is("of:00000000000000a1")); |
Jian Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 122 | assertThat(node.tunBridge().toString(), is("of:00000000000000a2")); |
Jian Li | cc01e45 | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 123 | assertThat(node.phyIntfs().size(), is(2)); |
| 124 | |
| 125 | node.phyIntfs().forEach(intf -> { |
| 126 | if (intf.network().equals("mgmtnetwork")) { |
| 127 | assertThat(intf.intf(), is("eth3")); |
| 128 | } |
| 129 | if (intf.network().equals("oamnetwork")) { |
| 130 | assertThat(intf.intf(), is("eth4")); |
| 131 | } |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | private KubevirtNode getKubevirtNode(String resourceName) throws IOException { |
| 136 | InputStream jsonStream = KubevirtNodeCodecTest.class.getResourceAsStream(resourceName); |
| 137 | JsonNode json = context.mapper().readTree(jsonStream); |
| 138 | MatcherAssert.assertThat(json, notNullValue()); |
| 139 | KubevirtNode node = kubevirtNodeCodec.decode((ObjectNode) json, context); |
| 140 | assertThat(node, notNullValue()); |
| 141 | return node; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Mock codec context for use in codec unit tests. |
| 146 | */ |
| 147 | private class MockCodecContext implements CodecContext { |
| 148 | |
| 149 | private final ObjectMapper mapper = new ObjectMapper(); |
| 150 | private final CodecManager manager = new CodecManager(); |
| 151 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 152 | |
| 153 | /** |
| 154 | * Constructs a new mock codec context. |
| 155 | */ |
| 156 | public MockCodecContext() { |
| 157 | manager.activate(); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public ObjectMapper mapper() { |
| 162 | return mapper; |
| 163 | } |
| 164 | |
| 165 | @SuppressWarnings("unchecked") |
| 166 | @Override |
| 167 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 168 | if (entityClass == KubevirtPhyInterface.class) { |
| 169 | return (JsonCodec<T>) kubevirtPhyInterfaceJsonCodec; |
| 170 | } |
| 171 | |
| 172 | return manager.getCodec(entityClass); |
| 173 | } |
| 174 | |
| 175 | @SuppressWarnings("unchecked") |
| 176 | @Override |
| 177 | public <T> T getService(Class<T> serviceClass) { |
| 178 | return (T) services.get(serviceClass); |
| 179 | } |
| 180 | |
| 181 | // for registering mock services |
| 182 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 183 | services.put(serviceClass, impl); |
| 184 | } |
| 185 | } |
| 186 | } |