Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.openstacknode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 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.net.DeviceId; |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 31 | import org.onosproject.net.behaviour.ControllerInfo; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 32 | import org.onosproject.openstacknode.api.DefaultOpenstackAuth; |
| 33 | import org.onosproject.openstacknode.api.DefaultOpenstackNode; |
| 34 | import org.onosproject.openstacknode.api.DpdkConfig; |
| 35 | import org.onosproject.openstacknode.api.DpdkInterface; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 36 | import org.onosproject.openstacknode.api.NodeState; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 37 | import org.onosproject.openstacknode.api.OpenstackAuth; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 38 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 39 | import org.onosproject.openstacknode.api.OpenstackPhyInterface; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 40 | import org.onosproject.openstacknode.api.OpenstackSshAuth; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 41 | import org.onosproject.openstacknode.impl.DefaultDpdkConfig; |
| 42 | import org.onosproject.openstacknode.impl.DefaultDpdkInterface; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 43 | import org.onosproject.openstacknode.impl.DefaultOpenstackPhyInterface; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 44 | import org.onosproject.openstacknode.impl.DefaultOpenstackSshAuth; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 45 | |
| 46 | import java.io.IOException; |
| 47 | import java.io.InputStream; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 48 | import java.util.ArrayList; |
| 49 | import java.util.Collection; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 50 | import java.util.HashMap; |
| 51 | import java.util.Map; |
| 52 | |
| 53 | import static org.easymock.EasyMock.createMock; |
| 54 | import static org.easymock.EasyMock.expect; |
| 55 | import static org.easymock.EasyMock.replay; |
| 56 | import static org.hamcrest.MatcherAssert.assertThat; |
| 57 | import static org.hamcrest.Matchers.is; |
| 58 | import static org.hamcrest.Matchers.notNullValue; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 59 | import static org.junit.Assert.assertEquals; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 60 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 61 | import static org.onosproject.openstacknode.codec.OpenstackNodeJsonMatcher.matchesOpenstackNode; |
| 62 | |
| 63 | /** |
| 64 | * Unit tests for OpenstackNode codec. |
| 65 | */ |
| 66 | public class OpenstackNodeCodecTest { |
| 67 | MockCodecContext context; |
| 68 | JsonCodec<OpenstackNode> openstackNodeCodec; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 69 | JsonCodec<OpenstackPhyInterface> openstackPhyIntfJsonCodec; |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 70 | JsonCodec<ControllerInfo> openstackControllerJsonCodec; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 71 | JsonCodec<OpenstackAuth> openstackAuthJsonCodec; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 72 | JsonCodec<OpenstackSshAuth> openstackSshAuthJsonCodec; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 73 | JsonCodec<DpdkConfig> dpdkConfigJsonCodec; |
| 74 | JsonCodec<DpdkInterface> dpdkInterfaceJsonCodec; |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 75 | |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 76 | final CoreService mockCoreService = createMock(CoreService.class); |
| 77 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 78 | |
| 79 | @Before |
| 80 | public void setUp() { |
| 81 | context = new MockCodecContext(); |
| 82 | openstackNodeCodec = new OpenstackNodeCodec(); |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 83 | openstackPhyIntfJsonCodec = new OpenstackPhyInterfaceCodec(); |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 84 | openstackControllerJsonCodec = new OpenstackControllerCodec(); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 85 | openstackAuthJsonCodec = new OpenstackAuthCodec(); |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 86 | openstackSshAuthJsonCodec = new OpenstackSshAuthCodec(); |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 87 | dpdkConfigJsonCodec = new DpdkConfigCodec(); |
| 88 | dpdkInterfaceJsonCodec = new DpdkInterfaceCodec(); |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 89 | |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 90 | assertThat(openstackNodeCodec, notNullValue()); |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 91 | assertThat(openstackPhyIntfJsonCodec, notNullValue()); |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 92 | assertThat(openstackControllerJsonCodec, notNullValue()); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 93 | assertThat(openstackAuthJsonCodec, notNullValue()); |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 94 | assertThat(openstackSshAuthJsonCodec, notNullValue()); |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 95 | assertThat(dpdkConfigJsonCodec, notNullValue()); |
| 96 | assertThat(dpdkInterfaceJsonCodec, notNullValue()); |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 97 | |
| 98 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 99 | .andReturn(APP_ID).anyTimes(); |
| 100 | replay(mockCoreService); |
| 101 | context.registerService(CoreService.class, mockCoreService); |
| 102 | } |
| 103 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 104 | /** |
| 105 | * Tests the openstack compute node encoding. |
| 106 | */ |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 107 | @Test |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 108 | public void testOpenstackComputeNodeEncode() { |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 109 | |
| 110 | OpenstackPhyInterface phyIntf1 = DefaultOpenstackPhyInterface.builder() |
| 111 | .network("mgmtnetwork") |
| 112 | .intf("eth3") |
| 113 | .build(); |
| 114 | OpenstackPhyInterface phyIntf2 = DefaultOpenstackPhyInterface.builder() |
| 115 | .network("oamnetwork") |
| 116 | .intf("eth4") |
| 117 | .build(); |
| 118 | |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 119 | OpenstackSshAuth sshAuth = DefaultOpenstackSshAuth.builder() |
| 120 | .id("sdn") |
| 121 | .password("sdn") |
| 122 | .build(); |
| 123 | |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 124 | ControllerInfo controller1 = |
| 125 | new ControllerInfo(IpAddress.valueOf("10.10.10.2"), 6653, "tcp"); |
| 126 | ControllerInfo controller2 = |
| 127 | new ControllerInfo(IpAddress.valueOf("10.10.10.3"), 6663, "tcp"); |
| 128 | |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 129 | OpenstackNode node = DefaultOpenstackNode.builder() |
| 130 | .hostname("compute") |
| 131 | .type(OpenstackNode.NodeType.COMPUTE) |
| 132 | .state(NodeState.INIT) |
| 133 | .managementIp(IpAddress.valueOf("10.10.10.1")) |
| 134 | .intgBridge(DeviceId.deviceId("br-int")) |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 135 | .vlanIntf("vlan") |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 136 | .dataIp(IpAddress.valueOf("20.20.20.2")) |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 137 | .phyIntfs(ImmutableList.of(phyIntf1, phyIntf2)) |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 138 | .controllers(ImmutableList.of(controller1, controller2)) |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 139 | .sshAuthInfo(sshAuth) |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 140 | .build(); |
| 141 | |
| 142 | ObjectNode nodeJson = openstackNodeCodec.encode(node, context); |
| 143 | assertThat(nodeJson, matchesOpenstackNode(node)); |
| 144 | } |
| 145 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 146 | /** |
| 147 | * Tests the openstack compute node decoding. |
| 148 | * |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 149 | * @throws IOException io exception |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 150 | */ |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 151 | @Test |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 152 | public void testOpenstackComputeNodeDecode() throws IOException { |
| 153 | OpenstackNode node = getOpenstackNode("OpenstackComputeNode.json"); |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 154 | |
| 155 | assertThat(node.hostname(), is("compute-01")); |
| 156 | assertThat(node.type().name(), is("COMPUTE")); |
| 157 | assertThat(node.managementIp().toString(), is("172.16.130.4")); |
| 158 | assertThat(node.dataIp().toString(), is("172.16.130.4")); |
| 159 | assertThat(node.intgBridge().toString(), is("of:00000000000000a1")); |
| 160 | assertThat(node.vlanIntf(), is("eth2")); |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 161 | assertThat(node.phyIntfs().size(), is(2)); |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 162 | assertThat(node.controllers().size(), is(2)); |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 163 | assertThat(node.sshAuthInfo().id(), is("sdn")); |
| 164 | assertThat(node.sshAuthInfo().password(), is("sdn")); |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 165 | assertThat(node.datapathType(), is(DpdkConfig.DatapathType.NORMAL)); |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 166 | |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 167 | |
| 168 | node.phyIntfs().forEach(intf -> { |
| 169 | if (intf.network().equals("mgmtnetwork")) { |
| 170 | assertThat(intf.intf(), is("eth3")); |
| 171 | } |
| 172 | if (intf.network().equals("oamnetwork")) { |
| 173 | assertThat(intf.intf(), is("eth4")); |
| 174 | } |
| 175 | }); |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 176 | |
| 177 | node.controllers().forEach(ctrl -> { |
| 178 | if (ctrl.ip().toString().equals("10.10.10.2")) { |
| 179 | assertThat(ctrl.port(), is(6653)); |
| 180 | } |
| 181 | if (ctrl.ip().toString().equals("10.10.10.3")) { |
| 182 | assertThat(ctrl.port(), is(6663)); |
| 183 | } |
| 184 | }); |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 185 | } |
Daniel Park | 92abf31 | 2018-08-08 17:01:35 +0900 | [diff] [blame] | 186 | |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 187 | /** |
| 188 | * Tests the openstack compute node encoding with dpdk config. |
| 189 | */ |
| 190 | @Test |
| 191 | public void testOpenstackDpdkComputeNodeEncode() { |
| 192 | DpdkInterface dpdkInterface1 = DefaultDpdkInterface.builder() |
| 193 | .deviceName("br-int") |
| 194 | .intf("dpdk0") |
| 195 | .mtu(Long.valueOf(1600)) |
| 196 | .pciAddress("0000:85:00.0") |
| 197 | .type(DpdkInterface.Type.DPDK) |
| 198 | .build(); |
Daniel Park | 92abf31 | 2018-08-08 17:01:35 +0900 | [diff] [blame] | 199 | |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 200 | DpdkInterface dpdkInterface2 = DefaultDpdkInterface.builder() |
| 201 | .deviceName("br-tun") |
| 202 | .intf("dpdk1") |
| 203 | .pciAddress("0000:85:00.1") |
| 204 | .type(DpdkInterface.Type.DPDK) |
| 205 | .build(); |
| 206 | |
| 207 | Collection<DpdkInterface> dpdkInterfaceCollection = new ArrayList<>(); |
| 208 | dpdkInterfaceCollection.add(dpdkInterface1); |
| 209 | dpdkInterfaceCollection.add(dpdkInterface2); |
| 210 | |
| 211 | |
| 212 | DpdkConfig dpdkConfig = DefaultDpdkConfig.builder() |
| 213 | .datapathType(DpdkConfig.DatapathType.NETDEV) |
| 214 | .dpdkIntfs(dpdkInterfaceCollection) |
| 215 | .build(); |
| 216 | |
| 217 | OpenstackNode node = DefaultOpenstackNode.builder() |
| 218 | .hostname("compute") |
| 219 | .type(OpenstackNode.NodeType.COMPUTE) |
| 220 | .state(NodeState.INIT) |
| 221 | .managementIp(IpAddress.valueOf("10.10.10.1")) |
| 222 | .intgBridge(DeviceId.deviceId("br-int")) |
| 223 | .vlanIntf("vlan") |
| 224 | .dataIp(IpAddress.valueOf("20.20.20.2")) |
| 225 | .dpdkConfig(dpdkConfig) |
| 226 | .build(); |
| 227 | |
| 228 | ObjectNode nodeJson = openstackNodeCodec.encode(node, context); |
| 229 | assertThat(nodeJson, matchesOpenstackNode(node)); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Tests the openstack compute node decoding with dpdk config. |
| 234 | * |
| 235 | * @throws IOException io exception |
| 236 | */ |
| 237 | @Test |
| 238 | public void testOpenstackDpdkComputeNodeDecode() throws IOException { |
| 239 | OpenstackNode node = getOpenstackNode("OpenstackDpdkComputeNode.json"); |
| 240 | |
| 241 | assertEquals(node.datapathType(), DpdkConfig.DatapathType.NETDEV); |
| 242 | assertEquals(node.socketDir(), "/var/lib/libvirt/qemu"); |
| 243 | assertEquals(node.dpdkConfig().dpdkIntfs().size(), 2); |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /** |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 247 | * Tests the openstack controller node encoding. |
| 248 | */ |
| 249 | @Test |
| 250 | public void testOpenstackControllerNodeEncode() { |
| 251 | OpenstackAuth auth = DefaultOpenstackAuth.builder() |
| 252 | .version("v2.0") |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 253 | .protocol(OpenstackAuth.Protocol.HTTP) |
| 254 | .project("admin") |
| 255 | .username("admin") |
| 256 | .password("nova") |
| 257 | .perspective(OpenstackAuth.Perspective.PUBLIC) |
| 258 | .build(); |
| 259 | |
| 260 | OpenstackNode node = DefaultOpenstackNode.builder() |
| 261 | .hostname("controller") |
| 262 | .type(OpenstackNode.NodeType.CONTROLLER) |
| 263 | .state(NodeState.INIT) |
| 264 | .managementIp(IpAddress.valueOf("172.16.130.10")) |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 265 | .endpoint("keystone-end-point-url") |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 266 | .authentication(auth) |
| 267 | .build(); |
| 268 | |
| 269 | ObjectNode nodeJson = openstackNodeCodec.encode(node, context); |
| 270 | assertThat(nodeJson, matchesOpenstackNode(node)); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Tests the openstack controller node decoding. |
| 275 | */ |
| 276 | @Test |
| 277 | public void testOpenstackControllerNodeDecode() throws IOException { |
| 278 | OpenstackNode node = getOpenstackNode("OpenstackControllerNode.json"); |
| 279 | |
| 280 | assertThat(node.hostname(), is("controller")); |
| 281 | assertThat(node.type().name(), is("CONTROLLER")); |
| 282 | assertThat(node.managementIp().toString(), is("172.16.130.10")); |
Jian Li | 6d41036 | 2018-08-17 09:41:08 +0900 | [diff] [blame] | 283 | assertThat(node.endpoint(), is("keystone-end-point-url")); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 284 | |
| 285 | OpenstackAuth auth = node.authentication(); |
| 286 | |
| 287 | assertThat(auth.version(), is("v2.0")); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 288 | assertThat(auth.protocol(), is(OpenstackAuth.Protocol.HTTP)); |
| 289 | assertThat(auth.username(), is("admin")); |
| 290 | assertThat(auth.password(), is("nova")); |
| 291 | assertThat(auth.project(), is("admin")); |
| 292 | assertThat(auth.perspective(), is(OpenstackAuth.Perspective.PUBLIC)); |
| 293 | } |
| 294 | |
| 295 | /** |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 296 | * Reads in an openstack node from the given resource and decodes it. |
| 297 | * |
| 298 | * @param resourceName resource to use to read the JSON for the rule |
| 299 | * @return decoded openstack node |
| 300 | * @throws IOException if processing the resource fails |
| 301 | */ |
| 302 | private OpenstackNode getOpenstackNode(String resourceName) throws IOException { |
| 303 | InputStream jsonStream = OpenstackNodeCodecTest.class.getResourceAsStream(resourceName); |
| 304 | JsonNode json = context.mapper().readTree(jsonStream); |
| 305 | MatcherAssert.assertThat(json, notNullValue()); |
| 306 | OpenstackNode node = openstackNodeCodec.decode((ObjectNode) json, context); |
| 307 | assertThat(node, notNullValue()); |
| 308 | return node; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Mock codec context for use in codec unit tests. |
| 313 | */ |
| 314 | private class MockCodecContext implements CodecContext { |
| 315 | private final ObjectMapper mapper = new ObjectMapper(); |
| 316 | private final CodecManager manager = new CodecManager(); |
| 317 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 318 | |
| 319 | /** |
| 320 | * Constructs a new mock codec context. |
| 321 | */ |
| 322 | public MockCodecContext() { |
| 323 | manager.activate(); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public ObjectMapper mapper() { |
| 328 | return mapper; |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | @SuppressWarnings("unchecked") |
| 333 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 334 | if (entityClass == OpenstackPhyInterface.class) { |
| 335 | return (JsonCodec<T>) openstackPhyIntfJsonCodec; |
| 336 | } |
Jian Li | 789fadb | 2018-07-10 13:59:47 +0900 | [diff] [blame] | 337 | if (entityClass == ControllerInfo.class) { |
| 338 | return (JsonCodec<T>) openstackControllerJsonCodec; |
| 339 | } |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 340 | if (entityClass == OpenstackAuth.class) { |
| 341 | return (JsonCodec<T>) openstackAuthJsonCodec; |
| 342 | } |
Daniel Park | deefa70 | 2018-07-17 17:55:51 +0900 | [diff] [blame] | 343 | if (entityClass == OpenstackSshAuth.class) { |
| 344 | return (JsonCodec<T>) openstackSshAuthJsonCodec; |
| 345 | } |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame^] | 346 | if (entityClass == DpdkConfig.class) { |
| 347 | return (JsonCodec<T>) dpdkConfigJsonCodec; |
| 348 | } |
| 349 | if (entityClass == DpdkInterface.class) { |
| 350 | return (JsonCodec<T>) dpdkInterfaceJsonCodec; |
| 351 | } |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 352 | return manager.getCodec(entityClass); |
| 353 | } |
| 354 | |
| 355 | @SuppressWarnings("unchecked") |
| 356 | @Override |
| 357 | public <T> T getService(Class<T> serviceClass) { |
| 358 | return (T) services.get(serviceClass); |
| 359 | } |
| 360 | |
| 361 | // for registering mock services |
| 362 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 363 | services.put(serviceClass, impl); |
| 364 | } |
| 365 | } |
| 366 | } |