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