Reformat catalog-be-plugins
[sdc.git] / catalog-be-plugins / etsi-nfv-nsd-csar-plugin / src / main / java / org / openecomp / sdc / be / plugins / etsi / nfv / nsd / builder / NsdCsarManifestBuilder.java
1  
2 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
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
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;
48
49     public NsdCsarManifestBuilder() {
50         metadataHeader = new MetadataHeader();
51         sources = new LinkedHashSet<>();
52         compatibleSpecificationVersions = new TreeSet<>();
53     }
54
55     /**
56      * Sets a value for the {@link #NSD_DESIGNER} manifest entry.
57      *
58      * @param designer the value
59      * @return the builder instance
60      */
61     public NsdCsarManifestBuilder withDesigner(final String designer) {
62         metadataHeader.designer = designer;
63         return this;
64     }
65
66     /**
67      * Sets a value for the {@link #NSD_INVARIANT_ID} manifest entry.
68      *
69      * @param invariantId the value
70      * @return the builder instance
71      */
72     public NsdCsarManifestBuilder withInvariantId(final String invariantId) {
73         metadataHeader.invariantId = invariantId;
74         return this;
75     }
76
77     /**
78      * Sets a value for the {@link #NSD_NAME} manifest entry.
79      *
80      * @param nsdName the value
81      * @return the builder instance
82      */
83     public NsdCsarManifestBuilder withName(final String nsdName) {
84         metadataHeader.nsdName = nsdName;
85         return this;
86     }
87
88     /**
89      * Sets the current date time to the {@link #NSD_RELEASE_DATE_TIME} manifest entry.
90      *
91      * @return the builder instance
92      */
93     public NsdCsarManifestBuilder withNowReleaseDateTime() {
94         metadataHeader.nsdReleaseDateTime = getNowDateTime();
95         return this;
96     }
97
98     /**
99      * Sets a value for the {@link #NSD_FILE_STRUCTURE_VERSION} manifest entry.
100      *
101      * @param fileStructureVersion the value
102      * @return the builder instance
103      */
104     public NsdCsarManifestBuilder withFileStructureVersion(final String fileStructureVersion) {
105         metadataHeader.fileStructureVersion = fileStructureVersion;
106         return this;
107     }
108
109     /**
110      * Add a list of {@link #SOURCE} entries to the manifest
111      *
112      * @param sources a list of source path
113      * @return the builder instance
114      */
115     public NsdCsarManifestBuilder withSources(final Collection<String> sources) {
116         this.sources.addAll(sources);
117         return this;
118     }
119
120     public NsdCsarManifestBuilder withCompatibleSpecificationVersion(final String version) {
121         this.compatibleSpecificationVersions.add(version);
122         return this;
123     }
124
125     /**
126      * Builds a string representing the manifest content based on provided values.
127      *
128      * @return a string representing the manifest content
129      */
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);
143         }
144         final StringBuilder builder = new StringBuilder();
145         builder.append(metadataBuilder).append(compatibleSpecificationVersionsBuilder).append(NEW_LINE).append(sourceBuilder);
146         return builder.toString();
147     }
148
149     private StringBuilder createMetadataBuilder() {
150         final StringBuilder metadataBuilder = new StringBuilder();
151         metadataBuilder.append(METADATA).append(ATTRIBUTE_SEPARATOR).append(NEW_LINE);
152         return metadataBuilder;
153     }
154
155     private String getNowDateTime() {
156         return ZonedDateTime.now().format(RFC_3339_DATE_TIME_FORMATTER);
157     }
158
159     private void appendEntry(final StringBuilder builder, final String entry, final String value) {
160         if (value != null) {
161             builder.append(entry).append(ATTRIBUTE_SEPARATOR).append(value).append(NEW_LINE);
162         }
163     }
164
165     private class MetadataHeader {
166
167         private String fileStructureVersion;
168         private String nsdName;
169         private String nsdReleaseDateTime;
170         private String designer;
171         private String invariantId;
172     }
173 }