diff options
author | Chenfei Gao <cgao@research.att.com> | 2019-03-25 22:42:39 -0400 |
---|---|---|
committer | Chenfei Gao <cgao@research.att.com> | 2019-03-26 09:13:48 -0400 |
commit | 2c5a396554a34cddc2f43d98c72665d5b1496f85 (patch) | |
tree | 95fb614e7793b98e9890060b8755dbb97569470e /models-tosca/src/main/java | |
parent | b6649e710c6aad05b6cbb64fa61d6aa0ad82f12a (diff) |
Implement serialization/deserialization for TOSCA concepts
Includes:
a) Build serialization for tosca policy
b) Build serialization for tosca policy type
c) Build serialization for tosca data type
d) Build deserialization for tosca policy
e) Build deserialization for tosca policy type
f) Build deserialization for tosca data type
g) Build plain tosca pojos for API endpoints
h) Build plain tosca pojo mapper to map from/to internal tosca representation
i) Add relevant junit tests
Issue-ID: POLICY-1441
Change-Id: I30a4a1337c756f675fc7a5521e5f9e1a5e3eb97a
Signed-off-by: Chenfei Gao <cgao@research.att.com>
Diffstat (limited to 'models-tosca/src/main/java')
24 files changed, 1425 insertions, 17 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaConstraint.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaConstraint.java new file mode 100644 index 000000000..85d0b5009 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaConstraint.java @@ -0,0 +1,41 @@ +/*- + * ============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.concepts; + +import com.google.gson.annotations.SerializedName; +import java.util.List; +import lombok.Data; + +/** + * Class to represent TOSCA constraint matching input/output from/to client. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@Data +public class PlainToscaConstraint { + + @SerializedName("valid_values") + private List<String> validValues; + + private String equal; +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaDataType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaDataType.java new file mode 100644 index 000000000..0581a7da1 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaDataType.java @@ -0,0 +1,47 @@ +/*- + * ============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.concepts; + +import com.google.gson.annotations.SerializedName; +import java.util.Map; +import lombok.Data; + +/** + * Class to represent TOSCA data type matching input/output from/to client. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@Data +public class PlainToscaDataType { + + @SerializedName("derived_from") + private String derivedFrom; + + private String version; + + private Map<String, String> metadata; + + private String description; + + private Map<String, PlainToscaProperty> properties; +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaEntrySchema.java new file mode 100644 index 000000000..ab9051aa9 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaEntrySchema.java @@ -0,0 +1,41 @@ +/*- + * ============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.concepts; + +import java.util.List; +import lombok.Data; + +/** + * Class to represent TOSCA entry schema matching input/output from/to client. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@Data +public class PlainToscaEntrySchema { + + private String type; + + private String description; + + private List<PlainToscaConstraint> constraints; +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaPolicy.java new file mode 100644 index 000000000..02ebe6537 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaPolicy.java @@ -0,0 +1,45 @@ +/*- + * ============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.concepts; + +import java.util.Map; +import lombok.Data; + +/** + * Class to represent TOSCA policy matching input/output from/to client. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@Data +public class PlainToscaPolicy { + + private String type; + + private String version; + + private String description; + + private Map<String, String> metadata; + + private Map<String, Object> properties; +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaPolicyType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaPolicyType.java new file mode 100644 index 000000000..499e2dd25 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaPolicyType.java @@ -0,0 +1,47 @@ +/*- + * ============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.concepts; + +import com.google.gson.annotations.SerializedName; +import java.util.Map; +import lombok.Data; + +/** + * Class to represent TOSCA policy type matching input/output from/to client. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@Data +public class PlainToscaPolicyType { + + @SerializedName("derived_from") + private String derivedFrom; + + private String version; + + private Map<String, String> metadata; + + private String description; + + private Map<String, PlainToscaProperty> properties; +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaProperty.java new file mode 100644 index 000000000..e5e282a95 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaProperty.java @@ -0,0 +1,50 @@ +/*- + * ============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.concepts; + +import com.google.gson.annotations.SerializedName; +import java.util.List; +import lombok.Data; + +/** + * Class to represent TOSCA property matching input/output from/to client. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@Data +public class PlainToscaProperty { + + private String type; + + private String description; + + private boolean required = false; + + @SerializedName("default") + private String defaultValue; + + @SerializedName("entry_schema") + private PlainToscaEntrySchema entrySchema; + + private List<PlainToscaConstraint> constraints; +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaServiceTemplate.java new file mode 100644 index 000000000..cf5e2d9d6 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaServiceTemplate.java @@ -0,0 +1,49 @@ +/*- + * ============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.concepts; + +import com.google.gson.annotations.SerializedName; +import java.util.List; +import java.util.Map; +import lombok.Data; + +/** + * Class to represent TOSCA service template matching input/output from/to client. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@Data +public class PlainToscaServiceTemplate { + + @SerializedName("tosca_definitions_version") + private String toscaDefinitionsVersion; + + @SerializedName("topology_template") + private PlainToscaTopologyTemplate toscaTopologyTemplate; + + @SerializedName("policy_types") + private List<Map<String, PlainToscaPolicyType>> policyTypes; + + @SerializedName("data_types") + private List<Map<String, PlainToscaDataType>> dataTypes; +}
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaTopologyTemplate.java new file mode 100644 index 000000000..16c1da05e --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaTopologyTemplate.java @@ -0,0 +1,40 @@ +/*- + * ============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.concepts; + +import java.util.List; +import java.util.Map; +import lombok.Data; + +/** + * Class to represent TOSCA topology template matching input/output from/to client. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@Data +public class PlainToscaTopologyTemplate { + + private String description; + + private List<Map<String, PlainToscaPolicy>> policies; +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/package-info.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/package-info.java new file mode 100644 index 000000000..2b98f9bc7 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/package-info.java @@ -0,0 +1,31 @@ +/*- + * ============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========================================================= + */ + +/** + * This package includes all TOSCA concept POJOs that can be parsed correctly by swagger-core and rendered + * as expected in swagger-ui. + */ +/** + * @author Chenfei Gao (cgao@research.att.com) + * + */ +package org.onap.policy.models.tosca.authorative.concepts;
\ No newline at end of file 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 new file mode 100644 index 000000000..aa7ca23b2 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapper.java @@ -0,0 +1,56 @@ +/*- + * ============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.PlainToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.mapping.ToscaServiceTemplateMapper; +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 ToscaServiceTemplateMapper<PlainToscaServiceTemplate, PlainToscaServiceTemplate> { + + private Gson defaultGson = new Gson(); + private Gson customGson = new ToscaServiceTemplateMessageBodyHandler().getGson(); + + @Override + public ToscaServiceTemplate toToscaServiceTemplate(PlainToscaServiceTemplate otherPolicy) { + + String serializedServiceTemplate = defaultGson.toJson(otherPolicy); + return customGson.fromJson(serializedServiceTemplate, ToscaServiceTemplate.class); + + } + + @Override + public PlainToscaServiceTemplate fromToscaServiceTemplate(ToscaServiceTemplate serviceTemplate) { + + String serializedServiceTemplate = customGson.toJson(serviceTemplate); + return defaultGson.fromJson(serializedServiceTemplate, PlainToscaServiceTemplate.class); + } +} 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/authorative/mapping/package-info.java new file mode 100644 index 000000000..e9b87c6ab --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/mapping/package-info.java @@ -0,0 +1,31 @@ +/*- + * ============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========================================================= + */ + +/** + * 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) + * + */ +package org.onap.policy.models.tosca.authorative.mapping;
\ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaConstraintValidValues.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaConstraintValidValues.java new file mode 100644 index 000000000..98629a603 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaConstraintValidValues.java @@ -0,0 +1,121 @@ +/*- + * ============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.concepts; + +import com.google.gson.annotations.SerializedName; + +import java.util.LinkedList; +import java.util.List; +import javax.persistence.ElementCollection; +import javax.ws.rs.core.Response; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; + +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfReferenceKey; + +/** + * This class represents valid_values TOSCA constraint. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +@EqualsAndHashCode(callSuper = false) +@Data +public class ToscaConstraintValidValues extends ToscaConstraint { + private static final long serialVersionUID = 3152323457560746844L; + + @SerializedName("valid_values") + @NonNull + @ElementCollection + private final List<String> validValues; + + /** + * The Default Constructor creates a {@link ToscaConstraintValidValues} object with a null key. + */ + public ToscaConstraintValidValues() { + this(new PfReferenceKey()); + } + + /** + * The Key Constructor creates a {@link ToscaConstraintValidValues} object with the given concept key. + * + * @param key the key of the constraint + */ + public ToscaConstraintValidValues(final PfReferenceKey key) { + super(key); + validValues = new LinkedList<>(); + } + + /** + * The Key Constructor creates a {@link ToscaConstraintLogical} 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 ToscaConstraintValidValues(final PfReferenceKey key, @NonNull final List<String> validValues) { + super(key); + this.validValues = validValues; + } + + /** + * Copy constructor. + * + * @param copyConcept the concept to copy from + */ + public ToscaConstraintValidValues(@NonNull final ToscaConstraintValidValues copyConcept) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "cannot copy an immutable constraint"); + } + + @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 ToscaConstraintValidValues other = (ToscaConstraintValidValues) otherConcept; + + int result = super.compareTo(other); + if (result != 0) { + return result; + } + + if (validValues.equals(other.validValues)) { + return 0; + } + return -1; + } + + @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/ToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaProperty.java index da13877c6..bc454c996 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaProperty.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaProperty.java @@ -23,6 +23,7 @@ package org.onap.policy.models.tosca.simple.concepts; +import com.google.gson.JsonElement; import com.google.gson.annotations.SerializedName; import java.util.List; @@ -82,7 +83,7 @@ public class ToscaProperty extends PfConcept { @Column(name = "default") @SerializedName("default") - private PfKey defaultValue; + private String defaultValue; @Column @NonNull @@ -137,10 +138,6 @@ public class ToscaProperty extends PfConcept { keyList.addAll(type.getKeys()); - if (defaultValue != null) { - keyList.addAll(defaultValue.getKeys()); - } - if (constraints != null) { for (ToscaConstraint constraint : constraints) { keyList.addAll(constraint.getKeys()); @@ -165,7 +162,7 @@ public class ToscaProperty extends PfConcept { } if (defaultValue != null) { - defaultValue.clean(); + defaultValue = defaultValue.trim(); } if (constraints != null) { @@ -212,7 +209,7 @@ public class ToscaProperty extends PfConcept { "property description may not be blank")); } - if (defaultValue != null && defaultValue.isNullKey()) { + if (defaultValue != null && defaultValue.trim().length() == 0) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "property default value may not be null")); } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java index 35381ec49..5b21ca0c1 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java @@ -78,7 +78,8 @@ public class ToscaTopologyTemplate extends PfConcept { } /** - * The Key Constructor creates a {@link ToscaTopologyTemplate} object with the given concept key. + * The Key Constructor creates a {@link ToscaTopologyTemplate} object with the given concept + * key. * * @param key the key */ 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 new file mode 100644 index 000000000..65e3d4ebf --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypeJsonAdapter.java @@ -0,0 +1,139 @@ +/*- + * ============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.ToscaDataType; +import org.onap.policy.models.tosca.simple.concepts.ToscaProperty; +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<ToscaDataType>, JsonDeserializer<ToscaDataType> { + + 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 ToscaDataType 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()); + } + ToscaDataType dataType = new ToscaDataType(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 (ToscaProperty property : dataType.getProperties()) { + property.getKey().setParentConceptKey(dataTypeKey); + property.getType().setVersion(dataType.getKey().getVersion()); + } + } + + return dataType; + } + + @Override + public JsonElement serialize(@NonNull final ToscaDataType 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 new file mode 100644 index 000000000..387b499cf --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypesJsonAdapter.java @@ -0,0 +1,93 @@ +/*- + * ============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.ToscaDataType; +import org.onap.policy.models.tosca.simple.concepts.ToscaDataTypes; +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<ToscaDataTypes>, JsonDeserializer<ToscaDataTypes> { + + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaDataTypesJsonAdapter.class); + + @Override + public ToscaDataTypes 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 ToscaDataTypes dataTypes = new ToscaDataTypes(dataTypesKey); + + // Get the dataTypes + Iterator<JsonElement> dataTypesIterator = dataTypesJsonArray.iterator(); + while (dataTypesIterator.hasNext()) { + ToscaDataType dataType = new ToscaDataTypeJsonAdapter() + .deserialize(dataTypesIterator.next(), ToscaDataType.class, context); + + dataTypes.getConceptMap().put(dataType.getKey(), dataType); + } + + return dataTypes; + } + + @Override + public JsonElement serialize(@NonNull final ToscaDataTypes 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 (ToscaDataType 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 index 53088d637..4b1b53c79 100644 --- 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 @@ -43,6 +43,7 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; * @author Chenfei Gao (cgao@research.att.com) */ public class ToscaPoliciesJsonAdapter implements JsonSerializer<ToscaPolicies>, JsonDeserializer<ToscaPolicies> { + @Override public ToscaPolicies deserialize(@NonNull final JsonElement policiesElement, @NonNull final Type type, @NonNull final JsonDeserializationContext context) { @@ -73,7 +74,6 @@ public class ToscaPoliciesJsonAdapter implements JsonSerializer<ToscaPolicies>, for (ToscaPolicy 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 index 95b4b3bba..b52634b83 100644 --- 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 @@ -151,7 +151,7 @@ public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, Json JsonObject propertiesMapObject = new JsonObject(); for (Entry<String, String> entry : policy.getProperties().entrySet()) { // TODO: This is the other direction of the HACK - JsonObject valueObject = gson.fromJson(entry.getValue(), JsonObject.class); + JsonElement valueObject = gson.fromJson(entry.getValue(), JsonElement.class); propertiesMapObject.add(entry.getKey(), valueObject); } policyValJsonObject.add(PROPERTIES, propertiesMapObject); 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 new file mode 100644 index 000000000..3bf98572f --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypeJsonAdapter.java @@ -0,0 +1,138 @@ +/*- + * ============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.ToscaPolicyType; +import org.onap.policy.models.tosca.simple.concepts.ToscaProperty; +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<ToscaPolicyType>, JsonDeserializer<ToscaPolicyType> { + + 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 ToscaPolicyType 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()); + } + ToscaPolicyType policyType = new ToscaPolicyType(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 (ToscaProperty property : policyType.getProperties()) { + property.getKey().setParentConceptKey(policyTypeKey); + property.getType().setVersion(policyType.getKey().getVersion()); + } + } + + return policyType; + } + + @Override + public JsonElement serialize(@NonNull final ToscaPolicyType 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; + } +}
\ No newline at end of file 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 new file mode 100644 index 000000000..c9e65117e --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypesJsonAdapter.java @@ -0,0 +1,94 @@ +/*- + * ============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.ToscaPolicyType; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes; +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<ToscaPolicyTypes>, + JsonDeserializer<ToscaPolicyTypes> { + + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypesJsonAdapter.class); + + @Override + public ToscaPolicyTypes 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 ToscaPolicyTypes policyTypes = new ToscaPolicyTypes(policyTypesKey); + + // Get the policyTypes + Iterator<JsonElement> policyTypesIterator = policyTypesJsonArray.iterator(); + while (policyTypesIterator.hasNext()) { + ToscaPolicyType policyType = new ToscaPolicyTypeJsonAdapter() + .deserialize(policyTypesIterator.next(), ToscaPolicyType.class, context); + + policyTypes.getConceptMap().put(policyType.getKey(), policyType); + } + + return policyTypes; + } + + @Override + public JsonElement serialize(@NonNull final ToscaPolicyTypes 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 (ToscaPolicyType 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 new file mode 100644 index 000000000..da15a800f --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPropertiesJsonAdapter.java @@ -0,0 +1,284 @@ +/*- + * ============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.ToscaConstraint; +import org.onap.policy.models.tosca.simple.concepts.ToscaConstraintLogical.Operation; +import org.onap.policy.models.tosca.simple.concepts.ToscaConstraintLogicalString; +import org.onap.policy.models.tosca.simple.concepts.ToscaConstraintValidValues; +import org.onap.policy.models.tosca.simple.concepts.ToscaEntrySchema; +import org.onap.policy.models.tosca.simple.concepts.ToscaProperty; +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<ToscaProperty> deserializeProperties(JsonElement propertiesElement) { + + final JsonObject propertiesMapObject = propertiesElement.getAsJsonObject(); + List<ToscaProperty> 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 + ToscaProperty property = new ToscaProperty( + 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 (ToscaConstraint 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<ToscaProperty> properties) { + + JsonObject propertiesJsonObject = new JsonObject(); + + for (ToscaProperty 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<ToscaConstraint> constraints) { + + JsonArray constraintsValJsonArray = new JsonArray(); + + for (ToscaConstraint 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 ToscaConstraintValidValues) { + JsonArray validValuesJsonArray = new JsonArray(); + for (String validValue : ((ToscaConstraintValidValues)c).getValidValues()) { + validValuesJsonArray.add(validValue); + } + constraintJsonObject.add(VALID_VALUES, validValuesJsonArray); + } else if (c instanceof ToscaConstraintLogicalString) { + constraintJsonObject.addProperty(EQUAL, ((ToscaConstraintLogicalString)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(ToscaEntrySchema 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 ToscaEntrySchema deserializeEntrySchema(JsonElement entrySchemaElement) { + + JsonObject entrySchemaJsonObject = entrySchemaElement.getAsJsonObject(); + + // Set entry_schema: key and type + ToscaEntrySchema entrySchema = new ToscaEntrySchema( + 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 (ToscaConstraint c : entrySchema.getConstraints()) { + c.getKey().setParentConceptKey(entrySchema.getType()); + } + } + + return entrySchema; + } + + private List<ToscaConstraint> deserializeConstraints(JsonElement constraintsElement) { + + JsonArray constraintsJsonArray = constraintsElement.getAsJsonArray(); + List<ToscaConstraint> 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()); + } + ToscaConstraint constraint = new ToscaConstraintValidValues( + new PfReferenceKey(new PfConceptKey(), VALID_VALUES), validValues); + constraints.add(constraint); + } else if (constraintJsonObject.get(EQUAL) != null) { + ToscaConstraint constraint = new ToscaConstraintLogicalString(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 index 40fe63c74..e25adfd3e 100644 --- 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 @@ -32,8 +32,13 @@ import java.lang.reflect.Type; import lombok.NonNull; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.simple.concepts.ToscaDataTypes; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * GSON type adapter for TOSCA policies. @@ -44,8 +49,12 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate; public class ToscaServiceTemplateJsonAdapter implements JsonSerializer<ToscaServiceTemplate>, JsonDeserializer<ToscaServiceTemplate> { + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaServiceTemplateJsonAdapter.class); + 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 ToscaServiceTemplate deserialize(@NonNull final JsonElement serviceTemplateElement, @NonNull final Type type, @@ -55,17 +64,31 @@ public class ToscaServiceTemplateJsonAdapter final JsonObject serviceTemplateJsonObject = serviceTemplateElement.getAsJsonObject(); // The outgoing object - final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + final PfConceptKey serviceTemplateKey = new PfConceptKey("IncomingServiceTemplate", "0.0.1"); + final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey); + + // Set tosca_definitions_version 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), ToscaTopologyTemplate.class, context)); + serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplateKey); + } + + // Set policy_types + if (serviceTemplateJsonObject.has(POLICY_TYPES)) { + serviceTemplate.setPolicyTypes(new ToscaPolicyTypesJsonAdapter().deserialize( + serviceTemplateJsonObject.get(POLICY_TYPES), ToscaPolicyTypes.class, context)); } - // Set the parent key of the topology template to be this service template - serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplate.getKey()); + // Set data_types + if (serviceTemplateJsonObject.has(DATA_TYPES)) { + serviceTemplate.setDataTypes(new ToscaDataTypesJsonAdapter().deserialize( + serviceTemplateJsonObject.get(DATA_TYPES), ToscaDataTypes.class, context)); + } return serviceTemplate; } @@ -75,11 +98,33 @@ public class ToscaServiceTemplateJsonAdapter @NonNull final JsonSerializationContext context) { JsonObject serviceTemplateJsonObject = new JsonObject(); - JsonElement topologyTemplateJsonElement = new ToscaTopologyTemplateJsonAdapter() - .serialize(serviceTemplate.getTopologyTemplate(), type, context); - serviceTemplateJsonObject.addProperty(TOSCA_DEFINITIONS_VERSION, serviceTemplate.getToscaDefinitionsVersion()); - serviceTemplateJsonObject.add(TOPOLOGY_TEMPLATE, topologyTemplateJsonElement); + // 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 index cf3e668b3..c7e78a524 100644 --- 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 @@ -21,8 +21,12 @@ 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.ToscaDataType; +import org.onap.policy.models.tosca.simple.concepts.ToscaDataTypes; import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate; import org.slf4j.Logger; @@ -56,6 +60,10 @@ public class ToscaServiceTemplateMessageBodyHandler extends GsonMessageBodyHandl .registerTypeAdapter(ToscaTopologyTemplate.class, new ToscaTopologyTemplateJsonAdapter()) .registerTypeAdapter(ToscaPolicies.class, new ToscaPoliciesJsonAdapter()) .registerTypeAdapter(ToscaPolicy.class, new ToscaPolicyJsonAdapter()) + .registerTypeAdapter(ToscaPolicyTypes.class, new ToscaPolicyTypesJsonAdapter()) + .registerTypeAdapter(ToscaPolicyType.class, new ToscaPolicyTypeJsonAdapter()) + .registerTypeAdapter(ToscaDataTypes.class, new ToscaDataTypesJsonAdapter()) + .registerTypeAdapter(ToscaDataType.class, new ToscaDataTypeJsonAdapter()) .setPrettyPrinting() .create() ); 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 index a2974fdbd..d302b7e6c 100644 --- 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 @@ -47,6 +47,7 @@ public class ToscaTopologyTemplateJsonAdapter implements JsonSerializer<ToscaTopologyTemplate>, JsonDeserializer<ToscaTopologyTemplate> { private static final String POLICIES = "policies"; + private static final String DESCRIPTION = "description"; @Override public ToscaTopologyTemplate deserialize(@NonNull final JsonElement toplogyTemplateElement, @@ -59,6 +60,10 @@ public class ToscaTopologyTemplateJsonAdapter final PfReferenceKey topologyTemplateKey = new PfReferenceKey(new PfConceptKey(), "IncomingTopologyTemplate"); final ToscaTopologyTemplate topologyTemplate = new ToscaTopologyTemplate(topologyTemplateKey); + if (topologyTemplateJsonObject.has(DESCRIPTION)) { + topologyTemplate.setDescription(topologyTemplateJsonObject.get(DESCRIPTION).getAsString()); + } + if (topologyTemplateJsonObject.has(POLICIES)) { topologyTemplate.setPolicies(new ToscaPoliciesJsonAdapter() .deserialize(topologyTemplateJsonObject.get(POLICIES), ToscaPolicies.class, context)); @@ -76,6 +81,11 @@ public class ToscaTopologyTemplateJsonAdapter .serialize(topologyTemplate.getPolicies(), type, context); topologyTemplateJsonObject.add(POLICIES, policiesJsonElement); + + if (topologyTemplate.getDescription() != null) { + topologyTemplateJsonObject.addProperty(DESCRIPTION, topologyTemplate.getDescription()); + } + return topologyTemplateJsonObject; } } |