push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / helper / impl / NameExtractorServiceImpl.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.helper.impl;
22
23 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
24 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
25 import org.openecomp.sdc.translator.services.heattotosca.helper.NameExtractorService;
26 import org.openecomp.sdc.translator.services.heattotosca.helper.PropertyRegexMatcher;
27
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Optional;
31 import java.util.regex.Pattern;
32
33 public class NameExtractorServiceImpl implements NameExtractorService {
34
35   @Override
36   public Optional<String> extractNodeTypeNameByPropertiesPriority(
37           Map<String, Object> properties,List<PropertyRegexMatcher> propertiesRegexMatchers) {
38     for (PropertyRegexMatcher propertyRegexMatcher : propertiesRegexMatchers) {
39       Optional<String> parameterNameValue =
40           getPropertyParameterNameValue(properties, propertyRegexMatcher.getPropertyName());
41       if (parameterNameValue.isPresent()) {
42         if (isPropertyValueMatchNamingConvention(propertyRegexMatcher, parameterNameValue.get())) {
43           return Optional.of(parameterNameValue.get().substring(0, parameterNameValue.get()
44               .lastIndexOf(propertyRegexMatcher.getStringToSearchForPropertyValue())));
45         }
46       }
47     }
48
49     return Optional.empty();
50   }
51
52   private boolean isPropertyValueMatchNamingConvention(PropertyRegexMatcher propertyRegexMatcher,
53                                                        String propertyValue) {
54     for (Pattern pattern : propertyRegexMatcher.getRegexPatterns()) {
55       if (pattern.matcher(propertyValue).matches()) {
56         return true;
57       }
58     }
59     return false;
60   }
61
62   private Optional<String> getPropertyParameterNameValue(Map<String, Object> properties,
63                                                          String prop) {
64     Object propObj = properties.get(prop);
65     Optional<AttachedResourceId> property = HeatToToscaUtil.extractProperty(propObj);
66     if (property.isPresent()) {
67       AttachedResourceId extractedProperty = property.get();
68       return getParameterName(extractedProperty);
69     }
70     return Optional.empty();
71   }
72
73   private Optional<String> getParameterName(AttachedResourceId extractedProperty) {
74     if (!extractedProperty.isGetParam()) {
75       return Optional.empty();
76     }
77     Object entityId = extractedProperty.getEntityId();
78     if (entityId instanceof String) {
79       return Optional.of((String) entityId);
80     } else {
81       return Optional.of((String) ((List) entityId).get(0));
82     }
83   }
84
85   @Override
86   public PropertyRegexMatcher getPropertyRegexMatcher(String propertyName,
87                                                       List<String> regexMatchers,
88                                                       String propertyValueSearchTerm) {
89     PropertyRegexMatcher propertyRegexMatcher = new PropertyRegexMatcher();
90     propertyRegexMatcher.setPropertyName(propertyName);
91     propertyRegexMatcher.setRegex(regexMatchers);
92     propertyRegexMatcher.setStringToSearchForPropertyValue(propertyValueSearchTerm);
93     return propertyRegexMatcher;
94   }
95 }