blob: da4680cf6403f6061bec1c7d8cb50fbdb8c45a63 [file] [log] [blame]
Jian Li5b402c72018-02-27 14:27:34 +09001/*
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 */
16package org.onosproject.openstacknode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Lie6312162018-03-21 21:41:00 +090021import com.google.common.collect.ImmutableList;
Jian Li5b402c72018-02-27 14:27:34 +090022import 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.net.DeviceId;
31import org.onosproject.openstacknode.api.NodeState;
Jian Li27841662018-04-14 01:59:47 +090032import org.onosproject.openstacknode.api.OpenstackAuth;
Jian Li5b402c72018-02-27 14:27:34 +090033import org.onosproject.openstacknode.api.OpenstackNode;
Jian Lie6312162018-03-21 21:41:00 +090034import org.onosproject.openstacknode.api.OpenstackPhyInterface;
Jian Li27841662018-04-14 01:59:47 +090035import org.onosproject.openstacknode.impl.DefaultOpenstackAuth;
Jian Li5b402c72018-02-27 14:27:34 +090036import org.onosproject.openstacknode.impl.DefaultOpenstackNode;
Jian Lie6312162018-03-21 21:41:00 +090037import org.onosproject.openstacknode.impl.DefaultOpenstackPhyInterface;
Jian Li5b402c72018-02-27 14:27:34 +090038
39import java.io.IOException;
40import java.io.InputStream;
41import java.util.HashMap;
42import java.util.Map;
43
44import static org.easymock.EasyMock.createMock;
45import static org.easymock.EasyMock.expect;
46import static org.easymock.EasyMock.replay;
47import static org.hamcrest.MatcherAssert.assertThat;
48import static org.hamcrest.Matchers.is;
49import static org.hamcrest.Matchers.notNullValue;
50import static org.onosproject.net.NetTestTools.APP_ID;
51import static org.onosproject.openstacknode.codec.OpenstackNodeJsonMatcher.matchesOpenstackNode;
52
53/**
54 * Unit tests for OpenstackNode codec.
55 */
56public class OpenstackNodeCodecTest {
57 MockCodecContext context;
58 JsonCodec<OpenstackNode> openstackNodeCodec;
Jian Lie6312162018-03-21 21:41:00 +090059 JsonCodec<OpenstackPhyInterface> openstackPhyIntfJsonCodec;
Jian Li27841662018-04-14 01:59:47 +090060 JsonCodec<OpenstackAuth> openstackAuthJsonCodec;
Jian Li5b402c72018-02-27 14:27:34 +090061 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 Lie6312162018-03-21 21:41:00 +090068 openstackPhyIntfJsonCodec = new OpenstackPhyInterfaceCodec();
Jian Li27841662018-04-14 01:59:47 +090069 openstackAuthJsonCodec = new OpenstackAuthCodec();
Jian Lie6312162018-03-21 21:41:00 +090070
Jian Li5b402c72018-02-27 14:27:34 +090071 assertThat(openstackNodeCodec, notNullValue());
Jian Lie6312162018-03-21 21:41:00 +090072 assertThat(openstackPhyIntfJsonCodec, notNullValue());
Jian Li27841662018-04-14 01:59:47 +090073 assertThat(openstackAuthJsonCodec, notNullValue());
Jian Li5b402c72018-02-27 14:27:34 +090074
75 expect(mockCoreService.registerApplication(REST_APP_ID))
76 .andReturn(APP_ID).anyTimes();
77 replay(mockCoreService);
78 context.registerService(CoreService.class, mockCoreService);
79 }
80
Jian Li27841662018-04-14 01:59:47 +090081 /**
82 * Tests the openstack compute node encoding.
83 */
Jian Li5b402c72018-02-27 14:27:34 +090084 @Test
Jian Li27841662018-04-14 01:59:47 +090085 public void testOpenstackComputeNodeEncode() {
Jian Lie6312162018-03-21 21:41:00 +090086
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 Li5b402c72018-02-27 14:27:34 +090096 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 Lie6312162018-03-21 21:41:00 +0900104 .phyIntfs(ImmutableList.of(phyIntf1, phyIntf2))
Jian Li5b402c72018-02-27 14:27:34 +0900105 .build();
106
107 ObjectNode nodeJson = openstackNodeCodec.encode(node, context);
108 assertThat(nodeJson, matchesOpenstackNode(node));
109 }
110
Jian Li27841662018-04-14 01:59:47 +0900111 /**
112 * Tests the openstack compute node decoding.
113 *
114 * @throws IOException
115 */
Jian Li5b402c72018-02-27 14:27:34 +0900116 @Test
Jian Li27841662018-04-14 01:59:47 +0900117 public void testOpenstackComputeNodeDecode() throws IOException {
118 OpenstackNode node = getOpenstackNode("OpenstackComputeNode.json");
Jian Li5b402c72018-02-27 14:27:34 +0900119
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 Lie6312162018-03-21 21:41:00 +0900126 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 Li5b402c72018-02-27 14:27:34 +0900136 }
137
138 /**
Jian Li27841662018-04-14 01:59:47 +0900139 * 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 Li92d42fc2018-05-25 16:23:49 +0900158 .endPoint("keystone-end-point-url")
Jian Li27841662018-04-14 01:59:47 +0900159 .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 Li92d42fc2018-05-25 16:23:49 +0900176 assertThat(node.endPoint(), is("keystone-end-point-url"));
Jian Li27841662018-04-14 01:59:47 +0900177
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 Li5b402c72018-02-27 14:27:34 +0900190 * 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 Lie6312162018-03-21 21:41:00 +0900228 if (entityClass == OpenstackPhyInterface.class) {
229 return (JsonCodec<T>) openstackPhyIntfJsonCodec;
230 }
Jian Li27841662018-04-14 01:59:47 +0900231 if (entityClass == OpenstackAuth.class) {
232 return (JsonCodec<T>) openstackAuthJsonCodec;
233 }
Jian Li5b402c72018-02-27 14:27:34 +0900234 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}