diff options
author | Sirisha_Manchikanti <sirisha.manchikanti@est.tech> | 2022-04-12 17:41:14 +0100 |
---|---|---|
committer | Sirisha_Manchikanti <sirisha.manchikanti@est.tech> | 2022-04-20 15:04:23 +0100 |
commit | 8584f219b638769603101b936497c276323950ca (patch) | |
tree | d72f99bd14962e179d6fa5209c228b1a5a48cb28 /runtime-acm | |
parent | 25b2378104e75f14fb950529eeeff09d7d9d585b (diff) |
Replace jackson library usage with GSON
Issue-ID: POLICY-4085
Signed-off-by: Sirisha_Manchikanti <sirisha.manchikanti@est.tech>
Change-Id: I6f8f55d2c49495b6efcdc7f58de03c440f72626d
Diffstat (limited to 'runtime-acm')
4 files changed, 89 insertions, 80 deletions
diff --git a/runtime-acm/pom.xml b/runtime-acm/pom.xml index 3bedf0daf..20751a2c2 100644 --- a/runtime-acm/pom.xml +++ b/runtime-acm/pom.xml @@ -50,16 +50,6 @@ <version>${project.version}</version> </dependency> <dependency> - <groupId>com.fasterxml.jackson.module</groupId> - <artifactId>jackson-module-jsonSchema</artifactId> - <exclusions> - <exclusion> - <groupId>javax.validation</groupId> - <artifactId>validation-api</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> @@ -107,6 +97,11 @@ <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.8.4</version> + </dependency> </dependencies> <build> diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProvider.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProvider.java index 9e07cde51..e0027d36b 100644 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProvider.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProvider.java @@ -21,10 +21,10 @@ package org.onap.policy.clamp.acm.runtime.commissioning; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategies; -import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializer; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -52,6 +52,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates; import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -67,10 +69,12 @@ public class CommissioningProvider { private final ServiceTemplateProvider serviceTemplateProvider; private final AutomationCompositionProvider acProvider; - private final ObjectMapper mapper = new ObjectMapper(); private final ParticipantProvider participantProvider; private final SupervisionHandler supervisionHandler; + private static final Map<String, String> sections = new HashMap<>(); + private Gson gson = new Gson(); + /** * Create a commissioning provider. * @@ -86,7 +90,42 @@ public class CommissioningProvider { this.acProvider = acProvider; this.supervisionHandler = supervisionHandler; this.participantProvider = participantProvider; - mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + } + + /** + * Event listener initiilize function called at ApplicationReadyEvent. + * + */ + @EventListener(ApplicationReadyEvent.class) + public void initialize() { + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(ToscaServiceTemplate.class, + (JsonSerializer<ToscaServiceTemplate>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.registerTypeAdapter(ToscaDataType.class, + (JsonSerializer<ToscaDataType>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.registerTypeAdapter(ToscaCapabilityType.class, + (JsonSerializer<ToscaCapabilityType>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.registerTypeAdapter(ToscaNodeType.class, + (JsonSerializer<ToscaNodeType>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.registerTypeAdapter(ToscaRelationshipType.class, + (JsonSerializer<ToscaRelationshipType>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.registerTypeAdapter(ToscaPolicyType.class, + (JsonSerializer<ToscaPolicyType>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.registerTypeAdapter(ToscaTopologyTemplate.class, + (JsonSerializer<ToscaTopologyTemplate>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.registerTypeAdapter(ToscaNodeTemplate.class, + (JsonSerializer<ToscaNodeTemplate>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.setPrettyPrinting(); + gson = builder.create(); + + sections.put("data_types", gson.toJson(new ToscaDataType())); + sections.put("capability_types", gson.toJson(new ToscaCapabilityType())); + sections.put("node_types", gson.toJson(new ToscaNodeType())); + sections.put("relationship_types", gson.toJson(new ToscaRelationshipType())); + sections.put("policy_types", gson.toJson(new ToscaPolicyType())); + sections.put("topology_template", gson.toJson(new ToscaTopologyTemplate())); + sections.put("node_templates", gson.toJson(new ToscaNodeTemplate())); + sections.put("all", gson.toJson(new ToscaServiceTemplate())); } /** @@ -293,12 +332,7 @@ public class CommissioningProvider { template.put("node_types", fullTemplate.getNodeTypes()); template.put("topology_template", fullTemplate.getToscaTopologyTemplate()); - try { - return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(template); - - } catch (JsonProcessingException e) { - throw new PfModelException(Status.BAD_REQUEST, "Converion to Json Schema failed", e); - } + return gson.toJson(template); } /** @@ -308,43 +342,8 @@ public class CommissioningProvider { * @return the specified tosca service template or section Json Schema * @throws PfModelException on errors with retrieving the classes */ - @Transactional(readOnly = true) public String getToscaServiceTemplateSchema(String section) throws PfModelException { - var visitor = new SchemaFactoryWrapper(); - - try { - switch (section) { - case "data_types": - mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaDataType.class), visitor); - break; - case "capability_types": - mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaCapabilityType.class), visitor); - break; - case "node_types": - mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaNodeType.class), visitor); - break; - case "relationship_types": - mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaRelationshipType.class), visitor); - break; - case "policy_types": - mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaPolicyType.class), visitor); - break; - case "topology_template": - mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaTopologyTemplate.class), visitor); - break; - case "node_templates": - mapper.acceptJsonFormatVisitor( - mapper.getTypeFactory().constructCollectionType(List.class, ToscaNodeTemplate.class), visitor); - break; - default: - mapper.acceptJsonFormatVisitor(mapper.constructType(ToscaServiceTemplate.class), visitor); - } - - var jsonSchema = visitor.finalSchema(); - return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema); - } catch (JsonProcessingException e) { - throw new PfModelException(Status.BAD_REQUEST, "Converion to Json Schema failed", e); - } + return sections.getOrDefault(section, sections.get("all")); } private List<ToscaServiceTemplate> filterToscaNodeTemplateInstance(List<ToscaServiceTemplate> serviceTemplates) { diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProviderTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProviderTest.java index 33a9fd85c..bcdb4ddd6 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProviderTest.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProviderTest.java @@ -29,9 +29,10 @@ import static org.mockito.Mockito.when; import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML; import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_ST_TEMPLATE_YAML; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategies; -import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializer; import java.util.List; import java.util.Map; import java.util.Objects; @@ -55,7 +56,6 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; class CommissioningProviderTest { private static final Coder CODER = new StandardCoder(); - private final ObjectMapper mapper = new ObjectMapper(); /** * Test the fetching of automation composition definitions (ToscaServiceTemplates). @@ -191,30 +191,30 @@ class CommissioningProviderTest { when(serviceTemplateProvider.createServiceTemplate(serviceTemplate)).thenReturn(serviceTemplate); provider.createAutomationCompositionDefinitions(serviceTemplate); + provider.initialize(); - mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); - - Map<String, Class<?>> sections = Map.of("all", ToscaServiceTemplate.class, "data_types", ToscaDataType.class, - "capability_types", ToscaCapabilityType.class, "node_types", ToscaNodeType.class, "relationship_types", + final Map<String, Class<?>> sections = Map.of("all", ToscaServiceTemplate.class, + "data_types", ToscaDataType.class, "capability_types", ToscaCapabilityType.class, + "node_types", ToscaNodeType.class, "relationship_types", ToscaRelationshipType.class, "policy_types", ToscaPolicyType.class, "topology_template", ToscaTopologyTemplate.class, "node_templates", List.class); + String localServiceTemplateSchema = new String(); + Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(ToscaTopologyTemplate.class, + (JsonSerializer<ToscaTopologyTemplate>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.setPrettyPrinting(); + gson = builder.create(); + for (Map.Entry<String, Class<?>> entry : sections.entrySet()) { String returnedServiceTemplateSchema = provider.getToscaServiceTemplateSchema(entry.getKey()); assertThat(returnedServiceTemplateSchema).isNotNull(); - var visitor = new SchemaFactoryWrapper(); - - if (entry.getKey().equals("node_templates")) { - mapper.acceptJsonFormatVisitor( - mapper.getTypeFactory().constructCollectionType(List.class, ToscaNodeTemplate.class), visitor); - } else { - mapper.acceptJsonFormatVisitor(mapper.constructType(entry.getValue()), visitor); + if (entry.getKey().equals("topology_template")) { + localServiceTemplateSchema = gson.toJson(new ToscaTopologyTemplate()); + assertThat(localServiceTemplateSchema).isEqualTo(returnedServiceTemplateSchema); } - - var jsonSchema = visitor.finalSchema(); - String localServiceTemplateSchema = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema); - assertThat(localServiceTemplateSchema).isEqualTo(returnedServiceTemplateSchema); } } } diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/rest/CommissioningControllerTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/rest/CommissioningControllerTest.java index a1f3b104b..f9a7b20e4 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/rest/CommissioningControllerTest.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/rest/CommissioningControllerTest.java @@ -29,6 +29,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML; import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_ST_TEMPLATE_YAML; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializer; import java.util.List; import java.util.Map; import javax.ws.rs.client.Entity; @@ -137,9 +143,18 @@ class CommissioningControllerTest extends CommonRestController { Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate"); Response rawresp = invocationBuilder.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class); - assertNotNull(template); - assertThat(template.getNodeTypes()).hasSize(7); + String template = rawresp.readEntity(String.class); + final JsonObject jsonObject = new JsonParser().parse(template).getAsJsonObject(); + + Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(ToscaNodeTemplate.class, + (JsonSerializer<ToscaNodeTemplate>) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); + builder.setPrettyPrinting(); + gson = builder.create(); + + ToscaNodeTemplate toscaNodeTemplate = gson.fromJson(jsonObject, ToscaNodeTemplate.class); + assertNotNull(toscaNodeTemplate); } @Test |