c7fd225c76770e8143e20e31762247e4812932e9
[sdc.git] /
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
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.TreeMap;
27 import org.openecomp.sdc.tosca.csar.CSARConstants;
28
29 /**
30  * Builds SOL0004 manifest file as a String.
31  */
32 public class ManifestBuilder {
33
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";
39
40     /**
41      * Adds a metadata property.
42      *
43      * @param metadataProperty      the property name
44      * @param value                 the property value
45      * @return
46      *  a reference to this object.
47      */
48     public ManifestBuilder withMetaData(final String metadataProperty, final String value) {
49         metadataMap.put(metadataProperty, value);
50         return this;
51     }
52
53     /**
54      * Adds a manifest source path.
55      *
56      * @param sourcePath    The source path
57      * @return
58      *  a reference to this object.
59      */
60     public ManifestBuilder withSource(final String sourcePath) {
61         sourceWithPropertiesMap.put(sourcePath, null);
62         return this;
63     }
64
65     /**
66      * Adds a manifest source path with the source sign.
67      *
68      * @param sourcePath    The source path
69      * @param hashAlgorithm     The hash algorithm
70      * @param hash          The hash representing the sign
71      * @return
72      *  a reference to this object.
73      */
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);
79         return this;
80     }
81
82     /**
83      * Adds a non mano artifact.
84      *
85      * @param artifactType  the artifact type
86      * @param sourcePath    the artifact source path
87      * @return
88      *  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     /**
98      * Builds the String representing the manifest file.
99      * @return
100      *  The manifest file as String
101      */
102     public String build() {
103         final StringBuilder stringBuilder = new StringBuilder();
104
105         if (!metadataMap.isEmpty()) {
106             stringBuilder.append(buildMetadata());
107         }
108
109         if (!sourceWithPropertiesMap.isEmpty()) {
110             stringBuilder.append(buildSource());
111         }
112
113         if (!nonManoArtifactMap.isEmpty()) {
114             stringBuilder.append(buildNonManoArtifact());
115         }
116
117         return stringBuilder.toString();
118     }
119
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()));
126         }
127         stringBuilder.append("\n");
128         return stringBuilder.toString();
129     }
130
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));
140                 }
141
142                 final String hash = propertiesMap.get(CSARConstants.HASH_MF_ATTRIBUTE);
143                 if (hash != null) {
144                     stringBuilder.append(String.format(PROPERTY_FORMAT, CSARConstants.HASH_MF_ATTRIBUTE, hash));
145                 }
146             }
147         }
148         stringBuilder.append("\n");
149         return stringBuilder.toString();
150     }
151
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));
161             }
162         }
163         return stringBuilder.toString();
164     }
165
166 }