From 86cf9ece99fdc169427426010d07d59b3d0d1d91 Mon Sep 17 00:00:00 2001 From: sebdet Date: Tue, 5 Mar 2019 05:17:11 +0100 Subject: Rework the hibernate adapter This was using the Jackson library so a new handmade hibernate adapter has been developed. Issue-ID: CLAMP-300 Change-Id: Ia1c1e8791370c139127e6fa4bd2b96c02f632fde Signed-off-by: sebdet --- src/main/java/org/onap/clamp/dao/model/Loop.java | 26 +++-- .../onap/clamp/dao/model/MicroServicePolicy.java | 19 ++-- .../onap/clamp/dao/model/OperationalPolicy.java | 13 +-- .../jsontype/JsonStringSqlTypeDescriptor.java | 109 +++++++++++++++++++++ .../dao/model/jsontype/JsonTypeDescriptor.java | 101 +++++++++++++++++++ .../dao/model/jsontype/StringJsonUserType.java | 52 ++++++++++ .../org/onap/clamp/dao/model/LoopToJsonTest.java | 15 +-- .../clamp/it/dao/model/LoopRepositoriesItCase.java | 15 +-- 8 files changed, 314 insertions(+), 36 deletions(-) create mode 100644 src/main/java/org/onap/clamp/dao/model/jsontype/JsonStringSqlTypeDescriptor.java create mode 100644 src/main/java/org/onap/clamp/dao/model/jsontype/JsonTypeDescriptor.java create mode 100644 src/main/java/org/onap/clamp/dao/model/jsontype/StringJsonUserType.java (limited to 'src') diff --git a/src/main/java/org/onap/clamp/dao/model/Loop.java b/src/main/java/org/onap/clamp/dao/model/Loop.java index c5946c9db..78175cc52 100644 --- a/src/main/java/org/onap/clamp/dao/model/Loop.java +++ b/src/main/java/org/onap/clamp/dao/model/Loop.java @@ -23,12 +23,11 @@ package org.onap.clamp.dao.model; +import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; -import com.vladmihalcea.hibernate.type.json.JsonStringType; import java.io.Serializable; import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.persistence.CascadeType; @@ -46,11 +45,12 @@ import javax.persistence.Table; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.dao.model.jsontype.StringJsonUserType; @Entity @Table(name = "loops") -//@JsonAdapter(JsonLoopAdapter.class) -@TypeDef(name = "json", typeClass = JsonStringType.class) +@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) public class Loop implements Serializable { /** @@ -71,6 +71,10 @@ public class Loop implements Serializable { @Column(name = "dcae_deployment_status_url") private String dcaeDeploymentStatusUrl; + @Expose + @Column(name = "dcae_blueprint_id") + private String dcaeBlueprintId; + @Expose @Column(name = "svg_representation") private String svgRepresentation; @@ -78,7 +82,7 @@ public class Loop implements Serializable { @Expose @Type(type = "json") @Column(columnDefinition = "json", name = "global_properties_json") - private Map globalPropertiesJson; + private JsonObject globalPropertiesJson; @Expose @Column(nullable = false, name = "blueprint_yaml") @@ -166,11 +170,11 @@ public class Loop implements Serializable { this.microServicePolicies = microServicePolicies; } - public Map getGlobalPropertiesJson() { + public JsonObject getGlobalPropertiesJson() { return globalPropertiesJson; } - public void setGlobalPropertiesJson(Map globalPropertiesJson) { + public void setGlobalPropertiesJson(JsonObject globalPropertiesJson) { this.globalPropertiesJson = globalPropertiesJson; } @@ -197,6 +201,14 @@ public class Loop implements Serializable { log.setLoop(this); } + public String getDcaeBlueprintId() { + return dcaeBlueprintId; + } + + public void setDcaeBlueprintId(String dcaeBlueprintId) { + this.dcaeBlueprintId = dcaeBlueprintId; + } + @Override public int hashCode() { final int prime = 31; diff --git a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java b/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java index 6014d8d56..8ea6d2650 100644 --- a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java @@ -23,12 +23,11 @@ package org.onap.clamp.dao.model; +import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; -import com.vladmihalcea.hibernate.type.json.JsonStringType; import java.io.Serializable; import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.persistence.Column; @@ -39,10 +38,12 @@ import javax.persistence.Table; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.dao.model.jsontype.StringJsonUserType; @Entity @Table(name = "micro_service_policies") -@TypeDef(name = "json", typeClass = JsonStringType.class) +@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) public class MicroServicePolicy implements Serializable { /** * @@ -57,7 +58,7 @@ public class MicroServicePolicy implements Serializable { @Expose @Type(type = "json") @Column(columnDefinition = "json", name = "properties") - private Map properties; + private JsonObject properties; @Expose @Column(name = "shared", nullable = false) @@ -70,7 +71,7 @@ public class MicroServicePolicy implements Serializable { @Expose @Type(type = "json") @Column(columnDefinition = "json", name = "json_representation", nullable = false) - private Map jsonRepresentation; + private JsonObject jsonRepresentation; @ManyToMany(mappedBy = "microServicePolicies") private Set usedByLoops = new HashSet<>(); @@ -83,11 +84,11 @@ public class MicroServicePolicy implements Serializable { this.name = name; } - public Map getProperties() { + public JsonObject getProperties() { return properties; } - public void setProperties(Map properties) { + public void setProperties(JsonObject properties) { this.properties = properties; } @@ -107,11 +108,11 @@ public class MicroServicePolicy implements Serializable { this.policyTosca = policyTosca; } - public Map getJsonRepresentation() { + public JsonObject getJsonRepresentation() { return jsonRepresentation; } - public void setJsonRepresentation(Map jsonRepresentation) { + public void setJsonRepresentation(JsonObject jsonRepresentation) { this.jsonRepresentation = jsonRepresentation; } diff --git a/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java b/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java index 23f757416..ba2c442c7 100644 --- a/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java @@ -23,11 +23,10 @@ package org.onap.clamp.dao.model; +import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; -import com.vladmihalcea.hibernate.type.json.JsonStringType; import java.io.Serializable; -import java.util.Map; import javax.persistence.Column; import javax.persistence.Entity; @@ -39,10 +38,12 @@ import javax.persistence.Table; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.dao.model.jsontype.StringJsonUserType; @Entity @Table(name = "operational_policies") -@TypeDef(name = "json", typeClass = JsonStringType.class) +@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) public class OperationalPolicy implements Serializable { /** * @@ -57,7 +58,7 @@ public class OperationalPolicy implements Serializable { @Expose @Type(type = "json") @Column(columnDefinition = "json", name = "configurations_json") - private Map configurationsJson; + private JsonObject configurationsJson; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "loop_id", nullable = false) @@ -79,11 +80,11 @@ public class OperationalPolicy implements Serializable { this.name = name; } - public Map getConfigurationsJson() { + public JsonObject getConfigurationsJson() { return configurationsJson; } - public void setConfigurationsJson(Map configurationsJson) { + public void setConfigurationsJson(JsonObject configurationsJson) { this.configurationsJson = configurationsJson; } diff --git a/src/main/java/org/onap/clamp/dao/model/jsontype/JsonStringSqlTypeDescriptor.java b/src/main/java/org/onap/clamp/dao/model/jsontype/JsonStringSqlTypeDescriptor.java new file mode 100644 index 000000000..611f5ec28 --- /dev/null +++ b/src/main/java/org/onap/clamp/dao/model/jsontype/JsonStringSqlTypeDescriptor.java @@ -0,0 +1,109 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.dao.model.jsontype; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; + +import org.hibernate.type.descriptor.ValueBinder; +import org.hibernate.type.descriptor.ValueExtractor; +import org.hibernate.type.descriptor.WrapperOptions; +import org.hibernate.type.descriptor.java.JavaTypeDescriptor; +import org.hibernate.type.descriptor.sql.BasicBinder; +import org.hibernate.type.descriptor.sql.BasicExtractor; +import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; + +public class JsonStringSqlTypeDescriptor implements SqlTypeDescriptor { + + /** + * + */ + private static final long serialVersionUID = 1103168570216921981L; + + public static final JsonStringSqlTypeDescriptor INSTANCE = new JsonStringSqlTypeDescriptor(); + + @Override + public int getSqlType() { + return Types.OTHER; + } + + @Override + public boolean canBeRemapped() { + return true; + } + + @Override + public ValueBinder getBinder(JavaTypeDescriptor javaTypeDescriptor) { + return new BasicBinder(javaTypeDescriptor, this) { + @Override + protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) + throws SQLException { + st.setString(index, javaTypeDescriptor.unwrap(value, String.class, options)); + } + + @Override + protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) + throws SQLException { + st.setString(name, javaTypeDescriptor.unwrap(value, String.class, options)); + } + }; + } + + @Override + public ValueExtractor getExtractor(JavaTypeDescriptor javaTypeDescriptor) { + return new BasicExtractor(javaTypeDescriptor, this) { + @Override + protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException { + return javaTypeDescriptor.wrap(extractJson(rs, name), options); + } + + @Override + protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException { + return javaTypeDescriptor.wrap(extractJson(statement, index), options); + } + + @Override + protected X doExtract(CallableStatement statement, String name, WrapperOptions options) + throws SQLException { + return javaTypeDescriptor.wrap(extractJson(statement, name), options); + } + }; + } + + protected Object extractJson(ResultSet rs, String name) throws SQLException { + return rs.getObject(name); + } + + protected Object extractJson(CallableStatement statement, int index) throws SQLException { + return statement.getObject(index); + } + + protected Object extractJson(CallableStatement statement, String name) throws SQLException { + return statement.getObject(name); + } + +} diff --git a/src/main/java/org/onap/clamp/dao/model/jsontype/JsonTypeDescriptor.java b/src/main/java/org/onap/clamp/dao/model/jsontype/JsonTypeDescriptor.java new file mode 100644 index 000000000..155758e12 --- /dev/null +++ b/src/main/java/org/onap/clamp/dao/model/jsontype/JsonTypeDescriptor.java @@ -0,0 +1,101 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.dao.model.jsontype; + +import com.google.gson.JsonObject; + +import java.io.Serializable; + +import org.hibernate.type.descriptor.WrapperOptions; +import org.hibernate.type.descriptor.java.AbstractTypeDescriptor; +import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan; +import org.onap.clamp.clds.util.JsonUtils; + +public class JsonTypeDescriptor extends AbstractTypeDescriptor { + + /** + * + */ + private static final long serialVersionUID = -3439698221196089003L; + + public static final JsonTypeDescriptor INSTANCE = new JsonTypeDescriptor(); + + public JsonTypeDescriptor() { + super(JsonObject.class, new ImmutableMutabilityPlan() { + + /** + * + */ + private static final long serialVersionUID = 1169396106518110214L; + + @Override + public Serializable disassemble(JsonObject value) { + return JsonUtils.GSON_JPA_MODEL.toJson(value); + } + + @Override + public JsonObject assemble(Serializable cached) { + return JsonUtils.GSON_JPA_MODEL.fromJson((String) cached, JsonObject.class); + } + + }); + } + + @Override + public String toString(JsonObject value) { + return JsonUtils.GSON_JPA_MODEL.toJson(value); + } + + @Override + public JsonObject fromString(String string) { + return JsonUtils.GSON_JPA_MODEL.fromJson(string, JsonObject.class); + } + + @Override + public X unwrap(JsonObject value, Class type, WrapperOptions options) { + if (value == null) + return null; + + if (String.class.isAssignableFrom(type)) { + return (X) toString(value); + } + + if (JsonObject.class.isAssignableFrom(type)) { + return (X) JsonUtils.GSON_JPA_MODEL.toJson(toString(value)); + } + throw unknownUnwrap(type); + } + + @Override + public JsonObject wrap(X value, WrapperOptions options) { + if (value == null) + return null; + + if (String.class.isInstance(value)) + return JsonUtils.GSON_JPA_MODEL.fromJson((String) value, JsonObject.class); + + throw unknownWrap(value.getClass()); + } + +} diff --git a/src/main/java/org/onap/clamp/dao/model/jsontype/StringJsonUserType.java b/src/main/java/org/onap/clamp/dao/model/jsontype/StringJsonUserType.java new file mode 100644 index 000000000..1831c174a --- /dev/null +++ b/src/main/java/org/onap/clamp/dao/model/jsontype/StringJsonUserType.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.dao.model.jsontype; + +import com.google.gson.JsonObject; + +import org.hibernate.type.AbstractSingleColumnStandardBasicType; +import org.hibernate.type.descriptor.java.JavaTypeDescriptor; +import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; + +public class StringJsonUserType extends AbstractSingleColumnStandardBasicType { + + /** + * + */ + private static final long serialVersionUID = -7929809808079327767L; + + public StringJsonUserType() { + super(JsonStringSqlTypeDescriptor.INSTANCE, JsonTypeDescriptor.INSTANCE); + } + + public StringJsonUserType(SqlTypeDescriptor sqlTypeDescriptor, JavaTypeDescriptor javaTypeDescriptor) { + super(sqlTypeDescriptor, javaTypeDescriptor); + } + + @Override + public String getName() { + return "json"; + } + +} diff --git a/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java b/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java index 496d8c0ec..2d9bfd2dc 100644 --- a/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java +++ b/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java @@ -27,8 +27,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertNotNull; import com.google.gson.Gson; +import com.google.gson.JsonObject; -import java.util.Map; import java.util.Random; import org.junit.Test; @@ -39,29 +39,30 @@ public class LoopToJsonTest { private OperationalPolicy getOperationalPolicy(String configJson, String name) { OperationalPolicy opPolicy = new OperationalPolicy(); opPolicy.setName(name); - opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, Map.class)); + opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, JsonObject.class)); return opPolicy; } private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, - String dcaeId, String dcaeUrl) { + String dcaeId, String dcaeUrl, String dcaeBlueprintId) { Loop loop = new Loop(); loop.setName(name); loop.setSvgRepresentation(svgRepresentation); loop.setBlueprint(blueprint); - loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, Map.class)); + loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); loop.setLastComputedState(LoopState.DESIGN); loop.setDcaeDeploymentId(dcaeId); loop.setDcaeDeploymentStatusUrl(dcaeUrl); + loop.setDcaeBlueprintId(dcaeBlueprintId); return loop; } private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, String jsonProperties, boolean shared) { MicroServicePolicy µService = new MicroServicePolicy(); - µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, Map.class)); + µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, JsonObject.class)); µService.setPolicyTosca(policyTosca); - µService.setProperties(new Gson().fromJson(jsonProperties, Map.class)); + µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); µService.setShared(shared); µService.setName(name); @@ -79,7 +80,7 @@ public class LoopToJsonTest { @Test public void LoopGsonTest() { Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org"); + "123456789", "https://dcaetest.org", "UUID-blueprint"); OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); loopTest.addOperationalPolicy(opPolicy); MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", diff --git a/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java b/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java index 67ab247f5..8a523fc0f 100644 --- a/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java +++ b/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java @@ -27,9 +27,9 @@ import static org.assertj.core.api.Assertions.assertThat; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; import java.time.Instant; -import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; @@ -70,29 +70,30 @@ public class LoopRepositoriesItCase { private OperationalPolicy getOperationalPolicy(String configJson, String name) { OperationalPolicy opPolicy = new OperationalPolicy(); opPolicy.setName(name); - opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, Map.class)); + opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, JsonObject.class)); return opPolicy; } private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, - String dcaeId, String dcaeUrl) { + String dcaeId, String dcaeUrl, String dcaeBlueprintId) { Loop loop = new Loop(); loop.setName(name); loop.setSvgRepresentation(svgRepresentation); loop.setBlueprint(blueprint); - loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, Map.class)); + loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); loop.setLastComputedState(LoopState.DESIGN); loop.setDcaeDeploymentId(dcaeId); loop.setDcaeDeploymentStatusUrl(dcaeUrl); + loop.setDcaeBlueprintId(dcaeBlueprintId); return loop; } private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, String jsonProperties, boolean shared) { MicroServicePolicy µService = new MicroServicePolicy(); - µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, Map.class)); + µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, JsonObject.class)); µService.setPolicyTosca(policyTosca); - µService.setProperties(new Gson().fromJson(jsonProperties, Map.class)); + µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); µService.setShared(shared); µService.setName(name); @@ -110,7 +111,7 @@ public class LoopRepositoriesItCase { @Transactional public void CrudTest() { Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org"); + "123456789", "https://dcaetest.org", "UUID-blueprint"); OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); loopTest.addOperationalPolicy(opPolicy); MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", -- cgit 1.2.3-korg