aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.model.csar.toscametafile/src/main/java/org/eclipse/winery/model/csar/toscametafile/TOSCAMetaFileParser.java
blob: c5b7c9d933b3618014fccc325f5f879fb68ef692 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*******************************************************************************
 * Copyright (c) 2013 Rene Trefft.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *    Rene Trefft - initial API and implementation and/or initial documentation
 *******************************************************************************/
package org.eclipse.winery.model.csar.toscametafile;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.springsource.util.parser.manifest.ManifestContents;
import com.springsource.util.parser.manifest.ManifestParser;
import com.springsource.util.parser.manifest.ManifestProblem;
import com.springsource.util.parser.manifest.RecoveringManifestParser;

/**
 * Parses and validates a TOSCA meta file.<br />
 * <br />
 * Copyright 2013 IAAS University of Stuttgart <br />
 * <br />
 * 
 * @author Rene Trefft - rene.trefft@developers.opentosca.org
 * 
 */
public class TOSCAMetaFileParser {
	
	final private static Logger LOG = LoggerFactory.getLogger(TOSCAMetaFileParser.class);
	
	
	/**
	 * Parses and validates the <code>toscaMetaFile</code>.<br />
	 * <br />
	 * 
	 * @param toscaMetaFile to process
	 * @return <code>TOSCAMetaFile</code> that gives access to the content of
	 *         the TOSCA meta file. If the given file doesn't exist or is
	 *         invalid <code>null</code>.
	 */
	public TOSCAMetaFile parse(Path toscaMetaFile) {
		
		// counts the errors during parsing
		int numErrors = 0;
		
		FileReader reader = null;
		ManifestParser parser = null;
		ManifestContents manifestContent = null;
		TOSCAMetaFile toscaMetaFileContent = null;
		
		try {
			
			parser = new RecoveringManifestParser();
			reader = new FileReader(toscaMetaFile.toFile());
			TOSCAMetaFileParser.LOG.debug("Parsing TOSCA meta file \"{}\"...", toscaMetaFile.getFileName().toString());
			manifestContent = parser.parse(reader);
			reader.close();
			
			for (ManifestProblem problem : parser.getProblems()) {
				this.logManifestProblem(problem);
				numErrors++;
			}
			
			numErrors += this.validateBlock0(manifestContent);
			numErrors += this.validateFileBlocks(manifestContent);
			
			if (numErrors == 0) {
				TOSCAMetaFileParser.LOG.debug("Parsing TOSCA meta file \"{}\" completed without errors. TOSCA meta file is valid.", toscaMetaFile.getFileName().toString());
				toscaMetaFileContent = new TOSCAMetaFile(manifestContent);
			} else {
				TOSCAMetaFileParser.LOG.error("Parsing TOSCA meta file \"{}\" failed - {} error(s) occured. TOSCA meta file is invalid.", toscaMetaFile.getFileName().toString(), numErrors);
			}
			
		} catch (FileNotFoundException exc) {
			TOSCAMetaFileParser.LOG.error("\"{}\" doesn't exist or is not a file.", toscaMetaFile, exc);
		} catch (IOException exc) {
			TOSCAMetaFileParser.LOG.error("An IO Exception occured.", exc);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException exc) {
					TOSCAMetaFileParser.LOG.warn("An IOException occured.", exc);
				}
			}
		}
		
		return toscaMetaFileContent;
		
	}
	
	/**
	 * Validates block 0 of the TOSCA meta file.<br />
	 * <br />
	 * Required attributes in block 0:
	 * <ul>
	 * <li><code>TOSCA-Meta-Version</code> (value must be <code>1.0</code>)</li>
	 * <li><code>CSAR-Version</code> (value must be <code>1.0</code>)</li>
	 * <li><code>Created-By</code></li>
	 * </ul>
	 * Optional attributes in block 0:
	 * <ul>
	 * <li><code>Entry-Definitions</code></li>
	 * <li><code>Description</code></li>
	 * <li><code>Topology</code></li>
	 * </ul>
	 * 
	 * Further, arbitrary attributes are also allowed.<br />
	 * <br />
	 * 
	 * @param mf to validate
	 * @return Number of errors occurred during validation.
	 */
	private int validateBlock0(ManifestContents mf) {
		
		int numErrors = 0;
		
		String metaFileVersion = null;
		String csarVersion = null;
		String createdBy = null;
		String entryDefinitions = null;
		String description = null;
		String topology = null;
		
		Map<String, String> mainAttr = mf.getMainAttributes();
		
		metaFileVersion = mainAttr.get(TOSCAMetaFileAttributes.TOSCA_META_VERSION);
		
		if (metaFileVersion == null) {
			this.logAttrMissing(TOSCAMetaFileAttributes.TOSCA_META_VERSION, 0);
			numErrors++;
		} else if (!(metaFileVersion = metaFileVersion.trim()).equals(TOSCAMetaFileAttributes.TOSCA_META_VERSION_VALUE)) {
			this.logAttrWrongVal(TOSCAMetaFileAttributes.TOSCA_META_VERSION, 0, TOSCAMetaFileAttributes.TOSCA_META_VERSION_VALUE);
			numErrors++;
		}
		
		csarVersion = mainAttr.get(TOSCAMetaFileAttributes.CSAR_VERSION);
		
		if (csarVersion == null) {
			this.logAttrMissing(TOSCAMetaFileAttributes.CSAR_VERSION, 0);
			numErrors++;
		} else if (!(csarVersion = csarVersion.trim()).equals(TOSCAMetaFileAttributes.TOSCA_META_VERSION_VALUE)) {
			this.logAttrWrongVal(TOSCAMetaFileAttributes.CSAR_VERSION, 0, TOSCAMetaFileAttributes.CSAR_VERSION_VALUE);
			numErrors++;
		}
		
		createdBy = mainAttr.get(TOSCAMetaFileAttributes.CREATED_BY);
		
		if (createdBy == null) {
			this.logAttrMissing(TOSCAMetaFileAttributes.CREATED_BY, 0);
			numErrors++;
		} else if ((createdBy = createdBy.trim()).isEmpty()) {
			this.logAttrValEmpty(TOSCAMetaFileAttributes.CREATED_BY, 0);
			numErrors++;
		}
		
		entryDefinitions = mainAttr.get(TOSCAMetaFileAttributes.ENTRY_DEFINITIONS);
		
		if ((entryDefinitions != null) && entryDefinitions.trim().isEmpty()) {
			this.logAttrValEmpty(TOSCAMetaFileAttributes.ENTRY_DEFINITIONS, 0);
			numErrors++;
		}
		
		description = mainAttr.get(TOSCAMetaFileAttributes.DESCRIPTION);
		
		if ((description != null) && description.trim().isEmpty()) {
			this.logAttrValEmpty(TOSCAMetaFileAttributes.DESCRIPTION, 0);
			numErrors++;
		}
		
		topology = mainAttr.get(TOSCAMetaFileAttributes.TOPOLOGY);
		
		if ((topology != null) && topology.trim().isEmpty()) {
			this.logAttrValEmpty(TOSCAMetaFileAttributes.TOPOLOGY, 0);
			numErrors++;
		}
		
		return numErrors;
		
	}
	
	/**
	 * Validates the file blocks (block 1 to last block) of the TOSCA meta file.<br />
	 * <br />
	 * Each file block has the following required attributes:
	 * <ul>
	 * <li><code>Name</code></li>
	 * <li><code>Content-Type</code> (will be checked for correct syntax)</li>
	 * </ul>
	 * 
	 * Further, arbitrary attributes are also allowed in a file block.<br />
	 * <br />
	 * 
	 * @param mf to validate.
	 * @return Number of errors occurred during validation.
	 */
	private int validateFileBlocks(ManifestContents mf) {
		
		int blockNr = 0;
		int numErrors = 0;
		
		String contentType;
		
		List<String> names = mf.getSectionNames();
		
		for (String name : names) {
			
			blockNr++;
			
			if ((name != null) && name.trim().isEmpty()) {
				this.logAttrValEmpty(name, blockNr);
				numErrors++;
			}
			
			Map<String, String> attr = mf.getAttributesForSection(name);
			contentType = attr.get(TOSCAMetaFileAttributes.CONTENT_TYPE);
			
			if (contentType == null) {
				this.logAttrMissing(TOSCAMetaFileAttributes.CONTENT_TYPE, blockNr);
				numErrors++;
			} else if (!contentType.trim().matches("^[-\\w\\+\\.]+/[-\\w\\+\\.]+$")) {
				this.logAttrWrongVal(TOSCAMetaFileAttributes.CONTENT_TYPE, blockNr);
				numErrors++;
			}
			
		}
		
		return numErrors;
		
	}
	
	/**
	 * Logs that attribute <code>attributeName</code> in block
	 * <code>blockNr</code> is missing.
	 * 
	 * @param attributeName
	 * @param blockNr
	 */
	private void logAttrMissing(String attributeName, int blockNr) {
		TOSCAMetaFileParser.LOG.warn("Required attribute {} in block {} is missing.", attributeName, blockNr);
	}
	
	/**
	 * Logs that attribute <code>attributeName</code> in block
	 * <code>blockNr</code> has an invalid value. Correct is
	 * <code>correctValue</code>.
	 * 
	 * @param attributeName
	 * @param blockNr
	 * @param correctValue
	 */
	private void logAttrWrongVal(String attributeName, int blockNr, String correctValue) {
		TOSCAMetaFileParser.LOG.warn("Attribute {} in block {} has an invalid value. Must be {}.", attributeName, blockNr, correctValue);
	}
	
	/**
	 * Logs that attribute <code>attributeName</code> in block
	 * <code>blockNr</code> has an invalid value.
	 * 
	 * @param attributeName
	 * @param blockNr
	 */
	private void logAttrWrongVal(String attributeName, int blockNr) {
		TOSCAMetaFileParser.LOG.warn("Attribute {} in block {} has an invalid value.", attributeName, blockNr);
	}
	
	/**
	 * Logs that attribute <code>attributeName</code> in block
	 * <code>blockNr</code> has an empty value.
	 * 
	 * @param attributeName
	 * @param blockNr
	 */
	private void logAttrValEmpty(String attributeName, int blockNr) {
		TOSCAMetaFileParser.LOG.warn("Attribute {} in block {} has a empty value.", attributeName, blockNr);
	}
	
	/**
	 * Logs the ManifestProblem <code>problem</code>.
	 * 
	 * @param problem
	 */
	private void logManifestProblem(ManifestProblem problem) {
		TOSCAMetaFileParser.LOG.warn(problem.toString());
	}
	
}