2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 * Copyright (C) 2018 Bell Canada. All rights reserved.
24 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
25 * the License. You may obtain a copy of the License at
27 * http://www.apache.org/licenses/LICENSE-2.0
29 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
30 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
31 * specific language governing permissions and limitations under the License.
33 package org.onap.so.heatbridge;
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertFalse;
37 import static org.junit.Assert.assertNotNull;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.ArgumentMatchers.anyString;
40 import static org.mockito.ArgumentMatchers.eq;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.times;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.when;
46 import java.io.IOException;
47 import java.nio.charset.Charset;
48 import java.util.ArrayList;
49 import java.util.Arrays;
50 import java.util.Collections;
51 import java.util.List;
52 import java.util.Objects;
53 import java.util.Optional;
54 import org.apache.commons.io.FileUtils;
55 import org.junit.Assert;
56 import org.junit.Before;
57 import org.junit.Ignore;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 import org.mockito.ArgumentCaptor;
61 import org.mockito.Mock;
62 import org.mockito.junit.MockitoJUnitRunner;
63 import org.onap.aai.domain.yang.LInterface;
64 import org.onap.aai.domain.yang.PInterface;
65 import org.onap.aai.domain.yang.SriovPf;
66 import org.onap.aai.domain.yang.Vserver;
67 import org.onap.so.client.aai.AAIObjectType;
68 import org.onap.so.client.aai.AAIResourcesClient;
69 import org.onap.so.client.aai.AAISingleTransactionClient;
70 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
71 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
72 import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed;
73 import org.onap.so.db.catalog.beans.CloudIdentity;
74 import org.onap.so.heatbridge.constants.HeatBridgeConstants;
75 import org.onap.so.heatbridge.openstack.api.OpenstackClient;
76 import org.onap.so.heatbridge.openstack.api.OpenstackClientException;
77 import org.openstack4j.model.compute.Flavor;
78 import org.openstack4j.model.compute.Image;
79 import org.openstack4j.model.compute.Server;
80 import org.openstack4j.model.compute.Server.Status;
81 import org.openstack4j.model.heat.Resource;
82 import org.openstack4j.model.network.Network;
83 import org.openstack4j.model.network.NetworkType;
84 import org.openstack4j.model.network.Port;
85 import org.openstack4j.openstack.heat.domain.HeatResource;
86 import org.openstack4j.openstack.heat.domain.HeatResource.Resources;
87 import com.fasterxml.jackson.databind.ObjectMapper;
88 import com.google.common.collect.ImmutableMap;
91 @RunWith(MockitoJUnitRunner.class)
92 public class HeatBridgeImplTest {
94 private static final String CLOUD_OWNER = "CloudOwner";
95 private static final String REGION_ID = "RegionOne";
96 private static final String TENANT_ID = "7320ec4a5b9d4589ba7c4412ccfd290f";
97 private static final ObjectMapper MAPPER = new ObjectMapper();
100 private OpenstackClient osClient;
102 private CloudIdentity cloudIdentity = new CloudIdentity();
105 private AAIResourcesClient resourcesClient;
107 private AAISingleTransactionClient transaction;
109 private HeatBridgeImpl heatbridge;
112 public void setUp() throws HeatBridgeException, OpenstackClientException, BulkProcessFailed {
114 when(resourcesClient.beginSingleTransaction()).thenReturn(transaction);
115 heatbridge = new HeatBridgeImpl(resourcesClient, cloudIdentity, CLOUD_OWNER, REGION_ID, REGION_ID, TENANT_ID);
120 public void testQueryNestedHeatStackResources() throws HeatBridgeException {
122 String heatStackId = "1234567";
123 List<Resource> expectedResourceList = (List<Resource>) extractTestStackResources();
124 when(osClient.getStackBasedResources(heatStackId, HeatBridgeConstants.OS_DEFAULT_HEAT_NESTING))
125 .thenReturn(expectedResourceList);
128 List<Resource> resourceList = heatbridge.queryNestedHeatStackResources(heatStackId);
131 verify(osClient).getStackBasedResources(heatStackId, HeatBridgeConstants.OS_DEFAULT_HEAT_NESTING);
132 assertEquals(resourceList, expectedResourceList);
136 public void testExtractStackResourceIdsByResourceType() throws HeatBridgeException {
138 List<Resource> expectedResourceList = (List<Resource>) extractTestStackResources();
139 List<String> expectedServerIds =
140 Arrays.asList("43c2159b-2c04-46ac-bda5-594110cae2d3", "7cff109a-b2b7-4933-97b4-ec44a8365568");
143 List<String> serverIds = heatbridge.extractStackResourceIdsByResourceType(expectedResourceList,
144 HeatBridgeConstants.OS_SERVER_RESOURCE_TYPE);
147 assertEquals(expectedServerIds, serverIds);
152 public void testGetAllOpenstackServers() {
154 List<Resource> stackResources = (List<Resource>) extractTestStackResources();
156 Server server1 = mock(Server.class);
157 Server server2 = mock(Server.class);
158 List<Server> expectedServers = Arrays.asList(server1, server2);
160 when(osClient.getServerById("43c2159b-2c04-46ac-bda5-594110cae2d3")).thenReturn(server1);
161 when(osClient.getServerById("7cff109a-b2b7-4933-97b4-ec44a8365568")).thenReturn(server2);
164 List<Server> servers = heatbridge.getAllOpenstackServers(stackResources);
167 assertEquals(expectedServers, servers);
172 public void testExtractOpenstackImagesFromServers() {
174 Server server1 = mock(Server.class);
175 Server server2 = mock(Server.class);
176 List<Server> servers = Arrays.asList(server1, server2);
178 Image image1 = mock(Image.class);
179 Image image2 = mock(Image.class);
180 when(image1.getId()).thenReturn("1");
181 when(image2.getId()).thenReturn("1");
182 List<Image> expectedDistinctImages = Collections.singletonList(image1);
184 when(server1.getImage()).thenReturn(image1);
185 when(server2.getImage()).thenReturn(image2);
188 List<Image> images = heatbridge.extractOpenstackImagesFromServers(servers);
191 assertEquals(expectedDistinctImages, images);
196 public void testExtractOpenstackFlavorsFromServers() {
198 Server server1 = mock(Server.class);
199 Server server2 = mock(Server.class);
200 List<Server> servers = Arrays.asList(server1, server2);
202 Flavor flavor1 = mock(Flavor.class);
203 Flavor flavor2 = mock(Flavor.class);
204 when(flavor1.getId()).thenReturn("1");
205 when(flavor2.getId()).thenReturn("2");
206 List<Flavor> expectedFlavors = Arrays.asList(flavor1, flavor2);
208 when(server1.getFlavor()).thenReturn(flavor1);
209 when(server2.getFlavor()).thenReturn(flavor2);
212 List<Flavor> flavors = heatbridge.extractOpenstackFlavorsFromServers(servers);
215 assertEquals(expectedFlavors, flavors);
219 public void testUpdateVserversToAai() throws HeatBridgeException {
221 Server server1 = mock(Server.class);
223 when(server1.getId()).thenReturn("test-server1-id");
224 when(server1.getHypervisorHostname()).thenReturn("test-hypervisor");
225 when(server1.getName()).thenReturn("test-server1-name");
226 when(server1.getStatus()).thenReturn(Status.ACTIVE);
227 when(server1.getLinks()).thenReturn(new ArrayList<>());
229 Server server2 = mock(Server.class);
230 when(server2.getId()).thenReturn("test-server2-id");
231 when(server2.getHypervisorHostname()).thenReturn("test-hypervisor");
232 when(server2.getName()).thenReturn("test-server2-name");
233 when(server2.getStatus()).thenReturn(Status.ACTIVE);
234 when(server2.getLinks()).thenReturn(new ArrayList<>());
236 List<Server> servers = Arrays.asList(server1, server2);
238 Image image = mock(Image.class);
239 when(server1.getImage()).thenReturn(image);
240 when(server2.getImage()).thenReturn(image);
241 when(image.getId()).thenReturn("test-image-id");
243 Flavor flavor = mock(Flavor.class);
244 when(server1.getFlavor()).thenReturn(flavor);
245 when(server2.getFlavor()).thenReturn(flavor);
246 when(flavor.getId()).thenReturn("test-flavor-id");
250 heatbridge.buildAddVserversToAaiAction("test-genericVnf-id", "test-vfModule-id", servers);
253 ArgumentCaptor<AAIResourceUri> captor = ArgumentCaptor.forClass(AAIResourceUri.class);
254 verify(transaction, times(2)).create(captor.capture(), any(Vserver.class));
256 List<AAIResourceUri> uris = captor.getAllValues();
257 assertEquals(AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, CLOUD_OWNER, REGION_ID, TENANT_ID,
258 server1.getId()), uris.get(0));
259 assertEquals(AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, CLOUD_OWNER, REGION_ID, TENANT_ID,
260 server2.getId()), uris.get(1));
265 public void testUpdateImagesToAai() throws HeatBridgeException {
267 Image image1 = mock(Image.class);
268 when(image1.getId()).thenReturn("test-image1-id");
269 when(image1.getName()).thenReturn("test-image1-name");
270 when(image1.getLinks()).thenReturn(new ArrayList<>());
272 Image image2 = mock(Image.class);
273 when(image2.getId()).thenReturn("test-image2-id");
274 when(image2.getName()).thenReturn("test-image2-name");
275 when(image2.getLinks()).thenReturn(new ArrayList<>());
277 List<Image> images = Arrays.asList(image1, image2);
280 heatbridge.buildAddImagesToAaiAction(images);
283 verify(transaction, times(2)).create(any(AAIResourceUri.class), any(org.onap.aai.domain.yang.Image.class));
286 heatbridge.buildAddImagesToAaiAction(images);
289 verify(transaction, times(4)).create(any(AAIResourceUri.class), any(org.onap.aai.domain.yang.Image.class));
293 public void testUpdateFlavorsToAai() throws HeatBridgeException {
295 Flavor flavor1 = mock(Flavor.class);
296 when(flavor1.getId()).thenReturn("test-flavor1-id");
297 when(flavor1.getName()).thenReturn("test-flavor1-name");
298 when(flavor1.getLinks()).thenReturn(new ArrayList<>());
300 Flavor flavor2 = mock(Flavor.class);
301 when(flavor2.getId()).thenReturn("test-flavor2-id");
302 when(flavor2.getName()).thenReturn("test-flavor2-name");
303 when(flavor2.getLinks()).thenReturn(new ArrayList<>());
305 List<Flavor> flavors = Arrays.asList(flavor1, flavor2);
308 heatbridge.buildAddFlavorsToAaiAction(flavors);
311 verify(transaction, times(2)).create(any(AAIResourceUri.class), any(org.onap.aai.domain.yang.Flavor.class));
314 heatbridge.buildAddFlavorsToAaiAction(flavors);
317 verify(transaction, times(4)).create(any(AAIResourceUri.class), any(org.onap.aai.domain.yang.Flavor.class));
322 public void testUpdateVserverLInterfacesToAai() throws HeatBridgeException {
324 List<Resource> stackResources = (List<Resource>) extractTestStackResources();
325 Port port = mock(Port.class);
326 when(port.getId()).thenReturn("test-port-id");
327 when(port.getName()).thenReturn("test-port-name");
328 when(port.getvNicType()).thenReturn(HeatBridgeConstants.OS_SRIOV_PORT_TYPE);
329 when(port.getMacAddress()).thenReturn("78:4f:43:68:e2:78");
330 when(port.getNetworkId()).thenReturn("890a203a-23gg-56jh-df67-731656a8f13a");
331 when(port.getDeviceId()).thenReturn("test-device-id");
332 when(port.getVifDetails()).thenReturn(ImmutableMap.of(HeatBridgeConstants.OS_VLAN_NETWORK_KEY, "2345"));
333 String pfPciId = "0000:08:00.0";
334 when(port.getProfile()).thenReturn(ImmutableMap.of(HeatBridgeConstants.OS_PCI_SLOT_KEY, pfPciId,
335 HeatBridgeConstants.OS_PHYSICAL_NETWORK_KEY, "physical_network_id"));
337 Network network = mock(Network.class);
338 when(network.getId()).thenReturn("test-network-id");
339 when(network.getNetworkType()).thenReturn(NetworkType.VLAN);
340 when(network.getProviderSegID()).thenReturn("2345");
342 when(osClient.getPortById("212a203a-9764-4f42-84ea-731536a8f13a")).thenReturn(port);
343 when(osClient.getPortById("387e3904-8948-43d1-8635-b6c2042b54da")).thenReturn(port);
344 when(osClient.getPortById("70a09dfd-f1c5-4bc8-bd8f-dc539b8d662a")).thenReturn(port);
345 when(osClient.getPortById("12f88b4d-c8a4-4fbd-bcb4-7e36af02430b")).thenReturn(port);
346 when(osClient.getPortById("c54b9f45-b413-4937-bbe4-3c8a5689cfc9")).thenReturn(port);
347 when(osClient.getNetworkById(anyString())).thenReturn(network);
349 SriovPf sriovPf = new SriovPf();
350 sriovPf.setPfPciId(pfPciId);
351 PInterface pIf = mock(PInterface.class);
352 when(pIf.getInterfaceName()).thenReturn("test-port-id");
353 when(resourcesClient.get(eq(PInterface.class), any(AAIResourceUri.class))).thenReturn(Optional.of(pIf));
356 heatbridge.buildAddVserverLInterfacesToAaiAction(stackResources, Arrays.asList("1", "2"));
359 verify(transaction, times(5)).create(any(AAIResourceUri.class), any(LInterface.class));
360 verify(osClient, times(5)).getPortById(anyString());
361 verify(osClient, times(5)).getNetworkById(anyString());
364 private List<? extends Resource> extractTestStackResources() {
365 List<HeatResource> stackResources = null;
367 stackResources = MAPPER.readValue(readTestResourceFile("stack-resources.json"), Resources.class).getList();
368 assertNotNull(stackResources);
369 assertFalse(stackResources.isEmpty());
370 } catch (IOException e) {
371 Assert.fail("Failed to extract test stack resources.");
373 return stackResources;
376 private String readTestResourceFile(String filePath) {
377 String content = null;
378 String pathname = Objects.requireNonNull(getClass().getClassLoader().getResource(filePath)).getFile();
379 File file = new File(pathname);
381 content = Objects.requireNonNull(FileUtils.readFileToString(file, Charset.defaultCharset()));
382 } catch (IOException e) {
383 Assert.fail(String.format("Failed to read test resource file (%s)", filePath));