Plugin to Generate Service ETSI NSD CSAR
[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  * ============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.HashSet;
26 import java.util.LinkedHashSet;
27
28 /**
29  * Builder for the manifest (.mf) file in a NSD CSAR
30  */
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");
43
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;
49
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)
57             .append(NEW_LINE);
58     }
59
60     /**
61      * Sets a value for the {@link #NSD_DESIGNER} manifest entry.
62      *
63      * @param designer the value
64      * @return the builder instance
65      */
66     public NsdCsarManifestBuilder withDesigner(final String designer) {
67         metadataHeader.designer = designer;
68         return this;
69     }
70
71     /**
72      * Sets a value for the {@link #NSD_INVARIANT_ID} manifest entry.
73      *
74      * @param invariantId the value
75      * @return the builder instance
76      */
77     public NsdCsarManifestBuilder withInvariantId(final String invariantId) {
78         metadataHeader.invariantId = invariantId;
79         return this;
80     }
81
82     /**
83      * Sets a value for the {@link #NSD_NAME} manifest entry.
84      *
85      * @param nsdName the value
86      * @return the builder instance
87      */
88     public NsdCsarManifestBuilder withName(final String nsdName) {
89         metadataHeader.nsdName = nsdName;
90         return this;
91     }
92
93     /**
94      * Sets the current date time to the {@link #NSD_RELEASE_DATE_TIME} manifest entry.
95      *
96      * @return the builder instance
97      */
98     public NsdCsarManifestBuilder withNowReleaseDateTime() {
99         metadataHeader.nsdReleaseDateTime = getNowDateTime();
100         return this;
101     }
102
103     /**
104      * Sets a value for the {@link #NSD_FILE_STRUCTURE_VERSION} manifest entry.
105      *
106      * @param fileStructureVersion the value
107      * @return the builder instance
108      */
109     public NsdCsarManifestBuilder withFileStructureVersion(final String fileStructureVersion) {
110         metadataHeader.fileStructureVersion = fileStructureVersion;
111         return this;
112     }
113
114     /**
115      * Add a list of {@link #SOURCE} entries to the manifest
116      *
117      * @param sources a list of source path
118      * @return the builder instance
119      */
120     public NsdCsarManifestBuilder withSources(final Collection<String> sources) {
121         this.sources.addAll(sources);
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         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));
137
138         builder.append(metadataBuilder)
139             .append(NEW_LINE)
140             .append(sourceBuilder);
141         return builder.toString();
142     }
143
144     private String getNowDateTime() {
145         return ZonedDateTime.now().format(RFC_3339_DATE_TIME_FORMATTER);
146     }
147
148     private void appendEntry(final StringBuilder builder, final String entry, final String value) {
149         if (value != null) {
150             builder.append(entry).append(ATTRIBUTE_SEPARATOR).append(value)
151                 .append(NEW_LINE);
152         }
153     }
154
155     private class MetadataHeader {
156         private String fileStructureVersion;
157         private String nsdName;
158         private String nsdReleaseDateTime;
159         private String designer;
160         private String invariantId;
161     }
162
163 }