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;
23 import static java.util.stream.Collectors.toList;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.Optional;
31 import java.util.stream.Collectors;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.commons.lang3.tuple.ImmutablePair;
35 import org.apache.commons.lang3.tuple.Pair;
36 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
37 import org.onap.sdc.tosca.parser.config.ConfigurationManager;
38 import org.onap.sdc.tosca.parser.utils.GeneralUtility;
39 import org.onap.sdc.tosca.parser.utils.SdcToscaUtility;
40 import org.onap.sdc.toscaparser.api.CapabilityAssignment;
41 import org.onap.sdc.toscaparser.api.CapabilityAssignments;
42 import org.onap.sdc.toscaparser.api.Group;
43 import org.onap.sdc.toscaparser.api.NodeTemplate;
44 import org.onap.sdc.toscaparser.api.Policy;
45 import org.onap.sdc.toscaparser.api.Property;
46 import org.onap.sdc.toscaparser.api.RequirementAssignment;
47 import org.onap.sdc.toscaparser.api.RequirementAssignments;
48 import org.onap.sdc.toscaparser.api.SubstitutionMappings;
49 import org.onap.sdc.toscaparser.api.TopologyTemplate;
50 import org.onap.sdc.toscaparser.api.ToscaTemplate;
51 import org.onap.sdc.toscaparser.api.elements.InterfacesDef;
52 import org.onap.sdc.toscaparser.api.elements.Metadata;
53 import org.onap.sdc.toscaparser.api.elements.NodeType;
54 import org.onap.sdc.toscaparser.api.functions.Function;
55 import org.onap.sdc.toscaparser.api.parameters.Input;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 public class SdcCsarHelperImpl implements ISdcCsarHelper {
61 private static final String PATH_DELIMITER = "#";
62 private static final String CUSTOMIZATION_UUID = "customizationUUID";
63 private ToscaTemplate toscaTemplate;
64 private ConfigurationManager configurationManager;
65 private static Logger log = LoggerFactory.getLogger(SdcCsarHelperImpl.class.getName());
67 public SdcCsarHelperImpl(ToscaTemplate toscaTemplate) {
68 this.toscaTemplate = toscaTemplate;
71 public SdcCsarHelperImpl(ToscaTemplate toscaTemplate, ConfigurationManager configurationManager) {
72 this.toscaTemplate = toscaTemplate;
73 this.configurationManager = configurationManager;
77 public List<Policy> getPoliciesOfTarget(NodeTemplate nodeTemplate) {
78 return getPoliciesOfNodeTemplate(nodeTemplate.getName())
80 .sorted(Policy::compareTo)
85 public List<Policy> getPoliciesOfOriginOfNodeTemplate(NodeTemplate nodeTemplate) {
86 if(StringUtils.isNotEmpty(nodeTemplate.getName())){
87 return getNodeTemplateByName(nodeTemplate.getName()).getOriginComponentTemplate().getPolicies();
89 return new ArrayList<>();
93 public List<Policy> getPoliciesOfTargetByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName) {
94 return getPoliciesOfNodeTemplate(nodeTemplate.getName())
96 .filter(p->p.getType().equals(policyTypeName))
97 .sorted(Policy::compareTo)
102 public List<Policy> getPoliciesOfOriginOfNodeTemplateByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName) {
103 return getPoliciesOfOriginOfNodeTemplate(nodeTemplate)
105 .filter(p->p.getType().equals(policyTypeName))
106 .sorted(Policy::compareTo)
111 public List<NodeTemplate> getPolicyTargetsFromTopologyTemplate(String policyName) {
112 if(toscaTemplate.getNodeTemplates() == null){
113 return new ArrayList<>();
115 List<String> targetNames = getPolicyTargets(policyName);
116 return toscaTemplate.getNodeTemplates().stream()
117 .filter(nt->targetNames.contains(nt.getName()))
122 public List<NodeTemplate> getPolicyTargetsFromOrigin(NodeTemplate nodeTemplate, String policyName) {
123 Optional<Policy> policyOpt = null;
124 if(StringUtils.isNotEmpty(nodeTemplate.getName())){
125 policyOpt = getNodeTemplateByName(nodeTemplate.getName()).getOriginComponentTemplate().getPolicies().stream().filter(p -> p.getName().equals(policyName)).findFirst();
127 if(policyOpt.isPresent()){
128 List<String> targets = policyOpt.get().getTargets();
129 return nodeTemplate.getOriginComponentTemplate().getNodeTemplates()
131 .filter(nt -> targets.contains(nt.getName())).collect(Collectors.toList());
133 return new ArrayList<>();
137 public List<Policy> getPoliciesOfTopologyTemplate(){
138 if(toscaTemplate.getPolicies() == null)
139 return new ArrayList<>();
140 return toscaTemplate.getPolicies()
142 .sorted(Policy::compareTo)
147 public List<Policy> getPoliciesOfTopologyTemplateByToscaPolicyType(String policyTypeName){
148 if(toscaTemplate.getPolicies() == null)
149 return new ArrayList<>();
150 return toscaTemplate.getPolicies()
152 .filter(p->p.getType().equals(policyTypeName))
153 .sorted(Policy::compareTo)
157 public NodeTemplate getNodeTemplateByName(String nodeTemplateName) {
158 if(toscaTemplate.getNodeTemplates() == null)
160 return toscaTemplate.getNodeTemplates()
162 .filter(nt -> nt.getName().equals(nodeTemplateName))
163 .findFirst().orElse(null);
166 private List<Policy> getPoliciesOfNodeTemplate(String nodeTemplateName) {
167 if(toscaTemplate.getPolicies() == null)
168 return new ArrayList<>();
169 return toscaTemplate.getPolicies()
171 .filter(p -> p.getTargets().contains(nodeTemplateName))
175 private List<String> getPolicyTargets(String policyName) {
176 return getPolicyByName(policyName).map(Policy::getTargets).orElse(new ArrayList<>());
179 private List<String> getGroupMembers(String groupName) {
180 return getGroupByName(groupName).map(Group::getMembers).orElse(new ArrayList<>());
183 private Optional<Policy> getPolicyByName(String policyName) {
184 if(toscaTemplate.getPolicies() == null)
185 return Optional.empty();
186 return toscaTemplate.getPolicies()
188 .filter(p -> p.getName().equals(policyName)).findFirst();
191 private Optional<Group> getGroupByName(String groupName) {
192 if(toscaTemplate.getGroups() == null)
193 return Optional.empty();
194 return toscaTemplate.getGroups()
196 .filter(g -> g.getName().equals(groupName)).findFirst();
200 //Sunny flow - covered with UT, flat and nested
201 public String getNodeTemplatePropertyLeafValue(NodeTemplate nodeTemplate, String leafValuePath) {
202 if (nodeTemplate == null) {
203 log.error("getNodeTemplatePropertyLeafValue - nodeTemplate is null");
206 if (GeneralUtility.isEmptyString(leafValuePath)) {
207 log.error("getNodeTemplatePropertyLeafValue - leafValuePath is null or empty");
210 String[] split = getSplittedPath(leafValuePath);
211 LinkedHashMap<String, Property> properties = nodeTemplate.getProperties();
212 Object property = processProperties(split, properties);
213 return property == null || property instanceof Function ? null : String.valueOf(property);
217 public Object getNodeTemplatePropertyAsObject(NodeTemplate nodeTemplate, String leafValuePath) {
218 if (nodeTemplate == null) {
219 log.error("getNodeTemplatePropertyAsObject - nodeTemplate is null");
222 if (GeneralUtility.isEmptyString(leafValuePath)) {
223 log.error("getNodeTemplatePropertyAsObject - leafValuePath is null or empty");
226 String[] split = getSplittedPath(leafValuePath);
227 LinkedHashMap<String, Property> properties = nodeTemplate.getProperties();
228 return processProperties(split, properties);
231 public Map<String, Map<String, Object>> getCpPropertiesFromVfcAsObject(NodeTemplate vfc) {
233 log.error("getCpPropertiesFromVfc - vfc is null");
234 return new HashMap<>();
237 String presetProperty = "_ip_requirements";
238 Map<String, Map<String, Object>> cps = new HashMap<>();
240 Map<String, Property> props = vfc.getProperties();
242 // find all port names by pre-set property (ip_requirements)
243 for (Map.Entry<String, Property> entry : props.entrySet()) {
244 if (entry.getKey().endsWith(presetProperty)) {
245 String portName = entry.getKey().replaceAll(presetProperty, "");
246 cps.put(portName, new HashMap<>());
250 findPutAllPortsProperties(cps, props);
256 private void findPutAllPortsProperties(Map<String, Map<String, Object>> cps, Map<String, Property> props) {
257 if (!cps.isEmpty()) {
258 for (Entry<String, Map<String, Object>> port : cps.entrySet()) {
259 for (Map.Entry<String, Property> property: props.entrySet()) {
260 if (property.getKey().startsWith(port.getKey())) {
261 String portProperty = property.getKey().replaceFirst(port.getKey() + "_", "");
262 if (property.getValue() != null) {
263 cps.get(port.getKey()).put(portProperty, property.getValue().getValue());
271 public Map<String, Map<String, Object>> getCpPropertiesFromVfc(NodeTemplate vfc) {
274 log.error("getCpPropertiesFromVfc - vfc is null");
275 return new HashMap<>();
278 String presetProperty = "_ip_requirements";
279 Map<String, Map<String, Object>> cps = new HashMap<>();
281 Map<String, Property> props = vfc.getProperties();
283 // find all port names by pre-set property (ip_requirements)
284 for (Map.Entry<String, Property> entry : props.entrySet()) {
285 if (entry.getKey().endsWith(presetProperty)) {
286 String portName = entry.getKey().replaceAll(presetProperty, "");
287 cps.put(portName, new HashMap<>());
290 findBuildPutAllPortsProperties(cps, props);
296 private void findBuildPutAllPortsProperties(Map<String, Map<String, Object>> cps, Map<String, Property> props) {
297 if (!cps.isEmpty()) {
298 for (Entry<String, Map<String, Object>> port : cps.entrySet()) {
299 for (Map.Entry<String, Property> property: props.entrySet()) {
300 if (property.getKey().startsWith(port.getKey())) {
301 Map<String, Object> portPaths = new HashMap<>();
302 String portProperty = property.getKey().replaceFirst(port.getKey() + "_", "");
303 buildPathMappedToValue(portProperty, property.getValue().getValue(), portPaths);
304 cps.get(port.getKey()).putAll(portPaths);
311 @SuppressWarnings("unchecked")
312 private void buildPathMappedToValue(String path, Object property, Map<String, Object> pathsMap) {
313 if (property instanceof Map) {
314 for (Map.Entry<String, Object> item : ((Map<String, Object>) property).entrySet()) {
315 if (item.getValue() instanceof Map || item.getValue() instanceof List) {
316 buildPathMappedToValue(path + PATH_DELIMITER + item.getKey(), item.getValue(), pathsMap);
318 pathsMap.put(path + PATH_DELIMITER + item.getKey(), item.getValue());
321 } else if (property instanceof List) {
322 for (Object item: (List<Object>)property) {
323 buildPathMappedToValue(path, item, pathsMap);
326 pathsMap.put(path, property);
332 //Sunny flow - covered with UT
333 public List<NodeTemplate> getServiceVlList() {
334 return getNodeTemplateBySdcType(toscaTemplate.getTopologyTemplate(), SdcTypes.VL);
338 //Sunny flow - covered with UT
339 public List<NodeTemplate> getServiceVfList() {
340 return getNodeTemplateBySdcType(toscaTemplate.getTopologyTemplate(), SdcTypes.VF);
344 //Sunny flow - covered with UT
345 public String getMetadataPropertyValue(Metadata metadata, String metadataPropertyName) {
346 if (GeneralUtility.isEmptyString(metadataPropertyName)) {
347 log.error("getMetadataPropertyValue - the metadataPropertyName is null or empty");
350 if (metadata == null) {
351 log.error("getMetadataPropertyValue - the metadata is null");
354 return metadata.getValue(metadataPropertyName);
359 //Sunny flow - covered with UT
360 public List<NodeTemplate> getServiceNodeTemplatesByType(String nodeType) {
361 if (GeneralUtility.isEmptyString(nodeType)) {
362 log.error("getServiceNodeTemplatesByType - nodeType - is null or empty");
363 return new ArrayList<>();
366 List<NodeTemplate> res = new ArrayList<>();
367 List<NodeTemplate> nodeTemplates = toscaTemplate.getNodeTemplates();
368 for (NodeTemplate nodeTemplate : nodeTemplates) {
369 if (nodeType.equals(nodeTemplate.getTypeDefinition().getType())) {
370 res.add(nodeTemplate);
379 public List<NodeTemplate> getServiceNodeTemplates() {
380 return toscaTemplate.getNodeTemplates();
384 //Sunny flow - covered with UT
385 public List<NodeTemplate> getVfcListByVf(String vfCustomizationId) {
386 if (GeneralUtility.isEmptyString(vfCustomizationId)) {
387 log.error("getVfcListByVf - vfCustomizationId - is null or empty");
388 return new ArrayList<>();
391 List<NodeTemplate> serviceVfList = getServiceVfList();
392 NodeTemplate vfInstance = getNodeTemplateByCustomizationUuid(serviceVfList, vfCustomizationId);
393 List<NodeTemplate> vfcs = getNodeTemplateBySdcType(vfInstance, SdcTypes.VFC);
394 vfcs.addAll(getNodeTemplateBySdcType(vfInstance, SdcTypes.CVFC));
400 //Sunny flow - covered with UT
401 public List<Group> getVfModulesByVf(String vfCustomizationUuid) {
402 List<NodeTemplate> serviceVfList = getServiceVfList();
403 NodeTemplate nodeTemplateByCustomizationUuid = getNodeTemplateByCustomizationUuid(serviceVfList, vfCustomizationUuid);
404 if (nodeTemplateByCustomizationUuid != null) {
405 String name = nodeTemplateByCustomizationUuid.getName();
406 String normaliseComponentInstanceName = SdcToscaUtility.normaliseComponentInstanceName(name);
407 List<Group> serviceLevelGroups = toscaTemplate.getTopologyTemplate().getGroups();
408 log.debug("getVfModulesByVf - VF node template name {}, normalized name {}. Searching groups on service level starting with VF normalized name...", name, normaliseComponentInstanceName);
409 if (serviceLevelGroups != null) {
410 return serviceLevelGroups
412 .filter(x -> "org.openecomp.groups.VfModule".equals(x.getTypeDefinition().getType()) && x.getName().startsWith(normaliseComponentInstanceName))
416 return new ArrayList<>();
420 //Sunny flow - covered with UT
421 public String getServiceInputLeafValueOfDefault(String inputLeafValuePath) {
422 if (GeneralUtility.isEmptyString(inputLeafValuePath)) {
423 log.error("getServiceInputLeafValueOfDefault - inputLeafValuePath is null or empty");
427 String[] split = getSplittedPath(inputLeafValuePath);
428 if (split.length < 2 || !split[1].equals("default")) {
429 log.error("getServiceInputLeafValue - inputLeafValuePath should be of format <input name>#default[optionally #<rest of path>] ");
433 List<Input> inputs = toscaTemplate.getInputs();
434 if (inputs != null) {
435 Optional<Input> findFirst = inputs.stream().filter(x -> x.getName().equals(split[0])).findFirst();
436 if (findFirst.isPresent()) {
437 Input input = findFirst.get();
438 Object current = input.getDefault();
439 Object property = iterateProcessPath(2, current, split);
440 return property == null || property instanceof Function? null : String.valueOf(property);
443 log.error("getServiceInputLeafValue - value not found");
448 public Object getServiceInputLeafValueOfDefaultAsObject(String inputLeafValuePath) {
449 if (GeneralUtility.isEmptyString(inputLeafValuePath)) {
450 log.error("getServiceInputLeafValueOfDefaultAsObject - inputLeafValuePath is null or empty");
454 String[] split = getSplittedPath(inputLeafValuePath);
455 if (split.length < 2 || !split[1].equals("default")) {
456 log.error("getServiceInputLeafValueOfDefaultAsObject - inputLeafValuePath should be of format <input name>#default[optionally #<rest of path>] ");
460 List<Input> inputs = toscaTemplate.getInputs();
461 if (inputs != null) {
462 Optional<Input> findFirst = inputs.stream().filter(x -> x.getName().equals(split[0])).findFirst();
463 if (findFirst.isPresent()) {
464 Input input = findFirst.get();
465 Object current = input.getDefault();
466 return iterateProcessPath(2, current, split);
469 log.error("getServiceInputLeafValueOfDefaultAsObject - value not found");
473 @SuppressWarnings({ "unchecked", "rawtypes" })
474 private Object iterateProcessPath(Integer index, Object current, String[] split) {
475 if (current == null) {
476 log.error("iterateProcessPath - this input has no default");
479 if (split.length > index) {
480 for (int i = index; i < split.length; i++) {
481 if (current instanceof Map) {
482 current = ((Map<String, Object>) current).get(split[i]);
483 } else if (current instanceof List) {
484 current = ((List) current).get(0);
488 log.error("iterateProcessPath - found an unexpected leaf where expected to find a complex type");
493 if (current != null) {
496 log.error("iterateProcessPath - Path not Found");
500 private String[] getSplittedPath(String inputLeafValuePath) {
501 return inputLeafValuePath.split(PATH_DELIMITER);
506 //Sunny flow - covered with UT
507 public String getServiceSubstitutionMappingsTypeName() {
508 SubstitutionMappings substitutionMappings = toscaTemplate.getTopologyTemplate().getSubstitutionMappings();
509 if (substitutionMappings == null) {
510 log.debug("getServiceSubstitutionMappingsTypeName - No Substitution Mappings defined");
514 NodeType nodeType = substitutionMappings.getNodeDefinition();
515 if (nodeType == null) {
516 log.debug("getServiceSubstitutionMappingsTypeName - No Substitution Mappings node defined");
520 return nodeType.getType();
524 //Sunny flow - covered with UT
525 public Metadata getServiceMetadata() {
526 return toscaTemplate.getMetaData();
530 //Sunny flow - covered with UT
531 public Map<String, Object> getServiceMetadataProperties() {
532 if (toscaTemplate.getMetaData() == null){
535 return new HashMap<>(toscaTemplate.getMetaData().getAllProperties());
539 public Map<String, String> getServiceMetadataAllProperties() {
540 if (toscaTemplate.getMetaData() == null){
543 return toscaTemplate.getMetaData().getAllProperties();
547 //Sunny flow - covered with UT
548 public List<Input> getServiceInputs() {
549 return toscaTemplate.getInputs();
553 //Sunny flow - covered with UT
554 public String getGroupPropertyLeafValue(Group group, String leafValuePath) {
556 log.error("getGroupPropertyLeafValue - group is null");
560 if (GeneralUtility.isEmptyString(leafValuePath)) {
561 log.error("getGroupPropertyLeafValue - leafValuePath is null or empty");
565 String[] split = getSplittedPath(leafValuePath);
566 LinkedHashMap<String, Property> properties = group.getProperties();
567 Object property = processProperties(split, properties);
568 return property == null || property instanceof Function? null : String.valueOf(property);
572 public Object getGroupPropertyAsObject(Group group, String leafValuePath) {
574 log.error("getGroupPropertyAsObject - group is null");
578 if (GeneralUtility.isEmptyString(leafValuePath)) {
579 log.error("getGroupPropertyAsObject - leafValuePath is null or empty");
583 String[] split = getSplittedPath(leafValuePath);
584 LinkedHashMap<String, Property> properties = group.getProperties();
585 return processProperties(split, properties);
589 //Sunny flow - covered with UT
590 public List<NodeTemplate> getCpListByVf(String vfCustomizationId) {
591 List<NodeTemplate> cpList = new ArrayList<>();
592 if (GeneralUtility.isEmptyString(vfCustomizationId)) {
593 log.error("getCpListByVf vfCustomizationId string is empty");
597 List<NodeTemplate> serviceVfList = getServiceVfList();
598 if (serviceVfList == null || serviceVfList.isEmpty()) {
599 log.error("getCpListByVf Vfs not exist for vfCustomizationId {}", vfCustomizationId);
602 NodeTemplate vfInstance = getNodeTemplateByCustomizationUuid(serviceVfList, vfCustomizationId);
603 if (vfInstance == null) {
604 log.debug("getCpListByVf vf list is null");
607 cpList = getNodeTemplateBySdcType(vfInstance, SdcTypes.CP);
608 if (cpList == null || cpList.isEmpty())
609 log.debug("getCpListByVf cps not exist for vfCustomizationId {}", vfCustomizationId);
614 //Sunny flow - covered with UT
615 public List<NodeTemplate> getMembersOfVfModule(NodeTemplate vf, Group serviceLevelVfModule) {
617 log.error("getMembersOfVfModule - vf is null");
618 return new ArrayList<>();
621 if (serviceLevelVfModule == null || serviceLevelVfModule.getMetadata() == null || serviceLevelVfModule.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID) == null) {
622 log.error("getMembersOfVfModule - vfModule or its metadata is null. Cannot match a VF group based on invariantUuid from missing metadata.");
623 return new ArrayList<>();
627 SubstitutionMappings substitutionMappings = vf.getSubMappingToscaTemplate();
628 if (substitutionMappings != null) {
629 List<Group> groups = substitutionMappings.getGroups();
630 if (groups != null) {
631 Optional<Group> findFirst = groups
633 .filter(x -> (x.getMetadata() != null && serviceLevelVfModule.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID).equals(x.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID)))).findFirst();
634 if (findFirst.isPresent()) {
635 List<String> members = findFirst.get().getMembers();
636 if (members != null) {
637 return substitutionMappings.getNodeTemplates().stream().filter(x -> members.contains(x.getName())).collect(toList());
642 return new ArrayList<>();
646 public List<Pair<NodeTemplate, NodeTemplate>> getNodeTemplatePairsByReqName(
647 List<NodeTemplate> listOfReqNodeTemplates, List<NodeTemplate> listOfCapNodeTemplates, String reqName) {
649 if (listOfReqNodeTemplates == null) {
650 log.error("getNodeTemplatePairsByReqName - listOfReqNodeTemplates is null");
651 return new ArrayList<>();
654 if (listOfCapNodeTemplates == null) {
655 log.error("getNodeTemplatePairsByReqName - listOfCapNodeTemplates is null");
656 return new ArrayList<>();
659 if (GeneralUtility.isEmptyString(reqName)) {
660 log.error("getNodeTemplatePairsByReqName - reqName is null or empty");
661 return new ArrayList<>();
664 List<Pair<NodeTemplate, NodeTemplate>> pairsList = new ArrayList<>();
666 for (NodeTemplate reqNodeTemplate : listOfReqNodeTemplates) {
667 List<RequirementAssignment> requirements = reqNodeTemplate.getRequirements().getRequirementsByName(reqName).getAll();
668 for (RequirementAssignment reqEntry : requirements) {
669 String node = reqEntry.getNodeTemplateName();
671 Optional<NodeTemplate> findFirst = listOfCapNodeTemplates.stream().filter(x -> x.getName().equals(node)).findFirst();
672 if (findFirst.isPresent()) {
673 pairsList.add(new ImmutablePair<NodeTemplate, NodeTemplate>(reqNodeTemplate, findFirst.get()));
683 //Sunny flow - covered with UT
684 public List<NodeTemplate> getAllottedResources() {
685 List<NodeTemplate> nodeTemplates = null;
686 nodeTemplates = toscaTemplate.getTopologyTemplate().getNodeTemplates();
687 if (nodeTemplates.isEmpty()) {
688 log.error("getAllottedResources nodeTemplates not exist");
690 nodeTemplates = nodeTemplates.stream().filter(
691 x -> x.getMetaData() != null && x.getMetaData().getValue("category").equals("Allotted Resource"))
693 if (nodeTemplates.isEmpty()) {
694 log.debug("getAllottedResources - allotted resources not exist");
696 return nodeTemplates;
700 //Sunny flow - covered with UT
701 public String getTypeOfNodeTemplate(NodeTemplate nodeTemplate) {
702 if (nodeTemplate == null) {
704 log.error("getTypeOfNodeTemplate nodeTemplate is null");
707 return nodeTemplate.getTypeDefinition().getType();
711 * This methdd is returning the csarConformanceLevel for input CSAR
712 * When csarConformanceLevel is configured with failOnError as False in Error Configuration; it
713 * assigns the default value to csarConformanceLevel which is the max level provided in
715 * @return csarConformanceLevel
718 public String getConformanceLevel() {
719 LinkedHashMap<String, Object> csarMeta = toscaTemplate.getMetaProperties("csar.meta");
720 if (csarMeta == null){
721 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.");
722 if (configurationManager != null && !configurationManager.getErrorConfiguration()
723 .getErrorInfo("CONFORMANCE_LEVEL_ERROR").getFailOnError()){
724 String csarConLevel = configurationManager.getConfiguration().getConformanceLevel().getMaxVersion();
725 log.warn("csarConformanceLevel is not found in input csar; defaulting to max version {}" , csarConLevel);
729 log.warn("csarConformanceLevel is not found in input csar; returning null as no defaults defined in error configuration");
734 Object conformanceLevel = csarMeta.get("SDC-TOSCA-Definitions-Version");
735 if (conformanceLevel != null){
736 String confLevelStr = conformanceLevel.toString();
737 log.debug("CSAR conformance level is {}", confLevelStr);
740 log.error("Invalid csar.meta file - no entry found for SDC-TOSCA-Definitions-Version key. This entry should hold the conformance level.");
747 public String getNodeTemplateCustomizationUuid(NodeTemplate nt) {
749 if (nt != null && nt.getMetaData() != null){
750 res = nt.getMetaData().getValue(CUSTOMIZATION_UUID);
752 log.error("Node template or its metadata is null");
757 public List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType) {
758 return getNodeTemplateBySdcType(parentNodeTemplate, sdcType, false);
761 private List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType, boolean isVNF) {
763 if (parentNodeTemplate == null) {
764 log.error("getNodeTemplateBySdcType - nodeTemplate is null or empty");
765 return new ArrayList<>();
768 if (sdcType == null) {
769 log.error("getNodeTemplateBySdcType - sdcType is null or empty");
770 return new ArrayList<>();
773 SubstitutionMappings substitutionMappings = parentNodeTemplate.getSubMappingToscaTemplate();
775 if (substitutionMappings != null) {
776 List<NodeTemplate> nodeTemplates = substitutionMappings.getNodeTemplates();
777 if (nodeTemplates != null && !nodeTemplates.isEmpty()) {
778 if (sdcType.equals(SdcTypes.VFC) && isVNF) {
779 return nodeTemplates.stream()
780 .filter(x -> (x.getMetaData() != null &&
781 sdcType.getValue().equals(x.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE))) && isVNFType(x))
785 return nodeTemplates.stream()
786 .filter(x -> (x.getMetaData() != null &&
787 sdcType.getValue().equals(x.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE))) && !isVNFType(x))
792 log.debug("getNodeTemplateBySdcType - SubstitutionMappings' node Templates not exist");
795 log.debug("getNodeTemplateBySdcType - SubstitutionMappings not exist");
797 return new ArrayList<>();
800 public Map<String, String> filterNodeTemplatePropertiesByValue(NodeTemplate nodeTemplate, FilterType filterType, String pattern) {
801 Map<String, String> filterMap = new HashMap<>();
803 if (nodeTemplate == null) {
804 log.error("filterNodeTemplatePropertiesByValue nodeTemplate is null");
808 if (filterType == null) {
809 log.error("filterNodeTemplatePropertiesByValue filterType is null");
813 if (GeneralUtility.isEmptyString(pattern)) {
814 log.error("filterNodeTemplatePropertiesByValue pattern string is empty");
818 Map<String, Property> ntProperties = nodeTemplate.getProperties();
820 if (ntProperties != null && ntProperties.size() > 0) {
822 for (Property current : ntProperties.values()) {
823 if (current.getValue() != null) {
824 filterProperties(current.getValue(), current.getName(), filterType, pattern, filterMap);
829 log.trace("filterNodeTemplatePropertiesByValue - filterMap value: {}", filterMap);
834 public NodeTemplate getVnfConfig(String vfCustomizationUuid) {
836 if (GeneralUtility.isEmptyString(vfCustomizationUuid)) {
837 log.error("getVnfConfig - vfCustomizationId - is null or empty");
841 List<NodeTemplate> serviceVfList = getServiceVfList();
842 NodeTemplate vfInstance = getNodeTemplateByCustomizationUuid(serviceVfList, vfCustomizationUuid);
843 return getNodeTemplateBySdcType(vfInstance, SdcTypes.VFC, true).stream().findAny().orElse(null);
847 public boolean hasTopology(NodeTemplate nodeTemplate) {
848 if (nodeTemplate == null) {
849 log.error("hasTopology - nodeTemplate - is null");
853 if (nodeTemplate.getMetaData() != null) {
854 String type = nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE);
855 log.debug("hasTopology - node template {} is a {} type", nodeTemplate.getName(), type);
856 return SdcTypes.isComplex(type);
863 public List<NodeTemplate> getNodeTemplateChildren(NodeTemplate nodeTemplate) {
864 if (nodeTemplate == null) {
865 log.error("getNodeTemplateChildren - nodeTemplate - is null");
866 return new ArrayList<>();
869 SubstitutionMappings substitutionMappings = nodeTemplate.getSubMappingToscaTemplate();
870 if (substitutionMappings != null) {
871 List<NodeTemplate> nodeTemplates = substitutionMappings.getNodeTemplates();
872 if (nodeTemplates != null && !nodeTemplates.isEmpty()) {
874 return nodeTemplates.stream()
875 .filter(x -> !isVNFType(x))
879 log.debug("getNodeTemplateChildren - SubstitutionMappings' node Templates not exist");
882 log.debug("getNodeTemplateChildren - SubstitutionMappings not exist");
884 return new ArrayList<>();
888 public NodeTemplate getServiceNodeTemplateByNodeName(String nodeName) {
889 if (GeneralUtility.isEmptyString(nodeName)) {
890 log.error("getServiceNodeTemplateByNodeName - nodeName - is null or empty");
894 List<NodeTemplate> nodeTemplates = getServiceNodeTemplates();
895 Optional<NodeTemplate> findFirst = nodeTemplates.stream().filter(nt -> nt.getName().equals(nodeName)).findFirst();
897 return findFirst.isPresent() ? findFirst.get() : null;
901 public Metadata getNodeTemplateMetadata(NodeTemplate nt) {
903 log.error("getNodeTemplateMetadata - nt (node template) - is null");
907 return nt.getMetaData();
911 public CapabilityAssignments getCapabilitiesOf(NodeTemplate nt) {
913 log.error("getCapabilitiesOf - nt (node template) - is null");
917 return nt.getCapabilities();
921 public RequirementAssignments getRequirementsOf(NodeTemplate nt) {
923 log.error("getRequirementsOf - nt (node template) - is null");
927 return nt.getRequirements();
931 public String getCapabilityPropertyLeafValue(CapabilityAssignment capability, String pathToPropertyLeafValue) {
932 if (capability == null) {
933 log.error("getCapabilityPropertyLeafValue - capability is null");
937 if (GeneralUtility.isEmptyString(pathToPropertyLeafValue)) {
938 log.error("getCapabilityPropertyLeafValue - pathToPropertyLeafValue is null or empty");
942 String[] split = getSplittedPath(pathToPropertyLeafValue);
943 LinkedHashMap<String, Property> properties = capability.getProperties();
944 Object property = processProperties(split, properties);
945 return property == null || property instanceof Function ? null : String.valueOf(property);
949 public ArrayList<Group> getGroupsOfOriginOfNodeTemplate(NodeTemplate nodeTemplate) {
950 if(StringUtils.isNotEmpty(nodeTemplate.getName())){
951 return getNodeTemplateByName(nodeTemplate.getName()).getSubMappingToscaTemplate().getGroups();
953 return new ArrayList<>();
957 public ArrayList<Group> getGroupsOfTopologyTemplateByToscaGroupType(String groupType) {
958 if(toscaTemplate.getGroups() == null)
959 return new ArrayList<>();
960 return (ArrayList<Group>) toscaTemplate.getGroups()
962 .filter(g->g.getType().equals(groupType))
963 .sorted(Group::compareTo)
968 public ArrayList<Group> getGroupsOfTopologyTemplate() {
969 return toscaTemplate.getGroups() == null ? new ArrayList<>() : toscaTemplate.getGroups();
973 public ArrayList<Group> getGroupsOfOriginOfNodeTemplateByToscaGroupType(NodeTemplate nodeTemplate, String groupType) {
974 return (ArrayList<Group>) getGroupsOfOriginOfNodeTemplate(nodeTemplate)
976 .filter(g->g.getType().equals(groupType))
977 .sorted(Group::compareTo)
982 public List<NodeTemplate> getGroupMembersFromTopologyTemplate(String groupName) {
983 if(toscaTemplate.getNodeTemplates() == null){
984 return new ArrayList<>();
986 List<String> membersNames = getGroupMembers(groupName);
987 return toscaTemplate.getNodeTemplates().stream()
988 .filter(nt->membersNames.contains(nt.getName()))
994 public List<NodeTemplate> getGroupMembersOfOriginOfNodeTemplate(NodeTemplate nodeTemplate, String groupName) {
995 ArrayList<Group> groups = getGroupsOfOriginOfNodeTemplate(nodeTemplate);
996 if(!groups.isEmpty()){
997 Optional<Group> group = groups.stream().filter(g -> g.getName().equals(groupName)).findFirst();
998 if(group.isPresent()){
999 return nodeTemplate.getSubMappingToscaTemplate().getNodeTemplates().stream()
1000 .filter(nt -> group.get().getMembers().contains(nt.getName()))
1004 return new ArrayList<>();
1007 public List<NodeTemplate> getServiceNodeTemplateBySdcType(SdcTypes sdcType) {
1008 if (sdcType == null) {
1009 log.error("getServiceNodeTemplateBySdcType - sdcType is null or empty");
1010 return new ArrayList<>();
1013 TopologyTemplate topologyTemplate = toscaTemplate.getTopologyTemplate();
1014 return getNodeTemplateBySdcType(topologyTemplate, sdcType);
1017 /************************************* helper functions ***********************************/
1018 private boolean isVNFType(NodeTemplate nt) {
1019 return nt.getType().endsWith("VnfConfiguration");
1022 @SuppressWarnings("unchecked")
1023 private Map<String, String> filterProperties(Object property, String path, FilterType filterType, String pattern, Map<String, String> filterMap) {
1025 if (property instanceof Map) {
1026 for (Map.Entry<String, Object> item: ((Map<String, Object>) property).entrySet()) {
1027 String itemPath = path + PATH_DELIMITER + item.getKey();
1028 filterProperties(item.getValue(), itemPath, filterType, pattern, filterMap);
1030 } else if (property instanceof List) {
1031 for (Object item: (List<Object>)property) {
1032 filterProperties(item, path, filterType, pattern, filterMap);
1035 if (filterType.isMatch(property.toString(), pattern)) {
1036 filterMap.put(path, property.toString());
1043 /************************************* helper functions ***********************************/
1044 private List<NodeTemplate> getNodeTemplateBySdcType(TopologyTemplate topologyTemplate, SdcTypes sdcType) {
1045 if (sdcType == null) {
1046 log.error("getNodeTemplateBySdcType - sdcType is null or empty");
1047 return new ArrayList<>();
1050 if (topologyTemplate == null) {
1051 log.error("getNodeTemplateBySdcType - topologyTemplate is null");
1052 return new ArrayList<>();
1055 List<NodeTemplate> nodeTemplates = topologyTemplate.getNodeTemplates();
1057 if (nodeTemplates != null && !nodeTemplates.isEmpty())
1058 return nodeTemplates.stream().filter(x -> (x.getMetaData() != null && sdcType.getValue().equals(x.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)))).collect(toList());
1060 log.debug("getNodeTemplateBySdcType - topologyTemplate's nodeTemplates not exist");
1061 return new ArrayList<>();
1064 //Assumed to be unique property for the list
1065 private NodeTemplate getNodeTemplateByCustomizationUuid(List<NodeTemplate> nodeTemplates, String customizationId) {
1066 if (customizationId != null) {
1067 Optional<NodeTemplate> findFirst = nodeTemplates.stream().filter(x -> (x.getMetaData() != null && customizationId.equals(x.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)))).findFirst();
1068 return findFirst.isPresent() ? findFirst.get() : null;
1071 log.error("getNodeTemplateByCustomizationUuid - customizationId is null");
1076 private Object processProperties(String[] split, LinkedHashMap<String, Property> properties) {
1077 Optional<Entry<String, Property>> findFirst = properties.entrySet().stream().filter(x -> x.getKey().equals(split[0])).findFirst();
1078 if (findFirst.isPresent()) {
1079 Property property = findFirst.get().getValue();
1080 Object current = property.getValue();
1081 return iterateProcessPath(1, current, split);
1083 String propName = (split != null && split.length > 0 ? split[0] : null);
1084 log.error("processProperties - property {} not found", propName);
1089 public Map<String, List<InterfacesDef>> getInterfacesOf(NodeTemplate nt){
1093 return nt.getAllInterfaceDetailsForNodeType();
1097 public List<String> getInterfaces(NodeTemplate nt){
1098 Map<String, List<InterfacesDef>> interfaceDetails = nt.getAllInterfaceDetailsForNodeType();
1099 return new ArrayList<>(interfaceDetails.keySet());
1103 public List<InterfacesDef> getInterfaceDetails(NodeTemplate nt, String interfaceName){
1104 Map<String, List<InterfacesDef>> interfaceDetails = nt.getAllInterfaceDetailsForNodeType();
1105 return interfaceDetails.get(interfaceName);
1109 public List<String> getAllInterfaceOperations(NodeTemplate nt, String interfaceName){
1110 Map<String, List<InterfacesDef>> interfaceDetails = nt.getAllInterfaceDetailsForNodeType();
1111 return interfaceDetails.values().stream().flatMap(List::stream).map(val -> val.getOperationName()).collect(
1112 Collectors.toList());
1116 public InterfacesDef getInterfaceOperationDetails(NodeTemplate nt, String interfaceName, String operationName){
1117 Map<String, List<InterfacesDef>> interfaceDetails = nt.getAllInterfaceDetailsForNodeType();
1118 if(!interfaceDetails.isEmpty()){
1119 List<InterfacesDef> interfaceDefs = interfaceDetails.get(interfaceName);
1120 return interfaceDefs.stream().filter(val -> val.getOperationName().equals(operationName)).findFirst().orElse(null);
1126 public List<Input> getInputsWithAnnotations() {
1127 return toscaTemplate.getInputs(true);