3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2020 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============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 {
34 static final String METADATA = "metadata";
35 static final String SOURCE = "Source";
36 static final String COMPATIBLE_SPECIFICATION_VERSIONS = "compatible_specification_versions";
37 static final String NSD_RELEASE_DATE_TIME = "nsd_release_date_time";
38 static final String ATTRIBUTE_SEPARATOR = ": ";
39 private static final String NSD_DESIGNER = "nsd_designer";
40 private static final String NSD_FILE_STRUCTURE_VERSION = "nsd_file_structure_version";
41 private static final String NSD_NAME = "nsd_name";
42 private static final String NSD_INVARIANT_ID = "nsd_invariant_id";
43 private static final String NEW_LINE = "\n";
44 private static final DateTimeFormatter RFC_3339_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
45 private final MetadataHeader metadataHeader;
46 private final Set<String> sources;
47 private final Set<String> compatibleSpecificationVersions;
49 public NsdCsarManifestBuilder() {
50 metadataHeader = new MetadataHeader();
51 sources = new LinkedHashSet<>();
52 compatibleSpecificationVersions = new TreeSet<>();
56 * Sets a value for the {@link #NSD_DESIGNER} manifest entry.
58 * @param designer the value
59 * @return the builder instance
61 public NsdCsarManifestBuilder withDesigner(final String designer) {
62 metadataHeader.designer = designer;
67 * Sets a value for the {@link #NSD_INVARIANT_ID} manifest entry.
69 * @param invariantId the value
70 * @return the builder instance
72 public NsdCsarManifestBuilder withInvariantId(final String invariantId) {
73 metadataHeader.invariantId = invariantId;
78 * Sets a value for the {@link #NSD_NAME} manifest entry.
80 * @param nsdName the value
81 * @return the builder instance
83 public NsdCsarManifestBuilder withName(final String nsdName) {
84 metadataHeader.nsdName = nsdName;
89 * Sets the current date time to the {@link #NSD_RELEASE_DATE_TIME} manifest entry.
91 * @return the builder instance
93 public NsdCsarManifestBuilder withNowReleaseDateTime() {
94 metadataHeader.nsdReleaseDateTime = getNowDateTime();
99 * Sets a value for the {@link #NSD_FILE_STRUCTURE_VERSION} manifest entry.
101 * @param fileStructureVersion the value
102 * @return the builder instance
104 public NsdCsarManifestBuilder withFileStructureVersion(final String fileStructureVersion) {
105 metadataHeader.fileStructureVersion = fileStructureVersion;
110 * Add a list of {@link #SOURCE} entries to the manifest
112 * @param sources a list of source path
113 * @return the builder instance
115 public NsdCsarManifestBuilder withSources(final Collection<String> sources) {
116 this.sources.addAll(sources);
120 public NsdCsarManifestBuilder withCompatibleSpecificationVersion(final String version) {
121 this.compatibleSpecificationVersions.add(version);
126 * Builds a string representing the manifest content based on provided values.
128 * @return a string representing the manifest content
130 public String build() {
131 final StringBuilder metadataBuilder = createMetadataBuilder();
132 appendEntry(metadataBuilder, NSD_DESIGNER, metadataHeader.designer);
133 appendEntry(metadataBuilder, NSD_INVARIANT_ID, metadataHeader.invariantId);
134 appendEntry(metadataBuilder, NSD_NAME, metadataHeader.nsdName);
135 appendEntry(metadataBuilder, NSD_RELEASE_DATE_TIME, metadataHeader.nsdReleaseDateTime);
136 appendEntry(metadataBuilder, NSD_FILE_STRUCTURE_VERSION, metadataHeader.fileStructureVersion);
137 final StringBuilder sourceBuilder = new StringBuilder();
138 sources.forEach(source -> appendEntry(sourceBuilder, SOURCE, source));
139 final StringBuilder compatibleSpecificationVersionsBuilder = new StringBuilder();
140 if (!compatibleSpecificationVersions.isEmpty()) {
141 compatibleSpecificationVersionsBuilder.append(COMPATIBLE_SPECIFICATION_VERSIONS).append(ATTRIBUTE_SEPARATOR)
142 .append(String.join(",", compatibleSpecificationVersions)).append(NEW_LINE);
144 final StringBuilder builder = new StringBuilder();
145 builder.append(metadataBuilder).append(compatibleSpecificationVersionsBuilder).append(NEW_LINE).append(sourceBuilder);
146 return builder.toString();
149 private StringBuilder createMetadataBuilder() {
150 final StringBuilder metadataBuilder = new StringBuilder();
151 metadataBuilder.append(METADATA).append(ATTRIBUTE_SEPARATOR).append(NEW_LINE);
152 return metadataBuilder;
155 private String getNowDateTime() {
156 return ZonedDateTime.now().format(RFC_3339_DATE_TIME_FORMATTER);
159 private void appendEntry(final StringBuilder builder, final String entry, final String value) {
161 builder.append(entry).append(ATTRIBUTE_SEPARATOR).append(value).append(NEW_LINE);
165 private class MetadataHeader {
167 private String fileStructureVersion;
168 private String nsdName;
169 private String nsdReleaseDateTime;
170 private String designer;
171 private String invariantId;