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.vendorsoftwareproduct.impl.orchestration.csar.validation;
22 import java.util.ArrayList;
23 import java.util.List;
25 import java.util.Map.Entry;
26 import java.util.TreeMap;
27 import org.openecomp.sdc.tosca.csar.CSARConstants;
30 * Builds SOL0004 manifest file as a String.
32 public class ManifestBuilder {
34 private final Map<String, Map<String, String>> sourceWithPropertiesMap = new TreeMap<>();
35 private final Map<String, List<String>> nonManoArtifactMap = new TreeMap<>();
36 private final Map<String, String> metadataMap = new TreeMap<>();
37 private static final String PROPERTY_FORMAT = "%s: %s%n";
38 private static final String SECTION_FORMAT = "%s:%n";
41 * Adds a metadata property.
43 * @param metadataProperty the property name
44 * @param value the property value
46 * a reference to this object.
48 public ManifestBuilder withMetaData(final String metadataProperty, final String value) {
49 metadataMap.put(metadataProperty, value);
54 * Adds a manifest source path.
56 * @param sourcePath The source path
58 * a reference to this object.
60 public ManifestBuilder withSource(final String sourcePath) {
61 sourceWithPropertiesMap.put(sourcePath, null);
66 * Adds a manifest source path with the source sign.
68 * @param sourcePath The source path
69 * @param hashAlgorithm The hash algorithm
70 * @param hash The hash representing the sign
72 * a reference to this object.
74 public ManifestBuilder withSignedSource(final String sourcePath, final String hashAlgorithm, final String hash) {
75 TreeMap<String, String> sourcePropertiesMap = new TreeMap<>();
76 sourcePropertiesMap.put(CSARConstants.ALGORITHM_MF_ATTRIBUTE, hashAlgorithm);
77 sourcePropertiesMap.put(CSARConstants.HASH_MF_ATTRIBUTE, hash);
78 sourceWithPropertiesMap.put(sourcePath, sourcePropertiesMap);
83 * Adds a non mano artifact.
85 * @param artifactType the artifact type
86 * @param sourcePath the artifact source path
88 * a reference to this object.
90 public ManifestBuilder withNonManoArtifact(final String artifactType, final String sourcePath) {
91 nonManoArtifactMap.putIfAbsent(artifactType, new ArrayList<>());
92 nonManoArtifactMap.get(artifactType).add(sourcePath);
98 * Builds the String representing the manifest file.
100 * The manifest file as String
102 public String build() {
103 final StringBuilder stringBuilder = new StringBuilder();
105 if (!metadataMap.isEmpty()) {
106 stringBuilder.append(buildMetadata());
109 if (!sourceWithPropertiesMap.isEmpty()) {
110 stringBuilder.append(buildSource());
113 if (!nonManoArtifactMap.isEmpty()) {
114 stringBuilder.append(buildNonManoArtifact());
117 return stringBuilder.toString();
120 private String buildMetadata() {
121 final StringBuilder stringBuilder = new StringBuilder();
122 stringBuilder.append(String.format(SECTION_FORMAT, CSARConstants.METADATA_MF_ATTRIBUTE));
123 for (Entry<String, String> metadataAndValue : metadataMap.entrySet()) {
124 stringBuilder.append("\t");
125 stringBuilder.append(String.format(PROPERTY_FORMAT, metadataAndValue.getKey(), metadataAndValue.getValue()));
127 stringBuilder.append("\n");
128 return stringBuilder.toString();
131 private String buildSource() {
132 final StringBuilder stringBuilder = new StringBuilder();
133 for (final Entry<String, Map<String, String>> signedSourceMap : sourceWithPropertiesMap.entrySet()) {
134 stringBuilder.append(String.format(PROPERTY_FORMAT, CSARConstants.SOURCE_MF_ATTRIBUTE, signedSourceMap.getKey()));
135 final Map<String, String> propertiesMap = signedSourceMap.getValue();
136 if (propertiesMap != null && !propertiesMap.isEmpty()) {
137 final String algorithm = propertiesMap.get(CSARConstants.ALGORITHM_MF_ATTRIBUTE);
138 if (algorithm != null) {
139 stringBuilder.append(String.format(PROPERTY_FORMAT, CSARConstants.ALGORITHM_MF_ATTRIBUTE, algorithm));
142 final String hash = propertiesMap.get(CSARConstants.HASH_MF_ATTRIBUTE);
144 stringBuilder.append(String.format(PROPERTY_FORMAT, CSARConstants.HASH_MF_ATTRIBUTE, hash));
148 stringBuilder.append("\n");
149 return stringBuilder.toString();
152 private String buildNonManoArtifact() {
153 final StringBuilder stringBuilder = new StringBuilder();
154 stringBuilder.append(String.format(SECTION_FORMAT, CSARConstants.NON_MANO_MF_ATTRIBUTE));
155 for (Entry<String, List<String>> artifactTypeAndSourcesEntry : nonManoArtifactMap.entrySet()) {
156 stringBuilder.append("\t");
157 stringBuilder.append(String.format(SECTION_FORMAT, artifactTypeAndSourcesEntry.getKey()));
158 for (String source : artifactTypeAndSourcesEntry.getValue()) {
159 stringBuilder.append("\t\t");
160 stringBuilder.append(String.format(PROPERTY_FORMAT, CSARConstants.SOURCE_MF_ATTRIBUTE, source));
163 return stringBuilder.toString();