diff options
author | liamfallon <liam.fallon@est.tech> | 2019-04-01 00:34:34 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-04-01 00:34:34 +0000 |
commit | 2e9186e1eb441b3ec47cf2a959e34b9409134aba (patch) | |
tree | a83a9e904c767db38ffc3f7d34aca6ece395e0f6 /models-tosca/src/main/java/org | |
parent | fbed360c0acdc127fdf56a99401c1aea691ce57e (diff) |
Refactor to authorative TOSCA serializtion
This review refactors the TOSCA support to use authorative
serialization and mapping.
It removes the JPA entities from the Proider interface
It brings the mapping support from and to authorative concepts out of
the JSON serialization classes directly into the JPA classes
It adapts the unit tests to work with the refactored structure.
Apologies for the review size but it all had to be done in a single block
of work.
Issue-ID: POLICY-1095
Change-Id: I4827d1dc67ef7aac98cba230ffcd79c6de71e805
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main/java/org')
39 files changed, 1081 insertions, 2041 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaConstraint.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaConstraint.java index 13b4adf23..4623b20e8 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaConstraint.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaConstraint.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,4 +39,17 @@ public class ToscaConstraint { private List<String> validValues; private String equal; + + @SerializedName("greater_than") + private String greaterThan; + + @SerializedName("greater_or_equal") + private String greaterOrEqual; + + @SerializedName("less_than") + private String lessThan; + + @SerializedName("less_or_equal") + private String lessOrEqual; + } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaDataType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaDataType.java index 9aae32040..b07163b6e 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaDataType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaDataType.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,9 +23,11 @@ package org.onap.policy.models.tosca.authorative.concepts; -import com.google.gson.annotations.SerializedName; +import java.util.List; import java.util.Map; import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; /** * Class to represent TOSCA data type matching input/output from/to client. @@ -32,16 +35,10 @@ import lombok.Data; * @author Chenfei Gao (cgao@research.att.com) */ @Data -public class ToscaDataType { - - @SerializedName("derived_from") - private String derivedFrom; - - private String version; - - private Map<String, String> metadata; - - private String description; +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +public class ToscaDataType extends ToscaEntity { + private List<ToscaConstraint> constraints; private Map<String, ToscaProperty> properties; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java new file mode 100644 index 000000000..87d0d9a75 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java @@ -0,0 +1,74 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import com.google.gson.annotations.SerializedName; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; + +import org.onap.policy.models.base.PfNameVersion; + +/** + * Class to represent TOSCA data type matching input/output from/to client. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@Data +@NoArgsConstructor +public class ToscaEntity implements PfNameVersion { + private String name; + + private String version; + + @SerializedName("derived_from") + private String derivedFrom; + + private Map<String, String> metadata; + + private String description; + + /** + * Copy COnstructor. + * + * @param copyObject object to copy from + */ + public ToscaEntity(@NonNull ToscaEntity copyObject) { + this.name = copyObject.name; + this.version = copyObject.version; + this.derivedFrom = copyObject.derivedFrom; + + if (copyObject.metadata != null) { + metadata = new LinkedHashMap<>(); + for (final Entry<String, String> metadataEntry : copyObject.metadata.entrySet()) { + metadata.put(metadataEntry.getKey(), metadataEntry.getValue()); + } + } + } + +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntrySchema.java index a3526f757..352dfa7fa 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntrySchema.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntrySchema.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,9 +33,12 @@ import lombok.Data; */ @Data public class ToscaEntrySchema { + private String name; private String type; + private String typeVersion; + private String description; private List<ToscaConstraint> constraints; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java index e8616061d..9bd84e8eb 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicy.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,9 +25,13 @@ package org.onap.policy.models.tosca.authorative.concepts; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; + import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.ToString; /** * Class to represent TOSCA policy matching input/output from/to client. @@ -34,16 +39,13 @@ import lombok.NonNull; * @author Chenfei Gao (cgao@research.att.com) */ @Data +@EqualsAndHashCode(callSuper = true) @NoArgsConstructor -public class ToscaPolicy { - +@ToString +public class ToscaPolicy extends ToscaEntity { private String type; - private String version; - - private String description; - - private Map<String, String> metadata; + private String typeVersion; private Map<String, Object> properties; @@ -53,10 +55,16 @@ public class ToscaPolicy { * @param copyObject the obejct to copy from. */ public ToscaPolicy(@NonNull ToscaPolicy copyObject) { + super(copyObject); + this.type = copyObject.type; - this.version = copyObject.version; - this.description = copyObject.description; - this.metadata = (metadata != null ? new LinkedHashMap<>(copyObject.metadata) : null); - this.properties = (properties != null ? new LinkedHashMap<>(copyObject.properties) : null); + this.typeVersion = copyObject.typeVersion; + + if (copyObject.properties != null) { + properties = new LinkedHashMap<>(); + for (final Entry<String, Object> propertyEntry : copyObject.properties.entrySet()) { + properties.put(propertyEntry.getKey(), propertyEntry.getValue()); + } + } } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java index c46402ba1..d64a5facd 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,9 +23,10 @@ package org.onap.policy.models.tosca.authorative.concepts; -import com.google.gson.annotations.SerializedName; import java.util.Map; import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; /** * Class to represent TOSCA policy type matching input/output from/to client. @@ -32,16 +34,8 @@ import lombok.Data; * @author Chenfei Gao (cgao@research.att.com) */ @Data -public class ToscaPolicyType { - - @SerializedName("derived_from") - private String derivedFrom; - - private String version; - - private Map<String, String> metadata; - - private String description; - +@EqualsAndHashCode(callSuper = true) +@NoArgsConstructor +public class ToscaPolicyType extends ToscaEntity { private Map<String, ToscaProperty> properties; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaProperty.java index 9eda83b90..84f798bc9 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaProperty.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaProperty.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,18 +34,27 @@ import lombok.Data; */ @Data public class ToscaProperty { + public enum Status { + SUPPORTED, UNSUPPORTED, EXPERIMENTAL, DEPRECATED + } + + private String name; private String type; - private String description; + private String typeVersion; - private boolean required = false; + private String description; @SerializedName("default") private String defaultValue; - @SerializedName("entry_schema") - private ToscaEntrySchema entrySchema; + private boolean required = false; + + private Status status; private List<ToscaConstraint> constraints; + + @SerializedName("entry_schema") + private ToscaEntrySchema entrySchema; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplate.java index 3d1b96313..a9a1783d7 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplate.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +27,7 @@ import com.google.gson.annotations.SerializedName; import java.util.List; import java.util.Map; import lombok.Data; +import lombok.EqualsAndHashCode; /** * Class to represent TOSCA service template matching input/output from/to client. @@ -33,7 +35,8 @@ import lombok.Data; * @author Chenfei Gao (cgao@research.att.com) */ @Data -public class ToscaServiceTemplate { +@EqualsAndHashCode(callSuper = true) +public class ToscaServiceTemplate extends ToscaEntity { @SerializedName("tosca_definitions_version") private String toscaDefinitionsVersion; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapper.java deleted file mode 100644 index cef83486d..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapper.java +++ /dev/null @@ -1,56 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.authorative.mapping; - -import com.google.gson.Gson; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; -import org.onap.policy.models.tosca.simple.mapping.JpaToscaServiceTemplateMapper; -import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler; - -/** - * This class maps a TOSCA service template from client input form to internal representation and vice verse. - * - * @author Chenfei Gao (cgao@research.att.com) - */ -public class PlainToscaServiceTemplateMapper - implements JpaToscaServiceTemplateMapper<ToscaServiceTemplate, ToscaServiceTemplate> { - - private Gson defaultGson = new Gson(); - private Gson customGson = new ToscaServiceTemplateMessageBodyHandler().getGson(); - - @Override - public JpaToscaServiceTemplate toToscaServiceTemplate(ToscaServiceTemplate otherPolicy) { - - String serializedServiceTemplate = defaultGson.toJson(otherPolicy); - return customGson.fromJson(serializedServiceTemplate, JpaToscaServiceTemplate.class); - - } - - @Override - public ToscaServiceTemplate fromToscaServiceTemplate(JpaToscaServiceTemplate serviceTemplate) { - - String serializedServiceTemplate = customGson.toJson(serviceTemplate); - return defaultGson.fromJson(serializedServiceTemplate, ToscaServiceTemplate.class); - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java new file mode 100644 index 000000000..0201bbea2 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java @@ -0,0 +1,157 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.provider; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider; + +/** + * This class provides the provision of information on TOSCA concepts in the database to callers. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class AuthorativeToscaProvider { + /** + * Get policy types. + * + * @param dao the DAO to use to access the database + * @param name the name of the policy type to get. + * @param version the version of the policy type to get. + * @return the policy types found + * @throws PfModelException on errors getting policy types + */ + public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final String name, + @NonNull final String version) throws PfModelException { + + return new SimpleToscaProvider().getPolicyTypes(dao, new PfConceptKey(name, version)).toAuthorative(); + } + + /** + * Create policy types. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definition of the policy types to be created + * @return the TOSCA service template containing the created policy types + * @throws PfModelException on errors creating policy types + */ + public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + + return new SimpleToscaProvider().createPolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)) + .toAuthorative(); + } + + /** + * Update policy types. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definition of the policy types to be modified + * @return the TOSCA service template containing the modified policy types + * @throws PfModelException on errors updating policy types + */ + public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + + return new SimpleToscaProvider().updatePolicyTypes(dao, new JpaToscaServiceTemplate(serviceTemplate)) + .toAuthorative(); + } + + /** + * Delete policy types. + * + * @param dao the DAO to use to access the database + * @param name the name of the policy type to delete. + * @param version the version of the policy type to delete. + * @return the TOSCA service template containing the policy types that were deleted + * @throws PfModelException on errors deleting policy types + */ + public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao, @NonNull final String name, + @NonNull final String version) throws PfModelException { + + return new SimpleToscaProvider().deletePolicyTypes(dao, new PfConceptKey(name, version)).toAuthorative(); + } + + /** + * Get policies. + * + * @param dao the DAO to use to access the database + * @param name the name of the policy to get. + * @param version the version of the policy to get. + * @return the policies found + * @throws PfModelException on errors getting policies + */ + public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final String name, + @NonNull final String version) throws PfModelException { + + return new SimpleToscaProvider().getPolicies(dao, new PfConceptKey(name, version)).toAuthorative(); + } + + /** + * Create policies. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definitions of the new policies to be created. + * @return the TOSCA service template containing the policy types that were created + * @throws PfModelException on errors creating policies + */ + public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + + return new SimpleToscaProvider().createPolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)) + .toAuthorative(); + } + + /** + * Update policies. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definitions of the policies to be updated. + * @return the TOSCA service template containing the policies that were updated + * @throws PfModelException on errors updating policies + */ + public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + + return new SimpleToscaProvider().updatePolicies(dao, new JpaToscaServiceTemplate(serviceTemplate)) + .toAuthorative(); + } + + /** + * Delete policies. + * + * @param dao the DAO to use to access the database + * @param name the name of the policy to delete. + * @param version the version of the policy to delete. + * @return the TOSCA service template containing the policies that were deleted + * @throws PfModelException on errors deleting policies + */ + public ToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final String name, + @NonNull final String version) throws PfModelException { + + return new SimpleToscaProvider().deletePolicies(dao, new PfConceptKey(name, version)).toAuthorative(); + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java index bde53c351..fad227c34 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java @@ -23,22 +23,13 @@ package org.onap.policy.models.tosca.simple.concepts; -import java.util.List; +import java.io.Serializable; -import javax.persistence.EmbeddedId; -import javax.ws.rs.core.Response; - -import lombok.Data; import lombok.EqualsAndHashCode; -import lombok.NonNull; +import lombok.NoArgsConstructor; -import org.onap.policy.models.base.PfConcept; -import org.onap.policy.models.base.PfKey; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.base.PfReferenceKey; -import org.onap.policy.models.base.PfValidationMessage; -import org.onap.policy.models.base.PfValidationResult; -import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.base.PfAuthorative; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; /** * Immutable class to represent the Constraint of property in TOSCA definition. @@ -46,80 +37,37 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; * @author Chenfei Gao (cgao@research.att.com) * @author Liam Fallon (liam.fallon@est.tech) */ -@Data -@EqualsAndHashCode(callSuper = false) -public abstract class JpaToscaConstraint extends PfConcept { - private static final long serialVersionUID = 6426438089914347734L; - - @EmbeddedId - private final PfReferenceKey key; +@NoArgsConstructor +@EqualsAndHashCode +public abstract class JpaToscaConstraint + implements PfAuthorative<ToscaConstraint>, Serializable, Comparable<JpaToscaConstraint> { + private static final long serialVersionUID = -2689472945262507455L; /** - * The Default Constructor creates a {@link JpaToscaConstraint} object with a null key. + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from */ - public JpaToscaConstraint() { - this(new PfReferenceKey()); + public JpaToscaConstraint(final ToscaConstraint authorativeConcept) { + this.fromAuthorative(authorativeConcept); } - /** - * The Key Constructor creates a {@link JpaToscaConstraint} object with the given concept key. - * - * @param key the key - */ - public JpaToscaConstraint(@NonNull final PfReferenceKey key) { - this.key = key; + @Override + public int compareTo(JpaToscaConstraint otherConstraint) { + return 0; } /** - * Copy constructor. + * Create instances of constraints of various types. * - * @param copyConcept the concept to copy from + * @param toscaConstraint the incoming constraint + * @return the constraint */ - public JpaToscaConstraint(@NonNull final JpaToscaConstraint copyConcept) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); - } - - @Override - public List<PfKey> getKeys() { - return getKey().getKeys(); - } - - @Override - public void clean() { - key.clean(); - } - - @Override - public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { - PfValidationResult result = resultIn; - - if (key.isNullKey()) { - result.addValidationMessage( - new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); - } - - return key.validate(result); - } - - @Override - public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } - if (this == otherConcept) { - return 0; + public static JpaToscaConstraint newInstance(final ToscaConstraint toscaConstraint) { + if (toscaConstraint.getValidValues() != null) { + return new JpaToscaConstraintValidValues(toscaConstraint); } - if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); - } - - final JpaToscaConstraint other = (JpaToscaConstraint) otherConcept; - return key.compareTo(other.key); - } - - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + return (new JpaToscaConstraintLogical(toscaConstraint)); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java index 231e26188..9841cbe82 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java @@ -21,16 +21,14 @@ package org.onap.policy.models.tosca.simple.concepts; import javax.persistence.Column; -import javax.ws.rs.core.Response; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NonNull; import lombok.ToString; -import org.onap.policy.models.base.PfConcept; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.base.PfReferenceKey; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; /** * This class represents a logical TOSCA constraint: =,>,>=,<,<=. @@ -38,82 +36,117 @@ import org.onap.policy.models.base.PfReferenceKey; @EqualsAndHashCode(callSuper = false) @ToString public class JpaToscaConstraintLogical extends JpaToscaConstraint { - private static final long serialVersionUID = 2562306457768745444L; - - public enum Operation { - EQ, - GT, - GE, - LT, - LE - } + private static final long serialVersionUID = -2730203215911880756L; @Column @NonNull @Getter - private final Operation operation; + private JpaToscaConstraintOperation operation; - /** - * The Default Constructor creates a {@link JpaToscaConstraintLogical} object with a null key. - */ - public JpaToscaConstraintLogical() { - this(new PfReferenceKey()); - } + @Column + @NonNull + @Getter + private String compareTo; /** - * The Key Constructor creates a {@link JpaToscaConstraintLogical} object with the given concept key. + * Constructor to set operation. * - * @param key the key of the constraint + * @param operation the operation to set + * @param compareTo the string to compare to */ - public JpaToscaConstraintLogical(final PfReferenceKey key) { - this(key, Operation.EQ); + public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintOperation operation, + @NonNull final String compareTo) { + this.operation = operation; + this.compareTo = compareTo; } /** - * The Key Constructor creates a {@link JpaToscaConstraintLogical} object with the given concept key and operation. - * - * @param key the key of the constraint - * @param operation the logical operation of the constraint + * Authorative constructor. * + * @param authorativeConcept the authorative concept to copy from */ - public JpaToscaConstraintLogical(final PfReferenceKey key, @NonNull final Operation operation) { - super(key); - this.operation = operation; + public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) { + super(authorativeConcept); } - /** - * Copy constructor. - * - * @param copyConcept the concept to copy from - */ - public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintLogical copyConcept) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + @Override + public ToscaConstraint toAuthorative() { + ToscaConstraint toscaConstraint = new ToscaConstraint(); + + switch (operation) { + case EQ: { + toscaConstraint.setEqual(compareTo); + break; + } + case GT: { + toscaConstraint.setGreaterThan(compareTo); + break; + } + case GE: { + toscaConstraint.setGreaterOrEqual(compareTo); + break; + } + case LT: { + toscaConstraint.setLessThan(compareTo); + break; + } + case LE: { + toscaConstraint.setLessOrEqual(compareTo); + break; + } + default: { + // Can't happen + } + } + + return toscaConstraint; } @Override - public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { + public void fromAuthorative(final ToscaConstraint toscaConstraint) { + // @formatter:off + if (toscaConstraint.getEqual() != null) { + operation = JpaToscaConstraintOperation.EQ; + compareTo = toscaConstraint.getEqual(); + } + else if (toscaConstraint.getGreaterThan() != null) { + operation = JpaToscaConstraintOperation.GT; + compareTo = toscaConstraint.getGreaterThan(); + } + else if (toscaConstraint.getGreaterOrEqual() != null) { + operation = JpaToscaConstraintOperation.GE; + compareTo = toscaConstraint.getGreaterOrEqual(); + } + else if (toscaConstraint.getLessThan() != null) { + operation = JpaToscaConstraintOperation.LT; + compareTo = toscaConstraint.getLessThan(); + } + else if (toscaConstraint.getLessOrEqual() != null) { + operation = JpaToscaConstraintOperation.LE; + compareTo = toscaConstraint.getLessOrEqual(); + } + // @formatter:on + } + + @Override + public int compareTo(JpaToscaConstraint otherConstraint) { + if (otherConstraint == null) { return -1; } - if (this == otherConcept) { + if (this == otherConstraint) { return 0; } - if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); + if (getClass() != otherConstraint.getClass()) { + return this.hashCode() - otherConstraint.hashCode(); } - final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConcept; + final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint; - int result = super.compareTo(other); + int result = ObjectUtils.compare(operation, other.operation); if (result != 0) { return result; } - return operation.compareTo(other.operation); - } - - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + return ObjectUtils.compare(compareTo, other.compareTo); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalKey.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalKey.java deleted file mode 100644 index 13902db19..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalKey.java +++ /dev/null @@ -1,155 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.concepts; - -import java.util.List; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.Table; -import javax.ws.rs.core.Response; - -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.NonNull; -import lombok.ToString; - -import org.onap.policy.models.base.PfConcept; -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfKey; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.base.PfReferenceKey; -import org.onap.policy.models.base.PfValidationMessage; -import org.onap.policy.models.base.PfValidationResult; -import org.onap.policy.models.base.PfValidationResult.ValidationResult; - -/** - * This immutable class represents a logical TOSCA constraint: =,>,>=,<,<= that compares the owner - * of an instance of the class to the referenced key. - */ -@Entity -@Table(name = "ToscaConstraintLogicalKey") -@Inheritance(strategy = InheritanceType.SINGLE_TABLE) -@EqualsAndHashCode(callSuper = false) -@ToString -public final class JpaToscaConstraintLogicalKey extends JpaToscaConstraintLogical { - private static final long serialVersionUID = -2420828090326264341L; - - @Column - @NonNull - @Getter - private final PfKey compareToKey; - - /** - * The Default Constructor creates a {@link JpaToscaConstraintLogicalKey} object with a null key. - */ - public JpaToscaConstraintLogicalKey() { - this(new PfReferenceKey()); - } - - /** - * The Key Constructor creates a {@link JpaToscaConstraintLogicalKey} object with the given concept - * key. - * - * @param key the key of the constraint - */ - public JpaToscaConstraintLogicalKey(final PfReferenceKey key) { - this(key, Operation.EQ, PfConceptKey.getNullKey()); - } - - /** - * The Key Constructor creates a {@link JpaToscaConstraintLogicalKey} object with the given concept - * key, operation, and compare key. - * - * @param key the key of the constraint - * @param operation the logical operation of the constraint - * @param compareToKey the key of the object to which the object that owns this constraint will - * be compared - */ - public JpaToscaConstraintLogicalKey(final PfReferenceKey key, @NonNull final Operation operation, - @NonNull final PfKey compareToKey) { - super(key, operation); - this.compareToKey = compareToKey; - } - - /** - * Copy constructor. - * - * @param copyConcept the concept to copy from - */ - public JpaToscaConstraintLogicalKey(@NonNull final JpaToscaConstraintLogical copyConcept) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); - } - - @Override - public List<PfKey> getKeys() { - final List<PfKey> keyList = super.getKeys(); - keyList.addAll(compareToKey.getKeys()); - return keyList; - } - - @Override - public void clean() { - super.clean(); - compareToKey.clean(); - } - - @Override - public PfValidationResult validate(final PfValidationResult resultIn) { - PfValidationResult result = super.validate(resultIn); - - if (compareToKey.isNullKey()) { - result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "comparison key is a null key")); - } - - return result; - } - - @Override - public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } - if (this == otherConcept) { - return 0; - } - if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); - } - - final JpaToscaConstraintLogicalKey other = (JpaToscaConstraintLogicalKey) otherConcept; - - int result = super.compareTo(other); - if (result != 0) { - return result; - } - - return compareToKey.compareTo(other.compareToKey); - } - - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalString.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalString.java deleted file mode 100644 index 512127343..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalString.java +++ /dev/null @@ -1,137 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.concepts; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.Table; -import javax.ws.rs.core.Response; - -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.NonNull; - -import org.onap.policy.common.utils.validation.ParameterValidationUtils; -import org.onap.policy.models.base.PfConcept; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.base.PfReferenceKey; -import org.onap.policy.models.base.PfValidationMessage; -import org.onap.policy.models.base.PfValidationResult; -import org.onap.policy.models.base.PfValidationResult.ValidationResult; - -/** - * This class represents a logical TOSCA constraint: =,>,>=,<,<= that compares the owner of an - * instance of the class to the given string. - */ -@Entity -@Table(name = "ToscaConstraintLogicalString") -@Inheritance(strategy = InheritanceType.SINGLE_TABLE) -@EqualsAndHashCode(callSuper = false) -public class JpaToscaConstraintLogicalString extends JpaToscaConstraintLogical { - private static final long serialVersionUID = 8167550632122339195L; - - @Column - @NonNull - @Getter - private final String compareToString; - - /** - * The Default Constructor creates a {@link JpaToscaConstraintLogicalString} object with a null key. - */ - public JpaToscaConstraintLogicalString() { - this(new PfReferenceKey()); - } - - /** - * The Key Constructor creates a {@link JpaToscaConstraintLogicalString} object with the given concept - * key. - * - * @param key the key of the constraint - */ - public JpaToscaConstraintLogicalString(final PfReferenceKey key) { - this(key, Operation.EQ, ""); - } - - /** - * The Key Constructor creates a {@link JpaToscaConstraintLogicalString} object with the given concept - * key, operation, and compare string. - * - * @param key the key of the constraint - * @param operation the logical operation of the constraint - * @param compareToString the key of the object to which the object that owns this constraint will - * be compared - */ - public JpaToscaConstraintLogicalString(final PfReferenceKey key, @NonNull final Operation operation, - @NonNull final String compareToString) { - super(key, operation); - this.compareToString = compareToString.trim(); - } - - /** - * Copy constructor. - * - * @param copyConcept the concept to copy from - */ - public JpaToscaConstraintLogicalString(@NonNull final JpaToscaConstraintLogical copyConcept) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); - } - - @Override - public PfValidationResult validate(final PfValidationResult resultIn) { - PfValidationResult result = super.validate(resultIn); - - if (!ParameterValidationUtils.validateStringParameter(compareToString)) { - result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, - "comparison string is null or blank")); - } - - return result; - } - - @Override - public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } - if (this == otherConcept) { - return 0; - } - if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); - } - - final JpaToscaConstraintLogicalString other = (JpaToscaConstraintLogicalString) otherConcept; - - int result = super.compareTo(other); - if (result != 0) { - return result; - } - - return compareToString.compareTo(other.compareToString); - } - - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/mapping/package-info.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintOperation.java index e9b87c6ab..0422ae273 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/mapping/package-info.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintOperation.java @@ -1,8 +1,6 @@ /*- * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,12 +18,29 @@ * ============LICENSE_END========================================================= */ +package org.onap.policy.models.tosca.simple.concepts; + /** - * This package includes all the mappers used to transform plain TOSCA POJOs into internal representation - * of TOSCA concepts with JPA annotations added. - */ -/** - * @author Chenfei Gao (cgao@research.att.com) + * ENUM for TOSCA constraint operations. * + * @author Liam Fallon (liam.fallon@est.tech) */ -package org.onap.policy.models.tosca.authorative.mapping;
\ No newline at end of file +public enum JpaToscaConstraintOperation { + // @formatter:off + EQ("equal_to"), + GT("greater_than"), + GE("greater_or_equal"), + LT("less_than"), + LE("less_or_equal"); + // @formatter:on + + private final String toscaToken; + + private JpaToscaConstraintOperation(final String toscaToken) { + this.toscaToken = toscaToken; + } + + public String getToscaToken() { + return toscaToken; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java index 608605f4e..248ca496c 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,20 +21,17 @@ package org.onap.policy.models.tosca.simple.concepts; -import com.google.gson.annotations.SerializedName; - -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import javax.persistence.ElementCollection; -import javax.ws.rs.core.Response; -import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NonNull; +import lombok.ToString; -import org.onap.policy.models.base.PfConcept; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; /** * This class represents valid_values TOSCA constraint. @@ -41,81 +39,64 @@ import org.onap.policy.models.base.PfReferenceKey; * @author Chenfei Gao (cgao@research.att.com) */ @EqualsAndHashCode(callSuper = false) -@Data +@ToString public class JpaToscaConstraintValidValues extends JpaToscaConstraint { - private static final long serialVersionUID = 3152323457560746844L; + private static final long serialVersionUID = -5060193250508635456L; - @SerializedName("valid_values") - @NonNull @ElementCollection - private final List<String> validValues; + @NonNull + @Getter + private List<String> validValues; /** - * The Default Constructor creates a {@link JpaToscaConstraintValidValues} object with a null key. + * Constructor to set the valid values. + * + * @param validValues the valid values that are allowed */ - public JpaToscaConstraintValidValues() { - this(new PfReferenceKey()); + public JpaToscaConstraintValidValues(@NonNull final List<String> validValues) { + this.validValues = validValues; } /** - * The Key Constructor creates a {@link JpaToscaConstraintValidValues} object with the given concept key. + * Authorative constructor. * - * @param key the key of the constraint + * @param authorativeConcept the authorative concept to copy from */ - public JpaToscaConstraintValidValues(final PfReferenceKey key) { - super(key); - validValues = new LinkedList<>(); + public JpaToscaConstraintValidValues(final ToscaConstraint authorativeConcept) { + super(authorativeConcept); } - /** - * The Key Constructor creates a {@link JpaToscaConstraintLogical} object with the given concept key - * and valid values list. - * - * @param key the key of the constraint - * @param validValues the valid values list of the constraint - * - */ - public JpaToscaConstraintValidValues(final PfReferenceKey key, @NonNull final List<String> validValues) { - super(key); - this.validValues = validValues; + @Override + public ToscaConstraint toAuthorative() { + ToscaConstraint toscaConstraint = new ToscaConstraint(); + + toscaConstraint.setValidValues(validValues); + + return toscaConstraint; } - /** - * Copy constructor. - * - * @param copyConcept the concept to copy from - */ - public JpaToscaConstraintValidValues(@NonNull final JpaToscaConstraintValidValues copyConcept) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + @Override + public void fromAuthorative(final ToscaConstraint toscaConstraint) { + if (toscaConstraint.getValidValues() != null) { + validValues = new ArrayList<>(); + validValues.addAll(toscaConstraint.getValidValues()); + } } @Override - public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { + public int compareTo(JpaToscaConstraint otherConstraint) { + if (otherConstraint == null) { return -1; } - if (this == otherConcept) { + if (this == otherConstraint) { return 0; } - if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); - } - - final JpaToscaConstraintValidValues other = (JpaToscaConstraintValidValues) otherConcept; - - int result = super.compareTo(other); - if (result != 0) { - return result; + if (getClass() != otherConstraint.getClass()) { + return this.hashCode() - otherConstraint.hashCode(); } - if (validValues.equals(other.validValues)) { - return 0; - } - return -1; - } + final JpaToscaConstraintValidValues other = (JpaToscaConstraintValidValues) otherConstraint; - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + return PfUtils.compareObjects(validValues, other.validValues); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java index 0035eb015..cf1150af8 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataType.java @@ -24,7 +24,10 @@ package org.onap.policy.models.tosca.simple.concepts; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import javax.persistence.ElementCollection; import javax.persistence.Entity; @@ -37,13 +40,18 @@ import lombok.EqualsAndHashCode; import lombok.NonNull; import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; /** * Class to represent custom data type in TOSCA definition. @@ -56,14 +64,14 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaDataType extends JpaToscaEntityType { +public class JpaToscaDataType extends JpaToscaEntityType<ToscaDataType> implements PfAuthorative<ToscaDataType> { private static final long serialVersionUID = -3922690413436539164L; @ElementCollection private List<JpaToscaConstraint> constraints; @ElementCollection - private List<JpaToscaProperty> properties; + private Map<String, JpaToscaProperty> properties; /** * The Default Constructor creates a {@link JpaToscaDataType} object with a null key. @@ -90,18 +98,72 @@ public class JpaToscaDataType extends JpaToscaEntityType { super(copyConcept); } + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaDataType(final ToscaDataType authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + @Override - public List<PfKey> getKeys() { - final List<PfKey> keyList = super.getKeys(); + public ToscaDataType toAuthorative() { + ToscaDataType toscaDataType = new ToscaDataType(); + super.setToscaEntity(toscaDataType); + super.toAuthorative(); if (constraints != null) { + List<ToscaConstraint> toscaConstraints = new ArrayList<>(); + for (JpaToscaConstraint constraint : constraints) { - keyList.addAll(constraint.getKeys()); + toscaConstraints.add(constraint.toAuthorative()); } + + toscaDataType.setConstraints(toscaConstraints); } if (properties != null) { - for (JpaToscaProperty property : properties) { + Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>(); + + for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) { + propertyMap.put(entry.getKey(), entry.getValue().toAuthorative()); + } + + toscaDataType.setProperties(propertyMap); + } + + return toscaDataType; + } + + @Override + public void fromAuthorative(final ToscaDataType toscaDataType) { + super.fromAuthorative(toscaDataType); + + if (toscaDataType.getConstraints() != null) { + constraints = new ArrayList<>(); + + for (ToscaConstraint toscaConstraint: toscaDataType.getConstraints()) { + constraints.add(JpaToscaConstraint.newInstance(toscaConstraint)); + } + } + + if (toscaDataType.getProperties() != null) { + properties = new LinkedHashMap<>(); + for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaDataType.getProperties().entrySet()) { + JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); + jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); + properties.put(toscaPropertyEntry.getKey(), jpaProperty); + } + } + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = super.getKeys(); + + if (properties != null) { + for (JpaToscaProperty property : properties.values()) { keyList.addAll(property.getKeys()); } } @@ -113,14 +175,8 @@ public class JpaToscaDataType extends JpaToscaEntityType { public void clean() { super.clean(); - if (constraints != null) { - for (JpaToscaConstraint constraint : constraints) { - constraint.clean(); - } - } - if (properties != null) { - for (JpaToscaProperty property : properties) { + for (JpaToscaProperty property : properties.values()) { property.clean(); } } @@ -154,8 +210,6 @@ public class JpaToscaDataType extends JpaToscaEntityType { if (constraint == null) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, "data type constraint may not be null ")); - } else { - result = constraint.validate(result); } } return result; @@ -170,7 +224,7 @@ public class JpaToscaDataType extends JpaToscaEntityType { private PfValidationResult validateProperties(final PfValidationResult resultIn) { PfValidationResult result = resultIn; - for (JpaToscaProperty property : properties) { + for (JpaToscaProperty property : properties.values()) { if (property == null) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, "data type property may not be null ")); @@ -221,8 +275,7 @@ public class JpaToscaDataType extends JpaToscaEntityType { if (constraints == null) { copy.setConstraints(null); - } - else { + } else { final List<JpaToscaConstraint> newConstraints = new ArrayList<>(); for (final JpaToscaConstraint constraint : constraints) { newConstraints.add(constraint); // Constraints are immutable @@ -232,15 +285,14 @@ public class JpaToscaDataType extends JpaToscaEntityType { if (properties == null) { copy.setProperties(null); - } - else { - final List<JpaToscaProperty> newProperties = new ArrayList<>(); - for (final JpaToscaProperty property : properties) { - newProperties.add(new JpaToscaProperty(property)); + } else { + final Map<String, JpaToscaProperty> newProperties = new LinkedHashMap<>(); + for (final Entry<String, JpaToscaProperty> propertyEntry : properties.entrySet()) { + newProperties.put(propertyEntry.getKey(), new JpaToscaProperty(propertyEntry.getValue())); } copy.setProperties(newProperties); } return copy; } -}
\ No newline at end of file +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypes.java index 7fedd063c..67d4ecf4f 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypes.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypes.java @@ -20,6 +20,7 @@ package org.onap.policy.models.tosca.simple.concepts; +import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -33,6 +34,7 @@ import lombok.EqualsAndHashCode; import org.onap.policy.models.base.PfConceptContainer; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; /** * This class is a container for TOSCA data types. @@ -44,23 +46,23 @@ import org.onap.policy.models.base.PfConceptKey; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaDataTypes extends PfConceptContainer<JpaToscaDataType> { +public class JpaToscaDataTypes extends PfConceptContainer<JpaToscaDataType, ToscaDataType> { private static final long serialVersionUID = 2941102271022190348L; public static final String DEFAULT_NAME = "ToscaDataTypesSimple"; public static final String DEFAULT_VERSION = "1.0.0"; /** - * The Default Constructor creates a {@link JpaToscaDataTypes} object with a null artifact key - * and creates an empty concept map. + * The Default Constructor creates a {@link JpaToscaDataTypes} object with a null artifact key and creates an empty + * concept map. */ public JpaToscaDataTypes() { super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); } /** - * The Key Constructor creates a {@link JpaToscaDataTypes} object with the given artifact key - * and creates an empty concept map. + * The Key Constructor creates a {@link JpaToscaDataTypes} object with the given artifact key and creates an empty + * concept map. * * @param key the concept key */ @@ -86,4 +88,13 @@ public class JpaToscaDataTypes extends PfConceptContainer<JpaToscaDataType> { public JpaToscaDataTypes(final JpaToscaDataTypes copyConcept) { super(copyConcept); } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaDataTypes(final List<Map<String, ToscaDataType>> authorativeConceptMapList) { + this.fromAuthorative(authorativeConceptMapList); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java index e7d51a500..f2ab2460f 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java @@ -20,6 +20,7 @@ package org.onap.policy.models.tosca.simple.concepts; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -39,6 +40,7 @@ import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; @@ -46,6 +48,7 @@ import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; /** * Class to represent the EntrySchema of list/map property in TOSCA definition. @@ -53,7 +56,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; @MappedSuperclass @Data @EqualsAndHashCode(callSuper = false) -public class JpaToscaEntityType extends PfConcept { +public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> { private static final long serialVersionUID = -1330661834220739393L; @EmbeddedId @@ -74,6 +77,8 @@ public class JpaToscaEntityType extends PfConcept { @Column private String description; + + private transient T toscaEntity; // @formatter:on /** @@ -97,10 +102,81 @@ public class JpaToscaEntityType extends PfConcept { * * @param copyConcept the concept to copy from */ - public JpaToscaEntityType(final JpaToscaEntityType copyConcept) { + public JpaToscaEntityType(final JpaToscaEntityType<T> copyConcept) { super(copyConcept); } + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaEntityType(final T authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public T toAuthorative() { + toscaEntity.setName(getKey().getName()); + toscaEntity.setVersion(getKey().getVersion()); + + if (derivedFrom != null) { + toscaEntity.setDerivedFrom(derivedFrom.getId()); + } + + if (description != null) { + toscaEntity.setDescription(description); + } + + if (metadata != null) { + Map<String, String> metadataMap = new LinkedHashMap<>(); + + for (Entry<String, String> entry : metadata.entrySet()) { + metadataMap.put(entry.getKey(), entry.getValue()); + } + + toscaEntity.setMetadata(metadataMap); + } + + return toscaEntity; + } + + @Override + public void fromAuthorative(T toscaEntity) { + key = new PfConceptKey(); + + if (toscaEntity.getName() != null) { + key.setName(toscaEntity.getName()); + } + + if (toscaEntity.getVersion() != null) { + key.setVersion(toscaEntity.getVersion()); + } + + + if (toscaEntity.getDerivedFrom() != null) { + // CHeck if the derived from field contains a name-version ID + if (toscaEntity.getDerivedFrom().contains(":")) { + derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom()); + } + else { + derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom(), PfKey.NULL_KEY_VERSION); + } + } + + if (toscaEntity.getDescription() != null) { + description = toscaEntity.getDescription(); + } + + if (toscaEntity.getMetadata() != null) { + metadata = new LinkedHashMap<>(); + + for (Entry<String, String> metadataEntry : toscaEntity.getMetadata().entrySet()) { + metadata.put(metadataEntry.getKey(), metadataEntry.getValue()); + } + } + } + @Override public List<PfKey> getKeys() { final List<PfKey> keyList = getKey().getKeys(); @@ -176,7 +252,8 @@ public class JpaToscaEntityType extends PfConcept { return this.hashCode() - otherConcept.hashCode(); } - final JpaToscaEntityType other = (JpaToscaEntityType) otherConcept; + @SuppressWarnings("unchecked") + final JpaToscaEntityType<T> other = (JpaToscaEntityType<T>) otherConcept; if (!key.equals(other.key)) { return key.compareTo(other.key); } @@ -199,7 +276,8 @@ public class JpaToscaEntityType extends PfConcept { final Object copyObject = target; Assertions.instanceOf(copyObject, PfConcept.class); - final JpaToscaEntityType copy = ((JpaToscaEntityType) copyObject); + @SuppressWarnings("unchecked") + final JpaToscaEntityType<T> copy = ((JpaToscaEntityType<T>) copyObject); copy.setKey(new PfConceptKey(key)); copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java index e5ae20e84..1177368b2 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java @@ -23,31 +23,29 @@ package org.onap.policy.models.tosca.simple.concepts; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.persistence.Column; import javax.persistence.ElementCollection; -import javax.persistence.EmbeddedId; -import javax.persistence.Entity; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.common.utils.validation.Assertions; -import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; -import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntrySchema; /** @@ -56,17 +54,13 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; * @author Chenfei Gao (cgao@research.att.com) * @author Liam Fallon (liam.fallon@est.tech) */ -@Entity -@Table(name = "ToscaEntrySchema") -@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = false) -public class JpaToscaEntrySchema extends PfConcept { +@NoArgsConstructor +public class JpaToscaEntrySchema + implements PfAuthorative<ToscaEntrySchema>, Serializable, Comparable<JpaToscaEntrySchema> { private static final long serialVersionUID = 3645882081163287058L; - @EmbeddedId - private PfReferenceKey key; - @Column private PfConceptKey type; @@ -77,29 +71,11 @@ public class JpaToscaEntrySchema extends PfConcept { private List<JpaToscaConstraint> constraints; /** - * The Default Constructor creates a {@link JpaToscaEntrySchema} object with a null key. - */ - public JpaToscaEntrySchema() { - this(new PfReferenceKey()); - } - - /** - * The Key Constructor creates a {@link JpaToscaEntrySchema} object with the given concept key. - * - * @param key the key - */ - public JpaToscaEntrySchema(@NonNull final PfReferenceKey key) { - this(key, new PfConceptKey()); - } - - /** * The full constructor creates a {@link JpaToscaEntrySchema} object with mandatory fields. * - * @param key the key * @param type the type of the entry schema */ - public JpaToscaEntrySchema(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) { - this.key = key; + public JpaToscaEntrySchema(@NonNull final PfConceptKey type) { this.type = type; } @@ -108,68 +84,95 @@ public class JpaToscaEntrySchema extends PfConcept { * * @param copyConcept the concept to copy from */ - public JpaToscaEntrySchema(final JpaToscaEntrySchema copyConcept) { - super(copyConcept); + public JpaToscaEntrySchema(@NonNull final JpaToscaEntrySchema copyConcept) { + copyConcept.copyTo(this); + } + + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaEntrySchema(final ToscaEntrySchema authorativeConcept) { + this.fromAuthorative(authorativeConcept); } @Override - public List<PfKey> getKeys() { - final List<PfKey> keyList = getKey().getKeys(); + public ToscaEntrySchema toAuthorative() { + ToscaEntrySchema toscaEntrySchema = new ToscaEntrySchema(); - keyList.addAll(type.getKeys()); + toscaEntrySchema.setType(type.getName()); + toscaEntrySchema.setTypeVersion(type.getVersion()); + + toscaEntrySchema.setDescription(description); if (constraints != null) { + List<ToscaConstraint> toscaConstraints = new ArrayList<>(); + for (JpaToscaConstraint constraint : constraints) { - keyList.addAll(constraint.getKeys()); + toscaConstraints.add(constraint.toAuthorative()); } + + toscaEntrySchema.setConstraints(toscaConstraints); } - return keyList; + return toscaEntrySchema; } @Override - public void clean() { - key.clean(); + public void fromAuthorative(final ToscaEntrySchema toscaEntrySchema) { + if (toscaEntrySchema.getTypeVersion() != null) { + type = new PfConceptKey(toscaEntrySchema.getType(), toscaEntrySchema.getTypeVersion()); + } else { + type = new PfConceptKey(toscaEntrySchema.getType(), PfKey.NULL_KEY_VERSION); + } - type.clean(); - description = (description != null ? description.trim() : null); + description = toscaEntrySchema.getDescription(); - if (constraints != null) { - for (JpaToscaConstraint constraint : constraints) { - constraint.clean(); + if (toscaEntrySchema.getConstraints() != null) { + constraints = new ArrayList<>(); + + for (ToscaConstraint toscaConstraint : toscaEntrySchema.getConstraints()) { + constraints.add(JpaToscaConstraint.newInstance(toscaConstraint)); } } } - @Override - public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { - PfValidationResult result = resultIn; + public List<PfKey> getKeys() { + return type.getKeys(); + } - if (key.isNullKey()) { - result.addValidationMessage( - new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); - } + public void clean() { + type.clean(); + description = (description != null ? description.trim() : null); + } - result = key.validate(result); + /** + * Validate the entry schema. + * + * @param resultIn the incoming result + * @return the ooutput result witht he result of this validation + */ + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = resultIn; if (type == null || type.isNullKey()) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "entry schema type may not be null")); + result.addValidationMessage(new PfValidationMessage(new PfConceptKey("EntrySchema", PfKey.NULL_KEY_VERSION), + this.getClass(), ValidationResult.INVALID, "entry schema type may not be null")); } if (description != null && description.trim().length() == 0) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "entry schema description may not be blank")); + result.addValidationMessage(new PfValidationMessage(new PfConceptKey("EntrySchema", PfKey.NULL_KEY_VERSION), + this.getClass(), ValidationResult.INVALID, "entry schema description may not be blank")); } if (constraints != null) { for (JpaToscaConstraint constraint : constraints) { if (constraint == null) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "property constraint may not be null ")); - } else { - result = constraint.validate(result); + result.addValidationMessage( + new PfValidationMessage(new PfConceptKey("EntrySchema", PfKey.NULL_KEY_VERSION), + this.getClass(), ValidationResult.INVALID, "property constraint may not be null ")); } } } @@ -178,25 +181,13 @@ public class JpaToscaEntrySchema extends PfConcept { } @Override - public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { + public int compareTo(final JpaToscaEntrySchema other) { + if (other == null) { return -1; } - if (this == otherConcept) { + if (this == other) { return 0; } - if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); - } - - final JpaToscaEntrySchema other = (JpaToscaEntrySchema) otherConcept; - if (!key.equals(other.key)) { - return key.compareTo(other.key); - } - - if (!type.equals(other.type)) { - return type.compareTo(other.type); - } int result = ObjectUtils.compare(description, other.description); if (result != 0) { @@ -206,12 +197,16 @@ public class JpaToscaEntrySchema extends PfConcept { return PfUtils.compareObjects(constraints, other.constraints); } - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { + /** + * Copy this entry schema to another. + * + * @param target the other schemaa + * @return the copied concept + */ + public JpaToscaEntrySchema copyTo(@NonNull final JpaToscaEntrySchema target) { Assertions.instanceOf(target, JpaToscaEntrySchema.class); - final JpaToscaEntrySchema copy = ((JpaToscaEntrySchema) target); - copy.setKey(new PfReferenceKey(key)); + final JpaToscaEntrySchema copy = (target); copy.setType(new PfConceptKey(type)); copy.setDescription(description); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicies.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicies.java index d59f470dd..012d8a26f 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicies.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicies.java @@ -20,6 +20,7 @@ package org.onap.policy.models.tosca.simple.concepts; +import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -31,8 +32,10 @@ import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; +import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConceptContainer; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; /** * This class is a container for TOSCA data types. @@ -44,23 +47,24 @@ import org.onap.policy.models.base.PfConceptKey; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaPolicies extends PfConceptContainer<JpaToscaPolicy> { +public class JpaToscaPolicies extends PfConceptContainer<JpaToscaPolicy, ToscaPolicy> + implements PfAuthorative<List<Map<String, ToscaPolicy>>> { private static final long serialVersionUID = -7526648702327776101L; public static final String DEFAULT_NAME = "ToscaPoliciesSimple"; public static final String DEFAULT_VERSION = "1.0.0"; /** - * The Default Constructor creates a {@link JpaToscaPolicies} object with a null artifact key and - * creates an empty concept map. + * The Default Constructor creates a {@link JpaToscaPolicies} object with a null artifact key and creates an empty + * concept map. */ public JpaToscaPolicies() { super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); } /** - * The Key Constructor creates a {@link JpaToscaPolicies} object with the given artifact key and - * creates an empty concept map. + * The Key Constructor creates a {@link JpaToscaPolicies} object with the given artifact key and creates an empty + * concept map. * * @param key the concept key */ @@ -86,4 +90,13 @@ public class JpaToscaPolicies extends PfConceptContainer<JpaToscaPolicy> { public JpaToscaPolicies(final JpaToscaPolicies copyConcept) { super(copyConcept); } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaPolicies(final List<Map<String, ToscaPolicy>> authorativeConceptMapList) { + this.fromAuthorative(authorativeConceptMapList); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java index e889192d8..67a833c94 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java @@ -24,6 +24,7 @@ package org.onap.policy.models.tosca.simple.concepts; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -43,6 +44,7 @@ import lombok.NonNull; import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; @@ -50,6 +52,7 @@ import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; /** * Class to represent the policy in TOSCA definition. @@ -62,7 +65,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaPolicy extends JpaToscaEntityType { +public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements PfAuthorative<ToscaPolicy> { private static final long serialVersionUID = 3265174757061982805L; // @formatter:off @@ -119,6 +122,66 @@ public class JpaToscaPolicy extends JpaToscaEntityType { super(copyConcept); } + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaPolicy(final ToscaPolicy authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaPolicy toAuthorative() { + ToscaPolicy toscaPolicy = new ToscaPolicy(); + super.setToscaEntity(toscaPolicy); + super.toAuthorative(); + + toscaPolicy.setType(type.getName()); + + if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) { + toscaPolicy.setTypeVersion(type.getVersion()); + } + else { + toscaPolicy.setTypeVersion(null); + } + + if (properties != null) { + Map<String, Object> propertyMap = new LinkedHashMap<>(); + + for (Entry<String, String> entry : properties.entrySet()) { + propertyMap.put(entry.getKey(), entry.getValue()); + } + + toscaPolicy.setProperties(propertyMap); + } + + return toscaPolicy; + } + + @Override + public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) { + super.fromAuthorative(toscaPolicy); + + type.setName(toscaPolicy.getType()); + type.setVersion(toscaPolicy.getTypeVersion()); + if (type.getVersion() == null) { + type.setVersion(PfKey.NULL_KEY_VERSION); + } + + if (toscaPolicy.getProperties() != null) { + properties = new LinkedHashMap<>(); + + for (Entry<String, Object> propertyEntry : toscaPolicy.getProperties().entrySet()) { + // TODO: This is a HACK, we need to validate the properties against their + // TODO: their data type in their policy type definition in TOSCA, which means reading + // TODO: the policy type from the database and parsing the property value object correctly + // TODO: Here we are simply serializing the property value into a string and storing it + // TODO: unvalidated into the database + properties.put(propertyEntry.getKey(), propertyEntry.getValue().toString()); + } + } + } @Override public List<PfKey> getKeys() { diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java index 610987ccb..fc982965c 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyType.java @@ -24,7 +24,10 @@ package org.onap.policy.models.tosca.simple.concepts; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import javax.persistence.ElementCollection; import javax.persistence.Entity; @@ -37,13 +40,17 @@ import lombok.EqualsAndHashCode; import lombok.NonNull; import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; /** * Class to represent the policy type in TOSCA definition. @@ -57,11 +64,11 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaPolicyType extends JpaToscaEntityType { +public class JpaToscaPolicyType extends JpaToscaEntityType<ToscaPolicyType> implements PfAuthorative<ToscaPolicyType> { private static final long serialVersionUID = -563659852901842616L; @ElementCollection - private List<JpaToscaProperty> properties; + private Map<String, JpaToscaProperty> properties; @ElementCollection private List<PfConceptKey> targets; @@ -94,13 +101,55 @@ public class JpaToscaPolicyType extends JpaToscaEntityType { super(copyConcept); } + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaPolicyType toAuthorative() { + ToscaPolicyType toscaPolicyType = new ToscaPolicyType(); + super.setToscaEntity(toscaPolicyType); + super.toAuthorative(); + + if (properties != null) { + Map<String, ToscaProperty> propertyMap = new LinkedHashMap<>(); + + for (Entry<String, JpaToscaProperty> entry : properties.entrySet()) { + propertyMap.put(entry.getKey(), entry.getValue().toAuthorative()); + } + + toscaPolicyType.setProperties(propertyMap); + } + + return toscaPolicyType; + } + + @Override + public void fromAuthorative(final ToscaPolicyType toscaPolicyType) { + super.fromAuthorative(toscaPolicyType); + + // Set properties + if (toscaPolicyType.getProperties() != null) { + properties = new LinkedHashMap<>(); + for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaPolicyType.getProperties().entrySet()) { + JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue()); + jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey())); + properties.put(toscaPropertyEntry.getKey(), jpaProperty); + } + } + } @Override public List<PfKey> getKeys() { final List<PfKey> keyList = super.getKeys(); if (properties != null) { - for (JpaToscaProperty property : properties) { + for (JpaToscaProperty property : properties.values()) { keyList.addAll(property.getKeys()); } } @@ -123,7 +172,7 @@ public class JpaToscaPolicyType extends JpaToscaEntityType { super.clean(); if (properties != null) { - for (JpaToscaProperty property : properties) { + for (JpaToscaProperty property : properties.values()) { property.clean(); } } @@ -169,7 +218,7 @@ public class JpaToscaPolicyType extends JpaToscaEntityType { private PfValidationResult validateProperties(final PfValidationResult resultIn) { PfValidationResult result = resultIn; - for (JpaToscaProperty property : properties) { + for (JpaToscaProperty property : properties.values()) { if (property == null) { result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, "policy property may not be null ")); @@ -258,13 +307,12 @@ public class JpaToscaPolicyType extends JpaToscaEntityType { final JpaToscaPolicyType copy = ((JpaToscaPolicyType) copyObject); super.copyTo(target); - final List<JpaToscaProperty> newProperties = new ArrayList<>(); - if (properties == null) { copy.setProperties(null); } else { - for (final JpaToscaProperty property : properties) { - newProperties.add(new JpaToscaProperty(property)); + final Map<String, JpaToscaProperty> newProperties = new LinkedHashMap<>(); + for (final Entry<String, JpaToscaProperty> propertyEntry : properties.entrySet()) { + newProperties.put(propertyEntry.getKey(), new JpaToscaProperty(propertyEntry.getValue())); } copy.setProperties(newProperties); } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypes.java index ce3a8130d..af8a21a60 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypes.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypes.java @@ -20,6 +20,7 @@ package org.onap.policy.models.tosca.simple.concepts; +import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -33,6 +34,7 @@ import lombok.EqualsAndHashCode; import org.onap.policy.models.base.PfConceptContainer; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; /** * This class is a container for TOSCA policy types. @@ -44,23 +46,23 @@ import org.onap.policy.models.base.PfConceptKey; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaPolicyTypes extends PfConceptContainer<JpaToscaPolicyType> { +public class JpaToscaPolicyTypes extends PfConceptContainer<JpaToscaPolicyType, ToscaPolicyType> { private static final long serialVersionUID = -4157979965271220098L; public static final String DEFAULT_NAME = "ToscaPolicyTypesSimple"; public static final String DEFAULT_VERSION = "1.0.0"; /** - * The Default Constructor creates a {@link JpaToscaPolicyTypes} object with a null artifact key - * and creates an empty concept map. + * The Default Constructor creates a {@link JpaToscaPolicyTypes} object with a null artifact key and creates an + * empty concept map. */ public JpaToscaPolicyTypes() { super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); } /** - * The Key Constructor creates a {@link JpaToscaPolicyTypes} object with the given artifact key and - * creates an empty concept map. + * The Key Constructor creates a {@link JpaToscaPolicyTypes} object with the given artifact key and creates an empty + * concept map. * * @param key the concept key */ @@ -86,4 +88,13 @@ public class JpaToscaPolicyTypes extends PfConceptContainer<JpaToscaPolicyType> public JpaToscaPolicyTypes(final JpaToscaPolicyTypes copyConcept) { super(copyConcept); } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaPolicyTypes(final List<Map<String, ToscaPolicyType>> authorativeConceptMapList) { + this.fromAuthorative(authorativeConceptMapList); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java index 376c2b3b2..38d5c0938 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java @@ -23,8 +23,7 @@ package org.onap.policy.models.tosca.simple.concepts; -import com.google.gson.annotations.SerializedName; - +import java.util.ArrayList; import java.util.List; import javax.persistence.Column; @@ -41,6 +40,7 @@ import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; @@ -49,6 +49,9 @@ import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; +import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; +import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty.Status; /** * Class to represent the property in TOSCA definition. @@ -61,13 +64,9 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = false) -public class JpaToscaProperty extends PfConcept { +public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> { private static final long serialVersionUID = 1675770231921107988L; - public enum Status { - SUPPORTED, UNSUPPORTED, EXPERIMENTAL, DEPRECATED - } - @EmbeddedId private PfReferenceKey key; @@ -81,7 +80,6 @@ public class JpaToscaProperty extends PfConcept { private boolean required = false; @Column(name = "default") - @SerializedName("default") private String defaultValue; @Column @@ -92,7 +90,6 @@ public class JpaToscaProperty extends PfConcept { private List<JpaToscaConstraint> constraints; @Column - @SerializedName("entry_schema") private JpaToscaEntrySchema entrySchema; /** @@ -131,18 +128,81 @@ public class JpaToscaProperty extends PfConcept { super(copyConcept); } + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaProperty(final ToscaProperty authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + @Override - public List<PfKey> getKeys() { - final List<PfKey> keyList = getKey().getKeys(); + public ToscaProperty toAuthorative() { + ToscaProperty toscaProperty = new ToscaProperty(); - keyList.addAll(type.getKeys()); + toscaProperty.setName(key.getLocalName()); + + toscaProperty.setType(type.getName()); + toscaProperty.setTypeVersion(type.getVersion()); + + toscaProperty.setDescription(description); + toscaProperty.setRequired(required); + toscaProperty.setDefaultValue(defaultValue); + toscaProperty.setStatus(status); if (constraints != null) { + List<ToscaConstraint> toscaConstraints = new ArrayList<>(); + for (JpaToscaConstraint constraint : constraints) { - keyList.addAll(constraint.getKeys()); + toscaConstraints.add(constraint.toAuthorative()); + } + + toscaProperty.setConstraints(toscaConstraints); + } + + if (entrySchema != null) { + toscaProperty.setEntrySchema(entrySchema.toAuthorative()); + } + + return toscaProperty; + } + + @Override + public void fromAuthorative(ToscaProperty toscaProperty) { + this.setKey(new PfReferenceKey()); + getKey().setLocalName(toscaProperty.getName()); + + if (toscaProperty.getTypeVersion() != null) { + type = new PfConceptKey(toscaProperty.getType(), toscaProperty.getTypeVersion()); + } else { + type = new PfConceptKey(toscaProperty.getType(), PfKey.NULL_KEY_VERSION); + } + + description = toscaProperty.getDescription(); + required = toscaProperty.isRequired(); + defaultValue = toscaProperty.getDefaultValue(); + status = toscaProperty.getStatus(); + + if (toscaProperty.getConstraints() != null) { + constraints = new ArrayList<>(); + + for (ToscaConstraint toscaConstraint : toscaProperty.getConstraints()) { + constraints.add(JpaToscaConstraint.newInstance(toscaConstraint)); } } + if (toscaProperty.getEntrySchema() != null) { + entrySchema = new JpaToscaEntrySchema(toscaProperty.getEntrySchema()); + } + } + + @Override + public List<PfKey> getKeys() { + final List<PfKey> keyList = getKey().getKeys(); + + keyList.addAll(type.getKeys()); + if (entrySchema != null) { keyList.addAll(entrySchema.getKeys()); } @@ -164,12 +224,6 @@ public class JpaToscaProperty extends PfConcept { defaultValue = defaultValue.trim(); } - if (constraints != null) { - for (JpaToscaConstraint constraint : constraints) { - constraint.clean(); - } - } - if (entrySchema != null) { entrySchema.clean(); } @@ -218,8 +272,6 @@ public class JpaToscaProperty extends PfConcept { if (constraint == null) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "property constraint may not be null ")); - } else { - result = constraint.validate(result); } } } @@ -247,8 +299,7 @@ public class JpaToscaProperty extends PfConcept { } /** - * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty - * object. + * Compare the fields of this ToscaProperty object with the fields of the other ToscaProperty object. * * @param other the other ToscaProperty object */ @@ -296,7 +347,17 @@ public class JpaToscaProperty extends PfConcept { copy.setRequired(required); copy.setDefaultValue(defaultValue); copy.setStatus(status); - copy.constraints = constraints; // Constraints are immutable + + if (constraints == null) { + copy.setConstraints(null); + } else { + final List<JpaToscaConstraint> newConstraints = new ArrayList<>(); + for (final JpaToscaConstraint constraint : constraints) { + newConstraints.add(constraint); // Constraints are immutable + } + copy.setConstraints(newConstraints); + } + copy.setEntrySchema(entrySchema != null ? new JpaToscaEntrySchema(entrySchema) : null); return copy; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java index fa2d2aac7..fda0c8014 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java @@ -39,16 +39,18 @@ import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** - * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA - * service template are implemented. + * This class holds a full TOSCA service template. Note: Only the policy specific parts of the TOSCA service template + * are implemented. * * @author Liam Fallon (liam.fallon@est.tech) */ @@ -57,7 +59,8 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaServiceTemplate extends JpaToscaEntityType { +public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemplate> + implements PfAuthorative<ToscaServiceTemplate> { private static final long serialVersionUID = 8084846046148349401L; public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple"; @@ -96,8 +99,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType { } /** - * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory - * parameters. + * The full constructor creates a {@link JpaToscaServiceTemplate} object with all mandatory parameters. * * @param key the key * @param toscaDefinitionsVersion the TOSCA version string @@ -116,6 +118,70 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType { super(copyConcept); } + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaServiceTemplate(final ToscaServiceTemplate authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaServiceTemplate toAuthorative() { + final ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate(); + + super.setToscaEntity(toscaServiceTemplate); + super.toAuthorative(); + + toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion); + + if (dataTypes != null) { + toscaServiceTemplate.setDataTypes(dataTypes.toAuthorative()); + } + + if (policyTypes != null) { + toscaServiceTemplate.setPolicyTypes(policyTypes.toAuthorative()); + } + + if (topologyTemplate != null) { + toscaServiceTemplate.setToscaTopologyTemplate(topologyTemplate.toAuthorative()); + } + + return toscaServiceTemplate; + } + + @Override + public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) { + super.fromAuthorative(toscaServiceTemplate); + + if (getKey().getName() == PfKey.NULL_KEY_NAME) { + getKey().setName(DEFAULT_NAME); + } + + if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) { + getKey().setVersion(DEFAULT_VERSION); + } + + toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion(); + + if (toscaServiceTemplate.getDataTypes() != null) { + dataTypes = new JpaToscaDataTypes(); + dataTypes.fromAuthorative(toscaServiceTemplate.getDataTypes()); + } + + if (toscaServiceTemplate.getPolicyTypes() != null) { + policyTypes = new JpaToscaPolicyTypes(); + policyTypes.fromAuthorative(toscaServiceTemplate.getPolicyTypes()); + } + + + if (toscaServiceTemplate.getToscaTopologyTemplate() != null) { + topologyTemplate = new JpaToscaTopologyTemplate(); + topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate()); + } + } + @Override public List<PfKey> getKeys() { final List<PfKey> keyList = super.getKeys(); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplates.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplates.java index 39e553144..08624f653 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplates.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplates.java @@ -20,6 +20,7 @@ package org.onap.policy.models.tosca.simple.concepts; +import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -33,6 +34,7 @@ import lombok.EqualsAndHashCode; import org.onap.policy.models.base.PfConceptContainer; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** * This class is a container for TOSCA service templates. @@ -44,7 +46,7 @@ import org.onap.policy.models.base.PfConceptKey; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = true) -public class JpaToscaServiceTemplates extends PfConceptContainer<JpaToscaServiceTemplate> { +public class JpaToscaServiceTemplates extends PfConceptContainer<JpaToscaServiceTemplate, ToscaServiceTemplate> { private static final long serialVersionUID = -3053257884307604114L; /** @@ -84,4 +86,13 @@ public class JpaToscaServiceTemplates extends PfConceptContainer<JpaToscaService public JpaToscaServiceTemplates(final JpaToscaServiceTemplates copyConcept) { super(copyConcept); } + + /** + * Authorative constructor. + * + * @param authorativeConceptMapList the authorative concept to copy from + */ + public JpaToscaServiceTemplates(final List<Map<String, ToscaServiceTemplate>> authorativeConceptMapList) { + this.fromAuthorative(authorativeConceptMapList); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java index 095435a7f..3476258cf 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java @@ -37,12 +37,14 @@ import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; /** * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are @@ -55,7 +57,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @EqualsAndHashCode(callSuper = false) -public class JpaToscaTopologyTemplate extends PfConcept { +public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> { private static final long serialVersionUID = 8969698734673232603L; public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple"; @@ -96,6 +98,38 @@ public class JpaToscaTopologyTemplate extends PfConcept { super(copyConcept); } + /** + * Authorative constructor. + * + * @param authorativeConcept the authorative concept to copy from + */ + public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) { + this.fromAuthorative(authorativeConcept); + } + + @Override + public ToscaTopologyTemplate toAuthorative() { + final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate(); + + toscaTopologyTemplate.setDescription(description); + + if (policies != null) { + toscaTopologyTemplate.setPolicies(policies.toAuthorative()); + } + + return toscaTopologyTemplate; + } + + @Override + public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) { + description = toscaTopologyTemplate.getDescription(); + + if (toscaTopologyTemplate.getPolicies() != null) { + policies = new JpaToscaPolicies(); + policies.fromAuthorative(toscaTopologyTemplate.getPolicies()); + } + } + @Override public List<PfKey> getKeys() { final List<PfKey> keyList = getKey().getKeys(); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTrigger.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTrigger.java index 0a32701c9..551dbe5bf 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTrigger.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTrigger.java @@ -148,12 +148,6 @@ public class JpaToscaTrigger extends PfConcept { if (targetFilter != null) { keyList.addAll(targetFilter.getKeys()); } - if (condition != null) { - keyList.addAll(condition.getKeys()); - } - if (constraint != null) { - keyList.addAll(constraint.getKeys()); - } return keyList; } @@ -170,12 +164,6 @@ public class JpaToscaTrigger extends PfConcept { if (targetFilter != null) { targetFilter.clean(); } - if (condition != null) { - condition.clean(); - } - if (constraint != null) { - constraint.clean(); - } method = (method != null ? method.trim() : method); action = action.trim(); @@ -233,8 +221,6 @@ public class JpaToscaTrigger extends PfConcept { result = (schedule != null ? schedule.validate(result) : result); result = (targetFilter != null ? targetFilter.validate(result) : result); - result = (condition != null ? condition.validate(result) : result); - result = (constraint != null ? constraint.validate(result) : result); return result; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypeJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypeJsonAdapter.java deleted file mode 100644 index 6b00b20b6..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypeJsonAdapter.java +++ /dev/null @@ -1,139 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import java.lang.reflect.Type; -import javax.ws.rs.core.Response; - -import lombok.NonNull; - -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * GSON type adapter for TOSCA data types. - * - * @author Chenfei Gao (cgao@research.att.com) - */ -public class ToscaDataTypeJsonAdapter implements JsonSerializer<JpaToscaDataType>, JsonDeserializer<JpaToscaDataType> { - - private static final Logger LOGGER = LoggerFactory.getLogger(ToscaDataTypeJsonAdapter.class); - - private static final String DERIVED_FROM = "derived_from"; - private static final String DESCRIPTION = "description"; - private static final String VERSION = "version"; - private static final String PROPERTIES = "properties"; - private static final String DEFAULT_VERSION = "1.0.0"; - - @Override - public JpaToscaDataType deserialize(@NonNull final JsonElement dataTypeElement, @NonNull final Type type, - @NonNull final JsonDeserializationContext context) { - - // The incoming JSON - final JsonObject dataTypeJsonMapObject = dataTypeElement.getAsJsonObject(); - - // We should only have a single entry for the policy type - if (dataTypeJsonMapObject.entrySet().size() != 1) { - String errorMessage = "a policy type list entry may only contain one and only one policy type"; - LOGGER.debug(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - final String dataTypeName = dataTypeJsonMapObject.entrySet().iterator().next().getKey(); - final JsonObject dataTypeJsonObject = dataTypeJsonMapObject.entrySet().iterator().next() - .getValue().getAsJsonObject(); - - // Set keys - PfConceptKey dataTypeKey; - if (dataTypeJsonObject.get(VERSION) == null) { - dataTypeKey = new PfConceptKey(dataTypeName, DEFAULT_VERSION); - } else { - dataTypeKey = new PfConceptKey(dataTypeName, dataTypeJsonObject.get(VERSION).getAsString()); - } - JpaToscaDataType dataType = new JpaToscaDataType(dataTypeKey); - - // Set derived_from - dataType.setDerivedFrom(new PfConceptKey(dataTypeJsonObject.get(DERIVED_FROM).getAsString(), - DEFAULT_VERSION)); - - // Set description - if (dataTypeJsonObject.has(DESCRIPTION)) { - final String dataTypeDescription = dataTypeJsonObject.get(DESCRIPTION).getAsString(); - dataType.setDescription(dataTypeDescription); - } - - // Set properties - if (dataTypeJsonObject.has(PROPERTIES)) { - dataType.setProperties( - new ToscaPropertiesJsonAdapter().deserializeProperties(dataTypeJsonObject.get(PROPERTIES))); - for (JpaToscaProperty property : dataType.getProperties()) { - property.getKey().setParentConceptKey(dataTypeKey); - property.getType().setVersion(dataType.getKey().getVersion()); - } - } - - return dataType; - } - - @Override - public JsonElement serialize(@NonNull final JpaToscaDataType dataType, @NonNull final Type type, - @NonNull final JsonSerializationContext context) { - - JsonObject dataTypeValJsonObject = new JsonObject(); - - // Add derived_from - if (dataType.getDerivedFrom() != null) { - dataTypeValJsonObject.addProperty(DERIVED_FROM, dataType.getDerivedFrom().getName()); - } - - // Add description - if (dataType.getDescription() != null) { - dataTypeValJsonObject.addProperty(DESCRIPTION, dataType.getDescription()); - } - - // Add version - if (dataType.getKey().getVersion() != null) { - dataTypeValJsonObject.addProperty(VERSION, dataType.getKey().getVersion()); - } - - // Add properties - if (dataType.getProperties() != null) { - JsonElement propertiesJsonElement = new ToscaPropertiesJsonAdapter() - .serializeProperties(dataType.getProperties()); - dataTypeValJsonObject.add(PROPERTIES, propertiesJsonElement); - } - - JsonObject dataTypeJsonObject = new JsonObject(); - dataTypeJsonObject.add(dataType.getKey().getName(), dataTypeValJsonObject); - return dataTypeJsonObject; - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypesJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypesJsonAdapter.java deleted file mode 100644 index 6131581ad..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypesJsonAdapter.java +++ /dev/null @@ -1,94 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import java.lang.reflect.Type; -import java.util.Iterator; -import javax.ws.rs.core.Response; -import lombok.NonNull; - -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * GSON type adapter for TOSCA data types. - * - * @author Chenfei Gao (cgao@research.att.com) - */ -public class ToscaDataTypesJsonAdapter - implements JsonSerializer<JpaToscaDataTypes>, JsonDeserializer<JpaToscaDataTypes> { - - private static final Logger LOGGER = LoggerFactory.getLogger(ToscaDataTypesJsonAdapter.class); - - @Override - public JpaToscaDataTypes deserialize(@NonNull final JsonElement dataTypesElement, @NonNull final Type type, - @NonNull final JsonDeserializationContext context) { - - // The incoming JSON - final JsonArray dataTypesJsonArray = dataTypesElement.getAsJsonArray(); - - // The outgoing object - final PfConceptKey dataTypesKey = new PfConceptKey("IncomingDataTypes", "0.0.1"); - final JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(dataTypesKey); - - // Get the dataTypes - Iterator<JsonElement> dataTypesIterator = dataTypesJsonArray.iterator(); - while (dataTypesIterator.hasNext()) { - JpaToscaDataType dataType = new ToscaDataTypeJsonAdapter().deserialize(dataTypesIterator.next(), - JpaToscaDataType.class, context); - - dataTypes.getConceptMap().put(dataType.getKey(), dataType); - } - - return dataTypes; - } - - @Override - public JsonElement serialize(@NonNull final JpaToscaDataTypes dataTypes, @NonNull final Type type, - @NonNull final JsonSerializationContext context) { - - JsonArray dataTypesJsonArray = new JsonArray(); - - if (dataTypes.getConceptMap().isEmpty()) { - String errorMessage = "data type list is empty"; - LOGGER.debug(errorMessage); - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage); - } - - for (JpaToscaDataType dataType : dataTypes.getConceptMap().values()) { - JsonElement dataTypeEntry = new ToscaDataTypeJsonAdapter().serialize(dataType, type, context); - dataTypesJsonArray.add(dataTypeEntry); - } - - return dataTypesJsonArray; - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPoliciesJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPoliciesJsonAdapter.java deleted file mode 100644 index ca0e3d937..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPoliciesJsonAdapter.java +++ /dev/null @@ -1,79 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import java.lang.reflect.Type; -import java.util.Iterator; -import lombok.NonNull; - -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; - -/** - * GSON type adapter for TOSCA policies. - * - * @author Liam Fallon (liam.fallon@est.tech) - * @author Chenfei Gao (cgao@research.att.com) - */ -public class ToscaPoliciesJsonAdapter implements JsonSerializer<JpaToscaPolicies>, JsonDeserializer<JpaToscaPolicies> { - - @Override - public JpaToscaPolicies deserialize(@NonNull final JsonElement policiesElement, @NonNull final Type type, - @NonNull final JsonDeserializationContext context) { - // The incoming JSON - final JsonArray policiesJsonArray = policiesElement.getAsJsonArray(); - - // The outgoing object - final PfConceptKey policiesKey = new PfConceptKey("IncomingPolicies", "0.0.1"); - final JpaToscaPolicies policies = new JpaToscaPolicies(policiesKey); - - // Get the policies - for (Iterator<JsonElement> policiesIterator = policiesJsonArray.iterator(); policiesIterator.hasNext(); ) { - JpaToscaPolicy policy = new ToscaPolicyJsonAdapter() - .deserialize(policiesIterator.next(), JpaToscaPolicy.class, context); - - policies.getConceptMap().put(policy.getKey(), policy); - } - - return policies; - } - - @Override - public JsonElement serialize(@NonNull final JpaToscaPolicies policies, @NonNull final Type type, - @NonNull final JsonSerializationContext context) { - - JsonArray policiesJsonArray = new JsonArray(); - - for (JpaToscaPolicy policy: policies.getConceptMap().values()) { - policiesJsonArray.add(new ToscaPolicyJsonAdapter().serialize(policy, type, context)); - } - return policiesJsonArray; - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyJsonAdapter.java deleted file mode 100644 index 6b666e214..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyJsonAdapter.java +++ /dev/null @@ -1,164 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import javax.ws.rs.core.Response; - -import lombok.NonNull; - -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * GSON type adapter for TOSCA policies. - * - * @author Liam Fallon (liam.fallon@est.tech) - * @author Chenfei Gao (cgao@research.att.com) - */ -public class ToscaPolicyJsonAdapter implements JsonSerializer<JpaToscaPolicy>, JsonDeserializer<JpaToscaPolicy> { - // Logger for this class - private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyJsonAdapter.class); - - private static final String TYPE = "type"; - private static final String DESCRIPTION = "description"; - private static final String VERSION = "version"; - private static final String METADATA = "metadata"; - private static final String PROPERTIES = "properties"; - - private final Gson gson = new GsonBuilder().setPrettyPrinting().create(); - - @Override - public JpaToscaPolicy deserialize(@NonNull final JsonElement policyElement, @NonNull final Type type, - @NonNull final JsonDeserializationContext context) { - - // The incoming JSON - final JsonObject policyJsonMapObject = policyElement.getAsJsonObject(); - - // We should only have a single entry for the policy - if (policyJsonMapObject.entrySet().size() != 1) { - String errorMessage = "a policy list entry may only contain one and only one policy"; - LOGGER.debug(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - final String policyName = policyJsonMapObject.entrySet().iterator().next().getKey(); - final JsonObject policyJsonObject = policyJsonMapObject.entrySet().iterator().next() - .getValue().getAsJsonObject(); - - // Set keys - PfConceptKey policyKey = new PfConceptKey(policyName, policyJsonObject.get(VERSION).getAsString()); - PfConceptKey policyTypeKey = new PfConceptKey( - policyJsonObject.get(TYPE).getAsString(), - policyJsonObject.get(VERSION).getAsString()); - JpaToscaPolicy policy = new JpaToscaPolicy(policyKey, policyTypeKey); - - // Set description - if (policyJsonObject.has(DESCRIPTION)) { - final String policyDescription = policyJsonObject.get(DESCRIPTION).getAsString(); - policy.setDescription(policyDescription); - } - - // Set metadata - if (policyJsonObject.has(METADATA)) { - final JsonObject policyMetadataMapObject = policyJsonObject.get(METADATA).getAsJsonObject(); - Map<String, String> policyMetadataMap = new HashMap<>(); - for (Entry<String, JsonElement> entry : policyMetadataMapObject.entrySet()) { - policyMetadataMap.put(entry.getKey(), entry.getValue().getAsString()); - } - policy.setMetadata(policyMetadataMap); - } - - // Set properties - if (policyJsonObject.has(PROPERTIES)) { - final JsonObject policyPropertiesMapObject = policyJsonObject.get(PROPERTIES).getAsJsonObject(); - Map<String, String> propertiesMap = new HashMap<>(); - for (Entry<String, JsonElement> entry : policyPropertiesMapObject.entrySet()) { - // TODO: This is a HACK, we need to validate the properties against their - // TODO: their data type in their policy type definition in TOSCA, which means reading - // TODO: the policy type from the database and parsing the property value object correctly - // TODO: Here we are simply serializing the property value into a string and storing it - // TODO: unvalidated into the database - propertiesMap.put(entry.getKey(), gson.toJson(entry.getValue())); - } - policy.setProperties(propertiesMap); - } - return policy; - } - - @Override - public JsonElement serialize(@NonNull final JpaToscaPolicy policy, @NonNull final Type type, - @NonNull final JsonSerializationContext context) { - - JsonObject policyValJsonObject = new JsonObject(); - - // Add type - policyValJsonObject.addProperty(TYPE, policy.getType().getName()); - - // Add version - policyValJsonObject.addProperty(VERSION, policy.getType().getVersion()); - - // Add description - if (policy.getDescription() != null) { - policyValJsonObject.addProperty(DESCRIPTION, policy.getDescription()); - } - - // Add metadata - if (policy.getMetadata() != null) { - JsonObject metadataMapObject = new JsonObject(); - for (Entry<String, String> entry : policy.getMetadata().entrySet()) { - metadataMapObject.addProperty(entry.getKey(), entry.getValue()); - } - policyValJsonObject.add(METADATA, metadataMapObject); - } - - // Add properties - if (policy.getProperties() != null) { - JsonObject propertiesMapObject = new JsonObject(); - for (Entry<String, String> entry : policy.getProperties().entrySet()) { - // TODO: This is the other direction of the HACK - JsonElement valueObject = gson.fromJson(entry.getValue(), JsonElement.class); - propertiesMapObject.add(entry.getKey(), valueObject); - } - policyValJsonObject.add(PROPERTIES, propertiesMapObject); - } - - JsonObject policyJsonObject = new JsonObject(); - policyJsonObject.add(policy.getKey().getName(), policyValJsonObject); - return policyJsonObject; - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypeJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypeJsonAdapter.java deleted file mode 100644 index a96642261..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypeJsonAdapter.java +++ /dev/null @@ -1,139 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import java.lang.reflect.Type; -import javax.ws.rs.core.Response; - -import lombok.NonNull; - -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * GSON type adapter for TOSCA policy types. - * - * @author Chenfei Gao (cgao@research.att.com) - */ -public class ToscaPolicyTypeJsonAdapter - implements JsonSerializer<JpaToscaPolicyType>, JsonDeserializer<JpaToscaPolicyType> { - - private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeJsonAdapter.class); - - private static final String DERIVED_FROM = "derived_from"; - private static final String DESCRIPTION = "description"; - private static final String VERSION = "version"; - private static final String PROPERTIES = "properties"; - private static final String DEFAULT_VERSION = "1.0.0"; - - @Override - public JpaToscaPolicyType deserialize(@NonNull final JsonElement policyTypeElement, @NonNull final Type type, - @NonNull final JsonDeserializationContext context) { - - // The incoming JSON - final JsonObject policyTypeJsonMapObject = policyTypeElement.getAsJsonObject(); - - // We should only have a single entry for the policy type - if (policyTypeJsonMapObject.entrySet().size() != 1) { - String errorMessage = "a policy type list entry may only contain one and only one policy type"; - LOGGER.debug(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - final String policyTypeName = policyTypeJsonMapObject.entrySet().iterator().next().getKey(); - final JsonObject policyTypeJsonObject = - policyTypeJsonMapObject.entrySet().iterator().next().getValue().getAsJsonObject(); - - // Set keys - PfConceptKey policyTypeKey; - if (policyTypeJsonObject.get(VERSION) == null) { - policyTypeKey = new PfConceptKey(policyTypeName, DEFAULT_VERSION); - } else { - policyTypeKey = new PfConceptKey(policyTypeName, policyTypeJsonObject.get(VERSION).getAsString()); - } - JpaToscaPolicyType policyType = new JpaToscaPolicyType(policyTypeKey); - - // Set derived_from - policyType.setDerivedFrom( - new PfConceptKey(policyTypeJsonObject.get(DERIVED_FROM).getAsString(), DEFAULT_VERSION)); - - // Set description - if (policyTypeJsonObject.has(DESCRIPTION)) { - final String policyTypeDescription = policyTypeJsonObject.get(DESCRIPTION).getAsString(); - policyType.setDescription(policyTypeDescription); - } - - // Set properties - if (policyTypeJsonObject.has(PROPERTIES)) { - policyType.setProperties( - new ToscaPropertiesJsonAdapter().deserializeProperties(policyTypeJsonObject.get(PROPERTIES))); - for (JpaToscaProperty property : policyType.getProperties()) { - property.getKey().setParentConceptKey(policyTypeKey); - property.getType().setVersion(policyType.getKey().getVersion()); - } - } - - return policyType; - } - - @Override - public JsonElement serialize(@NonNull final JpaToscaPolicyType policyType, @NonNull final Type type, - @NonNull final JsonSerializationContext context) { - - JsonObject policyTypeValJsonObject = new JsonObject(); - - // Add derived_from - if (policyType.getDerivedFrom() != null) { - policyTypeValJsonObject.addProperty(DERIVED_FROM, policyType.getDerivedFrom().getName()); - } - - // Add description - if (policyType.getDescription() != null) { - policyTypeValJsonObject.addProperty(DESCRIPTION, policyType.getDescription()); - } - - // Add version - if (policyType.getKey().getVersion() != null) { - policyTypeValJsonObject.addProperty(VERSION, policyType.getKey().getVersion()); - } - - // Add properties - if (policyType.getProperties() != null) { - JsonElement propertiesJsonElement = - new ToscaPropertiesJsonAdapter().serializeProperties(policyType.getProperties()); - policyTypeValJsonObject.add(PROPERTIES, propertiesJsonElement); - } - - JsonObject policyTypeJsonObject = new JsonObject(); - policyTypeJsonObject.add(policyType.getKey().getName(), policyTypeValJsonObject); - return policyTypeJsonObject; - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypesJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypesJsonAdapter.java deleted file mode 100644 index ac6d8e558..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypesJsonAdapter.java +++ /dev/null @@ -1,94 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import java.lang.reflect.Type; -import java.util.Iterator; -import javax.ws.rs.core.Response; -import lombok.NonNull; - -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * GSON type adapter for TOSCA policy types. - * - * @author Chenfei Gao (cgao@research.att.com) - */ -public class ToscaPolicyTypesJsonAdapter implements JsonSerializer<JpaToscaPolicyTypes>, - JsonDeserializer<JpaToscaPolicyTypes> { - - private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypesJsonAdapter.class); - - @Override - public JpaToscaPolicyTypes deserialize(@NonNull final JsonElement policyTypesElement, @NonNull final Type type, - @NonNull final JsonDeserializationContext context) { - - // The incoming JSON - final JsonArray policyTypesJsonArray = policyTypesElement.getAsJsonArray(); - - // The outgoing object - final PfConceptKey policyTypesKey = new PfConceptKey("IncomingPolicyTypes", "0.0.1"); - final JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(policyTypesKey); - - // Get the policyTypes - Iterator<JsonElement> policyTypesIterator = policyTypesJsonArray.iterator(); - while (policyTypesIterator.hasNext()) { - JpaToscaPolicyType policyType = new ToscaPolicyTypeJsonAdapter() - .deserialize(policyTypesIterator.next(), JpaToscaPolicyType.class, context); - - policyTypes.getConceptMap().put(policyType.getKey(), policyType); - } - - return policyTypes; - } - - @Override - public JsonElement serialize(@NonNull final JpaToscaPolicyTypes policyTypes, @NonNull final Type type, - @NonNull final JsonSerializationContext context) { - - JsonArray policyTypesJsonArray = new JsonArray(); - - if (policyTypes.getConceptMap().isEmpty()) { - String errorMessage = "policy type list is empty"; - LOGGER.debug(errorMessage); - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage); - } - - for (JpaToscaPolicyType policyType: policyTypes.getConceptMap().values()) { - JsonElement policyTypeEntry = new ToscaPolicyTypeJsonAdapter().serialize(policyType, type, context); - policyTypesJsonArray.add(policyTypeEntry); - } - - return policyTypesJsonArray; - } -}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPropertiesJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPropertiesJsonAdapter.java deleted file mode 100644 index b5c87557a..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPropertiesJsonAdapter.java +++ /dev/null @@ -1,284 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map.Entry; -import javax.ws.rs.core.Response; - -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.base.PfReferenceKey; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical.Operation; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogicalString; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintValidValues; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntrySchema; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * GSON type adapter for TOSCA properties. - * - * @author Chenfei Gao (cgao@research.att.com) - */ -public class ToscaPropertiesJsonAdapter { - - private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPropertiesJsonAdapter.class); - - private static final String DESCRIPTION = "description"; - private static final String REQUIRED = "required"; - private static final String DEFAULT = "default"; - private static final String TYPE = "type"; - private static final String ENTRY_SCHEMA = "entry_schema"; - private static final String CONSTRAINTS = "constraints"; - private static final String EQUAL = "equal"; - private static final String VALID_VALUES = "valid_values"; - private static final String DEFAULT_VERSION = "1.0.0"; - - /** - * Deserializes the properties. - * - * @param propertiesElement the properties in JsonElement - * - * @return deserialized ToscaProperty list - */ - public List<JpaToscaProperty> deserializeProperties(JsonElement propertiesElement) { - - final JsonObject propertiesMapObject = propertiesElement.getAsJsonObject(); - List<JpaToscaProperty> properties = new LinkedList<>(); - - for (Entry<String, JsonElement> entry : propertiesMapObject.entrySet()) { - final String propertyEntryKey = entry.getKey(); - final JsonElement propertyEntryVal = entry.getValue(); - - // Set property: key and type - JpaToscaProperty property = new JpaToscaProperty( - new PfReferenceKey(new PfConceptKey(), propertyEntryKey), - new PfConceptKey(propertyEntryVal.getAsJsonObject().get(TYPE).getAsString(), DEFAULT_VERSION)); - - // Set property: description - JsonObject propertyJsonObject = propertyEntryVal.getAsJsonObject(); - if (propertyJsonObject.has(DESCRIPTION)) { - property.setDescription(propertyJsonObject.get(DESCRIPTION).getAsString()); - } - - // Set property: required - if (propertyJsonObject.has(REQUIRED)) { - property.setRequired(propertyJsonObject.get(REQUIRED).getAsBoolean()); - } - - // Set property: default - if (propertyJsonObject.has(DEFAULT)) { - property.setDefaultValue(propertyJsonObject.get(DEFAULT).getAsString()); - } - - // Set property: entry_schema - if (propertyJsonObject.has(ENTRY_SCHEMA)) { - checkEntrySchemaCompatibility(property.getType().getName()); - property.setEntrySchema(deserializeEntrySchema(propertyJsonObject.get(ENTRY_SCHEMA))); - property.getEntrySchema().getKey().setParentConceptKey(property.getType()); - property.getEntrySchema().getType().setVersion(property.getType().getVersion()); - } - - // Set property: constraints - if (propertyJsonObject.has(CONSTRAINTS)) { - property.setConstraints(deserializeConstraints(propertyJsonObject.get(CONSTRAINTS))); - for (JpaToscaConstraint c : property.getConstraints()) { - c.getKey().setParentConceptKey(property.getType()); - } - } - - // Add property to properties list - properties.add(property); - } - - return properties; - } - - /** - * Serializes the properties. - * - * @param properties the list of ToscaProperty - * - * @return serialized JsonElement - */ - public JsonElement serializeProperties(List<JpaToscaProperty> properties) { - - JsonObject propertiesJsonObject = new JsonObject(); - - for (JpaToscaProperty property : properties) { - JsonObject propertyValJsonObject = new JsonObject(); - - // Add type - propertyValJsonObject.addProperty(TYPE, property.getType().getName()); - - // Add description - if (property.getDescription() != null) { - propertyValJsonObject.addProperty(DESCRIPTION, property.getDescription()); - } - - // Add required - propertyValJsonObject.addProperty(REQUIRED, property.isRequired()); - - // Add defaultValue - if (property.getDefaultValue() != null) { - propertyValJsonObject.addProperty(DEFAULT, property.getDefaultValue()); - } - - // Add constraints - if (property.getConstraints() != null) { - propertyValJsonObject.add(CONSTRAINTS, serializeConstraints(property.getConstraints())); - } - - // Add entry_schema - if (property.getEntrySchema() != null) { - propertyValJsonObject.add(ENTRY_SCHEMA, serializeEntrySchema(property.getEntrySchema())); - } - - propertiesJsonObject.add(property.getKey().getLocalName(), propertyValJsonObject); - } - - return propertiesJsonObject; - } - - private JsonElement serializeConstraints(List<JpaToscaConstraint> constraints) { - - JsonArray constraintsValJsonArray = new JsonArray(); - - for (JpaToscaConstraint c : constraints) { - JsonObject constraintJsonObject = new JsonObject(); - - // Check which type of constraint it is - // TODO: here we only support valid_values and equal - if (c instanceof JpaToscaConstraintValidValues) { - JsonArray validValuesJsonArray = new JsonArray(); - for (String validValue : ((JpaToscaConstraintValidValues)c).getValidValues()) { - validValuesJsonArray.add(validValue); - } - constraintJsonObject.add(VALID_VALUES, validValuesJsonArray); - } else if (c instanceof JpaToscaConstraintLogicalString) { - constraintJsonObject.addProperty(EQUAL, ((JpaToscaConstraintLogicalString)c).getCompareToString()); - } else { - String errorMessage = "constraint is neither valid_values nor equal"; - LOGGER.debug(errorMessage); - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage); - } - - constraintsValJsonArray.add(constraintJsonObject); - } - - return constraintsValJsonArray; - } - - private JsonElement serializeEntrySchema(JpaToscaEntrySchema entrySchema) { - - JsonObject entrySchemaValJsonObject = new JsonObject(); - - // Add type - entrySchemaValJsonObject.addProperty(TYPE, entrySchema.getType().getName()); - - // Add description - if (entrySchema.getDescription() != null) { - entrySchemaValJsonObject.addProperty(DESCRIPTION, entrySchema.getDescription()); - } - - // Add constraints - if (entrySchema.getConstraints() != null) { - entrySchemaValJsonObject.add(CONSTRAINTS, serializeConstraints(entrySchema.getConstraints())); - } - - return entrySchemaValJsonObject; - } - - private JpaToscaEntrySchema deserializeEntrySchema(JsonElement entrySchemaElement) { - - JsonObject entrySchemaJsonObject = entrySchemaElement.getAsJsonObject(); - - // Set entry_schema: key and type - JpaToscaEntrySchema entrySchema = new JpaToscaEntrySchema( - new PfReferenceKey(new PfConceptKey(), ENTRY_SCHEMA), - new PfConceptKey(entrySchemaJsonObject.get(TYPE).getAsString(), DEFAULT_VERSION)); - - // Set entry_schema: description - if (entrySchemaJsonObject.has(DESCRIPTION)) { - entrySchema.setDescription(entrySchemaJsonObject.get(DESCRIPTION).getAsString()); - } - - // Set entry_schema: constraints - if (entrySchemaJsonObject.has(CONSTRAINTS)) { - entrySchema.setConstraints(deserializeConstraints(entrySchemaJsonObject.get(CONSTRAINTS))); - for (JpaToscaConstraint c : entrySchema.getConstraints()) { - c.getKey().setParentConceptKey(entrySchema.getType()); - } - } - - return entrySchema; - } - - private List<JpaToscaConstraint> deserializeConstraints(JsonElement constraintsElement) { - - JsonArray constraintsJsonArray = constraintsElement.getAsJsonArray(); - List<JpaToscaConstraint> constraints = new LinkedList<>(); - - for (Iterator<JsonElement> constraintsIter = constraintsJsonArray.iterator(); constraintsIter.hasNext(); ) { - JsonObject constraintJsonObject = constraintsIter.next().getAsJsonObject(); - // Check which type of constraint it is - // TODO: here we only check 'valid_values' and 'equal' - if (constraintJsonObject.get(VALID_VALUES) != null) { - List<String> validValues = new LinkedList<>(); - for (Iterator<JsonElement> validValuesIter = constraintJsonObject.get(VALID_VALUES).getAsJsonArray() - .iterator(); validValuesIter.hasNext(); ) { - validValues.add(validValuesIter.next().getAsString()); - } - JpaToscaConstraint constraint = new JpaToscaConstraintValidValues( - new PfReferenceKey(new PfConceptKey(), VALID_VALUES), validValues); - constraints.add(constraint); - } else if (constraintJsonObject.get(EQUAL) != null) { - JpaToscaConstraint constraint = new JpaToscaConstraintLogicalString(new PfReferenceKey( - new PfConceptKey(), EQUAL), Operation.EQ, constraintJsonObject.get(EQUAL).getAsString()); - constraints.add(constraint); - } else { - String errorMessage = "specified constraint is neither valid_values nor equal"; - LOGGER.debug(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - } - - return constraints; - } - - private void checkEntrySchemaCompatibility(String type) { - if (!("list".equalsIgnoreCase(type)) && !("map".equalsIgnoreCase(type))) { - String errorMessage = "entry schema can only be specified for list or map property"; - LOGGER.debug(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java deleted file mode 100644 index bb2ace87e..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java +++ /dev/null @@ -1,122 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import java.lang.reflect.Type; - -import lombok.NonNull; - -import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; - -/** - * GSON type adapter for TOSCA policies. - * - * @author Liam Fallon (liam.fallon@est.tech) - * @author Chenfei Gao (cgao@research.att.com) - */ -public class ToscaServiceTemplateJsonAdapter - implements JsonSerializer<JpaToscaServiceTemplate>, JsonDeserializer<JpaToscaServiceTemplate> { - - private static final String TOPOLOGY_TEMPLATE = "topology_template"; - private static final String TOSCA_DEFINITIONS_VERSION = "tosca_definitions_version"; - private static final String POLICY_TYPES = "policy_types"; - private static final String DATA_TYPES = "data_types"; - - @Override - public JpaToscaServiceTemplate deserialize(@NonNull final JsonElement serviceTemplateElement, - @NonNull final Type type, @NonNull final JsonDeserializationContext context) { - - // The incoming JSON - final JsonObject serviceTemplateJsonObject = serviceTemplateElement.getAsJsonObject(); - - // The outgoing object - final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate - .setToscaDefinitionsVersion(serviceTemplateJsonObject.get(TOSCA_DEFINITIONS_VERSION).getAsString()); - - // Set topology_template - if (serviceTemplateJsonObject.has(TOPOLOGY_TEMPLATE)) { - serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplateJsonAdapter().deserialize( - serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE), JpaToscaTopologyTemplate.class, context)); - } - - // Set policy_types - if (serviceTemplateJsonObject.has(POLICY_TYPES)) { - serviceTemplate.setPolicyTypes(new ToscaPolicyTypesJsonAdapter() - .deserialize(serviceTemplateJsonObject.get(POLICY_TYPES), JpaToscaPolicyTypes.class, context)); - } - - // Set data_types - if (serviceTemplateJsonObject.has(DATA_TYPES)) { - serviceTemplate.setDataTypes(new ToscaDataTypesJsonAdapter() - .deserialize(serviceTemplateJsonObject.get(DATA_TYPES), JpaToscaDataTypes.class, context)); - } - - return serviceTemplate; - } - - @Override - public JsonElement serialize(@NonNull final JpaToscaServiceTemplate serviceTemplate, @NonNull final Type type, - @NonNull final JsonSerializationContext context) { - - JsonObject serviceTemplateJsonObject = new JsonObject(); - - // Serialize tosca_definitions_version - if (serviceTemplate.getToscaDefinitionsVersion() != null) { - serviceTemplateJsonObject.addProperty(TOSCA_DEFINITIONS_VERSION, - serviceTemplate.getToscaDefinitionsVersion()); - } - - // Serialize topoligy_template - if (serviceTemplate.getTopologyTemplate() != null) { - JsonElement topologyTemplateJsonElement = new ToscaTopologyTemplateJsonAdapter() - .serialize(serviceTemplate.getTopologyTemplate(), type, context); - serviceTemplateJsonObject.add(TOPOLOGY_TEMPLATE, topologyTemplateJsonElement); - } - - // Serialize policy_types - if (serviceTemplate.getPolicyTypes() != null) { - JsonElement policyTypesJsonElement = - new ToscaPolicyTypesJsonAdapter().serialize(serviceTemplate.getPolicyTypes(), type, context); - serviceTemplateJsonObject.add(POLICY_TYPES, policyTypesJsonElement); - } - - // Serialize data_types - if (serviceTemplate.getDataTypes() != null) { - JsonElement dataTypesJsonElement = - new ToscaDataTypesJsonAdapter().serialize(serviceTemplate.getDataTypes(), type, context); - serviceTemplateJsonObject.add(DATA_TYPES, dataTypesJsonElement); - } - - return serviceTemplateJsonObject; - } -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateMessageBodyHandler.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateMessageBodyHandler.java deleted file mode 100644 index b15a83016..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateMessageBodyHandler.java +++ /dev/null @@ -1,73 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.GsonBuilder; - -import org.onap.policy.common.gson.GsonMessageBodyHandler; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Provider used to serialize and deserialize TOSCA objects using GSON. - */ -public class ToscaServiceTemplateMessageBodyHandler extends GsonMessageBodyHandler { - - public static final Logger logger = LoggerFactory.getLogger(ToscaServiceTemplateMessageBodyHandler.class); - - /** - * Constructs the object. - */ - public ToscaServiceTemplateMessageBodyHandler() { - this(new GsonBuilder()); - - logger.info("Using GSON with TOSCA for REST calls"); - } - - /** - * Constructs the object. - * - * @param builder builder to use to create the gson object - */ - public ToscaServiceTemplateMessageBodyHandler(final GsonBuilder builder) { - // @formatter:off - super(builder - .registerTypeAdapter(JpaToscaServiceTemplate.class, new ToscaServiceTemplateJsonAdapter()) - .registerTypeAdapter(JpaToscaTopologyTemplate.class, new ToscaTopologyTemplateJsonAdapter()) - .registerTypeAdapter(JpaToscaPolicies.class, new ToscaPoliciesJsonAdapter()) - .registerTypeAdapter(JpaToscaPolicy.class, new ToscaPolicyJsonAdapter()) - .registerTypeAdapter(JpaToscaPolicyTypes.class, new ToscaPolicyTypesJsonAdapter()) - .registerTypeAdapter(JpaToscaPolicyType.class, new ToscaPolicyTypeJsonAdapter()) - .registerTypeAdapter(JpaToscaDataTypes.class, new ToscaDataTypesJsonAdapter()) - .registerTypeAdapter(JpaToscaDataType.class, new ToscaDataTypeJsonAdapter()) - .setPrettyPrinting() - .create() - ); - // @formatter:on - } - -} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaTopologyTemplateJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaTopologyTemplateJsonAdapter.java deleted file mode 100644 index df8ebb556..000000000 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaTopologyTemplateJsonAdapter.java +++ /dev/null @@ -1,91 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.simple.serialization; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import java.lang.reflect.Type; - -import lombok.NonNull; - -import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.base.PfReferenceKey; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; - -/** - * GSON type adapter for TOSCA policies. - * - * @author Liam Fallon (liam.fallon@est.tech) - * @author Chenfei Gao (cgao@research.att.com) - */ -public class ToscaTopologyTemplateJsonAdapter - implements JsonSerializer<JpaToscaTopologyTemplate>, JsonDeserializer<JpaToscaTopologyTemplate> { - - private static final String POLICIES = "policies"; - private static final String DESCRIPTION = "description"; - - @Override - public JpaToscaTopologyTemplate deserialize(@NonNull final JsonElement toplogyTemplateElement, - @NonNull final Type type, @NonNull final JsonDeserializationContext context) { - - // The incoming JSON - final JsonObject topologyTemplateJsonObject = toplogyTemplateElement.getAsJsonObject(); - - // The outgoing object - final PfReferenceKey topologyTemplateKey = new PfReferenceKey(new PfConceptKey(), "IncomingTopologyTemplate"); - final JpaToscaTopologyTemplate topologyTemplate = new JpaToscaTopologyTemplate(topologyTemplateKey); - - if (topologyTemplateJsonObject.has(DESCRIPTION)) { - topologyTemplate.setDescription(topologyTemplateJsonObject.get(DESCRIPTION).getAsString()); - } - - if (topologyTemplateJsonObject.has(POLICIES)) { - topologyTemplate.setPolicies(new ToscaPoliciesJsonAdapter() - .deserialize(topologyTemplateJsonObject.get(POLICIES), JpaToscaPolicies.class, context)); - } - - return topologyTemplate; - } - - @Override - public JsonElement serialize(@NonNull final JpaToscaTopologyTemplate topologyTemplate, @NonNull final Type type, - @NonNull final JsonSerializationContext context) { - - JsonObject topologyTemplateJsonObject = new JsonObject(); - JsonElement policiesJsonElement = new ToscaPoliciesJsonAdapter() - .serialize(topologyTemplate.getPolicies(), type, context); - - topologyTemplateJsonObject.add(POLICIES, policiesJsonElement); - - if (topologyTemplate.getDescription() != null) { - topologyTemplateJsonObject.addProperty(DESCRIPTION, topologyTemplate.getDescription()); - } - - return topologyTemplateJsonObject; - } -} |