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.HashSet;
26 import java.util.LinkedHashSet;
29 * Builder for the manifest (.mf) file in a NSD CSAR
31 public class NsdCsarManifestBuilder {
32 private static final String METADATA = "metadata";
33 private static final String SOURCE = "Source";
34 private static final String NSD_DESIGNER = "nsd_designer";
35 private static final String NSD_FILE_STRUCTURE_VERSION = "nsd_file_structure_version";
36 private static final String NSD_RELEASE_DATE_TIME = "nsd_release_date_time";
37 private static final String NSD_NAME = "nsd_name";
38 private static final String NSD_INVARIANT_ID = "nsd_invariant_id";
39 private static final String ATTRIBUTE_SEPARATOR = ": ";
40 private static final String NEW_LINE = "\n";
41 private static final DateTimeFormatter RFC_3339_DATE_TIME_FORMATTER =
42 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
44 private final StringBuilder builder;
45 private final StringBuilder metadataBuilder;
46 private final StringBuilder sourceBuilder;
47 private final MetadataHeader metadataHeader;
48 private final HashSet<String> sources;
50 public NsdCsarManifestBuilder() {
51 builder = new StringBuilder();
52 metadataBuilder = new StringBuilder();
53 sourceBuilder = new StringBuilder();
54 metadataHeader = new MetadataHeader();
55 sources = new LinkedHashSet<>();
56 metadataBuilder.append(METADATA).append(ATTRIBUTE_SEPARATOR)
61 * Sets a value for the {@link #NSD_DESIGNER} manifest entry.
63 * @param designer the value
64 * @return the builder instance
66 public NsdCsarManifestBuilder withDesigner(final String designer) {
67 metadataHeader.designer = designer;
72 * Sets a value for the {@link #NSD_INVARIANT_ID} manifest entry.
74 * @param invariantId the value
75 * @return the builder instance
77 public NsdCsarManifestBuilder withInvariantId(final String invariantId) {
78 metadataHeader.invariantId = invariantId;
83 * Sets a value for the {@link #NSD_NAME} manifest entry.
85 * @param nsdName the value
86 * @return the builder instance
88 public NsdCsarManifestBuilder withName(final String nsdName) {
89 metadataHeader.nsdName = nsdName;
94 * Sets the current date time to the {@link #NSD_RELEASE_DATE_TIME} manifest entry.
96 * @return the builder instance
98 public NsdCsarManifestBuilder withNowReleaseDateTime() {
99 metadataHeader.nsdReleaseDateTime = getNowDateTime();
104 * Sets a value for the {@link #NSD_FILE_STRUCTURE_VERSION} manifest entry.
106 * @param fileStructureVersion the value
107 * @return the builder instance
109 public NsdCsarManifestBuilder withFileStructureVersion(final String fileStructureVersion) {
110 metadataHeader.fileStructureVersion = fileStructureVersion;
115 * Add a list of {@link #SOURCE} entries to the manifest
117 * @param sources a list of source path
118 * @return the builder instance
120 public NsdCsarManifestBuilder withSources(final Collection<String> sources) {
121 this.sources.addAll(sources);
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 appendEntry(metadataBuilder, NSD_DESIGNER, metadataHeader.designer);
132 appendEntry(metadataBuilder, NSD_INVARIANT_ID, metadataHeader.invariantId);
133 appendEntry(metadataBuilder, NSD_NAME, metadataHeader.nsdName);
134 appendEntry(metadataBuilder, NSD_RELEASE_DATE_TIME, metadataHeader.nsdReleaseDateTime);
135 appendEntry(metadataBuilder, NSD_FILE_STRUCTURE_VERSION, metadataHeader.fileStructureVersion);
136 sources.forEach(source -> appendEntry(sourceBuilder, SOURCE, source));
138 builder.append(metadataBuilder)
140 .append(sourceBuilder);
141 return builder.toString();
144 private String getNowDateTime() {
145 return ZonedDateTime.now().format(RFC_3339_DATE_TIME_FORMATTER);
148 private void appendEntry(final StringBuilder builder, final String entry, final String value) {
150 builder.append(entry).append(ATTRIBUTE_SEPARATOR).append(value)
155 private class MetadataHeader {
156 private String fileStructureVersion;
157 private String nsdName;
158 private String nsdReleaseDateTime;
159 private String designer;
160 private String invariantId;