2 * ============LICENSE_START=======================================================
3 * sdc-distribution-client
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.onap.sdc.tosca.parser.impl;
24 import java.util.Map.Entry;
25 import java.util.stream.Collectors;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.apache.commons.lang3.tuple.Pair;
29 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
30 import org.onap.sdc.tosca.parser.config.ConfigurationManager;
31 import org.onap.sdc.tosca.parser.utils.GeneralUtility;
32 import org.onap.sdc.tosca.parser.utils.SdcToscaUtility;
33 import org.onap.sdc.toscaparser.api.*;
34 import org.onap.sdc.toscaparser.api.elements.Metadata;
35 import org.onap.sdc.toscaparser.api.elements.NodeType;
36 import org.onap.sdc.toscaparser.api.functions.Function;
37 import org.onap.sdc.toscaparser.api.parameters.Input;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class SdcCsarHelperImpl implements ISdcCsarHelper {
43 private static final String PATH_DELIMITER = "#";
44 private static final String CUSTOMIZATION_UUID = "customizationUUID";
45 private ToscaTemplate toscaTemplate;
46 private ConfigurationManager configurationManager;
47 private static Logger log = LoggerFactory.getLogger(SdcCsarHelperImpl.class.getName());
49 public SdcCsarHelperImpl(ToscaTemplate toscaTemplate) {
50 this.toscaTemplate = toscaTemplate;
53 public SdcCsarHelperImpl(ToscaTemplate toscaTemplate, ConfigurationManager configurationManager) {
54 this.toscaTemplate = toscaTemplate;
55 this.configurationManager = configurationManager;
59 //Sunny flow - covered with UT, flat and nested
60 public String getNodeTemplatePropertyLeafValue(NodeTemplate nodeTemplate, String leafValuePath) {
61 if (nodeTemplate == null) {
62 log.error("getNodeTemplatePropertyLeafValue - nodeTemplate is null");
65 if (GeneralUtility.isEmptyString(leafValuePath)) {
66 log.error("getNodeTemplatePropertyLeafValue - leafValuePath is null or empty");
69 String[] split = getSplittedPath(leafValuePath);
70 LinkedHashMap<String, Property> properties = nodeTemplate.getProperties();
71 Object property = processProperties(split, properties);
72 return property == null || property instanceof Function ? null : String.valueOf(property);
76 public Object getNodeTemplatePropertyAsObject(NodeTemplate nodeTemplate, String leafValuePath) {
77 if (nodeTemplate == null) {
78 log.error("getNodeTemplatePropertyAsObject - nodeTemplate is null");
81 if (GeneralUtility.isEmptyString(leafValuePath)) {
82 log.error("getNodeTemplatePropertyAsObject - leafValuePath is null or empty");
85 String[] split = getSplittedPath(leafValuePath);
86 LinkedHashMap<String, Property> properties = nodeTemplate.getProperties();
87 return processProperties(split, properties);
90 public Map<String, Map<String, Object>> getCpPropertiesFromVfcAsObject(NodeTemplate vfc) {
92 log.error("getCpPropertiesFromVfc - vfc is null");
93 return new HashMap<>();
96 String presetProperty = "_ip_requirements";
97 Map<String, Map<String, Object>> cps = new HashMap<>();
99 Map<String, Property> props = vfc.getProperties();
101 // find all port names by pre-set property (ip_requirements)
102 for (Map.Entry<String, Property> entry : props.entrySet()) {
103 if (entry.getKey().endsWith(presetProperty)) {
104 String portName = entry.getKey().replaceAll(presetProperty, "");
105 cps.put(portName, new HashMap<>());
109 if (cps.size() > 0) {
110 // ports found - find all their properties
111 for (String portName : cps.keySet()) {
112 for (Map.Entry<String, Property> property: props.entrySet()) {
113 if (property.getKey().startsWith(portName)) {
114 String portProperty = property.getKey().replaceFirst(portName + "_", "");
115 if (property.getValue() != null) {
116 cps.get(portName).put(portProperty, property.getValue().getValue());
127 public Map<String, Map<String, Object>> getCpPropertiesFromVfc(NodeTemplate vfc) {
130 log.error("getCpPropertiesFromVfc - vfc is null");
131 return new HashMap<>();
134 String presetProperty = "_ip_requirements";
135 Map<String, Map<String, Object>> cps = new HashMap<>();
137 Map<String, Property> props = vfc.getProperties();
139 // find all port names by pre-set property (ip_requirements)
140 for (Map.Entry<String, Property> entry : props.entrySet()) {
141 if (entry.getKey().endsWith(presetProperty)) {
142 String portName = entry.getKey().replaceAll(presetProperty, "");
143 cps.put(portName, new HashMap<>());
147 if (cps.size() > 0) {
148 // ports found - find all their properties
149 for (String portName : cps.keySet()) {
150 for (Map.Entry<String, Property> property: props.entrySet()) {
151 if (property.getKey().startsWith(portName)) {
152 Map<String, Object> portPaths = new HashMap<>();
153 String portProperty = property.getKey().replaceFirst(portName + "_", "");
154 buildPathMappedToValue(portProperty, property.getValue().getValue(), portPaths);
156 cps.get(portName).putAll(portPaths);
166 @SuppressWarnings("unchecked")
167 private void buildPathMappedToValue(String path, Object property, Map<String, Object> pathsMap) {
168 if (property instanceof Map) {
169 for (Map.Entry<String, Object> item : ((Map<String, Object>) property).entrySet()) {
170 if (item.getValue() instanceof Map || item.getValue() instanceof List) {
171 buildPathMappedToValue(path + PATH_DELIMITER + item.getKey(), item.getValue(), pathsMap);
173 pathsMap.put(path + PATH_DELIMITER + item.getKey(), item.getValue());
176 } else if (property instanceof List) {
177 for (Object item: (List<Object>)property) {
178 buildPathMappedToValue(path, item, pathsMap);
181 pathsMap.put(path, property);
187 //Sunny flow - covered with UT
188 public List<NodeTemplate> getServiceVlList() {
189 List<NodeTemplate> serviceVlList = getNodeTemplateBySdcType(toscaTemplate.getTopologyTemplate(), SdcTypes.VL);
190 return serviceVlList;
194 //Sunny flow - covered with UT
195 public List<NodeTemplate> getServiceVfList() {
196 List<NodeTemplate> serviceVfList = getNodeTemplateBySdcType(toscaTemplate.getTopologyTemplate(), SdcTypes.VF);
197 return serviceVfList;
201 //Sunny flow - covered with UT
202 public String getMetadataPropertyValue(Metadata metadata, String metadataPropertyName) {
203 if (GeneralUtility.isEmptyString(metadataPropertyName)) {
204 log.error("getMetadataPropertyValue - the metadataPropertyName is null or empty");
207 if (metadata == null) {
208 log.error("getMetadataPropertyValue - the metadata is null");
211 String metadataPropertyValue = metadata.getValue(metadataPropertyName);
212 return metadataPropertyValue;
217 //Sunny flow - covered with UT
218 public List<NodeTemplate> getServiceNodeTemplatesByType(String nodeType) {
219 if (GeneralUtility.isEmptyString(nodeType)) {
220 log.error("getServiceNodeTemplatesByType - nodeType - is null or empty");
221 return new ArrayList<>();
224 List<NodeTemplate> res = new ArrayList<>();
225 List<NodeTemplate> nodeTemplates = toscaTemplate.getNodeTemplates();
226 for (NodeTemplate nodeTemplate : nodeTemplates) {
227 if (nodeType.equals(nodeTemplate.getTypeDefinition().getType())) {
228 res.add(nodeTemplate);
237 public List<NodeTemplate> getServiceNodeTemplates() {
238 List<NodeTemplate> nodeTemplates = toscaTemplate.getNodeTemplates();
239 return nodeTemplates;
243 //Sunny flow - covered with UT
244 public List<NodeTemplate> getVfcListByVf(String vfCustomizationId) {
245 if (GeneralUtility.isEmptyString(vfCustomizationId)) {
246 log.error("getVfcListByVf - vfCustomizationId - is null or empty");
247 return new ArrayList<>();
250 List<NodeTemplate> serviceVfList = getServiceVfList();
251 NodeTemplate vfInstance = getNodeTemplateByCustomizationUuid(serviceVfList, vfCustomizationId);
252 List<NodeTemplate> vfcs = getNodeTemplateBySdcType(vfInstance, SdcTypes.VFC);
253 vfcs.addAll(getNodeTemplateBySdcType(vfInstance, SdcTypes.CVFC));
259 //Sunny flow - covered with UT
260 public List<Group> getVfModulesByVf(String vfCustomizationUuid) {
261 List<NodeTemplate> serviceVfList = getServiceVfList();
262 NodeTemplate nodeTemplateByCustomizationUuid = getNodeTemplateByCustomizationUuid(serviceVfList, vfCustomizationUuid);
263 if (nodeTemplateByCustomizationUuid != null) {
264 /*SubstitutionMappings substitutionMappings = nodeTemplateByCustomizationUuid.getSubstitutionMappings();
265 if (substitutionMappings != null){
266 List<Group> groups = substitutionMappings.getGroups();
268 List<Group> collect = groups.stream().filter(x -> "org.openecomp.groups.VfModule".equals(x.getTypeDefinition().getType())).collect(Collectors.toList());
269 log.debug("getVfModulesByVf - VfModules are {}", collect);
273 String name = nodeTemplateByCustomizationUuid.getName();
274 String normaliseComponentInstanceName = SdcToscaUtility.normaliseComponentInstanceName(name);
275 List<Group> serviceLevelGroups = toscaTemplate.getTopologyTemplate().getGroups();
276 log.debug("getVfModulesByVf - VF node template name {}, normalized name {}. Searching groups on service level starting with VF normalized name...", name, normaliseComponentInstanceName);
277 if (serviceLevelGroups != null) {
278 List<Group> collect = serviceLevelGroups
280 .filter(x -> "org.openecomp.groups.VfModule".equals(x.getTypeDefinition().getType()) && x.getName().startsWith(normaliseComponentInstanceName))
281 .collect(Collectors.toList());
285 return new ArrayList<>();
289 //Sunny flow - covered with UT
290 public String getServiceInputLeafValueOfDefault(String inputLeafValuePath) {
291 if (GeneralUtility.isEmptyString(inputLeafValuePath)) {
292 log.error("getServiceInputLeafValueOfDefault - inputLeafValuePath is null or empty");
296 String[] split = getSplittedPath(inputLeafValuePath);
297 if (split.length < 2 || !split[1].equals("default")) {
298 log.error("getServiceInputLeafValue - inputLeafValuePath should be of format <input name>#default[optionally #<rest of path>] ");
302 List<Input> inputs = toscaTemplate.getInputs();
303 if (inputs != null) {
304 Optional<Input> findFirst = inputs.stream().filter(x -> x.getName().equals(split[0])).findFirst();
305 if (findFirst.isPresent()) {
306 Input input = findFirst.get();
307 Object current = input.getDefault();
308 Object property = iterateProcessPath(2, current, split);
309 return property == null || property instanceof Function? null : String.valueOf(property);
312 log.error("getServiceInputLeafValue - value not found");
317 public Object getServiceInputLeafValueOfDefaultAsObject(String inputLeafValuePath) {
318 if (GeneralUtility.isEmptyString(inputLeafValuePath)) {
319 log.error("getServiceInputLeafValueOfDefaultAsObject - inputLeafValuePath is null or empty");
323 String[] split = getSplittedPath(inputLeafValuePath);
324 if (split.length < 2 || !split[1].equals("default")) {
325 log.error("getServiceInputLeafValueOfDefaultAsObject - inputLeafValuePath should be of format <input name>#default[optionally #<rest of path>] ");
329 List<Input> inputs = toscaTemplate.getInputs();
330 if (inputs != null) {
331 Optional<Input> findFirst = inputs.stream().filter(x -> x.getName().equals(split[0])).findFirst();
332 if (findFirst.isPresent()) {
333 Input input = findFirst.get();
334 Object current = input.getDefault();
335 return iterateProcessPath(2, current, split);
338 log.error("getServiceInputLeafValueOfDefaultAsObject - value not found");
342 @SuppressWarnings({ "unchecked", "rawtypes" })
343 private Object iterateProcessPath(Integer index, Object current, String[] split) {
344 if (current == null) {
345 log.error("iterateProcessPath - this input has no default");
348 if (split.length > index) {
349 for (int i = index; i < split.length; i++) {
350 if (current instanceof Map) {
351 current = ((Map<String, Object>) current).get(split[i]);
352 } else if (current instanceof List) {
353 current = ((List) current).get(0);
357 log.error("iterateProcessPath - found an unexpected leaf where expected to find a complex type");
362 if (current != null) {
365 log.error("iterateProcessPath - Path not Found");
369 private String[] getSplittedPath(String inputLeafValuePath) {
370 return inputLeafValuePath.split(PATH_DELIMITER);
375 //Sunny flow - covered with UT
376 public String getServiceSubstitutionMappingsTypeName() {
377 SubstitutionMappings substitutionMappings = toscaTemplate.getTopologyTemplate().getSubstitutionMappings();
378 if (substitutionMappings == null) {
379 log.debug("getServiceSubstitutionMappingsTypeName - No Substitution Mappings defined");
383 NodeType nodeType = substitutionMappings.getNodeDefinition();
384 if (nodeType == null) {
385 log.debug("getServiceSubstitutionMappingsTypeName - No Substitution Mappings node defined");
389 return nodeType.getType();
393 //Sunny flow - covered with UT
394 public Metadata getServiceMetadata() {
395 return toscaTemplate.getMetaData();
399 //Sunny flow - covered with UT
400 public Map<String, Object> getServiceMetadataProperties() {
401 if (toscaTemplate.getMetaData() == null){
404 return new HashMap<>(toscaTemplate.getMetaData().getAllProperties());
408 public Map<String, String> getServiceMetadataAllProperties() {
409 if (toscaTemplate.getMetaData() == null){
412 return toscaTemplate.getMetaData().getAllProperties();
416 //Sunny flow - covered with UT
417 public List<Input> getServiceInputs() {
418 return toscaTemplate.getInputs();
422 //Sunny flow - covered with UT
423 public String getGroupPropertyLeafValue(Group group, String leafValuePath) {
425 log.error("getGroupPropertyLeafValue - group is null");
429 if (GeneralUtility.isEmptyString(leafValuePath)) {
430 log.error("getGroupPropertyLeafValue - leafValuePath is null or empty");
434 String[] split = getSplittedPath(leafValuePath);
435 LinkedHashMap<String, Property> properties = group.getProperties();
436 Object property = processProperties(split, properties);
437 return property == null || property instanceof Function? null : String.valueOf(property);
441 public Object getGroupPropertyAsObject(Group group, String leafValuePath) {
443 log.error("getGroupPropertyAsObject - group is null");
447 if (GeneralUtility.isEmptyString(leafValuePath)) {
448 log.error("getGroupPropertyAsObject - leafValuePath is null or empty");
452 String[] split = getSplittedPath(leafValuePath);
453 LinkedHashMap<String, Property> properties = group.getProperties();
454 return processProperties(split, properties);
458 //Sunny flow - covered with UT
459 public List<NodeTemplate> getCpListByVf(String vfCustomizationId) {
460 List<NodeTemplate> cpList = new ArrayList<>();
461 if (GeneralUtility.isEmptyString(vfCustomizationId)) {
462 log.error("getCpListByVf vfCustomizationId string is empty");
466 List<NodeTemplate> serviceVfList = getServiceVfList();
467 if (serviceVfList == null || serviceVfList.size() == 0) {
468 log.error("getCpListByVf Vfs not exist for vfCustomizationId {}", vfCustomizationId);
471 NodeTemplate vfInstance = getNodeTemplateByCustomizationUuid(serviceVfList, vfCustomizationId);
472 if (vfInstance == null) {
473 log.debug("getCpListByVf vf list is null");
476 cpList = getNodeTemplateBySdcType(vfInstance, SdcTypes.CP);
477 if (cpList == null || cpList.size() == 0)
478 log.debug("getCpListByVf cps not exist for vfCustomizationId {}", vfCustomizationId);
483 //Sunny flow - covered with UT
484 public List<NodeTemplate> getMembersOfVfModule(NodeTemplate vf, Group serviceLevelVfModule) {
486 log.error("getMembersOfVfModule - vf is null");
487 return new ArrayList<>();
490 if (serviceLevelVfModule == null || serviceLevelVfModule.getMetadata() == null || serviceLevelVfModule.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID) == null) {
491 log.error("getMembersOfVfModule - vfModule or its metadata is null. Cannot match a VF group based on invariantUuid from missing metadata.");
492 return new ArrayList<>();
496 SubstitutionMappings substitutionMappings = vf.getSubMappingToscaTemplate();
497 if (substitutionMappings != null) {
498 List<Group> groups = substitutionMappings.getGroups();
499 if (groups != null) {
500 Optional<Group> findFirst = groups
502 .filter(x -> (x.getMetadata() != null && serviceLevelVfModule.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID).equals(x.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID)))).findFirst();
503 if (findFirst.isPresent()) {
504 List<String> members = findFirst.get().getMembers();
505 if (members != null) {
506 List<NodeTemplate> collect = substitutionMappings.getNodeTemplates().stream().filter(x -> members.contains(x.getName())).collect(Collectors.toList());
512 return new ArrayList<>();
516 //Sunny flow - covered with UT
517 @SuppressWarnings("unchecked")
518 public List<Pair<NodeTemplate, NodeTemplate>> getNodeTemplatePairsByReqName(
519 List<NodeTemplate> listOfReqNodeTemplates, List<NodeTemplate> listOfCapNodeTemplates, String reqName) {
521 if (listOfReqNodeTemplates == null) {
522 log.error("getNodeTemplatePairsByReqName - listOfReqNodeTemplates is null");
523 return new ArrayList<>();
526 if (listOfCapNodeTemplates == null) {
527 log.error("getNodeTemplatePairsByReqName - listOfCapNodeTemplates is null");
528 return new ArrayList<>();
531 if (GeneralUtility.isEmptyString(reqName)) {
532 log.error("getNodeTemplatePairsByReqName - reqName is null or empty");
533 return new ArrayList<>();
536 List<Pair<NodeTemplate, NodeTemplate>> pairsList = new ArrayList<>();
538 for (NodeTemplate reqNodeTemplate : listOfReqNodeTemplates) {
539 List<RequirementAssignment> requirements = reqNodeTemplate.getRequirements().getRequirementsByName(reqName).getAll();
540 for (RequirementAssignment reqEntry : requirements) {
541 String node = reqEntry.getNodeTemplateName();
543 Optional<NodeTemplate> findFirst = listOfCapNodeTemplates.stream().filter(x -> x.getName().equals(node)).findFirst();
544 if (findFirst.isPresent()) {
545 pairsList.add(new ImmutablePair<NodeTemplate, NodeTemplate>(reqNodeTemplate, findFirst.get()));
555 //Sunny flow - covered with UT
556 //TODO constant strings
557 public List<NodeTemplate> getAllottedResources() {
558 List<NodeTemplate> nodeTemplates = null;
559 nodeTemplates = toscaTemplate.getTopologyTemplate().getNodeTemplates();
560 if (nodeTemplates.isEmpty()) {
561 log.error("getAllottedResources nodeTemplates not exist");
563 nodeTemplates = nodeTemplates.stream().filter(
564 x -> x.getMetaData() != null && x.getMetaData().getValue("category").equals("Allotted Resource"))
565 .collect(Collectors.toList());
566 if (nodeTemplates.isEmpty()) {
567 log.debug("getAllottedResources - allotted resources not exist");
571 return nodeTemplates;
575 //Sunny flow - covered with UT
576 public String getTypeOfNodeTemplate(NodeTemplate nodeTemplate) {
577 if (nodeTemplate == null) {
579 log.error("getTypeOfNodeTemplate nodeTemplate is null");
582 return nodeTemplate.getTypeDefinition().getType();
586 * This methdd is returning the csarConformanceLevel for input CSAR
587 * When csarConformanceLevel is configured with failOnError as False in Error Configuration; it
588 * assigns the default value to csarConformanceLevel which is the max level provided in
590 * @return csarConformanceLevel
593 public String getConformanceLevel() {
594 LinkedHashMap<String, Object> csarMeta = toscaTemplate.getMetaProperties("csar.meta");
595 if (csarMeta == null){
596 log.warn("No csar.meta file is found in CSAR - this file should hold the conformance level of the CSAR. This might be OK for older CSARs.");
597 if (configurationManager != null && !configurationManager.getErrorConfiguration()
598 .getErrorInfo("CONFORMANCE_LEVEL_ERROR").getFailOnError()){
599 String csarConLevel = configurationManager.getConfiguration().getConformanceLevel().getMaxVersion();
600 log.warn("csarConformanceLevel is not found in input csar; defaulting to max version {}" , csarConLevel);
604 log.warn("csarConformanceLevel is not found in input csar; returning null as no defaults defined in error configuration");
609 Object conformanceLevel = csarMeta.get("SDC-TOSCA-Definitions-Version");
610 if (conformanceLevel != null){
611 String confLevelStr = conformanceLevel.toString();
612 log.debug("CSAR conformance level is {}", confLevelStr);
615 log.error("Invalid csar.meta file - no entry found for SDC-TOSCA-Definitions-Version key. This entry should hold the conformance level.");
622 public String getNodeTemplateCustomizationUuid(NodeTemplate nt) {
624 if (nt != null && nt.getMetaData() != null){
625 res = nt.getMetaData().getValue(CUSTOMIZATION_UUID);
627 log.error("Node template or its metadata is null");
632 public List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType) {
633 return getNodeTemplateBySdcType(parentNodeTemplate, sdcType, false);
636 private List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType, boolean isVNF) {
638 if (parentNodeTemplate == null) {
639 log.error("getNodeTemplateBySdcType - nodeTemplate is null or empty");
640 return new ArrayList<>();
643 if (sdcType == null) {
644 log.error("getNodeTemplateBySdcType - sdcType is null or empty");
645 return new ArrayList<>();
648 SubstitutionMappings substitutionMappings = parentNodeTemplate.getSubMappingToscaTemplate();
650 if (substitutionMappings != null) {
651 List<NodeTemplate> nodeTemplates = substitutionMappings.getNodeTemplates();
652 if (nodeTemplates != null && nodeTemplates.size() > 0) {
653 if (sdcType.equals(SdcTypes.VFC) && isVNF) {
654 return nodeTemplates.stream()
655 .filter(x -> (x.getMetaData() != null &&
656 sdcType.getValue().equals(x.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE))) && isVNFType(x))
657 .collect(Collectors.toList());
660 return nodeTemplates.stream()
661 .filter(x -> (x.getMetaData() != null &&
662 sdcType.getValue().equals(x.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE))) && !isVNFType(x))
663 .collect(Collectors.toList());
667 log.debug("getNodeTemplateBySdcType - SubstitutionMappings' node Templates not exist");
670 log.debug("getNodeTemplateBySdcType - SubstitutionMappings not exist");
672 return new ArrayList<>();
675 public Map<String, String> filterNodeTemplatePropertiesByValue(NodeTemplate nodeTemplate, FilterType filterType, String pattern) {
676 Map<String, String> filterMap = new HashMap<>();
678 if (nodeTemplate == null) {
679 log.error("filterNodeTemplatePropertiesByValue nodeTemplate is null");
683 if (filterType == null) {
684 log.error("filterNodeTemplatePropertiesByValue filterType is null");
688 if (GeneralUtility.isEmptyString(pattern)) {
689 log.error("filterNodeTemplatePropertiesByValue pattern string is empty");
693 Map<String, Property> ntProperties = nodeTemplate.getProperties();
695 if (ntProperties != null && ntProperties.size() > 0) {
697 for (Property current : ntProperties.values()) {
698 filterProperties(current.getValue(), current.getName(), filterType, pattern, filterMap);
702 log.trace("filterNodeTemplatePropertiesByValue - filterMap value: {}", filterMap);
707 public NodeTemplate getVnfConfig(String vfCustomizationUuid) {
709 if (GeneralUtility.isEmptyString(vfCustomizationUuid)) {
710 log.error("getVnfConfig - vfCustomizationId - is null or empty");
714 List<NodeTemplate> serviceVfList = getServiceVfList();
715 NodeTemplate vfInstance = getNodeTemplateByCustomizationUuid(serviceVfList, vfCustomizationUuid);
716 NodeTemplate vnfConfig = getNodeTemplateBySdcType(vfInstance, SdcTypes.VFC, true).stream().findAny().orElse(null);
721 public boolean hasTopology(NodeTemplate nodeTemplate) {
722 if (nodeTemplate == null) {
723 log.error("hasTopology - nodeTemplate - is null");
727 if (nodeTemplate.getMetaData() != null) {
728 String type = nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE);
729 log.debug("hasTopology - node template {} is a {} type", nodeTemplate.getName(), type);
730 return SdcTypes.isComplex(type);
737 public List<NodeTemplate> getNodeTemplateChildren(NodeTemplate nodeTemplate) {
738 if (nodeTemplate == null) {
739 log.error("getNodeTemplateChildren - nodeTemplate - is null");
740 return new ArrayList<>();
743 SubstitutionMappings substitutionMappings = nodeTemplate.getSubMappingToscaTemplate();
744 if (substitutionMappings != null) {
745 List<NodeTemplate> nodeTemplates = substitutionMappings.getNodeTemplates();
746 if (nodeTemplates != null && nodeTemplates.size() > 0) {
748 return nodeTemplates.stream()
749 .filter(x -> !isVNFType(x))
750 .collect(Collectors.toList());
753 log.debug("getNodeTemplateChildren - SubstitutionMappings' node Templates not exist");
756 log.debug("getNodeTemplateChildren - SubstitutionMappings not exist");
758 return new ArrayList<>();
762 public NodeTemplate getServiceNodeTemplateByNodeName(String nodeName) {
763 if (GeneralUtility.isEmptyString(nodeName)) {
764 log.error("getServiceNodeTemplateByNodeName - nodeName - is null or empty");
768 List<NodeTemplate> nodeTemplates = getServiceNodeTemplates();
769 Optional<NodeTemplate> findFirst = nodeTemplates.stream().filter(nt -> nt.getName().equals(nodeName)).findFirst();
771 return findFirst.isPresent() ? findFirst.get() : null;
775 public Metadata getNodeTemplateMetadata(NodeTemplate nt) {
777 log.error("getNodeTemplateMetadata - nt (node template) - is null");
781 return nt.getMetaData();
785 public CapabilityAssignments getCapabilitiesOf(NodeTemplate nt) {
787 log.error("getCapabilitiesOf - nt (node template) - is null");
791 return nt.getCapabilities();
795 public RequirementAssignments getRequirementsOf(NodeTemplate nt) {
797 log.error("getRequirementsOf - nt (node template) - is null");
801 return nt.getRequirements();
805 public String getCapabilityPropertyLeafValue(CapabilityAssignment capability, String pathToPropertyLeafValue) {
806 if (capability == null) {
807 log.error("getCapabilityPropertyLeafValue - capability is null");
811 if (GeneralUtility.isEmptyString(pathToPropertyLeafValue)) {
812 log.error("getCapabilityPropertyLeafValue - pathToPropertyLeafValue is null or empty");
816 String[] split = getSplittedPath(pathToPropertyLeafValue);
817 LinkedHashMap<String, Property> properties = capability.getProperties();
818 Object property = processProperties(split, properties);
819 return property == null || property instanceof Function ? null : String.valueOf(property);
822 /************************************* helper functions ***********************************/
823 private boolean isVNFType(NodeTemplate nt) {
824 return nt.getType().endsWith("VnfConfiguration");
827 @SuppressWarnings("unchecked")
828 private Map<String, String> filterProperties(Object property, String path, FilterType filterType, String pattern, Map<String, String> filterMap) {
830 if (property instanceof Map) {
831 for (Map.Entry<String, Object> item: ((Map<String, Object>) property).entrySet()) {
832 String itemPath = path + PATH_DELIMITER + item.getKey();
833 filterProperties(item.getValue(), itemPath, filterType, pattern, filterMap);
835 } else if (property instanceof List) {
836 for (Object item: (List<Object>)property) {
837 filterProperties(item, path, filterType, pattern, filterMap);
840 if (filterType.isMatch(property.toString(), pattern)) {
841 filterMap.put(path, property.toString());
848 public List<NodeTemplate> getServiceNodeTemplateBySdcType(SdcTypes sdcType) {
849 if (sdcType == null) {
850 log.error("getServiceNodeTemplateBySdcType - sdcType is null or empty");
851 return new ArrayList<>();
854 TopologyTemplate topologyTemplate = toscaTemplate.getTopologyTemplate();
855 return getNodeTemplateBySdcType(topologyTemplate, sdcType);
859 /************************************* helper functions ***********************************/
860 private List<NodeTemplate> getNodeTemplateBySdcType(TopologyTemplate topologyTemplate, SdcTypes sdcType) {
861 if (sdcType == null) {
862 log.error("getNodeTemplateBySdcType - sdcType is null or empty");
863 return new ArrayList<>();
866 if (topologyTemplate == null) {
867 log.error("getNodeTemplateBySdcType - topologyTemplate is null");
868 return new ArrayList<>();
871 List<NodeTemplate> nodeTemplates = topologyTemplate.getNodeTemplates();
873 if (nodeTemplates != null && nodeTemplates.size() > 0)
874 return nodeTemplates.stream().filter(x -> (x.getMetaData() != null && sdcType.getValue().equals(x.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)))).collect(Collectors.toList());
876 log.debug("getNodeTemplateBySdcType - topologyTemplate's nodeTemplates not exist");
877 return new ArrayList<>();
880 //Assumed to be unique property for the list
881 private NodeTemplate getNodeTemplateByCustomizationUuid(List<NodeTemplate> nodeTemplates, String customizationId) {
882 if (customizationId != null) {
883 Optional<NodeTemplate> findFirst = nodeTemplates.stream().filter(x -> (x.getMetaData() != null && customizationId.equals(x.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)))).findFirst();
884 return findFirst.isPresent() ? findFirst.get() : null;
887 log.error("getNodeTemplateByCustomizationUuid - customizationId is null");
892 private Object processProperties(String[] split, LinkedHashMap<String, Property> properties) {
893 Optional<Entry<String, Property>> findFirst = properties.entrySet().stream().filter(x -> x.getKey().equals(split[0])).findFirst();
894 if (findFirst.isPresent()) {
895 Property property = findFirst.get().getValue();
896 Object current = property.getValue();
897 return iterateProcessPath(1, current, split);
899 String propName = (split != null && split.length > 0 ? split[0] : null);
900 log.error("processProperties - property {} not found", propName);