eff1fb31cdcd0e3666584e5acce0892728b01d18
[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 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;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.TreeMap;
33
34 /**
35  * Builds SOL0004 manifest file as a String.
36  */
37 public class ManifestBuilder {
38
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";
44
45     /**
46      * Adds a metadata property.
47      *
48      * @param metadataProperty      the property name
49      * @param value                 the property value
50      * @return
51      *  a reference to this object.
52      */
53     public ManifestBuilder withMetaData(final String metadataProperty, final String value) {
54         metadataMap.put(metadataProperty, value);
55         return this;
56     }
57
58     /**
59      * Adds a manifest source path.
60      *
61      * @param sourcePath    The source path
62      * @return
63      *  a reference to this object.
64      */
65     public ManifestBuilder withSource(final String sourcePath) {
66         sourceWithPropertiesMap.put(sourcePath, null);
67         return this;
68     }
69
70     /**
71      * Adds a manifest source path with the source sign.
72      *
73      * @param sourcePath    The source path
74      * @param hashAlgorithm     The hash algorithm
75      * @param hash          The hash representing the sign
76      * @return
77      *  a reference to this object.
78      */
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);
84         return this;
85     }
86
87     /**
88      * Adds a non mano artifact.
89      *
90      * @param artifactType  the artifact type
91      * @param sourcePath    the artifact source path
92      * @return
93      *  a reference to this object.
94      */
95     public ManifestBuilder withNonManoArtifact(final String artifactType, final String sourcePath) {
96         nonManoArtifactMap.putIfAbsent(artifactType, new ArrayList<>());
97         nonManoArtifactMap.get(artifactType).add(sourcePath);
98         return this;
99     }
100
101
102     /**
103      * Builds the String representing the manifest file.
104      * @return
105      *  The manifest file as String
106      */
107     public String build() {
108         final StringBuilder stringBuilder = new StringBuilder();
109
110         if (!metadataMap.isEmpty()) {
111             stringBuilder.append(buildMetadata());
112         }
113
114         if (!sourceWithPropertiesMap.isEmpty()) {
115             stringBuilder.append(buildSource());
116         }
117
118         if (!nonManoArtifactMap.isEmpty()) {
119             stringBuilder.append(buildNonManoArtifact());
120         }
121
122         return stringBuilder.toString();
123     }
124
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()));
131         }
132         stringBuilder.append("\n");
133         return stringBuilder.toString();
134     }
135
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));
145                 }
146
147                 final String hash = propertiesMap.get(HASH.getToken());
148                 if (hash != null) {
149                     stringBuilder.append(String.format(PROPERTY_FORMAT, HASH.getToken(), hash));
150                 }
151             }
152         }
153         stringBuilder.append("\n");
154         return stringBuilder.toString();
155     }
156
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));
166             }
167         }
168         return stringBuilder.toString();
169     }
170
171 }