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 static org.openecomp.sdc.tosca.csar.ManifestTokenType.ALGORITHM;
23 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.HASH;
24 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.METADATA;
25 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.NON_MANO_ARTIFACT_SETS;
26 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.SOURCE;
28 import java.util.ArrayList;
29 import java.util.List;
31 import java.util.Map.Entry;
32 import java.util.TreeMap;
35 * Builds SOL0004 manifest file as a String.
37 public class ManifestBuilder {
39 private final Map<String, Map<String, String>> sourceWithPropertiesMap = new TreeMap<>();
40 private final Map<String, List<String>> nonManoArtifactMap = new TreeMap<>();
41 private final Map<String, String> metadataMap = new TreeMap<>();
42 private static final String PROPERTY_FORMAT = "%s: %s%n";
43 private static final String SECTION_FORMAT = "%s:%n";
46 * Adds a metadata property.
48 * @param metadataProperty the property name
49 * @param value the property value
51 * a reference to this object.
53 public ManifestBuilder withMetaData(final String metadataProperty, final String value) {
54 metadataMap.put(metadataProperty, value);
59 * Adds a manifest source path.
61 * @param sourcePath The source path
63 * a reference to this object.
65 public ManifestBuilder withSource(final String sourcePath) {
66 sourceWithPropertiesMap.put(sourcePath, null);
71 * Adds a manifest source path with the source sign.
73 * @param sourcePath The source path
74 * @param hashAlgorithm The hash algorithm
75 * @param hash The hash representing the sign
77 * a reference to this object.
79 public ManifestBuilder withSignedSource(final String sourcePath, final String hashAlgorithm, final String hash) {
80 TreeMap<String, String> sourcePropertiesMap = new TreeMap<>();
81 sourcePropertiesMap.put(ALGORITHM.getToken(), hashAlgorithm);
82 sourcePropertiesMap.put(HASH.getToken(), hash);
83 sourceWithPropertiesMap.put(sourcePath, sourcePropertiesMap);
88 * Adds a non mano artifact.
90 * @param artifactType the artifact type
91 * @param sourcePath the artifact source path
93 * a reference to this object.
95 public ManifestBuilder withNonManoArtifact(final String artifactType, final String sourcePath) {
96 nonManoArtifactMap.putIfAbsent(artifactType, new ArrayList<>());
97 nonManoArtifactMap.get(artifactType).add(sourcePath);
103 * Builds the String representing the manifest file.
105 * The manifest file as String
107 public String build() {
108 final StringBuilder stringBuilder = new StringBuilder();
110 if (!metadataMap.isEmpty()) {
111 stringBuilder.append(buildMetadata());
114 if (!sourceWithPropertiesMap.isEmpty()) {
115 stringBuilder.append(buildSource());
118 if (!nonManoArtifactMap.isEmpty()) {
119 stringBuilder.append(buildNonManoArtifact());
122 return stringBuilder.toString();
125 private String buildMetadata() {
126 final StringBuilder stringBuilder = new StringBuilder();
127 stringBuilder.append(String.format(SECTION_FORMAT, METADATA.getToken()));
128 for (Entry<String, String> metadataAndValue : metadataMap.entrySet()) {
129 stringBuilder.append("\t");
130 stringBuilder.append(String.format(PROPERTY_FORMAT, metadataAndValue.getKey(), metadataAndValue.getValue()));
132 stringBuilder.append("\n");
133 return stringBuilder.toString();
136 private String buildSource() {
137 final StringBuilder stringBuilder = new StringBuilder();
138 for (final Entry<String, Map<String, String>> signedSourceMap : sourceWithPropertiesMap.entrySet()) {
139 stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), signedSourceMap.getKey()));
140 final Map<String, String> propertiesMap = signedSourceMap.getValue();
141 if (propertiesMap != null && !propertiesMap.isEmpty()) {
142 final String algorithm = propertiesMap.get(ALGORITHM.getToken());
143 if (algorithm != null) {
144 stringBuilder.append(String.format(PROPERTY_FORMAT, ALGORITHM.getToken(), algorithm));
147 final String hash = propertiesMap.get(HASH.getToken());
149 stringBuilder.append(String.format(PROPERTY_FORMAT, HASH.getToken(), hash));
153 stringBuilder.append("\n");
154 return stringBuilder.toString();
157 private String buildNonManoArtifact() {
158 final StringBuilder stringBuilder = new StringBuilder();
159 stringBuilder.append(String.format(SECTION_FORMAT, NON_MANO_ARTIFACT_SETS.getToken()));
160 for (Entry<String, List<String>> artifactTypeAndSourcesEntry : nonManoArtifactMap.entrySet()) {
161 stringBuilder.append("\t");
162 stringBuilder.append(String.format(SECTION_FORMAT, artifactTypeAndSourcesEntry.getKey()));
163 for (String source : artifactTypeAndSourcesEntry.getValue()) {
164 stringBuilder.append("\t\t");
165 stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), source));
168 return stringBuilder.toString();