From 2c5a396554a34cddc2f43d98c72665d5b1496f85 Mon Sep 17 00:00:00 2001 From: Chenfei Gao Date: Mon, 25 Mar 2019 22:42:39 -0400 Subject: 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 --- .../authorative/concepts/PlainToscaConstraint.java | 41 ++ .../authorative/concepts/PlainToscaDataType.java | 47 +++ .../concepts/PlainToscaEntrySchema.java | 41 ++ .../authorative/concepts/PlainToscaPolicy.java | 45 +++ .../authorative/concepts/PlainToscaPolicyType.java | 47 +++ .../authorative/concepts/PlainToscaProperty.java | 50 +++ .../concepts/PlainToscaServiceTemplate.java | 49 +++ .../concepts/PlainToscaTopologyTemplate.java | 40 ++ .../tosca/authorative/concepts/package-info.java | 31 ++ .../mapping/PlainToscaServiceTemplateMapper.java | 56 +++ .../tosca/authorative/mapping/package-info.java | 31 ++ .../concepts/ToscaConstraintValidValues.java | 121 ++++++ .../tosca/simple/concepts/ToscaProperty.java | 11 +- .../simple/concepts/ToscaTopologyTemplate.java | 3 +- .../serialization/ToscaDataTypeJsonAdapter.java | 139 +++++++ .../serialization/ToscaDataTypesJsonAdapter.java | 93 +++++ .../serialization/ToscaPoliciesJsonAdapter.java | 2 +- .../serialization/ToscaPolicyJsonAdapter.java | 2 +- .../serialization/ToscaPolicyTypeJsonAdapter.java | 138 +++++++ .../serialization/ToscaPolicyTypesJsonAdapter.java | 94 +++++ .../serialization/ToscaPropertiesJsonAdapter.java | 284 ++++++++++++++ .../ToscaServiceTemplateJsonAdapter.java | 59 ++- .../ToscaServiceTemplateMessageBodyHandler.java | 8 + .../ToscaTopologyTemplateJsonAdapter.java | 10 + .../tosca/authorative/concepts/TestPojos.java | 52 +++ .../PlainToscaServiceTemplateMapperTest.java | 94 +++++ .../tosca/simple/concepts/ToscaPolicyTest.java | 4 +- .../tosca/simple/concepts/ToscaPropertyTest.java | 14 +- .../MonitoringPolicySerializationTest.java | 247 ++++++++++-- .../MonitoringPolicyTypeSerializationTest.java | 424 +++++++++++++++++++++ 30 files changed, 2223 insertions(+), 54 deletions(-) create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaConstraint.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaDataType.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaEntrySchema.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaPolicy.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaPolicyType.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaProperty.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaServiceTemplate.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/PlainToscaTopologyTemplate.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/package-info.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapper.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/mapping/package-info.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaConstraintValidValues.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypeJsonAdapter.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaDataTypesJsonAdapter.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypeJsonAdapter.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyTypesJsonAdapter.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPropertiesJsonAdapter.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java (limited to 'models-tosca') 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 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 metadata; + + private String description; + + private Map 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 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 metadata; + + private Map 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 metadata; + + private String description; + + private Map 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 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> policyTypes; + + @SerializedName("data_types") + private List> 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> 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 { + + 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 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 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, JsonDeserializer { + + 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, JsonDeserializer { + + 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 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, JsonDeserializer { + @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, 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, Json JsonObject propertiesMapObject = new JsonObject(); for (Entry 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, JsonDeserializer { + + 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, + JsonDeserializer { + + 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 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 deserializeProperties(JsonElement propertiesElement) { + + final JsonObject propertiesMapObject = propertiesElement.getAsJsonObject(); + List properties = new LinkedList<>(); + + for (Entry 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 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 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 deserializeConstraints(JsonElement constraintsElement) { + + JsonArray constraintsJsonArray = constraintsElement.getAsJsonArray(); + List constraints = new LinkedList<>(); + + for (Iterator 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 validValues = new LinkedList<>(); + for (Iterator 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, JsonDeserializer { + 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, JsonDeserializer { 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; } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java new file mode 100644 index 000000000..4dd55d562 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java @@ -0,0 +1,52 @@ +/*- + * ============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.openpojo.reflection.filters.FilterPackageInfo; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.GetterMustExistRule; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; +import org.junit.Test; +import org.onap.policy.common.utils.validation.ToStringTester; + +/** + * Class to perform unit tests of all pojos + * + * @author Chenfei Gao (cgao@research.att.com) + * + */ +public class TestPojos { + + private static final String POJO_PACKAGE = "org.onap.policy.models.tosca.authorative.concepts"; + + @Test + public void testPojos() { + final Validator validator = ValidatorBuilder.create().with(new ToStringTester()) + .with(new SetterMustExistRule()).with(new GetterMustExistRule()).with(new SetterTester()) + .with(new GetterTester()).build(); + validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java new file mode 100644 index 000000000..bd6b26bb9 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java @@ -0,0 +1,94 @@ +/*- + * ============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 static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import java.io.IOException; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.PlainToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; +import org.yaml.snakeyaml.Yaml; + +/** + * This class performs unit test of {@link PlainToscaServiceTemplateMapper}} + * + * @author Chenfei Gao (cgao@research.att.com) + */ +public class PlainToscaServiceTemplateMapperTest { + + private Gson defaultGson; + private PlainToscaServiceTemplateMapper mapper; + + @Before + public void setUp() { + defaultGson = new Gson(); + mapper = new PlainToscaServiceTemplateMapper(); + } + + @Test + public void testPlainToscaPolicies() throws JsonSyntaxException, IOException { + try { + String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"); + + PlainToscaServiceTemplate plainPolicies = defaultGson.fromJson(inputJson, PlainToscaServiceTemplate.class); + ToscaServiceTemplate internalPolicies = mapper.toToscaServiceTemplate(plainPolicies); + assertTrue(internalPolicies.validate(new PfValidationResult()).isValid()); + PlainToscaServiceTemplate plainPolicies2 = mapper.fromToscaServiceTemplate(internalPolicies); + assertTrue(plainPolicies.equals(plainPolicies2)); + + } catch (Exception e) { + fail("no exception should be thrown"); + } + } + + @Test + public void testPlainToscaPolicyTypes() throws JsonSyntaxException, IOException { + try { + Yaml yaml = new Yaml(); + String inputYaml = ResourceUtils.getResourceAsString( + "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml"); + Object yamlObject = yaml.load(inputYaml); + String yamlAsJsonString = defaultGson.toJson(yamlObject); + + PlainToscaServiceTemplate plainPolicyTypes = defaultGson.fromJson(yamlAsJsonString, + PlainToscaServiceTemplate.class); + ToscaServiceTemplate internalPolicyTypes = mapper.toToscaServiceTemplate(plainPolicyTypes); + assertTrue(internalPolicyTypes.validate(new PfValidationResult()).isValid()); + PlainToscaServiceTemplate plainPolicyTypes2 = mapper.fromToscaServiceTemplate(internalPolicyTypes); + ToscaServiceTemplate internalPolicyTypes2 = mapper.toToscaServiceTemplate(plainPolicyTypes2); + assertTrue(internalPolicyTypes2.validate(new PfValidationResult()).isValid()); + assertTrue(internalPolicyTypes.compareTo(internalPolicyTypes2) == 0); + + } catch (Exception e) { + fail("no exception should be thrown"); + } + + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTest.java index 807f33ed2..01c1377b9 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTest.java @@ -37,7 +37,7 @@ import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; /** - * DAO test for ToscaDatatype. + * DAO test for ToscaPolicy. * * @author Liam Fallon (liam.fallon@est.tech) */ @@ -171,4 +171,4 @@ public class ToscaPolicyTest { assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); } } -} +} \ No newline at end of file diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPropertyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPropertyTest.java index a33da605e..0fcf96a89 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPropertyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaPropertyTest.java @@ -99,8 +99,7 @@ public class ToscaPropertyTest { tp.setRequired(false); assertFalse(tp.isRequired()); - PfConceptKey tdefaultKey = new PfConceptKey("defaultKey", "0.0.1"); - tp.setDefaultValue(tdefaultKey); + tp.setDefaultValue("defaultKey"); tp.setStatus(ToscaProperty.Status.SUPPORTED); @@ -141,7 +140,7 @@ public class ToscaPropertyTest { assertFalse(tp.compareTo(otherDt) == 0); otherDt.setRequired(false); assertFalse(tp.compareTo(otherDt) == 0); - otherDt.setDefaultValue(tdefaultKey); + otherDt.setDefaultValue("defaultKey"); assertFalse(tp.compareTo(otherDt) == 0); otherDt.setStatus(ToscaProperty.Status.SUPPORTED); assertFalse(tp.compareTo(otherDt) == 0); @@ -168,7 +167,7 @@ public class ToscaPropertyTest { assertEquals("target is marked @NonNull but is null", exc.getMessage()); } - assertEquals(6, tp.getKeys().size()); + assertEquals(5, tp.getKeys().size()); assertEquals(2, new ToscaProperty().getKeys().size()); new ToscaProperty().clean(); @@ -197,12 +196,7 @@ public class ToscaPropertyTest { tp.setDefaultValue(null); assertTrue(tp.validate(new PfValidationResult()).isValid()); - tp.setDefaultValue(tdefaultKey); - assertTrue(tp.validate(new PfValidationResult()).isValid()); - - tp.setDefaultValue(PfConceptKey.getNullKey()); - assertFalse(tp.validate(new PfValidationResult()).isValid()); - tp.setDefaultValue(tdefaultKey); + tp.setDefaultValue("defaultKey"); assertTrue(tp.validate(new PfValidationResult()).isValid()); tp.getConstraints().add(null); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java index 95f0ac971..e49156330 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java @@ -1,6 +1,7 @@ /*- * ============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. @@ -23,17 +24,22 @@ package org.onap.policy.models.tosca.simple.serialization; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import java.io.IOException; - +import java.util.Map; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler; import org.slf4j.Logger; @@ -44,11 +50,19 @@ import org.yaml.snakeyaml.Yaml; * Test serialization of monitoring policies. * * @author Liam Fallon (liam.fallon@est.tech) + * @author Chenfei Gao (cgao@research.att.com) */ public class MonitoringPolicySerializationTest { - // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicySerializationTest.class); + private static final String VCPE_MONITORING_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json"; + private static final String VCPE_MONITORING_INPUT_YAML = "policies/vCPE.policy.monitoring.input.tosca.yaml"; + private static final String VDNS_MONITORING_INPUT_JSON = "policies/vDNS.policy.monitoring.input.tosca.json"; + private static final String VDNS_MONITORING_INPUT_YAML = "policies/vDNS.policy.monitoring.input.tosca.yaml"; + private static final String VFW_MONITORING_INPUT_JSON = "policies/vFirewall.policy.monitoring.input.tosca.json"; + private static final String VFW_MONITORING_INPUT_YAML = "policies/vFirewall.policy.monitoring.input.tosca.yaml"; + private Gson gson; @Before @@ -57,48 +71,231 @@ public class MonitoringPolicySerializationTest { } @Test - public void testJsonDeserialization() throws JsonSyntaxException, IOException { - String vcpePolicyJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"); + public void testDeserialization() { + try { + // vCPE + ToscaServiceTemplate serviceTemplateFromJson = deserializeMonitoringInputJson(VCPE_MONITORING_INPUT_JSON); + verifyVcpeMonitoringInputDeserialization(serviceTemplateFromJson); + ToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(VCPE_MONITORING_INPUT_YAML); + assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0); + + // vDNS + serviceTemplateFromJson = deserializeMonitoringInputJson(VDNS_MONITORING_INPUT_JSON); + verifyVdnsMonitoringInputDeserialization(serviceTemplateFromJson); + serviceTemplateFromYaml = deserializeMonitoringInputYaml(VDNS_MONITORING_INPUT_YAML); + assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0); + + // vFirewall + serviceTemplateFromJson = deserializeMonitoringInputJson(VFW_MONITORING_INPUT_JSON); + verifyVfwMonitoringInputDeserialization(serviceTemplateFromJson); + serviceTemplateFromYaml = deserializeMonitoringInputYaml(VFW_MONITORING_INPUT_YAML); + assertTrue(serviceTemplateFromJson.compareTo(serviceTemplateFromYaml) == 0); + + } catch (Exception e) { + fail("No exception should be thrown"); + } + } + + @Test + public void testSerialization() { + try { + // vCPE + ToscaServiceTemplate serviceTemplate = deserializeMonitoringInputJson(VCPE_MONITORING_INPUT_JSON); + String serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate); + verifyVcpeMonitoringOutputserialization(serializedServiceTemplate); + + // vDNS + serviceTemplate = deserializeMonitoringInputJson(VDNS_MONITORING_INPUT_JSON); + serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate); + verifyVdnsMonitoringOutputserialization(serializedServiceTemplate); + + // vFirewall + serviceTemplate = deserializeMonitoringInputJson(VFW_MONITORING_INPUT_JSON); + serializedServiceTemplate = serializeMonitoringServiceTemplate(serviceTemplate); + verifyVfwMonitoringOutputserialization(serializedServiceTemplate); + + } catch (Exception e) { + fail("No exception should be thrown"); + } + } + + private ToscaServiceTemplate deserializeMonitoringInputJson(String resourcePath) + throws JsonSyntaxException, IOException { - ToscaServiceTemplate serviceTemplate = gson.fromJson(vcpePolicyJson, ToscaServiceTemplate.class); + String policyJson = ResourceUtils.getResourceAsString(resourcePath); + ToscaServiceTemplate serviceTemplate = gson.fromJson(policyJson, ToscaServiceTemplate.class); + return serviceTemplate; + } + + private ToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) + throws JsonSyntaxException, IOException { + + Yaml yaml = new Yaml(); + String policyYaml = ResourceUtils.getResourceAsString(resourcePath); + Object yamlObject = yaml.load(policyYaml); + String yamlAsJsonString = new Gson().toJson(yamlObject); + ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class); + return serviceTemplate; + } + + private String serializeMonitoringServiceTemplate(ToscaServiceTemplate serviceTemplate) { + return gson.toJson(serviceTemplate); + } + + private void verifyVcpeMonitoringInputDeserialization(ToscaServiceTemplate serviceTemplate) { + + // Sanity check the entire structure assertNotNull(serviceTemplate); LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString()); assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); + // Check tosca_definitions_version + assertEquals("tosca_simple_yaml_1_0_0", + serviceTemplate.getToscaDefinitionsVersion()); + + Map policiesConceptMap = serviceTemplate.getTopologyTemplate() + .getPolicies().getConceptMap(); + + // Check policies + assertTrue(policiesConceptMap.size() == 1); + assertEquals("onap.restart.tca", policiesConceptMap.keySet().iterator().next().getName()); assertEquals("onap.restart.tca:1.0.0", serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId()); - String reserializedString = gson.toJson(serviceTemplate, ToscaServiceTemplate.class); - assertEquals(vcpePolicyJson.replaceAll("\\s+", ""), reserializedString.replaceAll("\\s+", "")); + ToscaPolicy policyVal = policiesConceptMap.values().iterator().next(); + + // Check metadata + assertTrue(policyVal.getMetadata().size() == 1); + assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey()); + assertEquals("onap.restart.tca", policyVal.getMetadata().entrySet().iterator().next().getValue()); - ToscaServiceTemplate serviceTemplate2 = gson.fromJson(reserializedString, ToscaServiceTemplate.class); - assertNotNull(serviceTemplate2); - assertEquals(serviceTemplate, serviceTemplate2); + // Check properties + assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1); + assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next()); + assertNotNull(policyVal.getProperties().values().iterator().next()); } - @Test - public void testYamlDeserialization() throws JsonSyntaxException, IOException { - Yaml yaml = new Yaml(); + private void verifyVdnsMonitoringInputDeserialization(ToscaServiceTemplate serviceTemplate) { - String vcpePolicyYaml = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.yaml"); - Object yamlObject = yaml.load(vcpePolicyYaml); + // Sanity check the entire structure + assertNotNull(serviceTemplate); + LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString()); + assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); - String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); + // Check tosca_definitions_version + assertEquals("tosca_simple_yaml_1_0_0", + serviceTemplate.getToscaDefinitionsVersion()); - ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class); + Map policiesConceptMap = serviceTemplate.getTopologyTemplate() + .getPolicies().getConceptMap(); + + // Check policies + assertTrue(policiesConceptMap.size() == 1); + assertEquals("onap.scaleout.tca", policiesConceptMap.keySet().iterator().next().getName()); + assertEquals("onap.scaleout.tca:1.0.0", + serviceTemplate.getTopologyTemplate().getPolicies().get("onap.scaleout.tca").getId()); + ToscaPolicy policyVal = policiesConceptMap.values().iterator().next(); + + // Check metadata + assertTrue(policyVal.getMetadata().size() == 1); + assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey()); + assertEquals("onap.scaleout.tca", policyVal.getMetadata().entrySet().iterator().next().getValue()); + + // Check properties + assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1); + assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next()); + assertNotNull(policyVal.getProperties().values().iterator().next()); + } + + private void verifyVfwMonitoringInputDeserialization(ToscaServiceTemplate serviceTemplate) { + + // Sanity check the entire structure assertNotNull(serviceTemplate); LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString()); assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); - assertEquals("onap.restart.tca:1.0.0", - serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId()); + // Check tosca_definitions_version + assertEquals("tosca_simple_yaml_1_0_0", + serviceTemplate.getToscaDefinitionsVersion()); + + Map policiesConceptMap = serviceTemplate.getTopologyTemplate() + .getPolicies().getConceptMap(); + + // Check policies + assertTrue(policiesConceptMap.size() == 1); + assertEquals("onap.vfirewall.tca", policiesConceptMap.keySet().iterator().next().getName()); + assertEquals("onap.vfirewall.tca:1.0.0", + serviceTemplate.getTopologyTemplate().getPolicies().get("onap.vfirewall.tca").getId()); + + ToscaPolicy policyVal = policiesConceptMap.values().iterator().next(); + + // Check metadata + assertTrue(policyVal.getMetadata().size() == 1); + assertEquals("policy-id", policyVal.getMetadata().entrySet().iterator().next().getKey()); + assertEquals("onap.vfirewall.tca", policyVal.getMetadata().entrySet().iterator().next().getValue()); + + // Check properties + assertTrue(policiesConceptMap.values().iterator().next().getProperties().size() == 1); + assertEquals("tca_policy", policyVal.getProperties().keySet().iterator().next()); + assertNotNull(policyVal.getProperties().values().iterator().next()); + } + + private void verifyVcpeMonitoringOutputserialization(String serializedServiceTemplate) { + + JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject(); + assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version") + .getAsString()); + JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template") + .getAsJsonObject(); + JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray(); + assertTrue(policiesJsonArray.size() == 1); + JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject(); + assertNotNull(policy.get("onap.restart.tca")); + JsonObject policyVal = policy.get("onap.restart.tca").getAsJsonObject(); + assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString()); + assertEquals("1.0.0", policyVal.get("version").getAsString()); + assertEquals("onap.restart.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id") + .getAsString()); + JsonObject properties = policyVal.get("properties").getAsJsonObject(); + assertNotNull(properties.get("tca_policy")); + } + + private void verifyVdnsMonitoringOutputserialization(String serializedServiceTemplate) { + + JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject(); + assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version") + .getAsString()); + JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject(); + JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray(); + assertTrue(policiesJsonArray.size() == 1); + JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject(); + assertNotNull(policy.get("onap.scaleout.tca")); + JsonObject policyVal = policy.get("onap.scaleout.tca").getAsJsonObject(); + assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString()); + assertEquals("1.0.0", policyVal.get("version").getAsString()); + assertEquals("onap.scaleout.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id") + .getAsString()); + JsonObject properties = policyVal.get("properties").getAsJsonObject(); + assertNotNull(properties.get("tca_policy")); + } - String reserializedString = gson.toJson(serviceTemplate, ToscaServiceTemplate.class); - assertEquals(yamlAsJsonString.replaceAll("\\s+", ""), reserializedString.replaceAll("\\s+", "")); + private void verifyVfwMonitoringOutputserialization(String serializedServiceTemplate) { - ToscaServiceTemplate serviceTemplate2 = gson.fromJson(reserializedString, ToscaServiceTemplate.class); - assertNotNull(serviceTemplate2); - assertEquals(serviceTemplate, serviceTemplate2); + JsonObject serviceTemplateJsonObject = new JsonParser().parse(serializedServiceTemplate).getAsJsonObject(); + assertEquals("tosca_simple_yaml_1_0_0", serviceTemplateJsonObject.get("tosca_definitions_version") + .getAsString()); + JsonObject topologyTemplateJsonObject = serviceTemplateJsonObject.get("topology_template").getAsJsonObject(); + JsonArray policiesJsonArray = topologyTemplateJsonObject.get("policies").getAsJsonArray(); + assertTrue(policiesJsonArray.size() == 1); + JsonObject policy = policiesJsonArray.iterator().next().getAsJsonObject(); + assertNotNull(policy.get("onap.vfirewall.tca")); + JsonObject policyVal = policy.get("onap.vfirewall.tca").getAsJsonObject(); + assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", policyVal.get("type").getAsString()); + assertEquals("1.0.0", policyVal.get("version").getAsString()); + assertEquals("onap.vfirewall.tca", policyVal.get("metadata").getAsJsonObject().get("policy-id") + .getAsString()); + JsonObject properties = policyVal.get("properties").getAsJsonObject(); + assertNotNull(properties.get("tca_policy")); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java new file mode 100644 index 000000000..c40b32e3c --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java @@ -0,0 +1,424 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; +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.ToscaDataType; +import org.onap.policy.models.tosca.simple.concepts.ToscaEntrySchema; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.simple.concepts.ToscaProperty; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; + +/** + * Test serialization of monitoring policy types. + * + * @author Chenfei Gao (cgao@research.att.com) + */ +public class MonitoringPolicyTypeSerializationTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPolicyTypeSerializationTest.class); + + private static final String MONITORING_TCA_YAML = "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml"; + private static final String MONITORING_COLLECTORS_YAML = + "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml"; + + private Gson gson; + + @Before + public void setUp() { + gson = new ToscaServiceTemplateMessageBodyHandler().getGson(); + } + + @Test + public void testDeserialization() { + try { + // TCA + ToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(MONITORING_TCA_YAML); + verifyTcaInputDeserialization(serviceTemplateFromYaml); + + // Collector + serviceTemplateFromYaml = deserializeMonitoringInputYaml(MONITORING_COLLECTORS_YAML); + verifyCollectorInputDeserialization(serviceTemplateFromYaml); + + } catch (Exception e) { + fail("No exception should be thrown"); + } + } + + @Test + public void testSerialization() { + try { + // TCA + ToscaServiceTemplate serviceTemplateFromYaml = deserializeMonitoringInputYaml(MONITORING_TCA_YAML); + String serializedServiceTemplate1 = serializeMonitoringServiceTemplate(serviceTemplateFromYaml); + ToscaServiceTemplate serviceTemplateFromJson = gson.fromJson(serializedServiceTemplate1, + ToscaServiceTemplate.class); + String serializedServiceTemplate2 = serializeMonitoringServiceTemplate(serviceTemplateFromJson); + assertEquals(serializedServiceTemplate1, serializedServiceTemplate2); + + // Collector + serviceTemplateFromYaml = deserializeMonitoringInputYaml(MONITORING_COLLECTORS_YAML); + serializedServiceTemplate1 = serializeMonitoringServiceTemplate(serviceTemplateFromYaml); + serviceTemplateFromJson = gson.fromJson(serializedServiceTemplate1, ToscaServiceTemplate.class); + serializedServiceTemplate2 = serializeMonitoringServiceTemplate(serviceTemplateFromJson); + assertEquals(serializedServiceTemplate1, serializedServiceTemplate2); + + } catch (Exception e) { + fail("No exception should be thrown"); + } + } + + private ToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) + throws JsonSyntaxException, IOException { + + Yaml yaml = new Yaml(); + String policyTypeYaml = ResourceUtils.getResourceAsString(resourcePath); + Object yamlObject = yaml.load(policyTypeYaml); + String yamlAsJsonString = new Gson().toJson(yamlObject); + ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class); + return serviceTemplate; + } + + private void verifyTcaInputDeserialization(ToscaServiceTemplate serviceTemplate) { + + // Sanity check the entire structure + assertNotNull(serviceTemplate); + LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString()); + assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); + + // Check tosca_definitions_version + assertEquals("tosca_simple_yaml_1_0_0", serviceTemplate.getToscaDefinitionsVersion()); + + // Check policy_types + Map policyTypesConceptMap = serviceTemplate.getPolicyTypes().getConceptMap(); + assertTrue(policyTypesConceptMap.size() == 2); + Iterator> policyTypesIter = policyTypesConceptMap.entrySet().iterator(); + + Entry firstPolicyType = policyTypesIter.next(); + assertEquals("onap.policies.Monitoring", firstPolicyType.getKey().getName()); + assertEquals("1.0.0", firstPolicyType.getKey().getVersion()); + assertEquals("tosca.policies.Root", firstPolicyType.getValue().getDerivedFrom().getName()); + assertEquals("a base policy type for all policies that governs monitoring provisioning", + firstPolicyType.getValue().getDescription()); + + Entry secondPolicyType = policyTypesIter.next(); + assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", secondPolicyType.getKey().getName()); + assertEquals("1.0.0", secondPolicyType.getKey().getVersion()); + assertEquals("onap.policies.Monitoring", secondPolicyType.getValue().getDerivedFrom().getName()); + assertTrue(secondPolicyType.getValue().getProperties().size() == 1); + + ToscaProperty property = secondPolicyType.getValue().getProperties().iterator().next(); + assertEquals("onap.policy.monitoring.cdap.tca.hi.lo.app", property.getKey().getParentKeyName()); + assertEquals("1.0.0", property.getKey().getParentKeyVersion()); + assertEquals("tca_policy", property.getKey().getLocalName()); + assertEquals("map", property.getType().getName()); + assertEquals("TCA Policy JSON", property.getDescription()); + + ToscaEntrySchema entrySchema = property.getEntrySchema(); + assertEquals("map", entrySchema.getKey().getParentKeyName()); + assertEquals("1.0.0", entrySchema.getKey().getParentKeyVersion()); + assertEquals("entry_schema", entrySchema.getKey().getLocalName()); + assertEquals("onap.datatypes.monitoring.tca_policy", entrySchema.getType().getName()); + + // Check data_types + Map dataTypesConceptMap = serviceTemplate.getDataTypes().getConceptMap(); + assertTrue(dataTypesConceptMap.size() == 3); + Iterator> dataTypesIter = dataTypesConceptMap.entrySet().iterator(); + + Entry firstDataType = dataTypesIter.next(); + assertEquals("onap.datatypes.monitoring.metricsPerEventName", firstDataType.getKey().getName()); + ToscaDataType firstDataTypeVal = firstDataType.getValue(); + assertEquals("tosca.datatypes.Root", firstDataTypeVal.getDerivedFrom().getName()); + assertEquals("1.0.0", firstDataTypeVal.getDerivedFrom().getVersion()); + assertTrue(firstDataTypeVal.getProperties().size() == 6); + Iterator firstDataTypePropertiesIter = firstDataTypeVal.getProperties().iterator(); + + ToscaProperty firstDataTypeFirstProperty = firstDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.metricsPerEventName", firstDataTypeFirstProperty.getKey() + .getParentKeyName()); + assertEquals("controlLoopSchemaType", firstDataTypeFirstProperty.getKey().getLocalName()); + assertEquals("string", firstDataTypeFirstProperty.getType().getName()); + assertTrue(firstDataTypeFirstProperty.isRequired()); + assertEquals("Specifies Control Loop Schema Type for the event Name e.g. VNF, VM", + firstDataTypeFirstProperty.getDescription()); + assertTrue(firstDataTypeFirstProperty.getConstraints().size() == 1); + assertEquals("valid_values", firstDataTypeFirstProperty.getConstraints().iterator().next().getKey() + .getLocalName()); + assertEquals("string", firstDataTypeFirstProperty.getConstraints().iterator().next().getKey() + .getParentKeyName()); + assertTrue(firstDataTypeFirstProperty.getConstraints().iterator().next() + instanceof ToscaConstraintValidValues); + assertTrue(((ToscaConstraintValidValues)(firstDataTypeFirstProperty.getConstraints().iterator().next())) + .getValidValues().size() == 2); + + ToscaProperty firstDataTypeSecondProperty = firstDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.metricsPerEventName", firstDataTypeSecondProperty.getKey() + .getParentKeyName()); + assertEquals("eventName", firstDataTypeSecondProperty.getKey().getLocalName()); + assertEquals("string", firstDataTypeSecondProperty.getType().getName()); + assertTrue(firstDataTypeSecondProperty.isRequired()); + assertEquals("Event name to which thresholds need to be applied", firstDataTypeSecondProperty + .getDescription()); + + ToscaProperty firstDataTypeThirdProperty = firstDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.metricsPerEventName", firstDataTypeThirdProperty.getKey() + .getParentKeyName()); + assertEquals("policyName", firstDataTypeThirdProperty.getKey().getLocalName()); + assertEquals("string", firstDataTypeThirdProperty.getType().getName()); + assertTrue(firstDataTypeThirdProperty.isRequired()); + assertEquals("TCA Policy Scope Name", firstDataTypeThirdProperty.getDescription()); + + ToscaProperty firstDataTypeFourthProperty = firstDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.metricsPerEventName", firstDataTypeFourthProperty.getKey() + .getParentKeyName()); + assertEquals("policyScope", firstDataTypeFourthProperty.getKey().getLocalName()); + assertEquals("string", firstDataTypeFourthProperty.getType().getName()); + assertTrue(firstDataTypeFourthProperty.isRequired()); + assertEquals("TCA Policy Scope", firstDataTypeFourthProperty.getDescription()); + + ToscaProperty firstDataTypeFifthProperty = firstDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.metricsPerEventName", firstDataTypeFifthProperty.getKey() + .getParentKeyName()); + assertEquals("policyVersion", firstDataTypeFifthProperty.getKey().getLocalName()); + assertEquals("string", firstDataTypeFifthProperty.getType().getName()); + assertTrue(firstDataTypeFifthProperty.isRequired()); + assertEquals("TCA Policy Scope Version", firstDataTypeFifthProperty.getDescription()); + + ToscaProperty firstDataTypeSixthProperty = firstDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.metricsPerEventName", firstDataTypeSixthProperty.getKey() + .getParentKeyName()); + assertEquals("thresholds", firstDataTypeSixthProperty.getKey().getLocalName()); + assertEquals("list", firstDataTypeSixthProperty.getType().getName()); + assertTrue(firstDataTypeSixthProperty.isRequired()); + assertEquals("Thresholds associated with eventName", firstDataTypeSixthProperty.getDescription()); + assertNotNull(firstDataTypeSixthProperty.getEntrySchema()); + assertEquals("entry_schema", firstDataTypeSixthProperty.getEntrySchema().getKey().getLocalName()); + assertEquals("list", firstDataTypeSixthProperty.getEntrySchema().getKey().getParentKeyName()); + assertEquals("onap.datatypes.monitoring.thresholds", firstDataTypeSixthProperty.getEntrySchema().getType() + .getName()); + + Entry secondDataType = dataTypesIter.next(); + assertEquals("onap.datatypes.monitoring.tca_policy", secondDataType.getKey().getName()); + ToscaDataType secondDataTypeVal = secondDataType.getValue(); + assertEquals("tosca.datatypes.Root", secondDataTypeVal.getDerivedFrom().getName()); + assertEquals("1.0.0", secondDataTypeVal.getDerivedFrom().getVersion()); + assertTrue(secondDataTypeVal.getProperties().size() == 2); + Iterator secondDataTypePropertiesIter = secondDataTypeVal.getProperties().iterator(); + + ToscaProperty secondDataTypeFirstProperty = secondDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.tca_policy", secondDataTypeFirstProperty.getKey().getParentKeyName()); + assertEquals("domain", secondDataTypeFirstProperty.getKey().getLocalName()); + assertEquals("string", secondDataTypeFirstProperty.getType().getName()); + assertTrue(secondDataTypeFirstProperty.isRequired()); + assertEquals("Domain name to which TCA needs to be applied", secondDataTypeFirstProperty.getDescription()); + assertEquals("measurementsForVfScaling", secondDataTypeFirstProperty.getDefaultValue()); + assertTrue(secondDataTypeFirstProperty.getConstraints().size() == 1); + assertEquals("string", secondDataTypeFirstProperty.getConstraints().iterator().next().getKey() + .getParentKeyName()); + assertEquals("equal", secondDataTypeFirstProperty.getConstraints().iterator().next().getKey().getLocalName()); + assertTrue(secondDataTypeFirstProperty.getConstraints().iterator().next() + instanceof ToscaConstraintLogicalString); + assertEquals("measurementsForVfScaling", ((ToscaConstraintLogicalString)(secondDataTypeFirstProperty + .getConstraints().iterator().next())).getCompareToString()); + + ToscaProperty secondDataTypeSecondProperty = secondDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.tca_policy", secondDataTypeSecondProperty.getKey().getParentKeyName()); + assertEquals("metricsPerEventName", secondDataTypeSecondProperty.getKey().getLocalName()); + assertEquals("list", secondDataTypeSecondProperty.getType().getName()); + assertTrue(secondDataTypeSecondProperty.isRequired()); + assertEquals("Contains eventName and threshold details that need to be applied to given eventName", + secondDataTypeSecondProperty.getDescription()); + assertNotNull(secondDataTypeSecondProperty.getEntrySchema()); + assertEquals("list", secondDataTypeSecondProperty.getEntrySchema().getKey().getParentKeyName()); + assertEquals("onap.datatypes.monitoring.metricsPerEventName", + secondDataTypeSecondProperty.getEntrySchema().getType().getName()); + assertEquals("entry_schema", secondDataTypeSecondProperty.getEntrySchema().getKey().getLocalName()); + + Entry thirdDataType = dataTypesIter.next(); + assertEquals("onap.datatypes.monitoring.thresholds", thirdDataType.getKey().getName()); + ToscaDataType thirdDataTypeVal = thirdDataType.getValue(); + assertEquals("tosca.datatypes.Root", thirdDataTypeVal.getDerivedFrom().getName()); + assertEquals("1.0.0", thirdDataTypeVal.getDerivedFrom().getVersion()); + assertTrue(thirdDataTypeVal.getProperties().size() == 7); + Iterator thirdDataTypePropertiesIter = thirdDataTypeVal.getProperties().iterator(); + + ToscaProperty thirdDataTypeFirstProperty = thirdDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.thresholds", thirdDataTypeFirstProperty.getKey().getParentKeyName()); + assertEquals("closedLoopControlName", thirdDataTypeFirstProperty.getKey().getLocalName()); + assertEquals("string", thirdDataTypeFirstProperty.getType().getName()); + assertTrue(thirdDataTypeFirstProperty.isRequired()); + assertEquals("Closed Loop Control Name associated with the threshold", thirdDataTypeFirstProperty + .getDescription()); + + ToscaProperty thirdDataTypeSecondProperty = thirdDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.thresholds", thirdDataTypeSecondProperty.getKey().getParentKeyName()); + assertEquals("closedLoopEventStatus", thirdDataTypeSecondProperty.getKey().getLocalName()); + assertEquals("string", thirdDataTypeSecondProperty.getType().getName()); + assertTrue(thirdDataTypeSecondProperty.isRequired()); + assertEquals("Closed Loop Event Status of the threshold", thirdDataTypeSecondProperty.getDescription()); + assertNotNull(thirdDataTypeSecondProperty.getConstraints()); + assertTrue(thirdDataTypeSecondProperty.getConstraints().size() == 1); + assertEquals("string", thirdDataTypeSecondProperty.getConstraints().iterator().next().getKey() + .getParentKeyName()); + assertEquals("valid_values", thirdDataTypeSecondProperty.getConstraints().iterator().next().getKey() + .getLocalName()); + assertTrue(thirdDataTypeSecondProperty.getConstraints().iterator().next() + instanceof ToscaConstraintValidValues); + assertTrue(((ToscaConstraintValidValues)(thirdDataTypeSecondProperty.getConstraints().iterator().next())) + .getValidValues().size() == 2); + + ToscaProperty thirdDataTypeThirdProperty = thirdDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.thresholds", thirdDataTypeThirdProperty.getKey().getParentKeyName()); + assertEquals("direction", thirdDataTypeThirdProperty.getKey().getLocalName()); + assertEquals("string", thirdDataTypeThirdProperty.getType().getName()); + assertTrue(thirdDataTypeThirdProperty.isRequired()); + assertEquals("Direction of the threshold", thirdDataTypeThirdProperty.getDescription()); + assertNotNull(thirdDataTypeThirdProperty.getConstraints()); + assertTrue(thirdDataTypeThirdProperty.getConstraints().size() == 1); + assertEquals("string", thirdDataTypeThirdProperty.getConstraints().iterator().next().getKey() + .getParentKeyName()); + assertEquals("valid_values", thirdDataTypeThirdProperty.getConstraints().iterator().next().getKey() + .getLocalName()); + assertTrue(((ToscaConstraintValidValues)(thirdDataTypeThirdProperty.getConstraints().iterator().next())) + .getValidValues().size() == 5); + + ToscaProperty thirdDataTypeFourthProperty = thirdDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.thresholds", thirdDataTypeFourthProperty.getKey().getParentKeyName()); + assertEquals("fieldPath", thirdDataTypeFourthProperty.getKey().getLocalName()); + assertEquals("string", thirdDataTypeFourthProperty.getType().getName()); + assertTrue(thirdDataTypeFourthProperty.isRequired()); + assertEquals("Json field Path as per CEF message which needs to be analyzed for TCA", + thirdDataTypeFourthProperty.getDescription()); + assertNotNull(thirdDataTypeFourthProperty.getConstraints()); + assertTrue(thirdDataTypeFourthProperty.getConstraints().size() == 1); + assertEquals("string", thirdDataTypeFourthProperty.getConstraints().iterator().next().getKey() + .getParentKeyName()); + assertEquals("valid_values", thirdDataTypeFourthProperty.getConstraints().iterator().next().getKey() + .getLocalName()); + assertTrue(((ToscaConstraintValidValues)(thirdDataTypeFourthProperty.getConstraints().iterator().next())) + .getValidValues().size() == 43); + + ToscaProperty thirdDataTypeFifthProperty = thirdDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.thresholds", thirdDataTypeFifthProperty.getKey().getParentKeyName()); + assertEquals("severity", thirdDataTypeFifthProperty.getKey().getLocalName()); + assertEquals("string", thirdDataTypeFifthProperty.getType().getName()); + assertTrue(thirdDataTypeFifthProperty.isRequired()); + assertEquals("Threshold Event Severity", thirdDataTypeFifthProperty.getDescription()); + assertNotNull(thirdDataTypeFifthProperty.getConstraints()); + assertTrue(thirdDataTypeFifthProperty.getConstraints().size() == 1); + assertEquals("string", thirdDataTypeFifthProperty.getConstraints().iterator().next().getKey() + .getParentKeyName()); + assertEquals("valid_values", thirdDataTypeFifthProperty.getConstraints().iterator().next().getKey() + .getLocalName()); + assertTrue(((ToscaConstraintValidValues)(thirdDataTypeFifthProperty.getConstraints().iterator().next())) + .getValidValues().size() == 5);; + + ToscaProperty thirdDataTypeSixthProperty = thirdDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.thresholds", thirdDataTypeSixthProperty.getKey().getParentKeyName()); + assertEquals("thresholdValue", thirdDataTypeSixthProperty.getKey().getLocalName()); + assertEquals("integer", thirdDataTypeSixthProperty.getType().getName()); + assertTrue(thirdDataTypeSixthProperty.isRequired()); + assertEquals("Threshold value for the field Path inside CEF message", thirdDataTypeSixthProperty + .getDescription()); + + ToscaProperty thirdDataTypeSeventhProperty = thirdDataTypePropertiesIter.next(); + assertEquals("onap.datatypes.monitoring.thresholds", thirdDataTypeSeventhProperty.getKey().getParentKeyName()); + assertEquals("version", thirdDataTypeSeventhProperty.getKey().getLocalName()); + assertEquals("string", thirdDataTypeSeventhProperty.getType().getName()); + assertTrue(thirdDataTypeSeventhProperty.isRequired()); + assertEquals("Version number associated with the threshold", thirdDataTypeSeventhProperty.getDescription()); + } + + private void verifyCollectorInputDeserialization(ToscaServiceTemplate serviceTemplate) { + + // Sanity check the entire structure + assertNotNull(serviceTemplate); + LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString()); + assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); + + // Check tosca_definitions_version + assertEquals("tosca_simple_yaml_1_0_0", serviceTemplate.getToscaDefinitionsVersion()); + + // Check policy_types + Map policyTypesConceptMap = serviceTemplate.getPolicyTypes().getConceptMap(); + assertTrue(policyTypesConceptMap.size() == 2); + Iterator> policyTypesIter = policyTypesConceptMap.entrySet().iterator(); + + Entry firstPolicyType = policyTypesIter.next(); + assertEquals("onap.policies.Monitoring", firstPolicyType.getKey().getName()); + assertEquals("1.0.0", firstPolicyType.getKey().getVersion()); + assertEquals("tosca.policies.Root", firstPolicyType.getValue().getDerivedFrom().getName()); + assertEquals("a base policy type for all policies that govern monitoring provision", + firstPolicyType.getValue().getDescription()); + + Entry secondPolicyType = policyTypesIter.next(); + assertEquals("onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server", + secondPolicyType.getKey().getName()); + assertEquals("1.0.0", secondPolicyType.getKey().getVersion()); + assertEquals("policy.nodes.Root", secondPolicyType.getValue().getDerivedFrom().getName()); + assertTrue(secondPolicyType.getValue().getProperties().size() == 2); + + Iterator propertiesIter = secondPolicyType.getValue().getProperties().iterator(); + + ToscaProperty firstProperty = propertiesIter.next(); + assertEquals("onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server", + firstProperty.getKey().getParentKeyName()); + assertEquals("1.0.0", firstProperty.getKey().getParentKeyVersion()); + assertEquals("buscontroller_feed_publishing_endpoint", firstProperty.getKey().getLocalName()); + assertEquals("string", firstProperty.getType().getName()); + assertEquals("DMAAP Bus Controller feed endpoint", firstProperty.getDescription()); + + ToscaProperty secondProperty = propertiesIter.next(); + assertEquals("onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server", + secondProperty.getKey().getParentKeyName()); + assertEquals("1.0.0", secondProperty.getKey().getParentKeyVersion()); + assertEquals("datafile.policy", secondProperty.getKey().getLocalName()); + assertEquals("string", secondProperty.getType().getName()); + assertEquals("datafile Policy JSON as string", secondProperty.getDescription()); + } + + private String serializeMonitoringServiceTemplate(ToscaServiceTemplate serviceTemplate) { + return gson.toJson(serviceTemplate); + } +} \ No newline at end of file -- cgit 1.2.3-korg