dd303c7bb9020c141710f61ab4ce5388822b727d
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.services.impl;
22
23 import org.openecomp.sdc.vendorsoftwareproduct.services.HeatFileAnalyzer;
24 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles;
25
26 import java.io.BufferedReader;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.util.*;
32 import java.util.regex.Pattern;
33
34 public class HeatFileAnalyzerRowDataImpl implements HeatFileAnalyzer {
35   private static final String HEAT_IDENTIFIER_REGEX = "^heat_template_version:.*";
36   private static final String HEAT_RESOURCES_REGEX = "^resources:\\s*$";
37   private static final String HEAT_PARAMETERS_REGEX = "^parameters:\\s*$";
38   private static final String HEAT_CONDITIONS_REGEX = "^conditions:\\s*$";
39   private static final String HEAT_OUTPUTS_REGEX = "^outputs:\\s*$";
40   private static final String HEAT_PARAMETER_GROUP_REGEX = "^parameter_groups:\\s*$";
41   private static final String HEAT_DESCRIPTION_REGEX = "^description:\\s*$";
42   //allowing spaces at start followed by 'type:' + spaces + any characters + ('.yml' or '.yaml')+
43   // spaces
44   private static final String HEAT_NESTED_RESOURCE_REGEX = "\\s*type:\\s*\\S*.(yml|yaml)\\s*$";
45
46
47   private static final String IDENTIFIER = "IDENTIFIER";
48   private static final String RESOURCES = "RESOURCES";
49   private static final String PARAMETERS = "PARAMETERS";
50   private static final String CONDITIONS = "CONDITIONS";
51   private static final String OUTPUTS = "OUTPUTS";
52   private static final String PARAMETER_GROUP = "PARAMETER_GROUP";
53   private static final String DESCRIPTION = "DESCRIPTION";
54   private static final String NESTED_PATTERN = "NESTED_PATTERN";
55
56   private final Map<String, Pattern> patterns;
57
58   public HeatFileAnalyzerRowDataImpl() {
59     patterns = new HashMap<>();
60     patterns.put(IDENTIFIER, Pattern.compile(HEAT_IDENTIFIER_REGEX));
61     patterns.put(RESOURCES, Pattern.compile(HEAT_RESOURCES_REGEX));
62     patterns.put(PARAMETERS, Pattern.compile(HEAT_PARAMETERS_REGEX));
63     patterns.put(CONDITIONS, Pattern.compile(HEAT_CONDITIONS_REGEX));
64     patterns.put(OUTPUTS, Pattern.compile(HEAT_OUTPUTS_REGEX));
65     patterns.put(PARAMETER_GROUP, Pattern.compile(HEAT_PARAMETER_GROUP_REGEX));
66     patterns.put(DESCRIPTION, Pattern.compile(HEAT_DESCRIPTION_REGEX));
67     patterns.put(NESTED_PATTERN, Pattern.compile(HEAT_NESTED_RESOURCE_REGEX));
68   }
69
70   @Override
71   public AnalyzedZipHeatFiles analyzeFilesNotEligibleForModulesFromFileAnalyzer(Map<String, byte[]> files)
72       throws IOException {
73     AnalyzedZipHeatFiles analyzedZipHeatFiles = new AnalyzedZipHeatFiles();
74
75     for (Map.Entry<String, byte[]> fileData : files.entrySet()) {
76       String fileName = fileData.getKey();
77       if (!HeatFileAnalyzer.isYamlFile(fileName)) {
78         analyzedZipHeatFiles.addOtherNonModuleFile(fileName);
79         continue;
80       }
81
82       boolean foundHeatIdentifier = false;
83       try (InputStream is = new ByteArrayInputStream(fileData.getValue());
84            BufferedReader bfReader = new BufferedReader(new InputStreamReader(is))) {
85
86         String line;
87         boolean isResourcesSection = false;
88         Set<String> nestedFilesNames = new HashSet<>();
89         while ((line = bfReader.readLine()) != null) {
90           if (!foundHeatIdentifier && isMatch(patterns.get(IDENTIFIER), line)) {
91             foundHeatIdentifier = true;
92             analyzedZipHeatFiles.addModuleFile(fileName);
93             if (isResourcesSection) // it means the identifier is located after the resources
94             // section
95             {
96               break;
97             }
98           } else if (isMatch(patterns.get(RESOURCES), line)) {
99             isResourcesSection = true;
100           } else if (isResourceSectionEnd(line, isResourcesSection)) {
101             if (foundHeatIdentifier) {
102               break;
103             }
104           } else if (isResourcesSection) {
105             Optional<String> optionalNestedFileName = fetchNestedFileName(line);
106             optionalNestedFileName
107                 .ifPresent(nestedFilesNames::add);
108           }
109         }
110         analyzedZipHeatFiles.addNestedFiles(fetchFileNamesToReturn(nestedFilesNames,
111             foundHeatIdentifier));
112       }
113     }
114
115     return analyzedZipHeatFiles;
116   }
117
118   private Optional<String> fetchNestedFileName(String line) {
119     if (isMatch(patterns.get(NESTED_PATTERN), line)) {
120       String trimmedLine = line.trim();
121       String nestedFileName = trimmedLine
122           .substring(trimmedLine.indexOf("type:") + "type:".length(), trimmedLine.length())
123           .trim();
124       return Optional.of(nestedFileName);
125     }
126     return Optional.empty();
127   }
128
129   private Set<String> fetchFileNamesToReturn(Set<String> filesNamesToReturn,
130                                              boolean foundHeatIdentifier) {
131     if (!foundHeatIdentifier) {
132       return new HashSet<>();
133     } else {
134       return filesNamesToReturn;
135     }
136   }
137
138   private boolean isResourceSectionEnd(String line, boolean isResourcesSection) {
139     return isResourcesSection && isStartOfNonResourcesHeatSection(line);
140   }
141
142   private boolean isStartOfNonResourcesHeatSection(String line) {
143     return isMatch(patterns.get(PARAMETERS), line) ||
144         isMatch(patterns.get(CONDITIONS), line) ||
145         isMatch(patterns.get(OUTPUTS), line) ||
146         isMatch(patterns.get(PARAMETER_GROUP), line) ||
147         isMatch(patterns.get(DESCRIPTION), line);
148   }
149
150   private boolean isMatch(Pattern pattern, String line) {
151     return pattern.matcher(line).matches();
152   }
153
154 }