2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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=========================================================
21 package org.openecomp.sdc.tosca.csar;
23 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ALGORITHM;
24 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.HASH;
25 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.METADATA;
26 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.NON_MANO_ARTIFACT_SETS;
27 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.SOURCE;
29 import java.util.ArrayList;
30 import java.util.List;
32 import java.util.Map.Entry;
33 import java.util.TreeMap;
36 * Builds SOL0004 manifest file as a String.
38 public class ManifestBuilder {
40 private static final String PROPERTY_FORMAT = "%s: %s%n";
41 private static final String SECTION_FORMAT = "%s:%n";
42 private final Map<String, Map<String, String>> sourceWithPropertiesMap = new TreeMap<>();
43 private final Map<String, List<String>> nonManoArtifactMap = new TreeMap<>();
44 private final Map<String, String> metadataMap = new TreeMap<>();
47 * Adds a metadata property.
49 * @param metadataProperty the property name
50 * @param value the property value
51 * @return 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
62 * @return a reference to this object.
64 public ManifestBuilder withSource(final String sourcePath) {
65 sourceWithPropertiesMap.put(sourcePath, null);
70 * Adds a manifest source path with the source sign.
72 * @param sourcePath The source path
73 * @param hashAlgorithm The hash algorithm
74 * @param hash The hash representing the sign
75 * @return a reference to this object.
77 public ManifestBuilder withSignedSource(final String sourcePath, final String hashAlgorithm, final String hash) {
78 TreeMap<String, String> sourcePropertiesMap = new TreeMap<>();
79 sourcePropertiesMap.put(ALGORITHM.getToken(), hashAlgorithm);
80 sourcePropertiesMap.put(HASH.getToken(), hash);
81 sourceWithPropertiesMap.put(sourcePath, sourcePropertiesMap);
86 * Adds a non mano artifact.
88 * @param artifactType the artifact type
89 * @param sourcePath the artifact source path
90 * @return a reference to this object.
92 public ManifestBuilder withNonManoArtifact(final String artifactType, final String sourcePath) {
93 nonManoArtifactMap.putIfAbsent(artifactType, new ArrayList<>());
94 nonManoArtifactMap.get(artifactType).add(sourcePath);
99 * Builds the String representing the manifest file.
101 * @return The manifest file as String
103 public String build() {
104 final StringBuilder stringBuilder = new StringBuilder();
105 if (!metadataMap.isEmpty()) {
106 stringBuilder.append(buildMetadata());
108 if (!sourceWithPropertiesMap.isEmpty()) {
109 stringBuilder.append(buildSource());
111 if (!nonManoArtifactMap.isEmpty()) {
112 stringBuilder.append(buildNonManoArtifact());
114 return stringBuilder.toString();
117 private String buildMetadata() {
118 final StringBuilder stringBuilder = new StringBuilder();
119 stringBuilder.append(String.format(SECTION_FORMAT, METADATA.getToken()));
120 for (Entry<String, String> metadataAndValue : metadataMap.entrySet()) {
121 stringBuilder.append("\t");
122 stringBuilder.append(String.format(PROPERTY_FORMAT, metadataAndValue.getKey(), metadataAndValue.getValue()));
124 stringBuilder.append("\n");
125 return stringBuilder.toString();
128 private String buildSource() {
129 final StringBuilder stringBuilder = new StringBuilder();
130 for (final Entry<String, Map<String, String>> signedSourceMap : sourceWithPropertiesMap.entrySet()) {
131 stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), signedSourceMap.getKey()));
132 final Map<String, String> propertiesMap = signedSourceMap.getValue();
133 if (propertiesMap != null && !propertiesMap.isEmpty()) {
134 final String algorithm = propertiesMap.get(ALGORITHM.getToken());
135 if (algorithm != null) {
136 stringBuilder.append(String.format(PROPERTY_FORMAT, ALGORITHM.getToken(), algorithm));
138 final String hash = propertiesMap.get(HASH.getToken());
140 stringBuilder.append(String.format(PROPERTY_FORMAT, HASH.getToken(), hash));
144 stringBuilder.append("\n");
145 return stringBuilder.toString();
148 private String buildNonManoArtifact() {
149 final StringBuilder stringBuilder = new StringBuilder();
150 stringBuilder.append(String.format(SECTION_FORMAT, NON_MANO_ARTIFACT_SETS.getToken()));
151 for (Entry<String, List<String>> artifactTypeAndSourcesEntry : nonManoArtifactMap.entrySet()) {
152 stringBuilder.append("\t");
153 stringBuilder.append(String.format(SECTION_FORMAT, artifactTypeAndSourcesEntry.getKey()));
154 for (String source : artifactTypeAndSourcesEntry.getValue()) {
155 stringBuilder.append("\t\t");
156 stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), source));
159 return stringBuilder.toString();