From 6d7284b5bcbbf6f57b843c5f30a50e9387b5ece8 Mon Sep 17 00:00:00 2001 From: shiria Date: Mon, 2 Jul 2018 11:10:56 +0300 Subject: Fix getFlat for datatype Fix getFlat when we driven from primitive type like string Adding support for service filter as tosca extension Issue-ID: SDC-1455 Change-Id: I57f05af13b394239ca55b71a946d69f56675bf19 Signed-off-by: shiria --- .../onap/sdc/tosca/datatypes/model/NodeFilter.java | 89 ++++++++++++---- .../model/extension/RequirementAssignment.java | 31 ++++++ .../datatypes/model/extension/ServiceFilter.java | 34 +++++++ .../sdc/tosca/error/ToscaRuntimeException.java | 32 ++++++ .../sdc/tosca/services/ToscaExtensionYamlUtil.java | 112 ++++++++++++--------- 5 files changed, 229 insertions(+), 69 deletions(-) create mode 100644 common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/RequirementAssignment.java create mode 100644 common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/ServiceFilter.java create mode 100644 common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/error/ToscaRuntimeException.java (limited to 'common') diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeFilter.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeFilter.java index d1d07b56e6..5f3d46504d 100644 --- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeFilter.java +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeFilter.java @@ -1,9 +1,6 @@ -/*- - * ============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 @@ -15,33 +12,83 @@ * 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.onap.sdc.tosca.datatypes.model; +import org.apache.commons.collections4.CollectionUtils; +import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; + +import java.util.Iterator; import java.util.List; import java.util.Map; public class NodeFilter { - List>> properties; - List> capabilities; + private List>> properties; + private List> capabilities; + + //can't not be removed, in used in snake yaml + public List> getCapabilities() { + return capabilities; + } + + public void setCapabilities(List> capabilities) { + this.capabilities = capabilities; + } + + //can't not be removed, in used in snake yaml + public List>> getProperties() { + return properties; + } + - public List> getCapabilities() { - return capabilities; - } + //use this function in order to get node filter properties instead of getProperties function + public List>> getNormalizeProperties() { + return getNormalizeProperties(properties); + } - public void setCapabilities(List> capabilities) { - this.capabilities = capabilities; - } + private List>> getNormalizeProperties(List>> properties) { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + if (CollectionUtils.isEmpty(properties)) { + return properties; + } + for (Map> propertyConstraintsEntity : properties) { + String propertyKey = propertyConstraintsEntity.keySet().iterator().next(); + List constraints = propertyConstraintsEntity.get(propertyKey); + Iterator iterator = constraints.iterator(); + while (iterator.hasNext()) { + Constraint constraintObj = iterator.next(); + Constraint constraint = toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(constraintObj), + Constraint.class); + constraints.remove(constraintObj); + constraints.add(constraint); + } + } + return properties; + } - public List>> getProperties() { - return properties; - } + //use this function in order to get node filter capabilities instead of getCapabilities function + public List> getNormalizeCapabilities() { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + if (CollectionUtils.isEmpty(capabilities)) { + return capabilities; + } + for (Map capabilityEntry : capabilities) { + String capabilityKey = capabilityEntry.keySet().iterator().next(); + Object capabilityFilterObj = capabilityEntry.get(capabilityKey); + CapabilityFilter capabilityFilter = toscaExtensionYamlUtil.yamlToObject( + toscaExtensionYamlUtil.objectToYaml(capabilityFilterObj), CapabilityFilter.class); + capabilityFilter.setProperties(getNormalizeProperties(capabilityFilter.getProperties())); + capabilityEntry.remove(capabilityKey); + capabilityEntry.put(capabilityKey, capabilityFilter); + } + return capabilities; + } - public void setProperties(List>> properties) { - this.properties = properties; - } + public void setProperties(List>> properties) { + this.properties = properties; + } } diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/RequirementAssignment.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/RequirementAssignment.java new file mode 100644 index 0000000000..07aed2b466 --- /dev/null +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/RequirementAssignment.java @@ -0,0 +1,31 @@ +/* + * 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. + */ + +package org.onap.sdc.tosca.datatypes.model.extension; + + +public class RequirementAssignment extends org.onap.sdc.tosca.datatypes.model.RequirementAssignment{ + private ServiceFilter service_filter; + + + public ServiceFilter getService_filter() { + return service_filter; + } + + public void setService_filter(ServiceFilter servicefilter) { + this.service_filter = servicefilter; + } +} diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/ServiceFilter.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/ServiceFilter.java new file mode 100644 index 0000000000..8423d5e293 --- /dev/null +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/ServiceFilter.java @@ -0,0 +1,34 @@ +/* + * 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. + */ + +package org.onap.sdc.tosca.datatypes.model.extension; + +import org.onap.sdc.tosca.datatypes.model.NodeFilter; + + +public class ServiceFilter extends NodeFilter { + + Object tosca_id; + + public Object getTosca_id() { + return tosca_id; + } + + public void setTosca_id(Object toscaId) { + this.tosca_id = toscaId; + } + +} diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/error/ToscaRuntimeException.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/error/ToscaRuntimeException.java new file mode 100644 index 0000000000..10c67bc4df --- /dev/null +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/error/ToscaRuntimeException.java @@ -0,0 +1,32 @@ +/* + * 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. + */ + +package org.onap.sdc.tosca.error; + +public class ToscaRuntimeException extends RuntimeException { + + public ToscaRuntimeException(String message) { + super(message); + } + + public ToscaRuntimeException(String message, Throwable cause) { + super(message, cause); + } + + public ToscaRuntimeException(Throwable cause) { + super(cause); + } +} diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/ToscaExtensionYamlUtil.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/ToscaExtensionYamlUtil.java index efd4982541..4b8436197d 100644 --- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/ToscaExtensionYamlUtil.java +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/ToscaExtensionYamlUtil.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 European Support Limited + * 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. @@ -16,6 +16,7 @@ package org.onap.sdc.tosca.services; +import org.onap.sdc.tosca.error.ToscaRuntimeException; import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.introspector.Property; import org.yaml.snakeyaml.introspector.PropertyUtils; @@ -27,60 +28,75 @@ import java.beans.IntrospectionException; public class ToscaExtensionYamlUtil extends YamlUtil { - @Override - public Constructor getConstructor(Class typClass) { - return new ToscaWithHeatExtensionConstructor(typClass); - } + public static final String TOSCA_MODEL_PARAMETER_DEFINITION = + "org.onap.sdc.tosca.datatypes.model.ParameterDefinition"; + public static final String TOSCA_MODEL_EXT_PARAMETER_DEFINITION = + "org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt"; + public static final String TOSCA_MODEL_REQUIREMENT_ASSIGNMENT = + "org.onap.sdc.tosca.datatypes.model.RequirementAssignment"; + public static final String TOSCA_MODEL_EXT_REQUIREMENT_ASSIGNMENT = + "org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment"; - @Override - protected PropertyUtils getPropertyUtils() { - return new ToscaPropertyUtilsWithHeatExtension(); - } + @Override + public Constructor getConstructor(Class typClass) { + return new ToscaWithHeatExtensionConstructor(typClass); + } - public class ToscaPropertyUtilsWithHeatExtension extends MyPropertyUtils { @Override - public Property getProperty(Class type, String name) - throws IntrospectionException { - Class classType = type; - try { - if (type - .equals(Class.forName("org.onap.sdc.tosca.datatypes.model.ParameterDefinition"))) { - classType = Class - .forName("org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt"); - } - } catch (ClassNotFoundException ex) { - throw new RuntimeException(ex); - } - return super.getProperty(classType, name); + protected PropertyUtils getPropertyUtils() { + return new ToscaPropertyUtilsWithHeatExtension(); } - } - protected class ToscaWithHeatExtensionConstructor extends StrictMapAppenderConstructor { - public ToscaWithHeatExtensionConstructor(Class theRoot) { - super(theRoot); - yamlClassConstructors.put(NodeId.mapping, new MyPersistentObjectConstruct()); + public class ToscaPropertyUtilsWithHeatExtension extends MyPropertyUtils { + + @Override + public Property getProperty(Class type, String name) throws IntrospectionException { + Class classType = type; + try { + if (type.equals(Class.forName(TOSCA_MODEL_PARAMETER_DEFINITION))) { + classType = Class.forName(TOSCA_MODEL_EXT_PARAMETER_DEFINITION); + } + if (type.equals(Class.forName(TOSCA_MODEL_REQUIREMENT_ASSIGNMENT))) { + classType = Class.forName(TOSCA_MODEL_EXT_REQUIREMENT_ASSIGNMENT); + } + } catch (ClassNotFoundException ex) { + throw new ToscaRuntimeException(ex); + } + return super.getProperty(classType, name); + } } - class MyPersistentObjectConstruct extends Constructor.ConstructMapping { - @Override - protected Object constructJavaBean2ndStep(MappingNode node, Object object) { - Class type = node.getType(); - try { - if (type.equals( - Class.forName("org.onap.sdc.tosca.datatypes.model.ParameterDefinition"))) { - Class extendHeatClass = Class.forName( - "org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt"); - Object extendHeatObject = extendHeatClass.newInstance(); - // create JavaBean - return super.constructJavaBean2ndStep(node, extendHeatObject); - } else { - // create JavaBean - return super.constructJavaBean2ndStep(node, object); - } - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) { - throw new RuntimeException(ex); + protected class ToscaWithHeatExtensionConstructor extends StrictMapAppenderConstructor { + + public ToscaWithHeatExtensionConstructor(Class theRoot) { + super(theRoot); + yamlClassConstructors.put(NodeId.mapping, new MyPersistentObjectConstruct()); + } + + class MyPersistentObjectConstruct extends Constructor.ConstructMapping { + + @Override + protected Object constructJavaBean2ndStep(MappingNode node, Object object) { + Class type = node.getType(); + try { + if (type.equals(Class.forName(TOSCA_MODEL_PARAMETER_DEFINITION))) { + Class extendHeatClass = Class.forName(TOSCA_MODEL_EXT_PARAMETER_DEFINITION); + Object extendHeatObject = extendHeatClass.newInstance(); + // create JavaBean + return super.constructJavaBean2ndStep(node, extendHeatObject); + } else if (type.equals(Class.forName(TOSCA_MODEL_REQUIREMENT_ASSIGNMENT))) { + Class extendHeatClass = Class.forName(TOSCA_MODEL_EXT_REQUIREMENT_ASSIGNMENT); + Object extendHeatObject = extendHeatClass.newInstance(); + // create JavaBean + return super.constructJavaBean2ndStep(node, extendHeatObject); + } else { + // create JavaBean + return super.constructJavaBean2ndStep(node, object); + } + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) { + throw new ToscaRuntimeException(ex); + } + } } - } } - } } -- cgit 1.2.3-korg