From eb71d70552c827e1d8405862f73e8cbd827c6e48 Mon Sep 17 00:00:00 2001 From: sebdet Date: Tue, 9 Apr 2019 18:10:32 +0200 Subject: PDP Group support Add support to push all policies to pdp group + log Issue-ID: CLAMP-303 Change-Id: Ie53b1dfd11ef8db9c2b5ccca0511e6f6343fb70a Signed-off-by: sebdet --- src/main/java/org/onap/clamp/loop/Loop.java | 26 +++++++++++++ src/main/resources/application-noaaf.properties | 2 +- src/main/resources/application.properties | 2 +- .../resources/clds/camel/rest/clamp-api-v2.xml | 11 +++++- .../resources/clds/camel/routes/flexible-flow.xml | 43 ++++++++++++++++++---- .../java/org/onap/clamp/loop/LoopToJsonTest.java | 24 +++++++++++- .../resources/tosca/pdp-group-policy-payload.json | 16 ++++++++ 7 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 src/test/resources/tosca/pdp-group-policy-payload.json (limited to 'src') diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index a24d3449f..83f938dde 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -23,6 +23,8 @@ package org.onap.clamp.loop; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; @@ -257,6 +259,30 @@ public class Loop implements Serializable { return buffer.toString().replace('.', '_').replaceAll(" ", ""); } + public String createPoliciesPayloadPdpGroup() { + JsonObject jsonObject = new JsonObject(); + JsonArray jsonArray = new JsonArray(); + jsonObject.add("policies", jsonArray); + + for (OperationalPolicy opPolicy : this.getOperationalPolicies()) { + JsonObject policyNode = new JsonObject(); + jsonArray.add(policyNode); + policyNode.addProperty("policy-id", opPolicy.getName()); + + for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) { + JsonObject guardPolicyNode = new JsonObject(); + jsonArray.add(guardPolicyNode); + guardPolicyNode.addProperty("policy-id", guardName); + } + } + for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) { + JsonObject policyNode = new JsonObject(); + jsonArray.add(policyNode); + policyNode.addProperty("policy-id", microServicePolicy.getName()); + } + return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject); + } + @Override public int hashCode() { final int prime = 31; diff --git a/src/main/resources/application-noaaf.properties b/src/main/resources/application-noaaf.properties index 84e97ea30..5316f8197 100644 --- a/src/main/resources/application-noaaf.properties +++ b/src/main/resources/application-noaaf.properties @@ -135,7 +135,7 @@ clamp.config.dcae.deployment.template=classpath:/clds/templates/dcae-deployment- # # # Configuration Settings for Policy Engine Components -clamp.config.policy.url=http://policy.api.simpledemo.onap.org:8081/policy/api/v1 +clamp.config.policy.url=http://policy.api.simpledemo.onap.org:8081 clamp.config.policy.userName=test clamp.config.policy.password=test clamp.config.policy.pdpUrl1=http://policy.api.simpledemo.onap.org:8081/pdp/ , testpdp, alpha123 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4792d057d..54cd4676a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -150,7 +150,7 @@ clamp.config.dcae.deployment.template=classpath:/clds/templates/dcae-deployment- # # # Configuration Settings for Policy Engine Components -clamp.config.policy.url=http://policy.api.simpledemo.onap.org:8081/policy/api/v1 +clamp.config.policy.url=http://policy.api.simpledemo.onap.org:8081 clamp.config.policy.userName=test clamp.config.policy.password=test clamp.config.policy.pdpUrl1=http://policy.api.simpledemo.onap.org:8081/pdp/ , testpdp, alpha123 diff --git a/src/main/resources/clds/camel/rest/clamp-api-v2.xml b/src/main/resources/clds/camel/rest/clamp-api-v2.xml index 810c9d205..8a39f1358 100644 --- a/src/main/resources/clds/camel/rest/clamp-api-v2.xml +++ b/src/main/resources/clds/camel/rest/clamp-api-v2.xml @@ -175,8 +175,17 @@ - + + true + + + + + diff --git a/src/main/resources/clds/camel/routes/flexible-flow.xml b/src/main/resources/clds/camel/routes/flexible-flow.xml index 33419c219..64af7b264 100644 --- a/src/main/resources/clds/camel/routes/flexible-flow.xml +++ b/src/main/resources/clds/camel/routes/flexible-flow.xml @@ -113,7 +113,7 @@ application/json - {{clamp.config.policy.url}}/policyTypes/${header.microServicePolicy.getModelType()}/versions/1.0.0/policies + {{clamp.config.policy.url}}/policy/api/v1/policyTypes/${header.microServicePolicy.getModelType()}/versions/1.0.0/policies DELETE - {{clamp.config.policy.url}}/policyTypes/${header.microServicePolicy.getModelType()}/versions/1.0.0/policies/${header.microServicePolicy.getName()} + {{clamp.config.policy.url}}/policy/api/v1/policyTypes/${header.microServicePolicy.getModelType()}/versions/1.0.0/policies/${header.microServicePolicy.getName()} POST - application/json + application/yaml; legacy-version - {{clamp.config.policy.url}}/policyTypes/onap.policies.controloop.operational/versions/1.0.0/policies + {{clamp.config.policy.url}}/policy/api/v1/policyTypes/onap.policies.controloop.operational/versions/1.0.0/policies DELETE - {{clamp.config.policy.url}}/policyTypes/onap.policies.controloop.operational/versions/1.0.0/policies/${header.operationalPolicy.getName()} + {{clamp.config.policy.url}}/policy/api/v1/policyTypes/onap.policies.controloop.operational/versions/1.0.0/policies/${header.operationalPolicy.getName()} application/json - {{clamp.config.policy.url}}/policyTypes/onap.policies.controlloop.Guard/versions/1.0.0/policies + {{clamp.config.policy.url}}/policy/api/v1/policyTypes/onap.policies.controlloop.Guard/versions/1.0.0/policies DELETE - {{clamp.config.policy.url}}/policyTypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/${header.guardPolicy.getKey()} + {{clamp.config.policy.url}}/policy/api/v1/policyTypes/onap.policies.controlloop.Guard/versions/1.0.0/policies/${header.guardPolicy.getKey()} - + + + + + + ${header.LoopObject.createPoliciesPayloadPdpGroup()} + + + + POST + + + application/json + + + {{clamp.config.policy.url}}/policy/pap/v1/pdps + + + + + + \ No newline at end of file diff --git a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java index 0e03e1b06..dcad1a516 100644 --- a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java +++ b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java @@ -29,15 +29,18 @@ import static org.junit.Assert.assertNotNull; import com.google.gson.Gson; import com.google.gson.JsonObject; +import java.io.IOException; import java.util.HashSet; import java.util.Random; import org.junit.Test; import org.onap.clamp.clds.util.JsonUtils; +import org.onap.clamp.clds.util.ResourceFileUtil; import org.onap.clamp.loop.log.LogType; import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.policy.microservice.MicroServicePolicy; import org.onap.clamp.policy.operational.OperationalPolicy; +import org.skyscreamer.jsonassert.JSONAssert; public class LoopToJsonTest { @@ -74,10 +77,11 @@ public class LoopToJsonTest { } @Test - public void LoopGsonTest() { + public void LoopGsonTest() throws IOException { Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", "123456789", "https://dcaetest.org", "UUID-blueprint"); - OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); + OperationalPolicy opPolicy = this.getOperationalPolicy( + ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest"); loopTest.addOperationalPolicy(opPolicy); MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "", "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", @@ -103,4 +107,20 @@ public class LoopToJsonTest { assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog, "loop"); } + + @Test + public void createPoliciesPayloadPdpGroupTest() throws IOException { + Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", + "123456789", "https://dcaetest.org", "UUID-blueprint"); + OperationalPolicy opPolicy = this.getOperationalPolicy( + ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest"); + loopTest.addOperationalPolicy(opPolicy); + MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "", + "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", + "{\"param1\":\"value1\"}", true); + loopTest.addMicroServicePolicy(microServicePolicy); + + JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"), + loopTest.createPoliciesPayloadPdpGroup(), false); + } } diff --git a/src/test/resources/tosca/pdp-group-policy-payload.json b/src/test/resources/tosca/pdp-group-policy-payload.json new file mode 100644 index 000000000..bf941e516 --- /dev/null +++ b/src/test/resources/tosca/pdp-group-policy-payload.json @@ -0,0 +1,16 @@ +{ + "policies": [ + { + "policy-id": "GuardOpPolicyTest" + }, + { + "policy-id": "guard2" + }, + { + "policy-id": "guard1" + }, + { + "policy-id": "configPolicyTest" + } + ] +} \ No newline at end of file -- cgit 1.2.3-korg From 9342b6c9ea3dd02feb9e1d8acc81e5b50c66f1e2 Mon Sep 17 00:00:00 2001 From: sebdet Date: Wed, 10 Apr 2019 12:39:53 +0200 Subject: Rework Gson in Camel Create new Camel DataFormat class to handle the Clamp JSon model specific type Issue-ID: CLAMP-303 Change-Id: I2abd173c7d2a96a03d333a8def4307bc3cf082d7 Signed-off-by: sebdet --- .../onap/clamp/clds/config/CamelConfiguration.java | 2 +- .../configuration/CamelGsonConfiguration.java | 63 -------- .../clamp/configuration/ClampGsonDataFormat.java | 177 +++++++++++++++++++++ .../org/apache/camel/dataformat/clamp-gson | 23 +++ 4 files changed, 201 insertions(+), 64 deletions(-) delete mode 100644 src/main/java/org/onap/clamp/configuration/CamelGsonConfiguration.java create mode 100644 src/main/java/org/onap/clamp/configuration/ClampGsonDataFormat.java create mode 100644 src/main/resources/META-INF/services/org/apache/camel/dataformat/clamp-gson (limited to 'src') diff --git a/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java b/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java index c3b24bc81..f178ce039 100644 --- a/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java +++ b/src/main/java/org/onap/clamp/clds/config/CamelConfiguration.java @@ -36,7 +36,7 @@ public class CamelConfiguration extends RouteBuilder { @Override public void configure() { - restConfiguration().component("servlet").bindingMode(RestBindingMode.json).jsonDataFormat("json-gson") + restConfiguration().component("servlet").bindingMode(RestBindingMode.json).jsonDataFormat("clamp-gson") .dataFormatProperty("prettyPrint", "true")// .enableCORS(true) // turn on swagger api-doc .apiContextPath("api-doc").apiVendorExtension(true).apiProperty("api.title", "Clamp Rest API") diff --git a/src/main/java/org/onap/clamp/configuration/CamelGsonConfiguration.java b/src/main/java/org/onap/clamp/configuration/CamelGsonConfiguration.java deleted file mode 100644 index f71d2f091..000000000 --- a/src/main/java/org/onap/clamp/configuration/CamelGsonConfiguration.java +++ /dev/null @@ -1,63 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2019 Nokia 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.configuration; - -import com.google.gson.ExclusionStrategy; -import com.google.gson.FieldAttributes; -import com.google.gson.annotations.Expose; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import org.apache.camel.component.gson.GsonDataFormat; -import org.apache.camel.spi.DataFormatCustomizer; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class CamelGsonConfiguration { - - @Bean - public List> provideGsonCustomizers() { - DataFormatCustomizer dataFormatCustomizer = dataformat -> - dataformat.setExclusionStrategies( - Collections.singletonList(new ExcludeFieldsWithoutExposedAnnotation()) - ); - return Collections.singletonList(dataFormatCustomizer); - } - - private static class ExcludeFieldsWithoutExposedAnnotation implements ExclusionStrategy { - - @Override - public boolean shouldSkipField(FieldAttributes f) { - return Optional.ofNullable(f.getAnnotation(Expose.class)) - .map(expose -> !expose.serialize()) - .orElse(true); - } - - @Override - public boolean shouldSkipClass(Class clazz) { - return false; - } - } -} diff --git a/src/main/java/org/onap/clamp/configuration/ClampGsonDataFormat.java b/src/main/java/org/onap/clamp/configuration/ClampGsonDataFormat.java new file mode 100644 index 000000000..aad1ab4c7 --- /dev/null +++ b/src/main/java/org/onap/clamp/configuration/ClampGsonDataFormat.java @@ -0,0 +1,177 @@ +/*- + * ============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.configuration; + +import com.google.gson.Gson; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; + +import org.apache.camel.Exchange; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatName; +import org.apache.camel.support.ServiceSupport; +import org.apache.camel.util.IOHelper; +import org.onap.clamp.clds.util.JsonUtils; + +public class ClampGsonDataFormat extends ServiceSupport implements DataFormat, DataFormatName { + private Gson gson; + private Class unmarshalType; + private Type unmarshalGenericType; + private boolean contentTypeHeader = true; + + public ClampGsonDataFormat() { + this(Object.class); + } + + /** + * Use the default Gson {@link Gson} and with a custom unmarshal type + * + * @param unmarshalType + * the custom unmarshal type + */ + public ClampGsonDataFormat(Class unmarshalType) { + this(null, unmarshalType); + } + + /** + * Use a custom Gson mapper and and unmarshal type + * + * @param gson + * the custom mapper + * @param unmarshalType + * the custom unmarshal type + */ + public ClampGsonDataFormat(Gson gson, Class unmarshalType) { + this.gson = gson; + this.unmarshalType = unmarshalType; + } + + /** + * Use the default Gson {@link Gson} and with a custom unmarshal generic type + * + * @param unmarshalGenericType + * the custom unmarshal generic type + */ + public ClampGsonDataFormat(Type unmarshalGenericType) { + this(null, unmarshalGenericType); + } + + /** + * Use a custom Gson mapper and and unmarshal token type + * + * @param gson + * the custom mapper + * @param unmarshalGenericType + * the custom unmarshal generic type + */ + public ClampGsonDataFormat(Gson gson, Type unmarshalGenericType) { + this.gson = gson; + this.unmarshalGenericType = unmarshalGenericType; + } + + @Override + public String getDataFormatName() { + return "clamp-gson"; + } + + @Override + public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception { + try (final OutputStreamWriter osw = new OutputStreamWriter(stream, StandardCharsets.UTF_8); + final BufferedWriter writer = IOHelper.buffered(osw)) { + gson.toJson(graph, writer); + } + + if (contentTypeHeader) { + if (exchange.hasOut()) { + exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json"); + } else { + exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json"); + } + } + } + + @Override + public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { + try (final InputStreamReader isr = new InputStreamReader(stream, StandardCharsets.UTF_8); + final BufferedReader reader = IOHelper.buffered(isr)) { + if (unmarshalGenericType == null) { + return gson.fromJson(reader, unmarshalType); + } else { + return gson.fromJson(reader, unmarshalGenericType); + } + } + } + + @Override + protected void doStart() throws Exception { + if (gson == null) { + gson = JsonUtils.GSON_JPA_MODEL; + } + } + + @Override + protected void doStop() throws Exception { + // noop + } + + // Properties + // ------------------------------------------------------------------------- + + public Class getUnmarshalType() { + return this.unmarshalType; + } + + public void setUnmarshalType(Class unmarshalType) { + this.unmarshalType = unmarshalType; + } + + public Type getUnmarshalGenericType() { + return this.unmarshalType; + } + + public void setUnmarshalGenericType(Type unmarshalGenericType) { + this.unmarshalGenericType = unmarshalGenericType; + } + + public boolean isContentTypeHeader() { + return contentTypeHeader; + } + + /** + * If enabled then Gson will set the Content-Type header to + * application/json when marshalling. + */ + public void setContentTypeHeader(boolean contentTypeHeader) { + this.contentTypeHeader = contentTypeHeader; + } + + public Gson getGson() { + return this.gson; + } +} diff --git a/src/main/resources/META-INF/services/org/apache/camel/dataformat/clamp-gson b/src/main/resources/META-INF/services/org/apache/camel/dataformat/clamp-gson new file mode 100644 index 000000000..3fa78e040 --- /dev/null +++ b/src/main/resources/META-INF/services/org/apache/camel/dataformat/clamp-gson @@ -0,0 +1,23 @@ +/*- + * ============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============================================ + * =================================================================== + */ + +class=org.onap.clamp.configuration.ClampGsonDataFormat \ No newline at end of file -- cgit 1.2.3-korg From cd64cc4b390a15602e084d0d94007ec83aa530a4 Mon Sep 17 00:00:00 2001 From: sebdet Date: Wed, 10 Apr 2019 14:07:35 +0200 Subject: Add delete support Move delete code to Camel way so that it's more flexible + introduce removal of the loop + test Issue-ID: CLAMP-315 Change-Id: I7df87e1441d4511ad6b42d0d269c3b2c8c7d8eef Signed-off-by: sebdet --- .../java/org/onap/clamp/loop/LoopOperation.java | 160 --------------------- src/main/java/org/onap/clamp/loop/LoopService.java | 4 + .../org/onap/clamp/loop/log/LoopLogService.java | 4 + .../resources/clds/camel/rest/clamp-api-v2.xml | 89 ++++++++++-- .../resources/clds/camel/routes/flexible-flow.xml | 14 +- .../org/onap/clamp/loop/LoopServiceTestItCase.java | 43 ++++++ 6 files changed, 132 insertions(+), 182 deletions(-) (limited to 'src') diff --git a/src/main/java/org/onap/clamp/loop/LoopOperation.java b/src/main/java/org/onap/clamp/loop/LoopOperation.java index 5b55ab0de..d9ea24871 100644 --- a/src/main/java/org/onap/clamp/loop/LoopOperation.java +++ b/src/main/java/org/onap/clamp/loop/LoopOperation.java @@ -31,7 +31,6 @@ import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; -import java.io.IOException; import java.lang.reflect.Array; import java.util.Collection; import java.util.Date; @@ -213,163 +212,4 @@ public class LoopOperation { return new JsonPrimitive(String.valueOf(o)); } - /** - * Submit the Ms policies. - * - * @param loopName - * the loop name - * @return the updated loop - * @throws IOException - * IO exception - * @throws Exceptions - * during the operation - */ - public Loop submitMsPolicies(String loopName) throws OperationException, IOException { - util.entering(request, "LoopOperation: delete microservice policies"); - Date startTime = new Date(); - Loop loop = loopService.getLoop(loopName); - - if (loop == null) { - String msg = "Submit MS policies exception: Not able to find closed loop:" + loopName; - util.exiting(HttpStatus.INTERNAL_SERVER_ERROR.toString(), msg, Level.INFO, - ONAPLogConstants.ResponseStatus.ERROR); - throw new OperationException(msg); - } - - // verify the current closed loop state - if (loop.getLastComputedState() != LoopState.SUBMITTED && loop.getLastComputedState() != LoopState.DESIGN) { - String msg = "Submit MS policies exception: This closed loop is in state:" + loop.getLastComputedState() - + ". It could be deleted only when it is in SUBMITTED state."; - util.exiting(HttpStatus.CONFLICT.toString(), msg, Level.INFO, ONAPLogConstants.ResponseStatus.ERROR); - throw new OperationException(msg); - } - - // Establish the api call to Policy to create the ms services - // policyOp.createMsPolicy(loop.getMicroServicePolicies()); - - // audit log - LoggingUtils.setTimeContext(startTime, new Date()); - auditLogger.info("Deletion of MS policies completed"); - util.exiting(HttpStatus.OK.toString(), "Successful", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED); - return loop; - } - - /** - * Delete the Ms policies. - * - * @param loopName - * the loop name - * @return the updated loop - * @throws IOException - * IO exception - * @throws Exceptions - * during the operation - */ - public Loop deleteMsPolicies(Exchange camelExchange, String loopName) throws OperationException, IOException { - util.entering(request, "LoopOperation: delete microservice policies"); - Date startTime = new Date(); - Loop loop = loopService.getLoop(loopName); - - if (loop == null) { - String msg = "Delete MS policies exception: Not able to find closed loop:" + loopName; - util.exiting(HttpStatus.INTERNAL_SERVER_ERROR.toString(), msg, Level.INFO, - ONAPLogConstants.ResponseStatus.ERROR); - throw new OperationException(msg); - } - - // verify the current closed loop state - if (loop.getLastComputedState() != LoopState.SUBMITTED) { - String msg = "Delete MS policies exception: This closed loop is in state:" + loop.getLastComputedState() - + ". It could be deleted only when it is in SUBMITTED state."; - util.exiting(HttpStatus.CONFLICT.toString(), msg, Level.INFO, ONAPLogConstants.ResponseStatus.ERROR); - throw new OperationException(msg); - } - - // Establish the api call to Policy to create the ms services - // policyOp.deleteMsPolicy(loop.getMicroServicePolicies()); - - // audit log - LoggingUtils.setTimeContext(startTime, new Date()); - auditLogger.info("Deletion of MS policies completed"); - util.exiting(HttpStatus.OK.toString(), "Successful", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED); - return loop; - } - - /** - * Delete the operational policy. - * - * @param loopName - * the loop name - * @return the updated loop - * @throws Exceptions - * during the operation - */ - public Loop deleteOpPolicy(Exchange camelExchange, String loopName) throws OperationException { - util.entering(request, "LoopOperation: delete guard policy"); - Date startTime = new Date(); - Loop loop = loopService.getLoop(loopName); - - if (loop == null) { - String msg = "Delete guard policy exception: Not able to find closed loop:" + loopName; - util.exiting(HttpStatus.INTERNAL_SERVER_ERROR.toString(), msg, Level.INFO, - ONAPLogConstants.ResponseStatus.ERROR); - throw new OperationException(msg); - } - - // verify the current closed loop state - if (loop.getLastComputedState() != LoopState.SUBMITTED) { - String msg = "Delete MS policies exception: This closed loop is in state:" + loop.getLastComputedState() - + ". It could be deleted only when it is in SUBMITTED state."; - util.exiting(HttpStatus.CONFLICT.toString(), msg, Level.INFO, ONAPLogConstants.ResponseStatus.ERROR); - throw new OperationException(msg); - } - - // Establish the api call to Policy to delete operational policy - // client.deleteOpPolicy(); - - // audit log - LoggingUtils.setTimeContext(startTime, new Date()); - auditLogger.info("Deletion of Guard policy completed"); - util.exiting(HttpStatus.OK.toString(), "Successful", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED); - return loop; - } - - /** - * Delete the Guard policy. - * - * @param loopName - * the loop name - * @return the updated loop - * @throws Exceptions - * during the operation - */ - public Loop deleteGuardPolicy(Exchange camelExchange, String loopName) throws OperationException { - util.entering(request, "LoopOperation: delete operational policy"); - Date startTime = new Date(); - Loop loop = loopService.getLoop(loopName); - - if (loop == null) { - String msg = "Delete operational policy exception: Not able to find closed loop:" + loopName; - util.exiting(HttpStatus.INTERNAL_SERVER_ERROR.toString(), msg, Level.INFO, - ONAPLogConstants.ResponseStatus.ERROR); - throw new OperationException(msg); - } - - // verify the current closed loop state - if (loop.getLastComputedState() != LoopState.SUBMITTED) { - String msg = "Delete MS policies exception: This closed loop is in state:" + loop.getLastComputedState() - + ". It could be deleted only when it is in SUBMITTED state."; - util.exiting(HttpStatus.CONFLICT.toString(), msg, Level.INFO, ONAPLogConstants.ResponseStatus.ERROR); - throw new OperationException(msg); - } - - // Establish the api call to Policy to delete Guard policy - // client.deleteOpPolicy(); - - // audit log - LoggingUtils.setTimeContext(startTime, new Date()); - auditLogger.info("Deletion of operational policy completed"); - util.exiting(HttpStatus.OK.toString(), "Successful", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED); - return loop; - } } diff --git a/src/main/java/org/onap/clamp/loop/LoopService.java b/src/main/java/org/onap/clamp/loop/LoopService.java index 8d61b877b..4c1392253 100644 --- a/src/main/java/org/onap/clamp/loop/LoopService.java +++ b/src/main/java/org/onap/clamp/loop/LoopService.java @@ -67,6 +67,10 @@ public class LoopService { return loopsRepository.findById(loopName).orElse(null); } + public void deleteLoop(String loopName) { + loopsRepository.deleteById(loopName); + } + Loop updateAndSaveOperationalPolicies(String loopName, List newOperationalPolicies) { Loop loop = findClosedLoopByName(loopName); Set newPolicies = operationalPolicyService.updatePolicies(loop, newOperationalPolicies); diff --git a/src/main/java/org/onap/clamp/loop/log/LoopLogService.java b/src/main/java/org/onap/clamp/loop/log/LoopLogService.java index b593b41ed..b02bc11c4 100644 --- a/src/main/java/org/onap/clamp/loop/log/LoopLogService.java +++ b/src/main/java/org/onap/clamp/loop/log/LoopLogService.java @@ -40,4 +40,8 @@ public class LoopLogService { public void addLog(String message, String logType, Loop loop) { repository.save(new LoopLog(message, LogType.valueOf(logType), loop)); } + + public boolean isExisting(Long logId) { + return repository.existsById(logId); + } } diff --git a/src/main/resources/clds/camel/rest/clamp-api-v2.xml b/src/main/resources/clds/camel/rest/clamp-api-v2.xml index 8a39f1358..ffa4719f0 100644 --- a/src/main/resources/clds/camel/rest/clamp-api-v2.xml +++ b/src/main/resources/clds/camel/rest/clamp-api-v2.xml @@ -97,14 +97,14 @@ - - ${header.loopName} - + message="Receive SUBMIT request for loop: ${header.loopName}" /> + + ${header.loopName} + @@ -120,12 +120,12 @@ - + false - + true @@ -143,12 +143,12 @@ - + false - + true @@ -165,18 +165,18 @@ loggingLevel="INFO" message="Processing Guard Policy: ${header.guardPolicy.getKey()}" /> - + false - + true - + true @@ -190,14 +190,73 @@ + + + ${header.loopName} + + - + uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('Receive DELETE request','INFO',${header.LoopObject})" /> + + ${header.LoopObject.getMicroServicePolicies()} + + + ${body} + + + + true + + + + + + + ${header.LoopObject.getOperationalPolicies()} + + + ${body} + + + + true + + + + + ${header.operationalPolicy.createGuardPolicyPayloads().entrySet()} + + + ${body} + + + + + true + + + + + + + uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('DELETE request successfully executed','INFO',${header.LoopObject})" /> diff --git a/src/main/resources/clds/camel/routes/flexible-flow.xml b/src/main/resources/clds/camel/routes/flexible-flow.xml index 64af7b264..1ce67ccee 100644 --- a/src/main/resources/clds/camel/routes/flexible-flow.xml +++ b/src/main/resources/clds/camel/routes/flexible-flow.xml @@ -120,7 +120,7 @@ loggingLevel="INFO" message="Endpoint to create microservice policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}" /> @@ -143,7 +143,7 @@ loggingLevel="INFO" message="Endpoint to delete microservice policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&deleteWithBody=false&mapHttpMessageBody=false&mapHttpMessageFormUrlEncodedBody=false" /> @@ -171,7 +171,7 @@ loggingLevel="INFO" message="Endpoint to create operational policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}" /> @@ -194,7 +194,7 @@ loggingLevel="INFO" message="Endpoint to delete operational policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&deleteWithBody=false&mapHttpMessageBody=false&mapHttpMessageFormUrlEncodedBody=false" /> @@ -222,7 +222,7 @@ loggingLevel="INFO" message="Endpoint to create guard policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}" /> @@ -245,7 +245,7 @@ loggingLevel="INFO" message="Endpoint to delete guard policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&deleteWithBody=false&mapHttpMessageBody=false&mapHttpMessageFormUrlEncodedBody=false" /> @@ -273,7 +273,7 @@ loggingLevel="INFO" message="Endpoint to create PDP Group policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}" /> diff --git a/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java index 23723386b..c4254ec8c 100644 --- a/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java +++ b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java @@ -38,8 +38,13 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.onap.clamp.clds.Application; import org.onap.clamp.clds.util.JsonUtils; +import org.onap.clamp.loop.log.LogType; +import org.onap.clamp.loop.log.LoopLog; +import org.onap.clamp.loop.log.LoopLogService; import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.microservice.MicroservicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; +import org.onap.clamp.policy.operational.OperationalPolicyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @@ -57,6 +62,15 @@ public class LoopServiceTestItCase { @Autowired LoopsRepository loopsRepository; + @Autowired + MicroservicePolicyService microServicePolicyService; + + @Autowired + OperationalPolicyService operationalPolicyService; + + @Autowired + LoopLogService loopLogService; + @After public void tearDown() { loopsRepository.deleteAll(); @@ -276,6 +290,35 @@ public class LoopServiceTestItCase { assertThat(returnedGlobalProperties.getAsJsonObject()).isEqualTo(updatedGlobalProperites); } + @Test + @Transactional + public void deleteAttempt() { + saveTestLoopToDb(); + // Add log + Loop loop = loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null); + loop.addLog(new LoopLog("test", LogType.INFO, loop)); + loop = loopService.saveOrUpdateLoop(loop); + // Add op policy + OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy)); + + // Add Micro service policy + MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", "", + "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy)); + + // Verify it's there + assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull(); + loopService.deleteLoop(EXAMPLE_LOOP_NAME); + // Verify it's well deleted and has been cascaded + assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull(); + assertThat(microServicePolicyService.isExisting("microPolicy")).isFalse(); + assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse(); + assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse(); + } + private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) { return new Loop(loopName, loopBlueprint, loopSvg); } -- cgit 1.2.3-korg From 7fc0739bccbfd999e281856bd7f8a517319b1fb8 Mon Sep 17 00:00:00 2001 From: sebdet Date: Wed, 10 Apr 2019 15:27:46 +0200 Subject: Remove SQL Dump Remove SQL dump in logs done by hibernate Issue-ID: CLAMP-254 Change-Id: Id357ac37dfa475a9774d0b2c3b5f254c84caa9d7 Signed-off-by: sebdet --- src/main/resources/application-noaaf.properties | 2 +- src/main/resources/application.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/resources/application-noaaf.properties b/src/main/resources/application-noaaf.properties index 5316f8197..56ad7e846 100644 --- a/src/main/resources/application-noaaf.properties +++ b/src/main/resources/application-noaaf.properties @@ -110,7 +110,7 @@ spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.use-new-id-generator-mappings=true # Whether to enable logging of SQL statements. -spring.jpa.show-sql=true +#spring.jpa.show-sql=true #Async Executor default Parameters async.core.pool.size=10 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 54cd4676a..df055e40b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -125,7 +125,7 @@ spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.use-new-id-generator-mappings=true # Whether to enable logging of SQL statements. -spring.jpa.show-sql=true +#spring.jpa.show-sql=true #Async Executor default Parameters async.core.pool.size=10 -- cgit 1.2.3-korg From ece804b26d14786cc382dedc5a955f0f36b54029 Mon Sep 17 00:00:00 2001 From: sebdet Date: Wed, 10 Apr 2019 15:29:16 +0200 Subject: Add timeout + Creds Add Creds and timeout to queries done to policy Engine in CAMEL routes Issue-ID: CLAMP-303 Change-Id: I8c744a3272e139e5968751bcece7c53612c23904 Signed-off-by: sebdet --- src/main/resources/clds/camel/routes/flexible-flow.xml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main/resources/clds/camel/routes/flexible-flow.xml b/src/main/resources/clds/camel/routes/flexible-flow.xml index 1ce67ccee..1f3e01e27 100644 --- a/src/main/resources/clds/camel/routes/flexible-flow.xml +++ b/src/main/resources/clds/camel/routes/flexible-flow.xml @@ -120,7 +120,7 @@ loggingLevel="INFO" message="Endpoint to create microservice policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&httpClient.connectTimeout=10000&authUsername={{clamp.config.policy.userName}}&authPassword={{clamp.config.policy.password}}" /> @@ -143,7 +143,7 @@ loggingLevel="INFO" message="Endpoint to delete microservice policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&httpClient.connectTimeout=10000&deleteWithBody=false&mapHttpMessageBody=false&mapHttpMessageFormUrlEncodedBody=false&authUsername={{clamp.config.policy.userName}}&authPassword={{clamp.config.policy.password}}" /> @@ -171,7 +171,7 @@ loggingLevel="INFO" message="Endpoint to create operational policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&httpClient.connectTimeout=10000&authUsername={{clamp.config.policy.userName}}&authPassword={{clamp.config.policy.password}}" /> @@ -194,7 +194,7 @@ loggingLevel="INFO" message="Endpoint to delete operational policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&httpClient.connectTimeout=10000&deleteWithBody=false&mapHttpMessageBody=false&mapHttpMessageFormUrlEncodedBody=false&authUsername={{clamp.config.policy.userName}}&authPassword={{clamp.config.policy.password}}" /> @@ -222,7 +222,7 @@ loggingLevel="INFO" message="Endpoint to create guard policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&httpClient.connectTimeout=10000&authUsername={{clamp.config.policy.userName}}&authPassword={{clamp.config.policy.password}}" /> @@ -245,7 +245,7 @@ loggingLevel="INFO" message="Endpoint to delete guard policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&httpClient.connectTimeout=10000&deleteWithBody=false&mapHttpMessageBody=false&mapHttpMessageFormUrlEncodedBody=false&authUsername={{clamp.config.policy.userName}}&authPassword={{clamp.config.policy.password}}" /> @@ -273,7 +273,7 @@ loggingLevel="INFO" message="Endpoint to create PDP Group policy: ${header.CamelHttpMethod} ${header.CamelHttpUri}"> + uri="http4://policyhost:8085?throwExceptionOnFailure=${header.RaiseHttpExceptionFlag}&httpClient.connectTimeout=10000&authUsername={{clamp.config.policy.userName}}&authPassword={{clamp.config.policy.password}}" /> -- cgit 1.2.3-korg