diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ResourceTranslationContrailV2VirtualNetworkImpl.java')
1 files changed, 158 insertions, 155 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ResourceTranslationContrailV2VirtualNetworkImpl.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ResourceTranslationContrailV2VirtualNetworkImpl.java index 5bc36c242e..f2addd0ffe 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ResourceTranslationContrailV2VirtualNetworkImpl.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ResourceTranslationContrailV2VirtualNetworkImpl.java @@ -1,35 +1,42 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2018 European Support Limited + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * ============LICENSE_END========================================================= */ package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation; +import static org.openecomp.sdc.tosca.services.DataModelUtil.createAttachmentRequirementAssignment; +import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_INVALID_NETWORK_POLICY_REFS_RESOURCE; +import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_INVALID_PROPERTY_FORMAT_GET_ATTR_FQ_NAME; +import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_INVALID_PROPERTY_FORMAT_GET_RESOURCE; +import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_INVALID_PROPERTY_VALUE_FORMAT; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; + import org.apache.commons.collections.CollectionUtils; +import org.onap.sdc.tosca.datatypes.model.NodeTemplate; import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes; import org.openecomp.sdc.heat.datatypes.model.Resource; +import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType; import org.openecomp.sdc.tosca.datatypes.ToscaNodeType; -import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType; -import org.onap.sdc.tosca.datatypes.model.NodeTemplate; -import org.onap.sdc.tosca.datatypes.model.RequirementAssignment; import org.openecomp.sdc.tosca.services.DataModelUtil; import org.openecomp.sdc.tosca.services.ToscaConstants; import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId; @@ -39,164 +46,160 @@ import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil; import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory; import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Optional; - public class ResourceTranslationContrailV2VirtualNetworkImpl extends ResourceTranslationBase { - protected static Logger logger = - (Logger) LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class); - - @Override - public void translate(TranslateTo translateTo) { - NodeTemplate nodeTemplate = new NodeTemplate(); - nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VIRTUAL_NETWORK); - nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter - .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(),translateTo. - getResourceId(),translateTo.getResource().getProperties(), - nodeTemplate.getProperties(), translateTo.getHeatFileName(), - translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(), - nodeTemplate, translateTo.getContext())); - DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(), - nodeTemplate); - linkToPolicyNodeTemplate(translateTo); - } - - private void linkToPolicyNodeTemplate(TranslateTo translateTo) { - List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo); - if (CollectionUtils.isEmpty(networkPolicyIdList)) { - return; - } - for (AttachedResourceId attachedResourceId : networkPolicyIdList) { - NodeTemplate policyNodeTemplate = DataModelUtil - .getNodeTemplate(translateTo.getServiceTemplate(), - (String) attachedResourceId.getTranslatedId()); - DataModelUtil - .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID, - createRequirementAssignment(translateTo.getTranslatedId())); - } - } - - private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) { - Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs"); - if (propertyValue != null) { - return extractNetworkPolicyId(propertyValue, translateTo); - } else { - return null; + private static final String FQ_NAME = "fq_name"; + protected static Logger logger = LoggerFactory.getLogger(ResourceTranslationContrailV2VirtualNetworkImpl.class); + + @Override + public void translate(TranslateTo translateTo) { + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VIRTUAL_NETWORK); + nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter + .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(), + translateTo.getResourceId(), translateTo.getResource().getProperties(), + nodeTemplate.getProperties(), translateTo.getHeatFileName(), + translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(), + nodeTemplate, translateTo.getContext())); + DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(), + nodeTemplate); + linkToPolicyNodeTemplate(translateTo); } - } - - private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue, - TranslateTo translateTo) { - List<AttachedResourceId> attachedResourceIdList = new ArrayList<>(); - - if (propertyValue instanceof List) { - for (Object value : (List) propertyValue) { - attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo)); - } - } else { - AttachedResourceId resourceId = parsNetworkPolicyId(propertyValue, translateTo); - if (resourceId != null) { - attachedResourceIdList.add(resourceId); - } + + private void linkToPolicyNodeTemplate(TranslateTo translateTo) { + List<AttachedResourceId> networkPolicyIdList = extractNetworkPolicyIdList(translateTo); + if (CollectionUtils.isEmpty(networkPolicyIdList)) { + return; + } + for (AttachedResourceId attachedResourceId : networkPolicyIdList) { + NodeTemplate policyNodeTemplate = DataModelUtil.getNodeTemplate(translateTo.getServiceTemplate(), + (String) attachedResourceId.getTranslatedId()); + DataModelUtil.addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID, + createAttachmentRequirementAssignment(translateTo.getTranslatedId())); + } } - return attachedResourceIdList; - } - - private AttachedResourceId parsNetworkPolicyId(Object propertyValue, TranslateTo translateTo) { - Optional<String> translatedPolicyResourceId; - String policyResourceId = extractResourceId(propertyValue, translateTo); - if (policyResourceId == null) { - return null; + + private List<AttachedResourceId> extractNetworkPolicyIdList(TranslateTo translateTo) { + Object propertyValue = translateTo.getResource().getProperties().get("network_policy_refs"); + if (propertyValue != null) { + return extractNetworkPolicyId(propertyValue, translateTo); + } + return Collections.emptyList(); } - Resource policyResource = HeatToToscaUtil - .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId, - translateTo.getHeatFileName()); - if (!policyResource.getType() - .equals(HeatResourcesTypes.CONTRAIL_V2_NETWORK_RULE_RESOURCE_TYPE.getHeatResource())) { - return null; + private List<AttachedResourceId> extractNetworkPolicyId(Object propertyValue, + TranslateTo translateTo) { + List<AttachedResourceId> attachedResourceIdList = new ArrayList<>(); + if (propertyValue instanceof List) { + for (Object value : (List) propertyValue) { + attachedResourceIdList.addAll(extractNetworkPolicyId(value, translateTo)); + } + } else { + AttachedResourceId resourceId = parseNetworkPolicyId(propertyValue, translateTo); + if (resourceId != null) { + attachedResourceIdList.add(resourceId); + } + } + return attachedResourceIdList; } - translatedPolicyResourceId = ResourceTranslationFactory.getInstance(policyResource) - .translateResource(translateTo.getHeatFileName(), translateTo.getServiceTemplate(), - translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId, - translateTo.getContext()); - if (!translatedPolicyResourceId.isPresent()) { - logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '" - + translateTo.getResource().getType() - + "' property network_policy_refs is referenced to an unsupported resource the " - + "connection will be ignored in TOSCA translation."); - return null; + + private AttachedResourceId parseNetworkPolicyId(Object propertyValue, TranslateTo translateTo) { + Optional<String> translatedPolicyResourceId; + String policyResourceId = extractResourceId(propertyValue, translateTo); + if (policyResourceId == null) { + return null; + } + + Resource policyResource = HeatToToscaUtil.getResource(translateTo.getHeatOrchestrationTemplate(), + policyResourceId, translateTo.getHeatFileName()); + if (!policyResource.getType().equals(HeatResourcesTypes.CONTRAIL_V2_NETWORK_RULE_RESOURCE_TYPE + .getHeatResource())) { + return null; + } + translatedPolicyResourceId = ResourceTranslationFactory.getInstance(policyResource) + .translateResource(translateTo.getHeatFileName(), translateTo.getServiceTemplate(), + translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId, + translateTo.getContext()); + if (!translatedPolicyResourceId.isPresent()) { + logger.warn(LOG_INVALID_NETWORK_POLICY_REFS_RESOURCE, + translateTo.getResourceId(), translateTo.getResource().getType()); + return null; + } + return new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId, ReferenceType.GET_ATTR); } - AttachedResourceId attachedResourceId = - new AttachedResourceId(translatedPolicyResourceId.get(), policyResourceId, - ReferenceType.GET_ATTR); - return attachedResourceId; - } - - private String extractResourceId(Object propertyValue, TranslateTo translateTo) { - - Object value; - if (propertyValue instanceof Map) { - if (((Map) propertyValue).containsKey("get_attr")) { - value = ((Map) propertyValue).get("get_attr"); - if (value instanceof List) { - if (((List) value).size() == 2 && ((List) value).get(1).equals("fq_name")) { - if (((List) value).get(0) instanceof String) { - return (String) ((List) value).get(0); - } else { - logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '" - + translateTo.getResource().getType() - + "' has property with invalid format of 'get_attr' function with 'fq_name' " - + "value, therefore this property will be ignored in TOSCA translation."); + + private String extractResourceId(Object propertyValue, TranslateTo translateTo) { + if (propertyValue instanceof Map) { + return extractResourceIdFromMapProperty((Map) propertyValue, translateTo); + } else if (propertyValue instanceof List) { + String resourceId = extractResourceIdFromListProperty((List) propertyValue, translateTo); + if (resourceId != null) { + return resourceId; } - } } - } else if (((Map) propertyValue).containsKey("get_resource")) { - value = ((Map) propertyValue).get("get_resource"); - if (value instanceof String) { - return (String) value; + logger.warn(LOG_INVALID_PROPERTY_VALUE_FORMAT, translateTo.getResourceId(), + translateTo.getResource().getType()); + return null; + } + + private String extractResourceIdFromMapProperty(Map propertyValue, TranslateTo translateTo) { + Object value; + String resourceId = null; + if (propertyValue.containsKey(ResourceReferenceFunctions.GET_ATTR.getFunction())) { + value = propertyValue.get(ResourceReferenceFunctions.GET_ATTR.getFunction()); + if (value instanceof List && extractResourceIdFromGetAttrList(translateTo, (List) value)) { + resourceId = (String) ((List) value).get(0); + } + } else if (propertyValue.containsKey(ResourceReferenceFunctions.GET_RESOURCE.getFunction())) { + resourceId = extractResourceIdFromGetResource(propertyValue, translateTo); } else { - logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '" - + translateTo.getResource().getType() - + "' has property invalid format of 'get_resource' function, therefore this property" - + " will be ignored in TOSCA translation."); + resourceId = extractResourceIdFromPropertyValues(propertyValue, translateTo); } - } else { - Collection<Object> valCollection = ((Map) propertyValue).values(); - for (Object entryValue : valCollection) { - String ret = extractResourceId(entryValue, translateTo); - if (ret != null) { - return ret; - } + return resourceId; + } + private boolean extractResourceIdFromGetAttrList(TranslateTo translateTo, List<Object> value) { + if (value.size() == 2 && FQ_NAME.equals(value.get(1))) { + if (value.get(0) instanceof String) { + return true; + } else { + logger.warn(LOG_INVALID_PROPERTY_FORMAT_GET_ATTR_FQ_NAME, translateTo.getResourceId(), + translateTo.getResource().getType()); + } } - } - } else if (propertyValue instanceof List) { - for (Object prop : (List) propertyValue) { - String ret = extractResourceId(prop, translateTo); - if (ret != null) { - return ret; + return false; + } + + private String extractResourceIdFromPropertyValues(Map propertyValue, TranslateTo translateTo) { + Collection<Object> valCollection = propertyValue.values(); + for (Object entryValue : valCollection) { + String ret = extractResourceId(entryValue, translateTo); + if (ret != null) { + return ret; + } } - } + return null; } - logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '" - + translateTo.getResource().getType() - + "' invalid format of property value, therefore this resource will be ignored in TOSCA " - + "translation."); - return null; - } - - private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) { - RequirementAssignment requirement = new RequirementAssignment(); - requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT); - requirement.setNode(translatedNetworkResourceId); - requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO); - return requirement; - } + private String extractResourceIdFromListProperty(List<Object> propertyValue, TranslateTo translateTo) { + for (Object prop : propertyValue) { + String resourceId = extractResourceId(prop, translateTo); + if (resourceId != null) { + return resourceId; + } + } + return null; + } + private String extractResourceIdFromGetResource(Map propertyValue, TranslateTo translateTo) { + Object value; + value = propertyValue.get(ResourceReferenceFunctions.GET_RESOURCE.getFunction()); + if (value instanceof String) { + return (String) value; + } else { + logger.warn(LOG_INVALID_PROPERTY_FORMAT_GET_RESOURCE, translateTo.getResourceId(), + translateTo.getResource().getType()); + } + return null; + } } |