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