2 * # ============LICENSE_START=======================================================
3 * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
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
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 * # ============LICENSE_END=========================================================
19 package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder;
21 import org.jetbrains.annotations.NotNull;
22 import org.onap.dcaegen2.platform.helmchartgenerator.Utils;
23 import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo;
24 import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.Metadata;
25 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.base.ComponentSpec;
26 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.Artifacts;
27 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.HealthCheck;
28 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.Parameters;
29 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.Policy;
30 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.PolicyInfo;
31 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.Self;
32 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.Service;
33 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.TlsInfo;
34 import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.Volumes;
35 import org.onap.dcaegen2.platform.helmchartgenerator.validation.ComponentSpecValidator;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.LinkedHashMap;
45 import java.util.List;
49 * ComponentSpecParser reads a componentspec file and collects useful data for helm chart generation.
50 * @author Dhrumin Desai
53 public class ComponentSpecParser {
56 private ComponentSpecValidator specValidator;
59 * Constructor for ComponentSpecParser
60 * @param specValidator ComponentSpecValidator implementation
62 public ComponentSpecParser(ComponentSpecValidator specValidator) {
63 this.specValidator = specValidator;
68 * @param specFileLocation location of the application specification json file
69 * @param chartTemplateLocation location of the base helm chart template
70 * @param specSchemaLocation location of the specification json schema file to validate the application spec
71 * @return ChartInfo object populated with key-values
74 public ChartInfo extractChartInfo(String specFileLocation, String chartTemplateLocation, String specSchemaLocation) throws Exception {
75 specValidator.validateSpecFile(specFileLocation, specSchemaLocation);
76 ComponentSpec cs = Utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class);
77 ChartInfo chartInfo = new ChartInfo();
78 chartInfo.setMetadata(extractMetadata(cs.getSelf()));
79 chartInfo.setValues(extractValues(cs, chartTemplateLocation));
83 private Map<String, Object> extractValues(ComponentSpec cs, String chartTemplateLocation) {
84 Map<String, Object> outerValues = new LinkedHashMap<>();
85 if(cs.getAuxilary() != null && cs.getAuxilary().getTlsInfo() != null){
86 Utils.putIfNotNull(outerValues,"certDirectory", cs.getAuxilary().getTlsInfo().getCertDirectory());
87 Utils.putIfNotNull(outerValues, "tlsServer", cs.getAuxilary().getTlsInfo().getUseTls());
89 if(cs.getAuxilary() != null && cs.getAuxilary().getLogInfo() != null) {
90 Utils.putIfNotNull(outerValues,"logDirectory", cs.getAuxilary().getLogInfo().get("log_directory"));
92 if(imageUriExistsForFirstArtifact(cs)){
93 Utils.putIfNotNull(outerValues,"image", cs.getArtifacts()[0].getUri());
95 populateApplicationConfigSection(outerValues, cs);
96 populateReadinessSection(outerValues, cs);
97 populateApplicationEnvSection(outerValues, cs);
98 populateServiceSection(outerValues, cs);
99 populateCertificatesSection(outerValues, cs, chartTemplateLocation);
100 populatePoliciesSection(outerValues, cs);
101 populateExternalVolumesSection(outerValues, cs);
102 populatePostgresSection(outerValues, cs);
103 populateSecretsSection(outerValues, cs);
107 private boolean imageUriExistsForFirstArtifact(ComponentSpec cs) {
108 final Artifacts[] artifacts = cs.getArtifacts();
109 return artifacts != null && artifacts.length > 0 && artifacts[0].getUri() != null;
112 private void populateApplicationConfigSection(Map<String, Object> outerValues, ComponentSpec cs) {
113 Map<String, Object> applicationConfig = new LinkedHashMap<>();
114 Parameters[] parameters = cs.getParameters();
115 for(Parameters param : parameters){
116 applicationConfig.put(param.getName(), param.getValue());
118 Utils.putIfNotNull(outerValues,"applicationConfig", applicationConfig);
121 private void populateReadinessSection(Map<String, Object> outerValues, ComponentSpec cs) {
123 if (!healthCheckExists(cs)) return;
125 Map<String, Object> readiness = new LinkedHashMap<>();
126 final HealthCheck healthcheck = cs.getAuxilary().getHealthcheck();
127 Utils.putIfNotNull(readiness, "initialDelaySeconds", healthcheck.getInitialDelaySeconds());
129 if(healthcheck.getInterval() != null) {
130 readiness.put("periodSeconds", getSeconds(healthcheck.getInterval(), "interval"));
132 if(healthcheck.getTimeout() != null) {
133 readiness.put("timeoutSeconds", getSeconds(healthcheck.getTimeout(), "timeout"));
136 readiness.put("path", healthcheck.getEndpoint());
137 readiness.put("scheme", healthcheck.getType());
138 readiness.put("port", healthcheck.getPort());
140 outerValues.put("readiness", readiness);
143 private int getSeconds(String value, String field) {
146 seconds = Integer.parseInt(value.replaceAll("[^\\d.]", ""));
148 catch (NumberFormatException e){
149 throw new RuntimeException(String.format("%s with %s is not given in a correct format", field, value));
154 private boolean healthCheckExists(ComponentSpec cs) {
155 return cs.getAuxilary() != null &&
156 cs.getAuxilary().getHealthcheck() != null;
159 private void populateApplicationEnvSection(Map<String, Object> outerValues, ComponentSpec cs){
160 if(applicationEnvExists(cs)) {
161 Object applicationEnv = cs.getAuxilary().getHelm().getApplicationEnv();
162 Utils.putIfNotNull(outerValues,"applicationEnv", applicationEnv);
166 private boolean applicationEnvExists(ComponentSpec cs) {
167 return cs.getAuxilary() != null &&
168 cs.getAuxilary().getHelm() != null &&
169 cs.getAuxilary().getHelm().getApplicationEnv() != null;
172 private void populateServiceSection(Map<String, Object> outerValues, ComponentSpec cs) {
173 if (!serviceExists(cs)) return;
175 Map<String, Object> service = new LinkedHashMap<>();
176 final Service serviceFromSpec = cs.getAuxilary().getHelm().getService();
178 if(serviceFromSpec.getPorts() != null){
179 List<Object> ports = mapServicePorts(serviceFromSpec.getPorts());
180 service.put("ports", ports);
181 Utils.putIfNotNull(service, "type", serviceFromSpec.getType());
183 Utils.putIfNotNull(service,"name", serviceFromSpec.getName());
184 Utils.putIfNotNull(service,"has_internal_only_ports", serviceFromSpec.getHasInternalOnlyPorts());
185 outerValues.put("service", service);
188 private boolean serviceExists(ComponentSpec cs) {
189 return cs.getAuxilary() != null &&
190 cs.getAuxilary().getHelm() != null &&
191 cs.getAuxilary().getHelm().getService() != null;
194 private List<Object> mapServicePorts(Object[] ports) {
195 List<Object> portsList = new ArrayList<>();
196 Collections.addAll(portsList, ports);
200 private void populatePoliciesSection(Map<String, Object> outerValues, ComponentSpec cs) {
201 Map<String, Object> policies = new LinkedHashMap<>();
202 final PolicyInfo policyInfo = cs.getPolicyInfo();
203 if(policyInfo != null && policyInfo.getPolicy() != null) {
204 List<String> policyList = new ArrayList<>();
205 for (Policy policyItem : policyInfo.getPolicy()) {
206 policyList.add('"' + policyItem.getPolicyID() + '"');
208 policies.put("policyRelease", "onap");
209 policies.put("duration", 300);
210 policies.put("policyID", "'" + policyList.toString() + "'\n");
211 outerValues.put("policies", policies);
215 private void populateCertificatesSection(Map<String, Object> outerValues, ComponentSpec cs, String chartTemplateLocation) {
216 Map<String, Object> certificate = new LinkedHashMap<>();
217 Map<String, Object> keystore = new LinkedHashMap<>();
218 Map<String, Object> passwordsSecretRef = new LinkedHashMap<>();
219 TlsInfo tlsInfo = cs.getAuxilary().getTlsInfo();
220 String componentName = getComponentNameWithOmitFirstWord(cs);
221 if(externalTlsExists(tlsInfo)) {
222 String mountPath = tlsInfo.getCertDirectory();
223 if(tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls()) {
224 checkCertificateYamlExists(chartTemplateLocation);
225 mountPath += "external";
227 passwordsSecretRef.put("name", componentName + "-cmpv2-keystore-password");
228 passwordsSecretRef.put("key", "password");
229 passwordsSecretRef.put("create", true);
230 keystore.put("outputType", List.of("jks"));
231 keystore.put("passwordSecretRef", passwordsSecretRef);
232 certificate.put("mountPath", mountPath);
233 Utils.putIfNotNull(certificate,"commonName", cs.getSelf().getName());
234 Utils.putIfNotNull(certificate,"dnsNames", List.of(cs.getSelf().getName()));
235 certificate.put("keystore", keystore);
236 outerValues.put("certificates", List.of(certificate));
240 private String getComponentNameWithOmitFirstWord(ComponentSpec cs) {
241 return cs.getSelf().getName().substring(cs.getSelf().getName().indexOf("-") + 1);
244 private boolean externalTlsExists(TlsInfo tlsInfo) {
245 return tlsInfo != null && tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls().equals(true);
248 private void checkCertificateYamlExists(String chartTemplateLocation) {
249 Path certificateYaml = Paths.get(chartTemplateLocation, "addons/templates/certificates.yaml");
250 if(!Files.exists(certificateYaml)) {
251 throw new RuntimeException("certificates.yaml not found under templates directory in addons");
255 private void populateExternalVolumesSection(Map<String, Object> outerValues, ComponentSpec cs) {
256 if(cs.getAuxilary().getVolumes() != null) {
257 List<Object> externalVolumes = new ArrayList<>();
258 Volumes[] volumes = cs.getAuxilary().getVolumes();
259 for (Volumes volume : volumes) {
260 if(volume.getHost() == null) {
261 Map<String, Object> tempVolume = new LinkedHashMap<>();
262 tempVolume.put("name", volume.getConfigVolume().getName());
263 tempVolume.put("type", "configMap");
264 tempVolume.put("mountPath", volume.getContainer().getBind());
265 tempVolume.put("optional", true);
266 externalVolumes.add(tempVolume);
269 if(!externalVolumes.isEmpty()) {
270 outerValues.put("externalVolumes", externalVolumes);
275 private void populatePostgresSection(Map<String, Object> outerValues, ComponentSpec cs) {
276 if(cs.getAuxilary().getDatabases() != null) {
277 String componentFullName = cs.getSelf().getName();
278 String component = getComponentNameWithOmitFirstWord(cs);
279 Map<String, Object> postgres = new LinkedHashMap<>();
280 Map<String, Object> service = new LinkedHashMap<>();
281 Map<String, Object> container = new LinkedHashMap<>();
282 Map<String, Object> name = new LinkedHashMap<>();
283 Map<String, Object> persistence = new LinkedHashMap<>();
284 Map<String, Object> config = new LinkedHashMap<>();
285 service.put("name", componentFullName + "-postgres");
286 service.put("name2", componentFullName + "-pg-primary");
287 service.put("name3", componentFullName + "-pg-replica");
288 name.put("primary", componentFullName + "-pg-primary");
289 name.put("replica", componentFullName + "-pg-replica");
290 container.put("name", name);
291 persistence.put("mountSubPath", componentFullName + "/data");
292 persistence.put("mountInitPath", componentFullName);
293 config.put("pgUserName", component);
294 config.put("pgDatabase", component);
295 config.put("pgUserExternalSecret", "{{ include \"common.release\" . }}-" + component + "-pg-user-creds");
297 postgres.put("enabled", true);
298 postgres.put("nameOverride", componentFullName + "-postgres");
299 postgres.put("service", service);
300 postgres.put("container", container);
301 postgres.put("persistence", persistence);
302 postgres.put("config", config);
303 outerValues.put("postgres", postgres);
307 private void populateSecretsSection(Map<String, Object> outerValues, ComponentSpec cs) {
308 if(cs.getAuxilary().getDatabases() != null) {
309 String component = getComponentNameWithOmitFirstWord(cs);
310 List<Object> secrets = new ArrayList<>();
311 Map<String, Object> secret = new LinkedHashMap<>();
312 secret.put("uid", "pg-user-creds");
313 secret.put("name", "{{ include \"common.release\" . }}-" + component + "-pg-user-creds");
314 secret.put("type", "basicAuth");
315 secret.put("externalSecret", "{{ ternary \"\" (tpl (default \"\" .Values.postgres.config.pgUserExternalSecret) .) (hasSuffix \"" + component + "-pg-user-creds\" .Values.postgres.config.pgUserExternalSecret) }}");
316 secret.put("login", "{{ .Values.postgres.config.pgUserName }}");
317 secret.put("password", "{{ .Values.postgres.config.pgUserPassword }}");
318 secret.put("passwordPolicy", "generate");
320 outerValues.put("secrets", secrets);
324 private Metadata extractMetadata(Self self) {
325 Metadata metadata = new Metadata();
326 metadata.setName(self.getName());
327 metadata.setDescription(self.getDescription());
328 metadata.setVersion(self.getVersion());