diff options
author | Sébastien Determe <sebastien.determe@intl.att.com> | 2019-03-05 14:25:59 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-03-05 14:25:59 +0000 |
commit | e577b6a44e1dda60fb68543bcdfc627f111b4a97 (patch) | |
tree | 4d1056c9924cb22dcf02b02e353cec7f529e3841 /src | |
parent | 179bae8d30eed732b671f1956ffab90de0700ac2 (diff) | |
parent | 86cf9ece99fdc169427426010d07d59b3d0d1d91 (diff) |
Merge "Rework the hibernate adapter"
Diffstat (limited to 'src')
8 files changed, 314 insertions, 36 deletions
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 c5946c9d..78175cc5 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 { /** @@ -72,13 +72,17 @@ public class Loop implements Serializable { private String dcaeDeploymentStatusUrl; @Expose + @Column(name = "dcae_blueprint_id") + private String dcaeBlueprintId; + + @Expose @Column(name = "svg_representation") private String svgRepresentation; @Expose @Type(type = "json") @Column(columnDefinition = "json", name = "global_properties_json") - private Map<String, Object> 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<String, Object> getGlobalPropertiesJson() { + public JsonObject getGlobalPropertiesJson() { return globalPropertiesJson; } - public void setGlobalPropertiesJson(Map<String, Object> 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 6014d8d5..8ea6d265 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<String, Object> 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<String, Object> jsonRepresentation; + private JsonObject jsonRepresentation; @ManyToMany(mappedBy = "microServicePolicies") private Set<Loop> usedByLoops = new HashSet<>(); @@ -83,11 +84,11 @@ public class MicroServicePolicy implements Serializable { this.name = name; } - public Map<String, Object> getProperties() { + public JsonObject getProperties() { return properties; } - public void setProperties(Map<String, Object> properties) { + public void setProperties(JsonObject properties) { this.properties = properties; } @@ -107,11 +108,11 @@ public class MicroServicePolicy implements Serializable { this.policyTosca = policyTosca; } - public Map<String, Object> getJsonRepresentation() { + public JsonObject getJsonRepresentation() { return jsonRepresentation; } - public void setJsonRepresentation(Map<String, Object> 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 23f75741..ba2c442c 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<String, Object> 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<String, Object> getConfigurationsJson() { + public JsonObject getConfigurationsJson() { return configurationsJson; } - public void setConfigurationsJson(Map<String, Object> 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 00000000..611f5ec2 --- /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 <X> ValueBinder<X> getBinder(JavaTypeDescriptor<X> javaTypeDescriptor) { + return new BasicBinder<X>(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 <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) { + return new BasicExtractor<X>(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 00000000..155758e1 --- /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<JsonObject> { + + /** + * + */ + private static final long serialVersionUID = -3439698221196089003L; + + public static final JsonTypeDescriptor INSTANCE = new JsonTypeDescriptor(); + + public JsonTypeDescriptor() { + super(JsonObject.class, new ImmutableMutabilityPlan<JsonObject>() { + + /** + * + */ + 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> X unwrap(JsonObject value, Class<X> 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 <X> 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 00000000..1831c174 --- /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<JsonObject> { + + /** + * + */ + private static final long serialVersionUID = -7929809808079327767L; + + public StringJsonUserType() { + super(JsonStringSqlTypeDescriptor.INSTANCE, JsonTypeDescriptor.INSTANCE); + } + + public StringJsonUserType(SqlTypeDescriptor sqlTypeDescriptor, JavaTypeDescriptor<JsonObject> 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 496d8c0e..2d9bfd2d 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", "<xml></xml>", "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 67ab247f..8a523fc0 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", "<xml></xml>", "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\"}", |