2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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=========================================================
21 package org.onap.so.asdc.installer.heat;
23 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
25 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNull;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Mockito.doNothing;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.doThrow;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.spy;
34 import static org.mockito.Mockito.times;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
39 import java.io.FileInputStream;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.List;
46 import javax.transaction.Transactional;
48 import org.apache.commons.io.IOUtils;
49 import org.hibernate.exception.LockAcquisitionException;
50 import org.junit.Before;
51 import org.junit.Ignore;
52 import org.junit.Rule;
53 import org.junit.Test;
54 import org.junit.rules.ExpectedException;
55 import org.mockito.Mock;
56 import org.mockito.MockitoAnnotations;
57 import org.onap.sdc.api.notification.IResourceInstance;
58 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
59 import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl;
60 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
61 import org.onap.sdc.toscaparser.api.CapabilityAssignment;
62 import org.onap.sdc.toscaparser.api.CapabilityAssignments;
63 import org.onap.sdc.toscaparser.api.Group;
64 import org.onap.sdc.toscaparser.api.NodeTemplate;
65 import org.onap.sdc.toscaparser.api.elements.Metadata;
66 import org.onap.sdc.utils.DistributionStatusEnum;
67 import org.onap.so.asdc.BaseTest;
68 import org.onap.so.asdc.client.ASDCConfiguration;
69 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
70 import org.onap.so.asdc.client.test.emulators.ArtifactInfoImpl;
71 import org.onap.so.asdc.client.test.emulators.JsonStatusData;
72 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
73 import org.onap.so.asdc.installer.VfModuleArtifact;
74 import org.onap.so.asdc.installer.VfResourceStructure;
75 import org.onap.so.db.catalog.beans.AllottedResource;
76 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
77 import org.onap.so.db.catalog.beans.ExternalServiceToInternalService;
78 import org.onap.so.db.catalog.beans.HeatTemplate;
79 import org.onap.so.db.catalog.beans.NetworkResource;
80 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
81 import org.onap.so.db.catalog.beans.Service;
82 import org.onap.so.db.catalog.beans.VnfResource;
83 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
84 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
85 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
86 import org.onap.so.db.catalog.data.repository.ExternalServiceToInternalServiceRepository;
87 import org.onap.so.db.catalog.data.repository.ServiceRepository;
88 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
89 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
90 import org.onap.so.db.request.beans.WatchdogServiceModVerIdLookup;
91 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
92 import org.springframework.beans.factory.annotation.Autowired;
94 public class ToscaResourceInstallerTest extends BaseTest {
96 private ToscaResourceInstaller toscaInstaller;
98 private WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
100 private AllottedResourceRepository allottedRepo;
102 private AllottedResourceCustomizationRepository allottedCustomizationRepo;
104 private ServiceRepository serviceRepo;
106 private SdcCsarHelperImpl sdcCsarHelper;
108 private Metadata metadata;
110 private ArtifactInfoImpl artifactInfo;
112 private List<Group> vfGroups;
114 private IResourceInstance resourceInstance;
116 public ExpectedException expectedException = ExpectedException.none();
118 private NotificationDataImpl notificationData;
119 private JsonStatusData statusData;
120 private static final String MSO = "SO";
123 public void before() {
124 MockitoAnnotations.initMocks(this);
126 notificationData = new NotificationDataImpl();
127 statusData = new JsonStatusData();
131 public void isResourceAlreadyDeployedTest() throws Exception {
132 notificationData.setServiceName("serviceName");
133 notificationData.setServiceVersion("123456");
134 notificationData.setServiceUUID("serviceUUID");
135 notificationData.setDistributionID("testStatusSuccessTosca");
137 WatchdogComponentDistributionStatus expectedComponentDistributionStatus =
138 new WatchdogComponentDistributionStatus(notificationData.getDistributionID(), MSO);
139 expectedComponentDistributionStatus.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
141 doReturn(true).when(vfResourceStructure).isDeployedSuccessfully();
142 doReturn(notificationData).when(vfResourceStructure).getNotification();
143 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
144 doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
145 doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
146 doReturn("resourceName").when(resourceInstance).getResourceName();
148 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
150 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId(notificationData.getDistributionID()), MSO);
152 verify(vfResourceStructure, times(3)).getResourceInstance();
153 verify(vfResourceStructure, times(5)).getNotification();
154 assertThat(actualWatchdogComponentDistributionStatus, sameBeanAs(expectedComponentDistributionStatus)
155 .ignoring("createTime")
156 .ignoring("modifyTime"));
160 public void isResourceAlreadyDeployedFalseTest() throws Exception {
161 notificationData.setServiceName("serviceName");
162 notificationData.setServiceVersion("123456");
163 notificationData.setServiceUUID("serviceUUID");
164 notificationData.setDistributionID("testStatusSuccess");
166 doThrow(RuntimeException.class).when(vfResourceStructure).isDeployedSuccessfully();
167 doReturn(notificationData).when(vfResourceStructure).getNotification();
168 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
169 doReturn("resourceInstanceName").when(resourceInstance).getResourceInstanceName();
170 doReturn("resourceCustomizationUUID").when(resourceInstance).getResourceCustomizationUUID();
171 doReturn("resourceName").when(resourceInstance).getResourceName();
173 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
175 verify(vfResourceStructure, times(3)).getResourceInstance();
176 verify(vfResourceStructure, times(4)).getNotification();
180 public void isResourceAlreadyDeployedExceptionTest() throws ArtifactInstallerException {
181 expectedException.expect(ArtifactInstallerException.class);
183 toscaInstaller.isResourceAlreadyDeployed(vfResourceStructure);
187 public void installTheComponentStatusTest() throws Exception {
188 String distributionId = "testStatusSuccessTosca";
189 String componentName = "testComponentName";
191 statusData = spy(JsonStatusData.class);
192 doReturn(distributionId).when(statusData).getDistributionID();
193 doReturn(componentName).when(statusData).getComponentName();
195 WatchdogComponentDistributionStatus expectedWatchdogComponentDistributionStatus =
196 new WatchdogComponentDistributionStatus(distributionId, componentName);
197 expectedWatchdogComponentDistributionStatus.setComponentDistributionStatus(statusData.getStatus().toString());
199 WatchdogComponentDistributionStatus cdStatus = new WatchdogComponentDistributionStatus(statusData.getDistributionID(),
200 statusData.getComponentName());
202 toscaInstaller.installTheComponentStatus(statusData);
204 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId("testStatusSuccessTosca"), statusData.getComponentName());
206 assertEquals(statusData.getDistributionID(), cdStatus.getDistributionId());
207 assertEquals(statusData.getComponentName(), cdStatus.getComponentName());
208 assertThat(actualWatchdogComponentDistributionStatus, sameBeanAs(expectedWatchdogComponentDistributionStatus)
209 .ignoring("createTime")
210 .ignoring("modifyTime"));
214 public void installTheComponentStatusExceptionTest() throws ArtifactInstallerException {
215 expectedException.expect(ArtifactInstallerException.class);
217 statusData = spy(JsonStatusData.class);
218 doReturn(null).when(statusData).getStatus();
220 toscaInstaller.installTheComponentStatus(statusData);
226 public void installTheResourceTest() throws Exception {
227 notificationData.setDistributionID("testStatusSuccessTosca");
228 notificationData.setServiceVersion("123456");
229 notificationData.setServiceUUID("5df8b6de-2083-11e7-93ae-92361f002671");
230 notificationData.setWorkloadContext("workloadContext");
232 HashMap<String, VfModuleArtifact> vfModuleArtifacts = mock(HashMap.class);
233 CapabilityAssignments capabilityAssignments = mock(CapabilityAssignments.class);
234 CapabilityAssignment capabilityAssignment = mock(CapabilityAssignment.class);
236 vfResourceStructure = spy(new VfResourceStructure(notificationData, resourceInstance));
238 VnfResource vnfResource = new VnfResource();
239 vnfResource.setModelName("modelName");
240 vnfResource.setModelVersion("1.1");
241 vnfResource.setModelUUID("modelUUID");
242 vnfResource.setOrchestrationMode("orchestrationMode");
244 VnfResourceCustomization vnfResourceCustomization = new VnfResourceCustomization();
245 vnfResourceCustomization.setVnfResources(vnfResource);
246 vnfResourceCustomization.setModelCustomizationUUID("vnfResCustModelCustomizationUUID");
247 vnfResourceCustomization.setModelInstanceName("modelInstanceName");
249 AllottedResource allottedResource = new AllottedResource();
250 allottedResource.setModelUUID("serviceMetadataValue");
251 allottedResource.setModelInvariantUUID("modelInvariantUUID");
252 allottedResource.setModelName("modelName");
253 allottedResource.setModelVersion("1.1");
255 AllottedResourceCustomization allottedResourceCustomization = new AllottedResourceCustomization();
256 allottedResourceCustomization.setAllottedResource(allottedResource);
257 allottedResourceCustomization.setModelCustomizationUUID("modelCustomizationUUID");
258 allottedResourceCustomization.setModelInstanceName("modelInstanceName");
260 Service catalogService = new Service();
261 catalogService.setServiceType("serviceType");
262 catalogService.setModelUUID("5df8b6de-2083-11e7-93ae-92361f002672");
263 catalogService.setModelInvariantUUID("modelInvariantUUID");
264 catalogService.setModelName("modelName");
265 catalogService.setModelVersion("modelVersion");
266 catalogService.getVnfCustomizations().add(vnfResourceCustomization);
268 Iterator artifactIterator = mock(Iterator.class);
269 Iterator nodeTemplateIterator = mock(Iterator.class);
270 IDistributionClientDownloadResult clientResult = mock(IDistributionClientDownloadResult.class);
271 doReturn(IOUtils.toByteArray(
274 getClass().getClassLoader().getResource("resource-examples/simpleTest.yaml").getFile())
275 ))).when(clientResult).getArtifactPayload();
276 VfModuleArtifact vfModuleArtifact = new VfModuleArtifact(artifactInfo, clientResult);
277 Collection<VfModuleArtifact> vfModuleArtifactsValues = mock(Collection.class);
279 NodeTemplate nodeTemplate = mock(NodeTemplate.class);
280 List<NodeTemplate> nodeTemplateList = new ArrayList<>();
281 nodeTemplateList.add(nodeTemplate);
283 HeatTemplate heatTemplate = new HeatTemplate();
284 heatTemplate.setArtifactUuid("ff874603-4222-11e7-9252-005056850d2e");
285 heatTemplate.setArtifactChecksum("MANUAL RECORD");
286 heatTemplate.setTemplateBody("templateBody");
287 heatTemplate.setTemplateName("module_mns_zrdm3frwl01exn_01_rgvm_1.yml");
288 heatTemplate.setVersion("1");
290 NetworkResource networkResource = new NetworkResource();
291 networkResource.setAicVersionMin("aicVersionMin");
292 networkResource.setModelUUID("modelUUID");
293 networkResource.setOrchestrationMode("orchestrationMode");
294 networkResource.setModelVersion("modelVersion");
295 networkResource.setNeutronNetworkType("neutronNetworkType");
296 networkResource.setAicVersionMax("aicVersionMax");
297 networkResource.setModelName("CONTRAIL30_GNDIRECT");
298 networkResource.setModelInvariantUUID("modelInvariantUUID");
299 networkResource.setHeatTemplate(heatTemplate);
301 NetworkResourceCustomization networkResourceCustomization = new NetworkResourceCustomization();
302 networkResourceCustomization.setModelCustomizationUUID("modelCustomizationUUID");
303 networkResourceCustomization.setModelInstanceName("modelInstanceName");
304 networkResourceCustomization.setNetworkResource(networkResource);
306 WatchdogServiceModVerIdLookup expectedModVerIdLookup = new WatchdogServiceModVerIdLookup(notificationData.getDistributionID(), notificationData.getServiceUUID());
307 WatchdogDistributionStatus expectedDistributionStatus = new WatchdogDistributionStatus(notificationData.getDistributionID());
308 WatchdogComponentDistributionStatus expectedComponentDistributionStatus = new WatchdogComponentDistributionStatus(notificationData.getDistributionID(), MSO);
309 expectedComponentDistributionStatus.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
311 doReturn(sdcCsarHelper).when(toscaResourceStruct).getSdcCsarHelper();
312 doReturn("metadataPropertyValue").when(sdcCsarHelper).getMetadataPropertyValue(any(Metadata.class), any(String.class));
313 doReturn("ntPropertyLeafValue").when(sdcCsarHelper).getNodeTemplatePropertyLeafValue(any(NodeTemplate.class), any(String.class));
314 doReturn("true").when(sdcCsarHelper).getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK);
315 doReturn("1").when(sdcCsarHelper).getCapabilityPropertyLeafValue(any(CapabilityAssignment.class), any(String.class));
316 doReturn(vfGroups).when(sdcCsarHelper).getVfModulesByVf(any(String.class));
317 doReturn(capabilityAssignments).when(sdcCsarHelper).getCapabilitiesOf(any(NodeTemplate.class));
318 doReturn(capabilityAssignment).when(capabilityAssignments).getCapabilityByName(any(String.class));
320 doReturn(catalogService).when(toscaResourceStruct).getCatalogService();
322 doReturn(artifactInfo).when(toscaResourceStruct).getToscaArtifact();
323 doReturn("artifactChecksum").when(artifactInfo).getArtifactChecksum();
324 doReturn("artifactUUID").when(artifactInfo).getArtifactUUID();
325 doReturn("artifactName").when(artifactInfo).getArtifactName();
326 doReturn("1.0").when(artifactInfo).getArtifactVersion();
327 doReturn("artifactDescription").when(artifactInfo).getArtifactDescription();
328 doReturn("artifactURL").when(artifactInfo).getArtifactURL();
330 doReturn(metadata).when(toscaResourceStruct).getServiceMetadata();
331 doReturn("serviceMetadataValue").when(metadata).getValue(any(String.class));
332 doReturn("CONTRAIL30_GNDIRECT").when(metadata).getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
334 doReturn("serviceVersion").when(toscaResourceStruct).getServiceVersion();
335 doReturn(nodeTemplateList).when(sdcCsarHelper).getServiceVfList();
336 doReturn(nodeTemplateList).when(sdcCsarHelper).getServiceVlList();
337 doReturn(nodeTemplateList).when(sdcCsarHelper).getAllottedResources();
338 doReturn(metadata).when(nodeTemplate).getMetaData();
339 doReturn("model_instance_name").when(nodeTemplate).getName();
340 doReturn(vnfResourceCustomization).when(toscaResourceStruct).getCatalogVnfResourceCustomization();
341 doReturn(allottedResource).when(toscaResourceStruct).getAllottedResource();
342 doReturn(allottedResourceCustomization).when(toscaResourceStruct).getCatalogAllottedResourceCustomization();
344 doReturn(vfModuleArtifacts).when(vfResourceStructure).getArtifactsMapByUUID();
345 when(vfModuleArtifacts.values()).thenReturn(vfModuleArtifactsValues);
346 when(vfModuleArtifactsValues.iterator()).thenReturn(artifactIterator);
347 when(artifactIterator.hasNext()).thenReturn(true, false);
348 when(artifactIterator.next()).thenReturn(vfModuleArtifact);
349 when(artifactInfo.getArtifactType()).thenReturn(ASDCConfiguration.OTHER);
350 when(nodeTemplateIterator.hasNext()).thenReturn(true, false);
352 doReturn(networkResource).when(toscaResourceStruct).getCatalogNetworkResource();
353 doReturn(networkResourceCustomization).when(toscaResourceStruct).getCatalogNetworkResourceCustomization();
355 doNothing().when(toscaResourceStruct).setSuccessfulDeployment();
357 toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
359 AllottedResource actualAllottedResource = allottedRepo.findResourceByModelUUID(allottedResource.getModelUUID());
360 AllottedResourceCustomization actualAllottedResourceCustomization = allottedCustomizationRepo.findOne(allottedResourceCustomization.getModelCustomizationUUID());
361 Service actualService = serviceRepo.findByServiceType(catalogService.getServiceType());
362 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = getWatchdogCDStatusWithName(watchdogCDStatusRepository.findByDistributionId(notificationData.getDistributionID()), MSO);
364 verify(toscaResourceStruct, times(1)).setSuccessfulDeployment();
365 assertThat(actualAllottedResource,
366 sameBeanAs(allottedResource));
367 assertThat(actualAllottedResourceCustomization, sameBeanAs(allottedResourceCustomization)
368 .ignoring("created"));
369 assertThat(actualService, sameBeanAs(catalogService)
371 .ignoring("networkCustomizations.0x1.created")
372 .ignoring("networkCustomizations.0x1.networkResource.created"));
373 assertThat(actualWatchdogComponentDistributionStatus, sameBeanAs(expectedComponentDistributionStatus)
374 .ignoring("createTime")
375 .ignoring("modifyTime"));
379 public void installTheResourceExceptionTest() throws Exception {
380 expectedException.expect(ArtifactInstallerException.class);
382 notificationData.setDistributionID("testStatusFailureTosca");
383 notificationData.setServiceVersion("123456");
384 notificationData.setServiceUUID("serviceUUID");
385 notificationData.setWorkloadContext("workloadContext");
387 doReturn(notificationData).when(vfResourceStructure).getNotification();
388 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
390 toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
394 public void installTheResourceDBExceptionTest() throws Exception {
395 notificationData.setDistributionID("testStatusSuccessTosca");
396 notificationData.setServiceVersion("123456");
397 notificationData.setServiceUUID("serviceUUID");
398 notificationData.setWorkloadContext("workloadContext");
400 doReturn(notificationData).when(vfResourceStructure).getNotification();
401 doReturn(resourceInstance).when(vfResourceStructure).getResourceInstance();
402 doThrow(LockAcquisitionException.class).when(toscaResourceStruct).getToscaArtifact();
404 toscaInstaller.installTheResource(toscaResourceStruct, vfResourceStructure);
408 public void verifyTheFilePrefixInStringTest() {
409 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
410 String filenameToVerify = "testfile.txt";
411 String expectedFileBody = "abcabcabctestfile.txtabcbabacbcabc";
413 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
415 assertEquals(expectedFileBody, newFileBody);
419 public void verifyTheFilePrefixInStringNullBodyTest() {
421 String filenameToVerify = "testfile.txt";
423 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
425 assertEquals(body, newFileBody);
429 public void verifyTheFilePrefixInStringEmptyBodyTest() {
431 String filenameToVerify = "testfile.txt";
433 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
435 assertEquals(body, newFileBody);
439 public void verifyTheFilePrefixInStringNullFilenameTest() {
440 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
441 String filenameToVerify = null;
443 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
445 assertEquals(body, newFileBody);
449 public void verifyTheFilePrefixInStringEmptyFilenameTest() {
450 String body = "abcabcabcfile:///testfile.txtabcbabacbcabc";
451 String filenameToVerify = "";
453 String newFileBody = toscaInstaller.verifyTheFilePrefixInString(body, filenameToVerify);
455 assertEquals(body, newFileBody);
458 private WatchdogComponentDistributionStatus getWatchdogCDStatusWithName(List<WatchdogComponentDistributionStatus> watchdogComponentDistributionStatuses, String componentName) {
459 WatchdogComponentDistributionStatus actualWatchdogComponentDistributionStatus = new WatchdogComponentDistributionStatus();
460 for(WatchdogComponentDistributionStatus watchdogComponentDistributionStatus : watchdogComponentDistributionStatuses) {
461 if(componentName.equals(watchdogComponentDistributionStatus.getComponentName())) {
462 actualWatchdogComponentDistributionStatus = watchdogComponentDistributionStatus;
466 return actualWatchdogComponentDistributionStatus;