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; |
| 31 | import org.onosproject.openstacknode.api.NodeState; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 32 | import org.onosproject.openstacknode.api.OpenstackAuth; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 33 | import org.onosproject.openstacknode.api.OpenstackNode; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 34 | import org.onosproject.openstacknode.api.OpenstackPhyInterface; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 35 | import org.onosproject.openstacknode.impl.DefaultOpenstackAuth; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 36 | import org.onosproject.openstacknode.impl.DefaultOpenstackNode; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 37 | import org.onosproject.openstacknode.impl.DefaultOpenstackPhyInterface; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 38 | |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.Map; |
| 43 | |
| 44 | import static org.easymock.EasyMock.createMock; |
| 45 | import static org.easymock.EasyMock.expect; |
| 46 | import static org.easymock.EasyMock.replay; |
| 47 | import static org.hamcrest.MatcherAssert.assertThat; |
| 48 | import static org.hamcrest.Matchers.is; |
| 49 | import static org.hamcrest.Matchers.notNullValue; |
| 50 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 51 | import static org.onosproject.openstacknode.codec.OpenstackNodeJsonMatcher.matchesOpenstackNode; |
| 52 | |
| 53 | /** |
| 54 | * Unit tests for OpenstackNode codec. |
| 55 | */ |
| 56 | public class OpenstackNodeCodecTest { |
| 57 | MockCodecContext context; |
| 58 | JsonCodec<OpenstackNode> openstackNodeCodec; |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 59 | JsonCodec<OpenstackPhyInterface> openstackPhyIntfJsonCodec; |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 60 | JsonCodec<OpenstackAuth> openstackAuthJsonCodec; |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 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 | openstackNodeCodec = new OpenstackNodeCodec(); |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 68 | openstackPhyIntfJsonCodec = new OpenstackPhyInterfaceCodec(); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 69 | openstackAuthJsonCodec = new OpenstackAuthCodec(); |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 70 | |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 71 | assertThat(openstackNodeCodec, notNullValue()); |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 72 | assertThat(openstackPhyIntfJsonCodec, notNullValue()); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 73 | assertThat(openstackAuthJsonCodec, notNullValue()); |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 74 | |
| 75 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 76 | .andReturn(APP_ID).anyTimes(); |
| 77 | replay(mockCoreService); |
| 78 | context.registerService(CoreService.class, mockCoreService); |
| 79 | } |
| 80 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 81 | /** |
| 82 | * Tests the openstack compute node encoding. |
| 83 | */ |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 84 | @Test |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 85 | public void testOpenstackComputeNodeEncode() { |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 86 | |
| 87 | OpenstackPhyInterface phyIntf1 = DefaultOpenstackPhyInterface.builder() |
| 88 | .network("mgmtnetwork") |
| 89 | .intf("eth3") |
| 90 | .build(); |
| 91 | OpenstackPhyInterface phyIntf2 = DefaultOpenstackPhyInterface.builder() |
| 92 | .network("oamnetwork") |
| 93 | .intf("eth4") |
| 94 | .build(); |
| 95 | |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 96 | OpenstackNode node = DefaultOpenstackNode.builder() |
| 97 | .hostname("compute") |
| 98 | .type(OpenstackNode.NodeType.COMPUTE) |
| 99 | .state(NodeState.INIT) |
| 100 | .managementIp(IpAddress.valueOf("10.10.10.1")) |
| 101 | .intgBridge(DeviceId.deviceId("br-int")) |
| 102 | .vlanIntf("vxlan") |
| 103 | .dataIp(IpAddress.valueOf("20.20.20.2")) |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 104 | .phyIntfs(ImmutableList.of(phyIntf1, phyIntf2)) |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 105 | .build(); |
| 106 | |
| 107 | ObjectNode nodeJson = openstackNodeCodec.encode(node, context); |
| 108 | assertThat(nodeJson, matchesOpenstackNode(node)); |
| 109 | } |
| 110 | |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 111 | /** |
| 112 | * Tests the openstack compute node decoding. |
| 113 | * |
| 114 | * @throws IOException |
| 115 | */ |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 116 | @Test |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 117 | public void testOpenstackComputeNodeDecode() throws IOException { |
| 118 | OpenstackNode node = getOpenstackNode("OpenstackComputeNode.json"); |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 119 | |
| 120 | assertThat(node.hostname(), is("compute-01")); |
| 121 | assertThat(node.type().name(), is("COMPUTE")); |
| 122 | assertThat(node.managementIp().toString(), is("172.16.130.4")); |
| 123 | assertThat(node.dataIp().toString(), is("172.16.130.4")); |
| 124 | assertThat(node.intgBridge().toString(), is("of:00000000000000a1")); |
| 125 | assertThat(node.vlanIntf(), is("eth2")); |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 126 | assertThat(node.phyIntfs().size(), is(2)); |
| 127 | |
| 128 | node.phyIntfs().forEach(intf -> { |
| 129 | if (intf.network().equals("mgmtnetwork")) { |
| 130 | assertThat(intf.intf(), is("eth3")); |
| 131 | } |
| 132 | if (intf.network().equals("oamnetwork")) { |
| 133 | assertThat(intf.intf(), is("eth4")); |
| 134 | } |
| 135 | }); |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 139 | * Tests the openstack controller node encoding. |
| 140 | */ |
| 141 | @Test |
| 142 | public void testOpenstackControllerNodeEncode() { |
| 143 | OpenstackAuth auth = DefaultOpenstackAuth.builder() |
| 144 | .version("v2.0") |
| 145 | .port(35357) |
| 146 | .protocol(OpenstackAuth.Protocol.HTTP) |
| 147 | .project("admin") |
| 148 | .username("admin") |
| 149 | .password("nova") |
| 150 | .perspective(OpenstackAuth.Perspective.PUBLIC) |
| 151 | .build(); |
| 152 | |
| 153 | OpenstackNode node = DefaultOpenstackNode.builder() |
| 154 | .hostname("controller") |
| 155 | .type(OpenstackNode.NodeType.CONTROLLER) |
| 156 | .state(NodeState.INIT) |
| 157 | .managementIp(IpAddress.valueOf("172.16.130.10")) |
Jian Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 158 | .endPoint("keystone-end-point-url") |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 159 | .authentication(auth) |
| 160 | .build(); |
| 161 | |
| 162 | ObjectNode nodeJson = openstackNodeCodec.encode(node, context); |
| 163 | assertThat(nodeJson, matchesOpenstackNode(node)); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Tests the openstack controller node decoding. |
| 168 | */ |
| 169 | @Test |
| 170 | public void testOpenstackControllerNodeDecode() throws IOException { |
| 171 | OpenstackNode node = getOpenstackNode("OpenstackControllerNode.json"); |
| 172 | |
| 173 | assertThat(node.hostname(), is("controller")); |
| 174 | assertThat(node.type().name(), is("CONTROLLER")); |
| 175 | assertThat(node.managementIp().toString(), is("172.16.130.10")); |
Jian Li | 92d42fc | 2018-05-25 16:23:49 +0900 | [diff] [blame] | 176 | assertThat(node.endPoint(), is("keystone-end-point-url")); |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 177 | |
| 178 | OpenstackAuth auth = node.authentication(); |
| 179 | |
| 180 | assertThat(auth.version(), is("v2.0")); |
| 181 | assertThat(auth.port(), is(35357)); |
| 182 | assertThat(auth.protocol(), is(OpenstackAuth.Protocol.HTTP)); |
| 183 | assertThat(auth.username(), is("admin")); |
| 184 | assertThat(auth.password(), is("nova")); |
| 185 | assertThat(auth.project(), is("admin")); |
| 186 | assertThat(auth.perspective(), is(OpenstackAuth.Perspective.PUBLIC)); |
| 187 | } |
| 188 | |
| 189 | /** |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 190 | * Reads in an openstack node from the given resource and decodes it. |
| 191 | * |
| 192 | * @param resourceName resource to use to read the JSON for the rule |
| 193 | * @return decoded openstack node |
| 194 | * @throws IOException if processing the resource fails |
| 195 | */ |
| 196 | private OpenstackNode getOpenstackNode(String resourceName) throws IOException { |
| 197 | InputStream jsonStream = OpenstackNodeCodecTest.class.getResourceAsStream(resourceName); |
| 198 | JsonNode json = context.mapper().readTree(jsonStream); |
| 199 | MatcherAssert.assertThat(json, notNullValue()); |
| 200 | OpenstackNode node = openstackNodeCodec.decode((ObjectNode) json, context); |
| 201 | assertThat(node, notNullValue()); |
| 202 | return node; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Mock codec context for use in codec unit tests. |
| 207 | */ |
| 208 | private class MockCodecContext implements CodecContext { |
| 209 | private final ObjectMapper mapper = new ObjectMapper(); |
| 210 | private final CodecManager manager = new CodecManager(); |
| 211 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 212 | |
| 213 | /** |
| 214 | * Constructs a new mock codec context. |
| 215 | */ |
| 216 | public MockCodecContext() { |
| 217 | manager.activate(); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public ObjectMapper mapper() { |
| 222 | return mapper; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | @SuppressWarnings("unchecked") |
| 227 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
Jian Li | e631216 | 2018-03-21 21:41:00 +0900 | [diff] [blame] | 228 | if (entityClass == OpenstackPhyInterface.class) { |
| 229 | return (JsonCodec<T>) openstackPhyIntfJsonCodec; |
| 230 | } |
Jian Li | 2784166 | 2018-04-14 01:59:47 +0900 | [diff] [blame] | 231 | if (entityClass == OpenstackAuth.class) { |
| 232 | return (JsonCodec<T>) openstackAuthJsonCodec; |
| 233 | } |
Jian Li | 5b402c7 | 2018-02-27 14:27:34 +0900 | [diff] [blame] | 234 | return manager.getCodec(entityClass); |
| 235 | } |
| 236 | |
| 237 | @SuppressWarnings("unchecked") |
| 238 | @Override |
| 239 | public <T> T getService(Class<T> serviceClass) { |
| 240 | return (T) services.get(serviceClass); |
| 241 | } |
| 242 | |
| 243 | // for registering mock services |
| 244 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 245 | services.put(serviceClass, impl); |
| 246 | } |
| 247 | } |
| 248 | } |