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