All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vmware.pscoe.iac.artifact.model.cs.CsPackageDescriptor Maven / Gradle / Ivy

/*
 * #%L
 * artifact-manager
 * %%
 * Copyright (C) 2023 VMware
 * %%
 * Build Tools for VMware Aria
 * Copyright 2023 VMware, Inc.
 * 
 * This product is licensed to you under the BSD-2 license (the "License"). You may not use this product except in compliance with the BSD-2 License.  
 * 
 * This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file.
 * #L%
 */
package com.vmware.pscoe.iac.artifact.model.cs;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.vmware.pscoe.iac.artifact.model.PackageDescriptor;
import com.vmware.pscoe.iac.artifact.model.PackageContent.Content;

public class CsPackageDescriptor extends PackageDescriptor {
	private static final Logger logger = LoggerFactory.getLogger(CsPackageDescriptor.class);

	private List pipeline;
	private List variable;
	private List customIntegration;
	private List endpoint;
	private List gitWebhook;
	private List dockerWebhook;
	private List gerritTrigger;
	private List gerritListener;

	public List getPipeline() {
		return pipeline;
	}

	public void setPipeline(List pipeline) {
		this.pipeline = pipeline;
	}

	public List getVariable() {
		return variable == null ? variable = new ArrayList<>() : variable;
	}

	public void setVariable(List variable) {
		this.variable = variable;
	}

	public List getCustomIntegration() {
		return customIntegration;
	}

	public void setCustomIntegration(List customIntegration) {
		this.customIntegration = customIntegration;
	}

	public List getEndpoint() {
		return endpoint;
	}

	public void setEndpoint(List endpoint) {
		this.endpoint = endpoint;
	}

	public List getGitWebhook() {
		return gitWebhook;
	}

	public void setGitWebhook(List gitWebhook) {
		this.gitWebhook = gitWebhook;
	}

	public List getDockerWebhook() {
		return dockerWebhook;
	}

	public void setDockerWebhook(List dockerWebhook) {
		this.dockerWebhook = dockerWebhook;
	}

	public List getGerritTrigger() {
		return gerritTrigger;
	}

	public void setGerritTrigger(List gerritTrigger) {
		this.gerritTrigger = gerritTrigger;
	}

	public List getGerritListener() {
		return gerritListener;
	}

	public void setGerritListener(List gerritListener) {
		this.gerritListener = gerritListener;
	}

	public List getMembersForType(CsPackageMemberType type) {
		switch (type) {
			case PIPELINE:
				return getPipeline();
			case VARIABLE:
				return getVariable();
			case CUSTOM_INTEGRATION:
				return getCustomIntegration();
			case ENDPOINT:
				return getEndpoint();
			case GIT_WEBHOOK:
				return getGitWebhook();
			case DOCKER_WEBHOOK:
				return getDockerWebhook();
			case GERRIT_TRIGGER:
				return getGerritTrigger();
			case GERRIT_LISTENER:
				return getGerritListener();
			default:
				throw new RuntimeException("ContentType is not supported!");
		}
	}

	public boolean hasNativeContent() {
		return Arrays.stream(CsPackageMemberType.values()).filter(type -> type.isNativeContent()).anyMatch(type -> {
			List memberNames = this.getMembersForType(type);
			return memberNames != null && !memberNames.isEmpty();
		});
	}

	public static CsPackageDescriptor getInstance(File filesystemPath) {
		ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
		mapper.setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE);
		try {
			CsPackageDescriptor pkgDescriptor = mapper.readValue(filesystemPath, CsPackageDescriptor.class);
			logger.debug(ReflectionToStringBuilder.toString(pkgDescriptor, ToStringStyle.MULTI_LINE_STYLE));
			return pkgDescriptor;
		} catch (Exception e) {
			throw new RuntimeException("Unable to load CS Package Descriptor[" + filesystemPath.getAbsolutePath() + "]",
					e);
		}
	}

	public static CsPackageDescriptor getInstance(CsPackageContent content) {
		HashMap> map = new HashMap<>();
		for (CsPackageContent.ContentType type : CsPackageContent.ContentType.values()) {
			map.put(type, new ArrayList<>());
		}

		for (Content c : content.getContent()) {
			map.get(c.getType()).add(c.getName());
		}

		CsPackageDescriptor pd = new CsPackageDescriptor();
		pd.pipeline = map.get(CsPackageContent.ContentType.PIPELINE);
		logger.debug(ReflectionToStringBuilder.toString(pd, ToStringStyle.MULTI_LINE_STYLE));
		return pd;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy