re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-api / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / services / impl / HeatFileAnalyzerRowDataImpl.java
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.*;
27 import java.util.*;
28 import java.util.regex.Pattern;
29
30 public class HeatFileAnalyzerRowDataImpl implements HeatFileAnalyzer {
31   private static final String HEAT_IDENTIFIER_REGEX = "^heat_template_version:.*";
32   private static final String HEAT_RESOURCES_REGEX = "^resources:\\s*$";
33   private static final String HEAT_PARAMETERS_REGEX = "^parameters:\\s*$";
34   private static final String HEAT_CONDITIONS_REGEX = "^conditions:\\s*$";
35   private static final String HEAT_OUTPUTS_REGEX = "^outputs:\\s*$";
36   private static final String HEAT_PARAMETER_GROUP_REGEX = "^parameter_groups:\\s*$";
37   private static final String HEAT_DESCRIPTION_REGEX = "^description:\\s*$";
38   //allowing spaces at start followed by 'type:' + spaces + any characters + ('.yml' or '.yaml')+
39   // spaces
40   private static final String HEAT_NESTED_RESOURCE_REGEX = "\\s*type:\\s*\\S*.(yml|yaml)\\s*$";
41
42
43   private static final String IDENTIFIER = "IDENTIFIER";
44   private static final String RESOURCES = "RESOURCES";
45   private static final String PARAMETERS = "PARAMETERS";
46   private static final String CONDITIONS = "CONDITIONS";
47   private static final String OUTPUTS = "OUTPUTS";
48   private static final String PARAMETER_GROUP = "PARAMETER_GROUP";
49   private static final String DESCRIPTION = "DESCRIPTION";
50   private static final String NESTED_PATTERN = "NESTED_PATTERN";
51
52   private final Map<String, Pattern> patterns;
53
54   public HeatFileAnalyzerRowDataImpl() {
55     patterns = new HashMap<>();
56     patterns.put(IDENTIFIER, Pattern.compile(HEAT_IDENTIFIER_REGEX));
57     patterns.put(RESOURCES, Pattern.compile(HEAT_RESOURCES_REGEX));
58     patterns.put(PARAMETERS, Pattern.compile(HEAT_PARAMETERS_REGEX));
59     patterns.put(CONDITIONS, Pattern.compile(HEAT_CONDITIONS_REGEX));
60     patterns.put(OUTPUTS, Pattern.compile(HEAT_OUTPUTS_REGEX));
61     patterns.put(PARAMETER_GROUP, Pattern.compile(HEAT_PARAMETER_GROUP_REGEX));
62     patterns.put(DESCRIPTION, Pattern.compile(HEAT_DESCRIPTION_REGEX));
63     patterns.put(NESTED_PATTERN, Pattern.compile(HEAT_NESTED_RESOURCE_REGEX));
64   }
65
66   @Override
67   public AnalyzedZipHeatFiles analyzeFilesNotEligibleForModulesFromFileAnalyzer(Map<String, byte[]> files)
68       throws IOException {
69     AnalyzedZipHeatFiles analyzedZipHeatFiles = new AnalyzedZipHeatFiles();
70
71     for (Map.Entry<String, byte[]> fileData : files.entrySet()) {
72       String fileName = fileData.getKey();
73       if (!HeatFileAnalyzer.isYamlFile(fileName)) {
74         analyzedZipHeatFiles.addOtherNonModuleFile(fileName);
75         continue;
76       }
77
78       boolean foundHeatIdentifier = false;
79       try (InputStream is = new ByteArrayInputStream(fileData.getValue());
80            BufferedReader bfReader = new BufferedReader(new InputStreamReader(is))) {
81
82         String line;
83         boolean isResourcesSection = false;
84         Set<String> nestedFilesNames = new HashSet<>();
85         while ((line = bfReader.readLine()) != null) {
86           if (!foundHeatIdentifier && isMatch(patterns.get(IDENTIFIER), line)) {
87             foundHeatIdentifier = true;
88             analyzedZipHeatFiles.addModuleFile(fileName);
89             if (isResourcesSection) // it means the identifier is located after the resources
90             // section
91             {
92               break;
93             }
94           } else if (isMatch(patterns.get(RESOURCES), line)) {
95             isResourcesSection = true;
96           } else if (isResourceSectionEnd(line, isResourcesSection)) {
97             if (foundHeatIdentifier) {
98               break;
99             }
100           } else if (isResourcesSection) {
101             Optional<String> optionalNestedFileName = fetchNestedFileName(line);
102             optionalNestedFileName
103                 .ifPresent(nestedFilesNames::add);
104           }
105         }
106         analyzedZipHeatFiles.addNestedFiles(fetchFileNamesToReturn(nestedFilesNames,
107             foundHeatIdentifier));
108       }
109     }
110
111     return analyzedZipHeatFiles;
112   }
113
114   private Optional<String> fetchNestedFileName(String line) {
115     if (isMatch(patterns.get(NESTED_PATTERN), line)) {
116       String trimmedLine = line.trim();
117       String nestedFileName = trimmedLine
118           .substring(trimmedLine.indexOf("type:") + "type:".length(), trimmedLine.length())
119           .trim();
120       return Optional.of(nestedFileName);
121     }
122     return Optional.empty();
123   }
124
125   private Set<String> fetchFileNamesToReturn(Set<String> filesNamesToReturn,
126                                              boolean foundHeatIdentifier) {
127     if (!foundHeatIdentifier) {
128       return new HashSet<>();
129     } else {
130       return filesNamesToReturn;
131     }
132   }
133
134   private boolean isResourceSectionEnd(String line, boolean isResourcesSection) {
135     return isResourcesSection && isStartOfNonResourcesHeatSection(line);
136   }
137
138   private boolean isStartOfNonResourcesHeatSection(String line) {
139     return isMatch(patterns.get(PARAMETERS), line) ||
140         isMatch(patterns.get(CONDITIONS), line) ||
141         isMatch(patterns.get(OUTPUTS), line) ||
142         isMatch(patterns.get(PARAMETER_GROUP), line) ||
143         isMatch(patterns.get(DESCRIPTION), line);
144   }
145
146   private boolean isMatch(Pattern pattern, String line) {
147     return pattern.matcher(line).matches();
148   }
149
150 }