summaryrefslogtreecommitdiffstats
path: root/mod/bpgenerator/src/main/java/org/onap/blueprintgenerator/models/blueprint/ExternalTlsInfo.java
blob: cf97dec9fb06563944fe7c111a8a0a82237a3141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**============LICENSE_START=======================================================
 org.onap.dcae
 ================================================================================
 Copyright (c) 2020 Nokia. All rights reserved.
 ================================================================================
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 ============LICENSE_END=========================================================
 */

package org.onap.blueprintgenerator.models.blueprint;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

@Getter
@Setter
@NoArgsConstructor
public class ExternalTlsInfo {

	static final String USE_EXTERNAL_TLS_FIELD = "use_external_tls";

	private static final String DEFAULT_CA = "RA";
	private static final Object DEFAULT_CERT_TYPE = "P12";

	private static final String INPUT_PREFIX = "external_cert_";
	private static final String EXTERNAL_CERT_DIRECTORY_FIELD = "external_cert_directory";
	private static final String CA_NAME_FIELD = "ca_name";
	private static final String CERT_TYPE_FIELD = "cert_type";
	private static final String EXTERNAL_CERTIFICATE_PARAMETERS_FIELD = "external_certificate_parameters";

	@JsonProperty(EXTERNAL_CERT_DIRECTORY_FIELD)
	private String externalCertDirectory;

	@JsonProperty(USE_EXTERNAL_TLS_FIELD)
	private GetInput useExternalTls;

	@JsonProperty(CA_NAME_FIELD)
	private GetInput caName;

	@JsonProperty(CERT_TYPE_FIELD)
	private GetInput certType;

	@JsonProperty(EXTERNAL_CERTIFICATE_PARAMETERS_FIELD)
	private ExternalCertificateParameters externalCertificateParameters;

	static ExternalTlsInfo createFromComponentSpec(ComponentSpec cs) {
		ExternalTlsInfo externalTlsInfoBp = new ExternalTlsInfo();
		TreeMap<String, Object> tlsInfoCs = cs.getAuxilary().getTls_info();

		externalTlsInfoBp.setExternalCertDirectory((String) tlsInfoCs.get("cert_directory"));
		externalTlsInfoBp.setUseExternalTls(createGetInput(USE_EXTERNAL_TLS_FIELD));
		externalTlsInfoBp.setCaName(createGetInput(CA_NAME_FIELD));
		externalTlsInfoBp.setCertType(createGetInput(CERT_TYPE_FIELD));

		ExternalCertificateParameters externalCertificateParameters =
				ExternalCertificateParameters.create();
		externalTlsInfoBp.setExternalCertificateParameters(externalCertificateParameters);

		return externalTlsInfoBp;
	}

	static Map<String, LinkedHashMap<String, Object>> createInputMapFromComponentSpec(ComponentSpec cs){
		Map<String, LinkedHashMap<String, Object>> retInputs = new HashMap<>();

		Map<String, Object> tlsInfoCs = cs.getAuxilary().getTls_info();
		LinkedHashMap<String, Object> useTlsFlagInput = Properties.makeInput("boolean",
				"Flag to indicate external tls enable/disable.",
				tlsInfoCs.get(USE_EXTERNAL_TLS_FIELD));
		retInputs.put(addPrefix(USE_EXTERNAL_TLS_FIELD), useTlsFlagInput);

		LinkedHashMap<String, Object> caNameInputMap = Properties.makeInput("string",
				"Name of Certificate Authority configured on CertService side.",
				DEFAULT_CA);
		retInputs.put(addPrefix(CA_NAME_FIELD), caNameInputMap);

		LinkedHashMap<String, Object> certTypeInputMap = Properties.makeInput("string",
				"Format of provided certificates",
				DEFAULT_CERT_TYPE);
		retInputs.put(addPrefix(CERT_TYPE_FIELD), certTypeInputMap);

		retInputs.putAll(ExternalCertificateParameters.createInputMap());
		return retInputs;
	}

	private static GetInput createGetInput(String fieldName) {
		return new GetInput(addPrefix(fieldName));
	}

	private static String addPrefix(String fieldName) {
		return INPUT_PREFIX + fieldName;
	}

	@Getter
	@Setter
	@NoArgsConstructor
	public static class ExternalCertificateParameters {

		private static final String DEFAULT_COMMON_NAME = "sample.onap.org";
		private static final String DEFAULT_SANS = "sample.onap.org:component.sample.onap.org";

		private static final String COMMON_NAME_FIELD = "common_name";
		private static final String SANS_FIELD = "sans";

		@JsonProperty(COMMON_NAME_FIELD)
		private GetInput commonName;

		@JsonProperty(SANS_FIELD)
		private GetInput sans;


		private static ExternalCertificateParameters create() {
			ExternalCertificateParameters externalCertificateParameters = new ExternalCertificateParameters();
			externalCertificateParameters.setCommonName(createGetInput(COMMON_NAME_FIELD));
			externalCertificateParameters.setSans(createGetInput(SANS_FIELD));
			return externalCertificateParameters;
		}

		private static Map<String, LinkedHashMap<String, Object>> createInputMap(){
			Map<String, LinkedHashMap<String, Object>> retInputs = new LinkedHashMap<>();

			LinkedHashMap<String, Object> commonNameInputMap = Properties.makeInput("string",
					"Common name which should be present in certificate.",
					DEFAULT_COMMON_NAME);
			retInputs.put(addPrefix(COMMON_NAME_FIELD), commonNameInputMap);

			LinkedHashMap<String, Object> sansInputMap = Properties.makeInput("string",
					"\"List of Subject Alternative Names (SANs) which should be present in certificate. " +
							"Delimiter - : Should contain common_name value and other FQDNs under which given " +
							"component is accessible.\"",
					DEFAULT_SANS);
			retInputs.put(addPrefix(SANS_FIELD), sansInputMap);
			return retInputs;
		}
	}
}