ee708b4176cda31ac28e72e439153a4ecec4e3a0
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / validation / ManifestBuilder.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 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
20
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;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.TreeMap;
32
33 /**
34  * Builds SOL0004 manifest file as a String.
35  */
36 public class ManifestBuilder {
37
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<>();
43
44     /**
45      * Adds a metadata property.
46      *
47      * @param metadataProperty the property name
48      * @param value            the property value
49      * @return a reference to this object.
50      */
51     public ManifestBuilder withMetaData(final String metadataProperty, final String value) {
52         metadataMap.put(metadataProperty, value);
53         return this;
54     }
55
56     /**
57      * Adds a manifest source path.
58      *
59      * @param sourcePath The source path
60      * @return a reference to this object.
61      */
62     public ManifestBuilder withSource(final String sourcePath) {
63         sourceWithPropertiesMap.put(sourcePath, null);
64         return this;
65     }
66
67     /**
68      * Adds a manifest source path with the source sign.
69      *
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.
74      */
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);
80         return this;
81     }
82
83     /**
84      * Adds a non mano artifact.
85      *
86      * @param artifactType the artifact type
87      * @param sourcePath   the artifact source path
88      * @return a reference to this object.
89      */
90     public ManifestBuilder withNonManoArtifact(final String artifactType, final String sourcePath) {
91         nonManoArtifactMap.putIfAbsent(artifactType, new ArrayList<>());
92         nonManoArtifactMap.get(artifactType).add(sourcePath);
93         return this;
94     }
95
96     /**
97      * Builds the String representing the manifest file.
98      *
99      * @return The manifest file as String
100      */
101     public String build() {
102         final StringBuilder stringBuilder = new StringBuilder();
103         if (!metadataMap.isEmpty()) {
104             stringBuilder.append(buildMetadata());
105         }
106         if (!sourceWithPropertiesMap.isEmpty()) {
107             stringBuilder.append(buildSource());
108         }
109         if (!nonManoArtifactMap.isEmpty()) {
110             stringBuilder.append(buildNonManoArtifact());
111         }
112         return stringBuilder.toString();
113     }
114
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()));
121         }
122         stringBuilder.append("\n");
123         return stringBuilder.toString();
124     }
125
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));
135                 }
136                 final String hash = propertiesMap.get(HASH.getToken());
137                 if (hash != null) {
138                     stringBuilder.append(String.format(PROPERTY_FORMAT, HASH.getToken(), hash));
139                 }
140             }
141         }
142         stringBuilder.append("\n");
143         return stringBuilder.toString();
144     }
145
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));
155             }
156         }
157         return stringBuilder.toString();
158     }
159 }