[SDC] Onboarding 1710 rebase.
[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.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 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     BufferedReader bfReader;
76     for (Map.Entry<String, byte[]> fileData : files.entrySet()) {
77       String fileName = fileData.getKey();
78       if (!HeatFileAnalyzer.isYamlFile(fileName)) {
79         analyzedZipHeatFiles.addOtherNonModuleFile(fileName);
80         continue;
81       }
82       boolean foundHeatIdentifier = false;
83       try (InputStream is = new ByteArrayInputStream(fileData.getValue())) {
84         bfReader = new BufferedReader(new InputStreamReader(is));
85         String line;
86         boolean isResourcesSection = false;
87         Set<String> nestedFilesNames = new HashSet<>();
88         while ((line = bfReader.readLine()) != null) {
89           if (!foundHeatIdentifier && isMatch(patterns.get(IDENTIFIER), line)) {
90             foundHeatIdentifier = true;
91             analyzedZipHeatFiles.addModuleFile(fileName);
92             if (isResourcesSection) // it means the identifier is located after the resources
93             // section
94             {
95               break;
96             }
97           } else if (isMatch(patterns.get(RESOURCES), line)) {
98             isResourcesSection = true;
99           } else if (isResourceSectionEnd(line, isResourcesSection)) {
100             if (foundHeatIdentifier) {
101               break;
102             }
103           } else if (isResourcesSection) {
104             Optional<String> optionalNestedFileName = fetchNestedFileName(line);
105             optionalNestedFileName
106                 .ifPresent(nestedFilesNames::add);
107           }
108         }
109         analyzedZipHeatFiles.addNestedFiles(fetchFileNamesToReturn(nestedFilesNames,
110             foundHeatIdentifier));
111         if (Objects.nonNull(bfReader)) {
112           bfReader.close();
113         }
114       }
115     }
116     return analyzedZipHeatFiles;
117   }
118
119   private Optional<String> fetchNestedFileName(String line) {
120     if (isMatch(patterns.get(NESTED_PATTERN), line)) {
121       String trimmedLine = line.trim();
122       String nestedFileName = trimmedLine
123           .substring(trimmedLine.indexOf("type:") + "type:".length(), trimmedLine.length())
124           .trim();
125       return Optional.of(nestedFileName);
126     }
127     return Optional.empty();
128   }
129
130   private Set<String> fetchFileNamesToReturn(Set<String> filesNamesToReturn,
131                                              boolean foundHeatIdentifier) {
132     if (!foundHeatIdentifier) {
133       return new HashSet<>();
134     } else {
135       return filesNamesToReturn;
136     }
137   }
138
139   private boolean isResourceSectionEnd(String line, boolean isResourcesSection) {
140     return isResourcesSection && isStartOfNonResourcesHeatSection(line);
141   }
142
143   private boolean isStartOfNonResourcesHeatSection(String line) {
144     return isMatch(patterns.get(PARAMETERS), line) ||
145         isMatch(patterns.get(CONDITIONS), line) ||
146         isMatch(patterns.get(OUTPUTS), line) ||
147         isMatch(patterns.get(PARAMETER_GROUP), line) ||
148         isMatch(patterns.get(DESCRIPTION), line);
149   }
150
151   private boolean isMatch(Pattern pattern, String line) {
152     return pattern.matcher(line).matches();
153   }
154
155 }