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 / NsdToscaMetadataBuilder.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 /**
23  * Builder for the TOSCA.meta file in a NSD CSAR
24  */
25 public class NsdToscaMetadataBuilder {
26
27     public static final String CSAR_VERSION = "CSAR-Version";
28     public static final String CREATED_BY = "Created-By";
29     public static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
30     public static final String ENTRY_DEFINITIONS = "Entry-Definitions";
31     public static final String ETSI_ENTRY_CHANGE_LOG = "ETSI-Entry-Change-Log";
32     public static final String ETSI_ENTRY_MANIFEST = "ETSI-Entry-Manifest";
33     private static final String ATTRIBUTE_SEPARATOR = ": ";
34     private static final String NEW_LINE = "\n";
35     private final StringBuilder builder = new StringBuilder();
36     private String csarVersion;
37     private String createdBy;
38     private String entryDefinitionsPath;
39     private String toscaMetaVersion;
40     private String entryManifest;
41     private String changeLogPath;
42
43     /**
44      * Sets a value for the {@link #CSAR_VERSION} metadata entry.
45      *
46      * @param csarVersion the value
47      * @return the builder instance
48      */
49     public NsdToscaMetadataBuilder withCsarVersion(final String csarVersion) {
50         this.csarVersion = csarVersion;
51         return this;
52     }
53
54     /**
55      * Sets a value for the {@link #CREATED_BY} metadata entry.
56      *
57      * @param createdBy the value
58      * @return the builder instance
59      */
60     public NsdToscaMetadataBuilder withCreatedBy(final String createdBy) {
61         this.createdBy = createdBy;
62         return this;
63     }
64
65     /**
66      * Sets a value for the {@link #TOSCA_META_FILE_VERSION} metadata entry.
67      *
68      * @param toscaMetaVersion the value
69      * @return the builder instance
70      */
71     public NsdToscaMetadataBuilder withToscaMetaVersion(final String toscaMetaVersion) {
72         this.toscaMetaVersion = toscaMetaVersion;
73         return this;
74     }
75
76     /**
77      * Sets a value for the {@link #ENTRY_DEFINITIONS} metadata entry.
78      *
79      * @param entryDefinitionsPath the value
80      * @return the builder instance
81      */
82     public NsdToscaMetadataBuilder withEntryDefinitions(final String entryDefinitionsPath) {
83         this.entryDefinitionsPath = entryDefinitionsPath;
84         return this;
85     }
86
87     /**
88      * Sets a value for the {@link #ETSI_ENTRY_MANIFEST} metadata entry.
89      *
90      * @param entryManifest the value
91      * @return the builder instance
92      */
93     public NsdToscaMetadataBuilder withEntryManifest(final String entryManifest) {
94         this.entryManifest = entryManifest;
95         return this;
96     }
97
98     /**
99      * Sets a value for the {@link #ETSI_ENTRY_CHANGE_LOG} metadata entry.
100      *
101      * @param changeLogPath the value
102      * @return the builder instance
103      */
104     public NsdToscaMetadataBuilder withEntryChangeLog(final String changeLogPath) {
105         this.changeLogPath = changeLogPath;
106         return this;
107     }
108
109     /**
110      * Builds a string representing the TOSCA metadata content based on provided values.
111      *
112      * @return a string representing the TOSCA metadata content
113      */
114     public String build() {
115         appendEntry(CSAR_VERSION, csarVersion);
116         appendEntry(CREATED_BY, createdBy);
117         appendEntry(TOSCA_META_FILE_VERSION, toscaMetaVersion);
118         appendEntry(ENTRY_DEFINITIONS, entryDefinitionsPath);
119         appendEntry(ETSI_ENTRY_MANIFEST, entryManifest);
120         appendEntry(ETSI_ENTRY_CHANGE_LOG, changeLogPath);
121         return builder.toString();
122     }
123
124     private void appendEntry(final String entry, final String value) {
125         if (value != null) {
126             builder.append(entry).append(ATTRIBUTE_SEPARATOR).append(value).append(NEW_LINE);
127         }
128     }
129 }