ed3a7fc4ee0f7bc81e0c162e49c82cbed07b8119
[sdc.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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.be.plugins.etsi.nfv.nsd.builder;
21
22 import java.time.ZonedDateTime;
23 import java.time.format.DateTimeFormatter;
24 import java.util.Collection;
25 import java.util.LinkedHashSet;
26 import java.util.Set;
27 import java.util.TreeSet;
28
29 /**
30  * Builder for the manifest (.mf) file in a NSD CSAR
31  */
32 public class NsdCsarManifestBuilder {
33     static final String METADATA = "metadata";
34     static final String SOURCE = "Source";
35     static final String COMPATIBLE_SPECIFICATION_VERSIONS = "compatible_specification_versions";
36     private static final String NSD_DESIGNER = "nsd_designer";
37     private static final String NSD_FILE_STRUCTURE_VERSION = "nsd_file_structure_version";
38     static final String NSD_RELEASE_DATE_TIME = "nsd_release_date_time";
39     private static final String NSD_NAME = "nsd_name";
40     private static final String NSD_INVARIANT_ID = "nsd_invariant_id";
41     static final String ATTRIBUTE_SEPARATOR = ": ";
42     private static final String NEW_LINE = "\n";
43     private static final DateTimeFormatter RFC_3339_DATE_TIME_FORMATTER =
44         DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
45
46     private final MetadataHeader metadataHeader;
47     private final Set<String> sources;
48     private final Set<String> compatibleSpecificationVersions;
49
50     public NsdCsarManifestBuilder() {
51         metadataHeader = new MetadataHeader();
52         sources = new LinkedHashSet<>();
53         compatibleSpecificationVersions = new TreeSet<>();
54     }
55
56     /**
57      * Sets a value for the {@link #NSD_DESIGNER} manifest entry.
58      *
59      * @param designer the value
60      * @return the builder instance
61      */
62     public NsdCsarManifestBuilder withDesigner(final String designer) {
63         metadataHeader.designer = designer;
64         return this;
65     }
66
67     /**
68      * Sets a value for the {@link #NSD_INVARIANT_ID} manifest entry.
69      *
70      * @param invariantId the value
71      * @return the builder instance
72      */
73     public NsdCsarManifestBuilder withInvariantId(final String invariantId) {
74         metadataHeader.invariantId = invariantId;
75         return this;
76     }
77
78     /**
79      * Sets a value for the {@link #NSD_NAME} manifest entry.
80      *
81      * @param nsdName the value
82      * @return the builder instance
83      */
84     public NsdCsarManifestBuilder withName(final String nsdName) {
85         metadataHeader.nsdName = nsdName;
86         return this;
87     }
88
89     /**
90      * Sets the current date time to the {@link #NSD_RELEASE_DATE_TIME} manifest entry.
91      *
92      * @return the builder instance
93      */
94     public NsdCsarManifestBuilder withNowReleaseDateTime() {
95         metadataHeader.nsdReleaseDateTime = getNowDateTime();
96         return this;
97     }
98
99     /**
100      * Sets a value for the {@link #NSD_FILE_STRUCTURE_VERSION} manifest entry.
101      *
102      * @param fileStructureVersion the value
103      * @return the builder instance
104      */
105     public NsdCsarManifestBuilder withFileStructureVersion(final String fileStructureVersion) {
106         metadataHeader.fileStructureVersion = fileStructureVersion;
107         return this;
108     }
109
110     /**
111      * Add a list of {@link #SOURCE} entries to the manifest
112      *
113      * @param sources a list of source path
114      * @return the builder instance
115      */
116     public NsdCsarManifestBuilder withSources(final Collection<String> sources) {
117         this.sources.addAll(sources);
118         return this;
119     }
120
121     public NsdCsarManifestBuilder withCompatibleSpecificationVersion(final String version) {
122         this.compatibleSpecificationVersions.add(version);
123         return this;
124     }
125
126     /**
127      * Builds a string representing the manifest content based on provided values.
128      *
129      * @return a string representing the manifest content
130      */
131     public String build() {
132         final StringBuilder metadataBuilder = createMetadataBuilder();
133         appendEntry(metadataBuilder, NSD_DESIGNER, metadataHeader.designer);
134         appendEntry(metadataBuilder, NSD_INVARIANT_ID, metadataHeader.invariantId);
135         appendEntry(metadataBuilder, NSD_NAME, metadataHeader.nsdName);
136         appendEntry(metadataBuilder, NSD_RELEASE_DATE_TIME, metadataHeader.nsdReleaseDateTime);
137         appendEntry(metadataBuilder, NSD_FILE_STRUCTURE_VERSION, metadataHeader.fileStructureVersion);
138         final StringBuilder sourceBuilder = new StringBuilder();
139         sources.forEach(source -> appendEntry(sourceBuilder, SOURCE, source));
140
141         final StringBuilder compatibleSpecificationVersionsBuilder = new StringBuilder();
142         if (!compatibleSpecificationVersions.isEmpty()) {
143             compatibleSpecificationVersionsBuilder.append(COMPATIBLE_SPECIFICATION_VERSIONS)
144                 .append(ATTRIBUTE_SEPARATOR)
145                 .append(String.join(",", compatibleSpecificationVersions))
146                 .append(NEW_LINE);
147         }
148
149         final StringBuilder builder = new StringBuilder();
150
151         builder.append(metadataBuilder)
152             .append(compatibleSpecificationVersionsBuilder)
153             .append(NEW_LINE)
154             .append(sourceBuilder);
155         return builder.toString();
156     }
157
158     private StringBuilder createMetadataBuilder() {
159         final StringBuilder metadataBuilder = new StringBuilder();
160         metadataBuilder.append(METADATA).append(ATTRIBUTE_SEPARATOR).append(NEW_LINE);
161         return metadataBuilder;
162     }
163
164     private String getNowDateTime() {
165         return ZonedDateTime.now().format(RFC_3339_DATE_TIME_FORMATTER);
166     }
167
168     private void appendEntry(final StringBuilder builder, final String entry, final String value) {
169         if (value != null) {
170             builder.append(entry).append(ATTRIBUTE_SEPARATOR).append(value)
171                 .append(NEW_LINE);
172         }
173     }
174
175     private class MetadataHeader {
176         private String fileStructureVersion;
177         private String nsdName;
178         private String nsdReleaseDateTime;
179         private String designer;
180         private String invariantId;
181     }
182
183 }