/* * Copyright © 2016-2020 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. */ package org.openecomp.sdc.be.tosca; import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT; import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INPUTS; import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.OPERATIONS; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import org.apache.commons.collections.MapUtils; import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Product; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType; import org.openecomp.sdc.be.tosca.model.ToscaInput; import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType; import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil; import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class InterfacesOperationsConverter { private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard"; private static final String DERIVED_FROM_BASE_DEFAULT = "org.openecomp.interfaces.node.lifecycle."; private static final String DEFAULT_HAS_UNDERSCORE = "_default"; private static final String DOT = "."; private static final String DEFAULTP = "defaultp"; public static final String SELF = "SELF"; private static final String LOCAL_INTERFACE_TYPE = "Local"; private final PropertyConvertor propertyConvertor; @Autowired public InterfacesOperationsConverter(final PropertyConvertor propertyConvertor) { this.propertyConvertor = propertyConvertor; } /** * Creates the interface_types element. * * @param component to work on * @return the added element */ public static Map addInterfaceTypeElement(Component component, List allInterfaceTypes) { if (component instanceof Product) { return null; } final Map interfaces = component.getInterfaces(); if (MapUtils.isEmpty(interfaces)) { return null; } Map toscaInterfaceTypes = new HashMap<>(); for (InterfaceDefinition interfaceDefinition : interfaces.values()) { boolean isInterfaceTypeExistInGlobalType = allInterfaceTypes.stream().anyMatch(type -> type.equalsIgnoreCase(interfaceDefinition.getType())); if (!isInterfaceTypeExistInGlobalType) { ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType(); toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE); final Map operations = interfaceDefinition.getOperations(); Map toscaOperations = new HashMap<>(); for (Map.Entry operationEntry : operations.entrySet()) { toscaOperations.put(operationEntry.getValue().getName(), null); } toscaInterfaceType.setOperations(toscaOperations); Map interfacesAsMap = getObjectAsMap(toscaInterfaceType); Map operationsMap = (Map) interfacesAsMap.remove(OPERATIONS.getElementName()); interfacesAsMap.putAll(operationsMap); toscaInterfaceTypes.put(getInterfaceType(component, LOCAL_INTERFACE_TYPE), interfacesAsMap); } } return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null; } /** * Adds the 'interfaces' element to the node type provided. * * @param component to work on * @param nodeType to which the interfaces element will be added */ public void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType, Map dataTypes, boolean isAssociatedComponent) { if (component instanceof Product) { return; } final Map interfaces = component.getInterfaces(); if (MapUtils.isEmpty(interfaces)) { return; } Map toscaInterfaceDefinitions = getInterfacesMap(component, dataTypes, isAssociatedComponent); if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) { nodeType.setInterfaces(toscaInterfaceDefinitions); } } private Map getInterfacesMap(Component component, Map dataTypes, boolean isAssociatedComponent) { return getInterfacesMap(component, null, component.getInterfaces(), dataTypes, isAssociatedComponent, false); } public Map getInterfacesMap(final Component component, final ComponentInstance componentInstance, final Map interfaces, final Map dataTypes, final boolean isAssociatedComponent, final boolean isServiceProxyInterface) { if(MapUtils.isEmpty(interfaces)) { return null; } Map toscaInterfaceDefinitions = new HashMap<>(); for (InterfaceDefinition interfaceDefinition : interfaces.values()) { ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition(); String interfaceType; if(componentInstance != null && LOCAL_INTERFACE_TYPE.equals(interfaceDefinition.getType())) { interfaceType = DERIVED_FROM_BASE_DEFAULT + componentInstance.getSourceModelName(); } else { interfaceType = getInterfaceType(component, interfaceDefinition.getType()); } toscaInterfaceDefinition.setType(interfaceType); final Map operations = interfaceDefinition.getOperations(); Map toscaOperationMap = new HashMap<>(); String operationArtifactPath; for (Map.Entry operationEntry : operations.entrySet()) { ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition(); if (isArtifactPresent(operationEntry)) { operationArtifactPath = OperationArtifactUtil .createOperationArtifactPath(component, componentInstance, operationEntry.getValue(), isAssociatedComponent); toscaOperation.setImplementation(operationArtifactPath); } toscaOperation.setDescription(operationEntry.getValue().getDescription()); fillToscaOperationInputs(operationEntry.getValue(), dataTypes, toscaOperation, isServiceProxyInterface); toscaOperationMap.put(operationEntry.getValue().getName(), toscaOperation); } toscaInterfaceDefinition.setOperations(toscaOperationMap); final Map interfaceInputMap = createInterfaceInputMap(interfaceDefinition, dataTypes); if (!interfaceInputMap.isEmpty()) { toscaInterfaceDefinition.setInputs(interfaceInputMap); } Map interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition); if (interfaceDefAsMap.containsKey(INPUTS.getElementName())) { handleDefaults((Map) interfaceDefAsMap.get(INPUTS.getElementName())); } Map operationsMap = (Map) interfaceDefAsMap.remove(OPERATIONS.getElementName()); if (isServiceProxyInterface) { //Remove input type and copy default value directly into the proxy node template from the node type handleServiceProxyOperationInputValue(operationsMap, interfaceType); } else { handleDefaults(operationsMap); } interfaceDefAsMap.putAll(operationsMap); toscaInterfaceDefinitions.put(getLastPartOfName(interfaceType), interfaceDefAsMap); } return toscaInterfaceDefinitions; } private Map createInterfaceInputMap(final InterfaceDefinition interfaceDefinition, final Map allDataTypeMap) { final Map inputMap = interfaceDefinition.getInputs(); if (MapUtils.isEmpty(inputMap)) { return Collections.emptyMap(); } final Map toscaInterfaceInputMap = new HashMap<>(); for (final Entry inputEntry : inputMap.entrySet()) { final InputDataDefinition inputDataDefinition = inputEntry.getValue(); final ToscaProperty toscaProperty = propertyConvertor .convertProperty(allDataTypeMap, new PropertyDefinition(inputDataDefinition), PropertyType.INPUT); toscaInterfaceInputMap.put(inputEntry.getKey(), new ToscaInput(toscaProperty)); } return toscaInterfaceInputMap; } private static void handleServiceProxyOperationInputValue(Map operationsMap, String parentKey) { for (Map.Entry operationEntry : operationsMap.entrySet()) { final Object value = operationEntry.getValue(); final String key = operationEntry.getKey(); if (value instanceof Map) { if ("inputs".equals(parentKey)) { Object defaultValue = getDefaultValue((Map) value); operationsMap.put(key, defaultValue); } else { handleServiceProxyOperationInputValue((Map) value, key); } } } } private static Object getDefaultValue(Map inputValueMap) { Object defaultValue = null; for (Map.Entry operationEntry : inputValueMap.entrySet()) { final Object value = operationEntry.getValue(); if (value instanceof Map) { getDefaultValue((Map) value); } final String key = operationEntry.getKey(); if (key.equals(DEFAULTP)) { defaultValue = inputValueMap.remove(key); } } return defaultValue; } /* * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in * ToscaExportHandler so, any string Map key named "defaultp" will have its named changed to "default" * @param operationsMap the map to update */ private void handleDefaults(Map operationsMap) { for (Map.Entry operationEntry : operationsMap.entrySet()) { final Object value = operationEntry.getValue(); if (value instanceof Map) { handleDefaults((Map) value); } final String key = operationEntry.getKey(); if (key.equals(DEFAULTP)) { Object removed = operationsMap.remove(key); operationsMap.put(ToscaTagNamesEnum.DEFAULT.getElementName(), removed); } } } private static String getLastPartOfName(String toscaResourceName) { return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1); } private static boolean isArtifactPresent(Map.Entry operationEntry) { final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation()); if (isImplementationPresent) { return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName()); } return false; } private void fillToscaOperationInputs(OperationDataDefinition operation, Map dataTypes, ToscaLifecycleOperationDefinition toscaOperation, boolean isServiceProxyInterface) { if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) { toscaOperation.setInputs(null); return; } Map toscaInputs = new HashMap<>(); for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) { ToscaProperty toscaInput = new ToscaProperty(); toscaInput.setDescription(input.getDescription()); toscaInput.setType(input.getType()); toscaInput.setRequired(input.isRequired()); if (isServiceProxyInterface) { String inputValue = Objects.nonNull(input.getValue()) ? getInputValue(input.getValue()) : getInputValue(input.getToscaDefaultValue()); toscaInput.setDefaultp(propertyConvertor.convertToToscaObject(input, inputValue, dataTypes, false)); } else { toscaInput.setDefaultp(propertyConvertor .convertToToscaObject(input, getInputValue(input.getToscaDefaultValue()), dataTypes, false)); } toscaInputs.put(input.getName(), toscaInput); } toscaOperation.setInputs(toscaInputs); } private static String getInputValue(String inputValue) { String toscaInputValue = inputValue; if (Objects.nonNull(inputValue) && inputValue.contains(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) { Gson gson = new Gson(); Map> consumptionValue = gson.fromJson(inputValue, Map.class); List mappedOutputValue = consumptionValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); //Extract the interface name from the interface type String interfaceType = mappedOutputValue.get(1); String interfaceName = interfaceType.substring(interfaceType.lastIndexOf('.') + 1); mappedOutputValue.remove(1); mappedOutputValue.add(1, interfaceName); toscaInputValue = gson.toJson(consumptionValue); } return toscaInputValue; } private static String getInterfaceType(Component component, String interfaceType) { if (LOCAL_INTERFACE_TYPE.equals(interfaceType)) { return DERIVED_FROM_BASE_DEFAULT + component.getComponentMetadataDefinition() .getMetadataDataDefinition().getSystemName(); } return interfaceType; } private static Map getObjectAsMap(Object obj) { ObjectMapper objectMapper = new ObjectMapper(); if (obj instanceof ToscaInterfaceDefinition) { //Prevent empty field serialization in interface definition objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); } Map objectAsMap = obj instanceof Map ? (Map) obj : objectMapper.convertValue(obj, Map.class); final String defaultEntry = DEFAULT.getElementName(); if (objectAsMap.containsKey(defaultEntry)) { objectAsMap.put(DEFAULT_HAS_UNDERSCORE, objectAsMap.remove(defaultEntry)); } return objectAsMap; } }