2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.components.impl;
22 import org.apache.commons.collections.CollectionUtils;
23 import org.openecomp.sdc.be.components.csar.CsarInfo;
24 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
25 import org.openecomp.sdc.be.csar.pnf.PnfSoftwareInformation;
26 import org.openecomp.sdc.be.csar.pnf.PnfSoftwareVersion;
27 import org.openecomp.sdc.be.csar.pnf.SoftwareInformationArtifactYamlParser;
28 import org.openecomp.sdc.be.model.PropertyDefinition;
29 import org.openecomp.sdc.be.model.Resource;
30 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
34 import java.util.List;
35 import java.util.Optional;
37 import static java.util.stream.Collectors.toList;
38 import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue;
40 @Component("softwareInformationBusinessLogic")
41 public class SoftwareInformationBusinessLogic {
43 private final PropertyBusinessLogic propertyBusinessLogic;
44 private static final String SOFTWARE_VERSION_PROPERTY_NAME = "software_versions";
47 public SoftwareInformationBusinessLogic(final PropertyBusinessLogic propertyBusinessLogic) {
48 this.propertyBusinessLogic = propertyBusinessLogic;
52 * Adds the software information from a csar package to the resource {@link SoftwareInformationBusinessLogic#SOFTWARE_VERSION_PROPERTY_NAME}
53 * property.<br/> The csar package must contain the expected non-mano yaml file with the software information. Also
54 * the resource must have the {@link SoftwareInformationBusinessLogic#SOFTWARE_VERSION_PROPERTY_NAME} property.
56 * @param resource the resource to add the software information
57 * @param csarInfo the csar package representation
58 * @return if the expected property exists in the resource and the csar package contains the software information
59 * file, an Optional<PropertyDefinition> with the updated property; otherwise Optional.empty().
60 * @throws BusinessLogicException when there was a problem while updating the property
62 public Optional<PropertyDefinition> setSoftwareInformation(final Resource resource,
63 final CsarInfo csarInfo) throws BusinessLogicException {
64 final Optional<String> softwareInformation = csarInfo.getSoftwareInformationPath();
65 if (!softwareInformation.isPresent()) {
66 return Optional.empty();
68 final PropertyDefinition propertyDefinition = findSoftwareVersionPropertyDefinition(resource).orElse(null);
69 if (propertyDefinition == null) {
70 return Optional.empty();
72 final byte[] softwareInformationYaml = csarInfo.getCsar().get(softwareInformation.get());
73 final PnfSoftwareInformation pnfSoftwareInformation =
74 parseSoftwareInformation(softwareInformationYaml).orElse(null);
75 if (pnfSoftwareInformation == null) {
76 return Optional.empty();
79 final List<String> versionList = pnfSoftwareInformation.getSoftwareVersionSet().stream()
80 .map(PnfSoftwareVersion::getVersion).collect(toList());
81 final String softwareVersionInformation =
82 getPropertyJsonStringValue(versionList, ToscaPropertyType.LIST.getType());
83 propertyDefinition.setValue(softwareVersionInformation);
85 final PropertyDefinition updatedPropertyDefinition =
86 propertyBusinessLogic.updateComponentProperty(resource.getUniqueId(), propertyDefinition);
87 return Optional.ofNullable(updatedPropertyDefinition);
91 * Parses the non-mano software information yaml file.
93 * @param softwareInformationYaml the file byte array
94 * @return an {@code Optional<PnfSoftwareInformation>} if the file was successful parsed, otherwise {@code
97 private Optional<PnfSoftwareInformation> parseSoftwareInformation(byte[] softwareInformationYaml) {
98 return SoftwareInformationArtifactYamlParser.parse(softwareInformationYaml);
102 * Finds the {@link SoftwareInformationBusinessLogic#SOFTWARE_VERSION_PROPERTY_NAME} property in a Resource
103 * @param resource the resource to search for the property
104 * @return an {@code Optional<PnfSoftwareInformation>} if the property was found, otherwise {@code Optional.empty()}
106 private Optional<PropertyDefinition> findSoftwareVersionPropertyDefinition(final Resource resource) {
107 if (CollectionUtils.isEmpty(resource.getProperties())) {
108 return Optional.empty();
110 return resource.getProperties().stream()
111 .filter(propertyDefinition -> propertyDefinition.getName().equals(SOFTWARE_VERSION_PROPERTY_NAME))
116 * Removes the non-mano software information file from the csar package
118 * @param csarInfo the csar package representation
119 * @return {@code true} if the file was removed, otherwise {@code false}
121 public boolean removeSoftwareInformationFile(final CsarInfo csarInfo) {
122 final Optional<String> softwareInformation = csarInfo.getSoftwareInformationPath();
123 if (!softwareInformation.isPresent()) {
127 csarInfo.getCsar().remove(softwareInformation.get());