blob: eee6bf7f3852e0c54a6cd48b48596693b6ea77ac [file] [log] [blame]
Jian Li171582e2020-12-21 01:44:05 +09001/*
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 */
16package org.onosproject.kubevirtnode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableList;
22import org.hamcrest.MatcherAssert;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.IpAddress;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.core.CoreService;
30import org.onosproject.kubevirtnode.api.DefaultKubevirtNode;
31import org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface;
32import org.onosproject.kubevirtnode.api.KubevirtNode;
33import org.onosproject.kubevirtnode.api.KubevirtNodeState;
34import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
35import org.onosproject.net.DeviceId;
36
37import java.io.IOException;
38import java.io.InputStream;
39import java.util.HashMap;
40import java.util.Map;
41
42import static org.easymock.EasyMock.createMock;
43import static org.easymock.EasyMock.expect;
44import static org.easymock.EasyMock.replay;
45import static org.hamcrest.MatcherAssert.assertThat;
46import static org.hamcrest.Matchers.is;
47import static org.hamcrest.Matchers.notNullValue;
48import static org.onosproject.kubevirtnode.codec.KubevirtNodeJsonMatcher.matchesKubevirtNode;
49import static org.onosproject.net.NetTestTools.APP_ID;
50
51/**
52 * Unit tests for KubevirtNode codec.
53 */
54public 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 Li138f51f2021-01-06 03:29:58 +090099 .tunBridge(DeviceId.deviceId("br-tun"))
Jian Li171582e2020-12-21 01:44:05 +0900100 .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 Li138f51f2021-01-06 03:29:58 +0900122 assertThat(node.tunBridge().toString(), is("of:00000000000000a2"));
Jian Li171582e2020-12-21 01:44:05 +0900123 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 /**
Daniel Park54d8baf2021-02-22 17:12:20 +0900145 * Tests the kubevirt gateway node encoding.
146 */
147 @Test
148 public void testKubevirtGatweayNodeEncode() {
149 KubevirtNode node = DefaultKubevirtNode.builder()
150 .hostname("gateway")
151 .type(KubevirtNode.Type.GATEWAY)
152 .state(KubevirtNodeState.INIT)
153 .managementIp(IpAddress.valueOf("10.10.10.1"))
154 .intgBridge(DeviceId.deviceId("br-int"))
155 .tunBridge(DeviceId.deviceId("br-tun"))
156 .dataIp(IpAddress.valueOf("20.20.20.2"))
157 .gatewayBridgeName("gateway")
158 .build();
159
160 ObjectNode nodeJson = kubevirtNodeCodec.encode(node, context);
161 assertThat(nodeJson, matchesKubevirtNode(node));
162 }
163
164 /**
165 * Tests the kubevirt gateway node decoding.
166 *
167 * @throws IOException io exception
168 */
169 @Test
170 public void testKubevirtGatewayNodeDecode() throws IOException {
171 KubevirtNode node = getKubevirtNode("KubevirtGatewayNode.json");
172
173 assertThat(node.hostname(), is("gateway-01"));
174 assertThat(node.type().name(), is("GATEWAY"));
175 assertThat(node.managementIp().toString(), is("172.16.130.4"));
176 assertThat(node.dataIp().toString(), is("172.16.130.4"));
177 assertThat(node.intgBridge().toString(), is("of:00000000000000a1"));
178 assertThat(node.tunBridge().toString(), is("of:00000000000000a2"));
179 assertThat(node.gatewayBridgeName(), is("gateway"));
180 }
181
182 /**
Jian Li171582e2020-12-21 01:44:05 +0900183 * Mock codec context for use in codec unit tests.
184 */
185 private class MockCodecContext implements CodecContext {
186
187 private final ObjectMapper mapper = new ObjectMapper();
188 private final CodecManager manager = new CodecManager();
189 private final Map<Class<?>, Object> services = new HashMap<>();
190
191 /**
192 * Constructs a new mock codec context.
193 */
194 public MockCodecContext() {
195 manager.activate();
196 }
197
198 @Override
199 public ObjectMapper mapper() {
200 return mapper;
201 }
202
203 @SuppressWarnings("unchecked")
204 @Override
205 public <T> JsonCodec<T> codec(Class<T> entityClass) {
206 if (entityClass == KubevirtPhyInterface.class) {
207 return (JsonCodec<T>) kubevirtPhyInterfaceJsonCodec;
208 }
209
210 return manager.getCodec(entityClass);
211 }
212
213 @SuppressWarnings("unchecked")
214 @Override
215 public <T> T getService(Class<T> serviceClass) {
216 return (T) services.get(serviceClass);
217 }
218
219 // for registering mock services
220 public <T> void registerService(Class<T> serviceClass, T impl) {
221 services.put(serviceClass, impl);
222 }
223 }
224}