From 0655a5a4adad158173fcbac1ab02e21a2c2cb18c Mon Sep 17 00:00:00 2001 From: ERIMROB Date: Thu, 10 Jun 2021 09:13:50 +0100 Subject: Make policy participant interact with Policy API Change the policy participant from interacting directly with the database, into participant interacting with policy API instead Issue-ID: POLICY-3330 Change-Id: I02420b1e43f1b18e337b00ebd300f03bb9b7412d Signed-off-by: ERIMROB --- .../policy/client/AbstractHttpClient.java | 75 +++++++++++++++++++ .../policy/client/PolicyApiHttpClient.java | 84 ++++++++++++++++++++++ .../policy/config/ParticipantConfig.java | 16 ----- .../main/handler/ControlLoopElementHandler.java | 27 +++---- .../parameters/ParticipantPolicyParameters.java | 4 +- .../resources/config/PolicyParticipantConfig.json | 66 +++++++++++------ 6 files changed, 220 insertions(+), 52 deletions(-) create mode 100644 participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/AbstractHttpClient.java create mode 100644 participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyApiHttpClient.java (limited to 'participant/participant-impl/participant-impl-policy/src/main') diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/AbstractHttpClient.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/AbstractHttpClient.java new file mode 100644 index 000000000..4b6686156 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/AbstractHttpClient.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.participant.policy.client; + +import java.io.Closeable; +import java.io.IOException; +import java.util.Collections; +import java.util.Map; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class AbstractHttpClient implements Closeable { + + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractHttpClient.class); + private final HttpClient httpclient; + + + /** + * Constructor. + */ + protected AbstractHttpClient(BusTopicParams policyApiParameters) { + try { + httpclient = HttpClientFactoryInstance.getClientFactory().build(policyApiParameters); + } catch (final Exception e) { + throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR, " Client failed to start", e); + } + } + + protected Response executePost(String path, final Entity entity) { + Response response = httpclient.post(path, entity, Map.of(HttpHeaders.ACCEPT, + MediaType.APPLICATION_JSON, HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)); + if (response.getStatus() / 100 != 2) { + LOGGER.error( + "Invocation of path {} failed for entity {}. Response status: {}, Response status info: {}", + path, entity, response.getStatus(), response.getStatusInfo()); + } + return response; + } + + protected Response executeDelete(String path) { + return httpclient.delete(path, Collections.emptyMap()); + } + + @Override + public void close() throws IOException { + httpclient.shutdown(); + } +} \ No newline at end of file diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyApiHttpClient.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyApiHttpClient.java new file mode 100644 index 000000000..3d1047ce4 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyApiHttpClient.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.participant.policy.client; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.springframework.stereotype.Component; + +@Component +public class PolicyApiHttpClient extends AbstractHttpClient { + + private static final String POLICY_URI = "/policy/api/v1/"; + + /** + * Constructor. + */ + public PolicyApiHttpClient(ParticipantPolicyParameters parameters) { + super(parameters.getPolicyApiParameters()); + } + + /** + * Create Policy Types. + * + * @param toscaServiceTemplate the whole ToscaServiceTemplate + * @return Response + */ + public Response createPolicyType(ToscaServiceTemplate toscaServiceTemplate) throws PfModelException { + return executePost(POLICY_URI + "policytypes", Entity.entity(toscaServiceTemplate, MediaType.APPLICATION_JSON)); + } + + /** + * Create Policies. + * + * @param toscaServiceTemplate the whole ToscaServiceTemplate + * @return Response + */ + public Response createPolicy(final ToscaServiceTemplate toscaServiceTemplate) { + return executePost(POLICY_URI + "policies", Entity.entity(toscaServiceTemplate, MediaType.APPLICATION_JSON)); + } + + /** + * Delete Policies. + * + * @param policyName the name of the policy to be deleted + * @param policyVersion the version of the policy to be deleted + * @return Response + */ + public Response deletePolicy(final String policyName, final String policyVersion) { + return executeDelete(POLICY_URI + "policies/" + policyName + "/versions/" + policyVersion); + } + + /** + * Delete Policy types. + * + * @param policyTypeName the name of the policy to be deleted + * @param policyTypeVersion the version of the policy to be deleted + * @return Response + */ + public Response deletePolicyType(final String policyTypeName, final String policyTypeVersion) { + return executeDelete(POLICY_URI + "policytypes/" + policyTypeName + "/versions/" + policyTypeVersion); + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParticipantConfig.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParticipantConfig.java index afef1a5d2..3b8324151 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParticipantConfig.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/config/ParticipantConfig.java @@ -24,9 +24,6 @@ import org.onap.policy.clamp.controlloop.participant.intermediary.api.Participan import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryFactory; import org.onap.policy.clamp.controlloop.participant.policy.main.handler.ControlLoopElementHandler; import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -49,17 +46,4 @@ public class ParticipantConfig { controlLoopElementHandler.setIntermediaryApi(intermediaryApi); return intermediaryApi; } - - /** - * Create PolicyModelsProvider. - * - * @param parameters the Participant Policy Parameters - * @return PolicyModelsProvider - * @throws PfModelException in case of an exception - */ - @Bean - public PolicyModelsProvider databaseProvider(ParticipantPolicyParameters parameters) throws PfModelException { - return new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters.getDatabaseProviderParameters()); - } - } diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java index 5b07568da..1b176f076 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java @@ -32,9 +32,9 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.controlloop.participant.policy.client.PolicyApiHttpClient; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -52,7 +52,7 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { private final Map policyTypeMap = new LinkedHashMap<>(); private final Map policyMap = new LinkedHashMap<>(); - private final PolicyModelsProvider databaseProvider; + private final PolicyApiHttpClient apiHttpClient; @Setter private ParticipantIntermediaryApi intermediaryApi; @@ -60,10 +60,10 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { /** * constructor. * - * @param databaseProvider the Policy Models Provider + * @param apiHttpClient the Policy Api Http Client */ - public ControlLoopElementHandler(PolicyModelsProvider databaseProvider) { - this.databaseProvider = databaseProvider; + public ControlLoopElementHandler(PolicyApiHttpClient apiHttpClient) { + this.apiHttpClient = apiHttpClient; } /** @@ -82,7 +82,7 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { try { deletePolicyData(controlLoopElementId, newState); } catch (PfModelRuntimeException e) { - LOGGER.debug("Delete policytpes failed", e); + LOGGER.debug("Deleting policy data failed", e); } break; case PASSIVE: @@ -100,12 +100,12 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { private void deletePolicyData(UUID controlLoopElementId, ControlLoopOrderedState newState) throws PfModelException { // Delete all policies of this controlLoop from policy framework for (Entry policy : policyMap.entrySet()) { - databaseProvider.deletePolicy(policy.getKey(), policy.getValue()); + apiHttpClient.deletePolicy(policy.getKey(), policy.getValue()); } policyMap.clear(); // Delete all policy types of this control loop from policy framework for (Entry policyType : policyTypeMap.entrySet()) { - databaseProvider.deletePolicyType(policyType.getKey(), policyType.getValue()); + apiHttpClient.deletePolicyType(policyType.getKey(), policyType.getValue()); } policyTypeMap.clear(); intermediaryApi.updateControlLoopElementState(controlLoopElementId, newState, ControlLoopState.UNINITIALISED); @@ -127,17 +127,20 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { for (ToscaPolicyType policyType : controlLoopDefinition.getPolicyTypes().values()) { policyTypeMap.put(policyType.getName(), policyType.getVersion()); } - databaseProvider.createPolicyTypes(controlLoopDefinition); + LOGGER.debug("Found Policy Types in control loop definition: {} , Creating Policy Types", + controlLoopDefinition.getName()); + apiHttpClient.createPolicyType(controlLoopDefinition); } if (controlLoopDefinition.getToscaTopologyTemplate().getPolicies() != null) { for (Map foundPolicyMap : controlLoopDefinition.getToscaTopologyTemplate() .getPolicies()) { - for (Entry policyEntry : foundPolicyMap.entrySet()) { - ToscaPolicy policy = policyEntry.getValue(); + for (ToscaPolicy policy : foundPolicyMap.values()) { policyMap.put(policy.getName(), policy.getVersion()); } } - databaseProvider.createPolicies(controlLoopDefinition); + LOGGER.debug("Found Policies in control loop definition: {} , Creating Policies", + controlLoopDefinition.getName()); + apiHttpClient.createPolicy(controlLoopDefinition); } } diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java index 13d89faba..490722a45 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java @@ -23,9 +23,9 @@ package org.onap.policy.clamp.controlloop.participant.policy.main.parameters; import javax.validation.constraints.NotBlank; import lombok.Getter; import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.parameters.ParameterGroupImpl; import org.onap.policy.common.parameters.annotations.NotNull; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; /** * Class to hold all parameters needed for the policy participant. @@ -36,7 +36,7 @@ import org.onap.policy.models.provider.PolicyModelsProviderParameters; @Getter public class ParticipantPolicyParameters extends ParameterGroupImpl { private ParticipantIntermediaryParameters intermediaryParameters; - private PolicyModelsProviderParameters databaseProviderParameters; + private BusTopicParams policyApiParameters; /** * Create the policy participant parameter group. diff --git a/participant/participant-impl/participant-impl-policy/src/main/resources/config/PolicyParticipantConfig.json b/participant/participant-impl/participant-impl-policy/src/main/resources/config/PolicyParticipantConfig.json index e6b3c8eb1..bf458fae9 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/resources/config/PolicyParticipantConfig.json +++ b/participant/participant-impl/participant-impl-policy/src/main/resources/config/PolicyParticipantConfig.json @@ -1,31 +1,53 @@ { - "name":"ParticipantParameterGroup", - "participantStatusParameters":{ - "timeIntervalMs":10000, - "description":"Participant Status", - "participantId":{ - "name": "PolicyParticipant0", - "version":"1.0.0" - }, + "name": "ControlLoopParticipantGroup", + "intermediaryParameters": { + "name": "Participant parameters", + "reportingTimeInterval": 120000, + "description": "Participant Description", "participantType":{ "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", "version":"2.3.1" }, - "participantDefinition":{ - "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", - "version":"2.3.1" + "participantId": { + "name": "org.onap.PM_Policy", + "version": "1.0.0" + }, + "clampControlLoopTopics": { + "topicSources": [ + { + "topic": "POLICY-CLRUNTIME-PARTICIPANT", + "servers": [ + "message-router" + ], + "topicCommInfrastructure": "dmaap", + "fetchTimeout": 15000 + } + ], + "topicSinks": [ + { + "topic": "POLICY-CLRUNTIME-PARTICIPANT", + "servers": [ + "message-router" + ], + "topicCommInfrastructure": "dmaap" + }, + { + "topic": "POLICY-NOTIFICATION", + "servers": [ + "message-router" + ], + "topicCommInfrastructure": "dmaap" + } + ] } }, - "topicParameterGroup": { - "topicSources" : [{ - "topic" : "POLICY-CLRUNTIME-PARTICIPANT", - "servers" : [ "127.0.0.1:3904" ], - "topicCommInfrastructure" : "dmaap" - }], - "topicSinks" : [{ - "topic" : "POLICY-CLRUNTIME-PARTICIPANT", - "servers" : [ "127.0.0.1:3904" ], - "topicCommInfrastructure" : "dmaap" - }] + "policyApiParameters": { + "clientName": "api", + "hostname": "policy-api", + "port": "6969", + "userName": "healthcheck", + "password": "zb!XztG34", + "https": true, + "allowSelfSignedCerts": true } } -- cgit 1.2.3-korg