Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / SoftwareInformationBusinessLogic.java
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.components.impl;
21
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;
33
34 import java.util.List;
35 import java.util.Optional;
36
37 import static java.util.stream.Collectors.toList;
38 import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue;
39
40 @Component("softwareInformationBusinessLogic")
41 public class SoftwareInformationBusinessLogic {
42
43     private final PropertyBusinessLogic propertyBusinessLogic;
44     private static final String SOFTWARE_VERSION_PROPERTY_NAME = "software_versions";
45
46     @Autowired
47     public SoftwareInformationBusinessLogic(final PropertyBusinessLogic propertyBusinessLogic) {
48         this.propertyBusinessLogic = propertyBusinessLogic;
49     }
50
51     /**
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.
55      *
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
61      */
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();
67         }
68         final PropertyDefinition propertyDefinition = findSoftwareVersionPropertyDefinition(resource).orElse(null);
69         if (propertyDefinition == null) {
70             return Optional.empty();
71         }
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();
77         }
78
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);
84
85         final PropertyDefinition updatedPropertyDefinition =
86             propertyBusinessLogic.updateComponentProperty(resource.getUniqueId(), propertyDefinition);
87         return Optional.ofNullable(updatedPropertyDefinition);
88     }
89
90     /**
91      * Parses the non-mano software information yaml file.
92      *
93      * @param softwareInformationYaml the file byte array
94      * @return an {@code Optional<PnfSoftwareInformation>} if the file was successful parsed, otherwise {@code
95      * Optional.empty()}
96      */
97     private Optional<PnfSoftwareInformation> parseSoftwareInformation(byte[] softwareInformationYaml) {
98         return SoftwareInformationArtifactYamlParser.parse(softwareInformationYaml);
99     }
100
101     /**
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()}
105      */
106     private Optional<PropertyDefinition> findSoftwareVersionPropertyDefinition(final Resource resource) {
107         if (CollectionUtils.isEmpty(resource.getProperties())) {
108             return Optional.empty();
109         }
110         return resource.getProperties().stream()
111             .filter(propertyDefinition -> propertyDefinition.getName().equals(SOFTWARE_VERSION_PROPERTY_NAME))
112             .findFirst();
113     }
114
115     /**
116      * Removes the non-mano software information file from the csar package
117      *
118      * @param csarInfo the csar package representation
119      * @return {@code true} if the file was removed, otherwise {@code false}
120      */
121     public boolean removeSoftwareInformationFile(final CsarInfo csarInfo) {
122         final Optional<String> softwareInformation = csarInfo.getSoftwareInformationPath();
123         if (!softwareInformation.isPresent()) {
124             return false;
125         }
126
127         csarInfo.getCsar().remove(softwareInformation.get());
128         return true;
129     }
130 }