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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.builder;
22 import java.time.ZonedDateTime;
23 import java.time.format.DateTimeFormatter;
24 import java.util.Collection;
25 import java.util.LinkedHashSet;
27 import java.util.TreeSet;
30 * Builder for the manifest (.mf) file in a NSD CSAR
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");
46 private final MetadataHeader metadataHeader;
47 private final Set<String> sources;
48 private final Set<String> compatibleSpecificationVersions;
50 public NsdCsarManifestBuilder() {
51 metadataHeader = new MetadataHeader();
52 sources = new LinkedHashSet<>();
53 compatibleSpecificationVersions = new TreeSet<>();
57 * Sets a value for the {@link #NSD_DESIGNER} manifest entry.
59 * @param designer the value
60 * @return the builder instance
62 public NsdCsarManifestBuilder withDesigner(final String designer) {
63 metadataHeader.designer = designer;
68 * Sets a value for the {@link #NSD_INVARIANT_ID} manifest entry.
70 * @param invariantId the value
71 * @return the builder instance
73 public NsdCsarManifestBuilder withInvariantId(final String invariantId) {
74 metadataHeader.invariantId = invariantId;
79 * Sets a value for the {@link #NSD_NAME} manifest entry.
81 * @param nsdName the value
82 * @return the builder instance
84 public NsdCsarManifestBuilder withName(final String nsdName) {
85 metadataHeader.nsdName = nsdName;
90 * Sets the current date time to the {@link #NSD_RELEASE_DATE_TIME} manifest entry.
92 * @return the builder instance
94 public NsdCsarManifestBuilder withNowReleaseDateTime() {
95 metadataHeader.nsdReleaseDateTime = getNowDateTime();
100 * Sets a value for the {@link #NSD_FILE_STRUCTURE_VERSION} manifest entry.
102 * @param fileStructureVersion the value
103 * @return the builder instance
105 public NsdCsarManifestBuilder withFileStructureVersion(final String fileStructureVersion) {
106 metadataHeader.fileStructureVersion = fileStructureVersion;
111 * Add a list of {@link #SOURCE} entries to the manifest
113 * @param sources a list of source path
114 * @return the builder instance
116 public NsdCsarManifestBuilder withSources(final Collection<String> sources) {
117 this.sources.addAll(sources);
121 public NsdCsarManifestBuilder withCompatibleSpecificationVersion(final String version) {
122 this.compatibleSpecificationVersions.add(version);
127 * Builds a string representing the manifest content based on provided values.
129 * @return a string representing the manifest content
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));
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))
149 final StringBuilder builder = new StringBuilder();
151 builder.append(metadataBuilder)
152 .append(compatibleSpecificationVersionsBuilder)
154 .append(sourceBuilder);
155 return builder.toString();
158 private StringBuilder createMetadataBuilder() {
159 final StringBuilder metadataBuilder = new StringBuilder();
160 metadataBuilder.append(METADATA).append(ATTRIBUTE_SEPARATOR).append(NEW_LINE);
161 return metadataBuilder;
164 private String getNowDateTime() {
165 return ZonedDateTime.now().format(RFC_3339_DATE_TIME_FORMATTER);
168 private void appendEntry(final StringBuilder builder, final String entry, final String value) {
170 builder.append(entry).append(ATTRIBUTE_SEPARATOR).append(value)
175 private class MetadataHeader {
176 private String fileStructureVersion;
177 private String nsdName;
178 private String nsdReleaseDateTime;
179 private String designer;
180 private String invariantId;