Nested input params appears in MainServiceTemplate
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / NameExtractorUtil.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.translator.services.heattotosca;
22
23 import org.openecomp.sdc.translator.datatypes.heattotosca.PropertyRegexMatcher;
24
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.regex.Pattern;
29
30 public class NameExtractorUtil {
31
32   /**
33    * Extract Node Type Name By Properties Priority.
34    * @param properties properties list
35    * @param propertiesRegexMatchers Regex expression list
36    * @return node type name
37    */
38   public static Optional<String> extractNodeTypeNameByPropertiesPriority(
39       Map<String, Object> properties,
40       List<PropertyRegexMatcher> propertiesRegexMatchers) {
41
42     for (PropertyRegexMatcher propertyRegexMatcher : propertiesRegexMatchers) {
43       Optional<String> parameterNameValue =
44           HeatToToscaUtil.getPropertyParameterNameValue(
45               properties.get(propertyRegexMatcher.getPropertyName()));
46       if (parameterNameValue.isPresent()) {
47         if (isPropertyValueMatchNamingConvention(propertyRegexMatcher, parameterNameValue.get())) {
48           return Optional.of(parameterNameValue.get().substring(0, parameterNameValue.get()
49               .lastIndexOf(propertyRegexMatcher.getStringToSearchForPropertyValue())));
50         }
51       }
52     }
53     return Optional.empty();
54   }
55
56   /**
57    * Check if property value match the naming convention using Regex expression.
58    * @param propertyRegexMatcher naming convention using Regex expression
59    * @param propertyValue property value
60    * @return true is there is a match, false otherwise
61    */
62   public static boolean isPropertyValueMatchNamingConvention(PropertyRegexMatcher
63                                                                  propertyRegexMatcher,
64                                                              String propertyValue) {
65     for (Pattern pattern : propertyRegexMatcher.getRegexPatterns()) {
66       if (pattern.matcher(propertyValue).matches()) {
67         return true;
68       }
69     }
70     return false;
71   }
72
73
74 }