Merge "Add so-simulator project"
[so.git] / adapters / mso-openstack-adapters / src / test / java / org / onap / so / heatbridge / HeatBridgeImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 /*
22  * Copyright (C) 2018 Bell Canada. All rights reserved.
23  *
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
26  *
27  * http://www.apache.org/licenses/LICENSE-2.0
28  *
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.
32  */
33 package org.onap.so.heatbridge;
34
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;
45 import java.io.File;
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;
89
90
91 @RunWith(MockitoJUnitRunner.class)
92 public class HeatBridgeImplTest {
93
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();
98
99     @Mock
100     private OpenstackClient osClient;
101
102     private CloudIdentity cloudIdentity = new CloudIdentity();
103
104     @Mock
105     private AAIResourcesClient resourcesClient;
106     @Mock
107     private AAISingleTransactionClient transaction;
108
109     private HeatBridgeImpl heatbridge;
110
111     @Before
112     public void setUp() throws HeatBridgeException, OpenstackClientException, BulkProcessFailed {
113
114         when(resourcesClient.beginSingleTransaction()).thenReturn(transaction);
115         heatbridge = new HeatBridgeImpl(resourcesClient, cloudIdentity, CLOUD_OWNER, REGION_ID, REGION_ID, TENANT_ID);
116     }
117
118     @Ignore
119     @Test
120     public void testQueryNestedHeatStackResources() throws HeatBridgeException {
121         // Arrange
122         String heatStackId = "1234567";
123         List<Resource> expectedResourceList = (List<Resource>) extractTestStackResources();
124         when(osClient.getStackBasedResources(heatStackId, HeatBridgeConstants.OS_DEFAULT_HEAT_NESTING))
125                 .thenReturn(expectedResourceList);
126
127         // Act
128         List<Resource> resourceList = heatbridge.queryNestedHeatStackResources(heatStackId);
129
130         // Assert
131         verify(osClient).getStackBasedResources(heatStackId, HeatBridgeConstants.OS_DEFAULT_HEAT_NESTING);
132         assertEquals(resourceList, expectedResourceList);
133     }
134
135     @Test
136     public void testExtractStackResourceIdsByResourceType() throws HeatBridgeException {
137         // Arrange
138         List<Resource> expectedResourceList = (List<Resource>) extractTestStackResources();
139         List<String> expectedServerIds =
140                 Arrays.asList("43c2159b-2c04-46ac-bda5-594110cae2d3", "7cff109a-b2b7-4933-97b4-ec44a8365568");
141
142         // Act
143         List<String> serverIds = heatbridge.extractStackResourceIdsByResourceType(expectedResourceList,
144                 HeatBridgeConstants.OS_SERVER_RESOURCE_TYPE);
145
146         // Assert
147         assertEquals(expectedServerIds, serverIds);
148     }
149
150     @Ignore
151     @Test
152     public void testGetAllOpenstackServers() {
153         // Arrange
154         List<Resource> stackResources = (List<Resource>) extractTestStackResources();
155
156         Server server1 = mock(Server.class);
157         Server server2 = mock(Server.class);
158         List<Server> expectedServers = Arrays.asList(server1, server2);
159
160         when(osClient.getServerById("43c2159b-2c04-46ac-bda5-594110cae2d3")).thenReturn(server1);
161         when(osClient.getServerById("7cff109a-b2b7-4933-97b4-ec44a8365568")).thenReturn(server2);
162
163         // Act
164         List<Server> servers = heatbridge.getAllOpenstackServers(stackResources);
165
166         // Assert
167         assertEquals(expectedServers, servers);
168     }
169
170     @Ignore
171     @Test
172     public void testExtractOpenstackImagesFromServers() {
173         // Arrange
174         Server server1 = mock(Server.class);
175         Server server2 = mock(Server.class);
176         List<Server> servers = Arrays.asList(server1, server2);
177
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);
183
184         when(server1.getImage()).thenReturn(image1);
185         when(server2.getImage()).thenReturn(image2);
186
187         // Act
188         List<Image> images = heatbridge.extractOpenstackImagesFromServers(servers);
189
190         // Assert
191         assertEquals(expectedDistinctImages, images);
192     }
193
194     @Ignore
195     @Test
196     public void testExtractOpenstackFlavorsFromServers() {
197         // Arrange
198         Server server1 = mock(Server.class);
199         Server server2 = mock(Server.class);
200         List<Server> servers = Arrays.asList(server1, server2);
201
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);
207
208         when(server1.getFlavor()).thenReturn(flavor1);
209         when(server2.getFlavor()).thenReturn(flavor2);
210
211         // Act
212         List<Flavor> flavors = heatbridge.extractOpenstackFlavorsFromServers(servers);
213
214         // Assert
215         assertEquals(expectedFlavors, flavors);
216     }
217
218     @Test
219     public void testUpdateVserversToAai() throws HeatBridgeException {
220         // Arrange
221         Server server1 = mock(Server.class);
222
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<>());
228
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<>());
235
236         List<Server> servers = Arrays.asList(server1, server2);
237
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");
242
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");
247
248
249         // Act
250         heatbridge.buildAddVserversToAaiAction("test-genericVnf-id", "test-vfModule-id", servers);
251
252         // Assert
253         ArgumentCaptor<AAIResourceUri> captor = ArgumentCaptor.forClass(AAIResourceUri.class);
254         verify(transaction, times(2)).create(captor.capture(), any(Vserver.class));
255
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));
261
262     }
263
264     @Test
265     public void testUpdateImagesToAai() throws HeatBridgeException {
266         // Arrange
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<>());
271
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<>());
276
277         List<Image> images = Arrays.asList(image1, image2);
278
279         // Act #1
280         heatbridge.buildAddImagesToAaiAction(images);
281
282         // Assert #1
283         verify(transaction, times(2)).create(any(AAIResourceUri.class), any(org.onap.aai.domain.yang.Image.class));
284
285         // Act #2
286         heatbridge.buildAddImagesToAaiAction(images);
287
288         // Assert #2
289         verify(transaction, times(4)).create(any(AAIResourceUri.class), any(org.onap.aai.domain.yang.Image.class));
290     }
291
292     @Test
293     public void testUpdateFlavorsToAai() throws HeatBridgeException {
294         // Arrange
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<>());
299
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<>());
304
305         List<Flavor> flavors = Arrays.asList(flavor1, flavor2);
306
307         // Act #1
308         heatbridge.buildAddFlavorsToAaiAction(flavors);
309
310         // Assert #1
311         verify(transaction, times(2)).create(any(AAIResourceUri.class), any(org.onap.aai.domain.yang.Flavor.class));
312
313         // Act #2
314         heatbridge.buildAddFlavorsToAaiAction(flavors);
315
316         // Assert #2
317         verify(transaction, times(4)).create(any(AAIResourceUri.class), any(org.onap.aai.domain.yang.Flavor.class));
318     }
319
320     @Ignore
321     @Test
322     public void testUpdateVserverLInterfacesToAai() throws HeatBridgeException {
323         // Arrange
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"));
336
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");
341
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);
348
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));
354
355         // Act
356         heatbridge.buildAddVserverLInterfacesToAaiAction(stackResources, Arrays.asList("1", "2"));
357
358         // Assert
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());
362     }
363
364     private List<? extends Resource> extractTestStackResources() {
365         List<HeatResource> stackResources = null;
366         try {
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.");
372         }
373         return stackResources;
374     }
375
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);
380         try {
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));
384         }
385         return content;
386     }
387 }