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=========================================================
19 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
21 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ALGORITHM;
22 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.HASH;
23 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.METADATA;
24 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.NON_MANO_ARTIFACT_SETS;
25 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.SOURCE;
27 import java.util.ArrayList;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import java.util.TreeMap;
34 * Builds SOL0004 manifest file as a String.
36 public class ManifestBuilder {
38 private static final String PROPERTY_FORMAT = "%s: %s%n";
39 private static final String SECTION_FORMAT = "%s:%n";
40 private final Map<String, Map<String, String>> sourceWithPropertiesMap = new TreeMap<>();
41 private final Map<String, List<String>> nonManoArtifactMap = new TreeMap<>();
42 private final Map<String, String> metadataMap = new TreeMap<>();
45 * Adds a metadata property.
47 * @param metadataProperty the property name
48 * @param value the property value
49 * @return a reference to this object.
51 public ManifestBuilder withMetaData(final String metadataProperty, final String value) {
52 metadataMap.put(metadataProperty, value);
57 * Adds a manifest source path.
59 * @param sourcePath The source path
60 * @return a reference to this object.
62 public ManifestBuilder withSource(final String sourcePath) {
63 sourceWithPropertiesMap.put(sourcePath, null);
68 * Adds a manifest source path with the source sign.
70 * @param sourcePath The source path
71 * @param hashAlgorithm The hash algorithm
72 * @param hash The hash representing the sign
73 * @return a reference to this object.
75 public ManifestBuilder withSignedSource(final String sourcePath, final String hashAlgorithm, final String hash) {
76 TreeMap<String, String> sourcePropertiesMap = new TreeMap<>();
77 sourcePropertiesMap.put(ALGORITHM.getToken(), hashAlgorithm);
78 sourcePropertiesMap.put(HASH.getToken(), hash);
79 sourceWithPropertiesMap.put(sourcePath, sourcePropertiesMap);
84 * Adds a non mano artifact.
86 * @param artifactType the artifact type
87 * @param sourcePath the artifact source path
88 * @return 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);
97 * Builds the String representing the manifest file.
99 * @return The manifest file as String
101 public String build() {
102 final StringBuilder stringBuilder = new StringBuilder();
103 if (!metadataMap.isEmpty()) {
104 stringBuilder.append(buildMetadata());
106 if (!sourceWithPropertiesMap.isEmpty()) {
107 stringBuilder.append(buildSource());
109 if (!nonManoArtifactMap.isEmpty()) {
110 stringBuilder.append(buildNonManoArtifact());
112 return stringBuilder.toString();
115 private String buildMetadata() {
116 final StringBuilder stringBuilder = new StringBuilder();
117 stringBuilder.append(String.format(SECTION_FORMAT, METADATA.getToken()));
118 for (Entry<String, String> metadataAndValue : metadataMap.entrySet()) {
119 stringBuilder.append("\t");
120 stringBuilder.append(String.format(PROPERTY_FORMAT, metadataAndValue.getKey(), metadataAndValue.getValue()));
122 stringBuilder.append("\n");
123 return stringBuilder.toString();
126 private String buildSource() {
127 final StringBuilder stringBuilder = new StringBuilder();
128 for (final Entry<String, Map<String, String>> signedSourceMap : sourceWithPropertiesMap.entrySet()) {
129 stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), signedSourceMap.getKey()));
130 final Map<String, String> propertiesMap = signedSourceMap.getValue();
131 if (propertiesMap != null && !propertiesMap.isEmpty()) {
132 final String algorithm = propertiesMap.get(ALGORITHM.getToken());
133 if (algorithm != null) {
134 stringBuilder.append(String.format(PROPERTY_FORMAT, ALGORITHM.getToken(), algorithm));
136 final String hash = propertiesMap.get(HASH.getToken());
138 stringBuilder.append(String.format(PROPERTY_FORMAT, HASH.getToken(), hash));
142 stringBuilder.append("\n");
143 return stringBuilder.toString();
146 private String buildNonManoArtifact() {
147 final StringBuilder stringBuilder = new StringBuilder();
148 stringBuilder.append(String.format(SECTION_FORMAT, NON_MANO_ARTIFACT_SETS.getToken()));
149 for (Entry<String, List<String>> artifactTypeAndSourcesEntry : nonManoArtifactMap.entrySet()) {
150 stringBuilder.append("\t");
151 stringBuilder.append(String.format(SECTION_FORMAT, artifactTypeAndSourcesEntry.getKey()));
152 for (String source : artifactTypeAndSourcesEntry.getValue()) {
153 stringBuilder.append("\t\t");
154 stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), source));
157 return stringBuilder.toString();