2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.helper.impl;
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;
28 import java.util.List;
30 import java.util.Optional;
31 import java.util.regex.Pattern;
33 public class NameExtractorServiceImpl implements NameExtractorService {
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())));
49 return Optional.empty();
52 private boolean isPropertyValueMatchNamingConvention(PropertyRegexMatcher propertyRegexMatcher,
53 String propertyValue) {
54 for (Pattern pattern : propertyRegexMatcher.getRegexPatterns()) {
55 if (pattern.matcher(propertyValue).matches()) {
62 private Optional<String> getPropertyParameterNameValue(Map<String, Object> properties,
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);
70 return Optional.empty();
73 private Optional<String> getParameterName(AttachedResourceId extractedProperty) {
74 if (!extractedProperty.isGetParam()) {
75 return Optional.empty();
77 Object entityId = extractedProperty.getEntityId();
78 if (entityId instanceof String) {
79 return Optional.of((String) entityId);
81 return Optional.of((String) ((List) entityId).get(0));
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;