Create onboarding validator for ASD VSPs
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / csar / ManifestBuilder.java
1 /*
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
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  */
21 package org.openecomp.sdc.tosca.csar;
22
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;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.TreeMap;
34
35 /**
36  * Builds SOL0004 manifest file as a String.
37  */
38 public class ManifestBuilder {
39
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<>();
45
46     /**
47      * Adds a metadata property.
48      *
49      * @param metadataProperty the property name
50      * @param value            the property value
51      * @return 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 a reference to this object.
63      */
64     public ManifestBuilder withSource(final String sourcePath) {
65         sourceWithPropertiesMap.put(sourcePath, null);
66         return this;
67     }
68
69     /**
70      * Adds a manifest source path with the source sign.
71      *
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.
76      */
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);
82         return this;
83     }
84
85     /**
86      * Adds a non mano artifact.
87      *
88      * @param artifactType the artifact type
89      * @param sourcePath   the artifact source path
90      * @return a reference to this object.
91      */
92     public ManifestBuilder withNonManoArtifact(final String artifactType, final String sourcePath) {
93         nonManoArtifactMap.putIfAbsent(artifactType, new ArrayList<>());
94         nonManoArtifactMap.get(artifactType).add(sourcePath);
95         return this;
96     }
97
98     /**
99      * Builds the String representing the manifest file.
100      *
101      * @return The manifest file as String
102      */
103     public String build() {
104         final StringBuilder stringBuilder = new StringBuilder();
105         if (!metadataMap.isEmpty()) {
106             stringBuilder.append(buildMetadata());
107         }
108         if (!sourceWithPropertiesMap.isEmpty()) {
109             stringBuilder.append(buildSource());
110         }
111         if (!nonManoArtifactMap.isEmpty()) {
112             stringBuilder.append(buildNonManoArtifact());
113         }
114         return stringBuilder.toString();
115     }
116
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()));
123         }
124         stringBuilder.append("\n");
125         return stringBuilder.toString();
126     }
127
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));
137                 }
138                 final String hash = propertiesMap.get(HASH.getToken());
139                 if (hash != null) {
140                     stringBuilder.append(String.format(PROPERTY_FORMAT, HASH.getToken(), hash));
141                 }
142             }
143         }
144         stringBuilder.append("\n");
145         return stringBuilder.toString();
146     }
147
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));
157             }
158         }
159         return stringBuilder.toString();
160     }
161 }