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