diff options
Diffstat (limited to 'participant/participant-impl/participant-impl-policy/src/main')
11 files changed, 901 insertions, 0 deletions
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 new file mode 100644 index 000000000..932ebbece --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java @@ -0,0 +1,159 @@ +/*- + * ============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.main.handler; + +import java.time.Instant; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.UUID; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class handles implementation of controlLoopElement updates. + */ +public class ControlLoopElementHandler implements ControlLoopElementListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class); + private static final Map<String, String> policyTypeMap = new LinkedHashMap<>(); + private static final Map<String, String> policyMap = new LinkedHashMap<>(); + + /** + * Callback method to handle a control loop element state change. + * + * @param controlLoopElementId the ID of the control loop element + * @param currentState the current state of the control loop element + * @param newState the state to which the control loop element is changing to + * @throws PfModelException in case of an exception + */ + @Override + public void controlLoopElementStateChange(UUID controlLoopElementId, + ControlLoopState currentState, + ControlLoopOrderedState newState) throws PfModelException { + PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider(); + switch (newState) { + case UNINITIALISED: + try { + deletePolicyData(controlLoopElementId, newState); + } catch (PfModelRuntimeException e) { + LOGGER.debug("Delete policytpes failed", e); + } + break; + case PASSIVE: + policyProvider.getIntermediaryApi() + .updateControlLoopElementState(controlLoopElementId, newState, + ControlLoopState.PASSIVE); + break; + case RUNNING: + policyProvider.getIntermediaryApi() + .updateControlLoopElementState(controlLoopElementId, newState, + ControlLoopState.RUNNING); + break; + default: + LOGGER.debug("Unknown orderedstate {}", newState); + break; + } + } + + private void deletePolicyData(UUID controlLoopElementId, + ControlLoopOrderedState newState) throws PfModelException { + PolicyModelsProvider dbProvider = PolicyHandler.getInstance().getDatabaseProvider(); + PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider(); + if (policyMap != null) { + // Delete all policies of this controlLoop from policy framework + for (Entry<String, String> policy : policyMap.entrySet()) { + dbProvider.deletePolicy(policy.getKey(), policy.getValue()); + } + } + if (policyTypeMap != null) { + // Delete all policy types of this control loop from policy framework + for (Entry<String, String> policy : policyTypeMap.entrySet()) { + dbProvider.deletePolicyType(policy.getKey(), policy.getValue()); + } + } + policyProvider.getIntermediaryApi() + .updateControlLoopElementState(controlLoopElementId, newState, + ControlLoopState.UNINITIALISED); + } + + /** + * Callback method to handle an update on a control loop element. + * + * @param element the information on the control loop element + * @param controlLoopDefinition toscaServiceTemplate + * @throws PfModelException in case of an exception + */ + @Override + public void controlLoopElementUpdate(ControlLoopElement element, + ToscaServiceTemplate controlLoopDefinition) throws PfModelException { + PolicyModelsProvider dbProvider = PolicyHandler.getInstance().getDatabaseProvider(); + PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider(); + + policyProvider.getIntermediaryApi() + .updateControlLoopElementState(element.getId(), element.getOrderedState(), ControlLoopState.PASSIVE); + if (controlLoopDefinition.getPolicyTypes() != null) { + for (ToscaPolicyType policyType : controlLoopDefinition.getPolicyTypes().values()) { + policyTypeMap.put(policyType.getName(), policyType.getVersion()); + } + dbProvider.createPolicyTypes(controlLoopDefinition); + } + if (controlLoopDefinition.getToscaTopologyTemplate().getPolicies() != null) { + for (Map<String, ToscaPolicy> foundPolicyMap : controlLoopDefinition + .getToscaTopologyTemplate().getPolicies()) { + for (ToscaPolicy policy : foundPolicyMap.values()) { + policyMap.put(policy.getName(), policy.getVersion()); + } + } + dbProvider.createPolicies(controlLoopDefinition); + } + } + + /** + * Handle controlLoopElement statistics. + * + * @param controlLoopElementId controlloop element id + */ + @Override + public void handleStatistics(UUID controlLoopElementId) { + PolicyProvider policyProvider = PolicyHandler.getInstance().getPolicyProvider(); + ControlLoopElement clElement = policyProvider.getIntermediaryApi() + .getControlLoopElement(controlLoopElementId); + if (clElement != null) { + ClElementStatistics clElementStatistics = new ClElementStatistics(); + clElementStatistics.setControlLoopState(clElement.getState()); + clElementStatistics.setTimeStamp(Instant.now()); + policyProvider.getIntermediaryApi() + .updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics); + } + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyHandler.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyHandler.java new file mode 100644 index 000000000..d62e5f9f3 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyHandler.java @@ -0,0 +1,100 @@ +/*- + * ============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.main.handler; + +import java.io.IOException; +import java.util.List; +import java.util.Set; +import javax.ws.rs.core.Response; +import lombok.Getter; +import org.onap.policy.clamp.controlloop.common.handler.ControlLoopHandler; +import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters; +import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; +import org.onap.policy.common.endpoints.event.comm.TopicSink; +import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher; +import org.onap.policy.common.utils.services.Registry; +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.provider.PolicyModelsProviderFactory; + +/** + * This class handles policy participant and control loop elements. + * + * <p/>It is effectively a singleton that is started at system start. + */ +public class PolicyHandler extends ControlLoopHandler { + + private final ParticipantIntermediaryParameters participantParameters; + private final ParticipantPolicyParameters policyParameters; + + @Getter + private PolicyProvider policyProvider; + @Getter + private PolicyModelsProvider databaseProvider; + + /** + * Create a handler. + * + * @param parameters the parameters for access to the database + * @throws PfModelException in case of an exception + */ + public PolicyHandler(ParticipantPolicyParameters parameters) throws PfModelException { + super(parameters.getDatabaseProviderParameters()); + participantParameters = parameters.getIntermediaryParameters(); + policyParameters = parameters; + } + + public static PolicyHandler getInstance() { + return Registry.get(PolicyHandler.class.getName()); + } + + @Override + public Set<Class<?>> getProviderClasses() { + return null; + } + + @Override + public void startProviders() { + try { + policyProvider = new PolicyProvider(participantParameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider( + policyParameters.getDatabaseProviderParameters()); + } catch (PfModelException e) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "Start providers failed ", e); + } + } + + @Override + public void stopProviders() { + try { + policyProvider.close(); + } catch (IOException e) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "Stop providers failed ", e); + } + + try { + databaseProvider.close(); + } catch (PfModelException e) { + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "Stop providers failed ", e); + } + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyProvider.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyProvider.java new file mode 100644 index 000000000..420c77ee3 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/PolicyProvider.java @@ -0,0 +1,57 @@ +/*- + * ============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.main.handler; + +import java.io.Closeable; +import java.io.IOException; +import lombok.Getter; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryFactory; +import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters; + +/** + * Provider class for policy participant. + */ +public class PolicyProvider implements Closeable { + @Getter + private final ParticipantIntermediaryApi intermediaryApi; + + private final ControlLoopElementHandler controlLoopElementHandler; + + /** + * Create a policy participant provider. + * + * @throws ControlLoopRuntimeException on errors creating the provider + */ + public PolicyProvider(ParticipantIntermediaryParameters participantParameters) + throws ControlLoopRuntimeException { + intermediaryApi = new ParticipantIntermediaryFactory().createApiImplementation(); + intermediaryApi.init(participantParameters); + controlLoopElementHandler = new ControlLoopElementHandler(); + intermediaryApi.registerControlLoopElementListener(controlLoopElementHandler); + } + + @Override + public void close() throws IOException { + intermediaryApi.close(); + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameterHandler.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameterHandler.java new file mode 100644 index 000000000..98cea821a --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameterHandler.java @@ -0,0 +1,79 @@ +/*- + * ============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.main.parameters; + +import java.io.File; +import javax.ws.rs.core.Response; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; +import org.onap.policy.clamp.controlloop.participant.policy.main.startstop.ParticipantPolicyCommandLineArguments; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; + +/** + * This class handles reading, parsing and validating of policy participant parameters from JSON files. + */ +public class ParticipantPolicyParameterHandler { + + private static final Coder CODER = new StandardCoder(); + + /** + * Read the parameters from the parameter file. + * + * @param arguments the arguments passed to policy + * @return the parameters read from the configuration file + * @throws ControlLoopException on parameter exceptions + */ + public ParticipantPolicyParameters getParameters(final ParticipantPolicyCommandLineArguments arguments) + throws ControlLoopException { + ParticipantPolicyParameters parameters = null; + + // Read the parameters + try { + // Read the parameters from JSON + File file = new File(arguments.getFullConfigurationFilePath()); + parameters = CODER.decode(file, ParticipantPolicyParameters.class); + } catch (final CoderException e) { + final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath() + + "\"\n" + "(" + e.getClass().getSimpleName() + ")"; + throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, errorMessage, e); + } + + // The JSON processing returns null if there is an empty file + if (parameters == null) { + final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\""; + throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, errorMessage); + } + + // validate the parameters + final ValidationResult validationResult = parameters.validate(); + if (!validationResult.isValid()) { + String returnMessage = + "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n"; + returnMessage += validationResult.getResult(); + + throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, returnMessage); + } + + return parameters; + } +} 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 new file mode 100644 index 000000000..13d89faba --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParameters.java @@ -0,0 +1,49 @@ +/*- + * ============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.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.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. + * + */ +@NotNull +@NotBlank +@Getter +public class ParticipantPolicyParameters extends ParameterGroupImpl { + private ParticipantIntermediaryParameters intermediaryParameters; + private PolicyModelsProviderParameters databaseProviderParameters; + + /** + * Create the policy participant parameter group. + * + * @param name the parameter group name + */ + public ParticipantPolicyParameters(final String name) { + super(name); + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/Main.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/Main.java new file mode 100644 index 000000000..9a6bfdf7d --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/Main.java @@ -0,0 +1,141 @@ +/*- + * ============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.main.startstop; + +import java.util.Arrays; +import javax.ws.rs.core.Response; +import lombok.Getter; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameterHandler; +import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; +import org.onap.policy.common.utils.resources.MessageConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class initiates Policy Participant. + */ +public class Main { + + private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); + + private ParticipantPolicyActivator activator; + + @Getter + private ParticipantPolicyParameters parameterGroup; + + /** + * Instantiates policy participant. + * + * @param args the command line arguments + */ + public Main(final String[] args) { + final String argumentString = Arrays.toString(args); + LOGGER.info("Starting the control loop participant service with arguments - {}", argumentString); + + // Check the arguments + final ParticipantPolicyCommandLineArguments arguments = new ParticipantPolicyCommandLineArguments(); + try { + // The arguments return a string if there is a message to print and we should exit + final String argumentMessage = arguments.parse(args); + if (argumentMessage != null) { + LOGGER.info(argumentMessage); + return; + } + // Validate that the arguments are sane + arguments.validate(); + + // Read the parameters + parameterGroup = new ParticipantPolicyParameterHandler().getParameters(arguments); + + // Now, create the activator for the service + activator = new ParticipantPolicyActivator(parameterGroup); + + // Start the activator + activator.start(); + } catch (Exception exp) { + throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, + String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP), exp); + } + + // Add a shutdown hook to shut everything down in an orderly manner + Runtime.getRuntime().addShutdownHook(new ClRuntimeShutdownHookClass()); + String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_CLAMP); + LOGGER.info(successMsg); + } + + /** + * Check if main is running. + */ + public boolean isRunning() { + return activator != null && activator.isAlive(); + } + + /** + * Shut down Execution. + * + * @throws ControlLoopException on shutdown errors + */ + public void shutdown() throws ControlLoopException { + // clear the parameterGroup variable + parameterGroup = null; + + // clear the cl participant activator + if (activator != null) { + activator.stop(); + } + } + + /** + * The Class ClRuntimeShutdownHookClass terminates the policy participant + * when its run method is called. + */ + private class ClRuntimeShutdownHookClass extends Thread { + /* + * (non-Javadoc) + * + * @see java.lang.Runnable#run() + */ + @Override + public void run() { + try { + // Shutdown the control loop participant service and wait for everything to stop + shutdown(); + } catch (final RuntimeException | ControlLoopException e) { + LOGGER.warn("error occured during shut down of the policy participant", e); + } + } + } + + /** + * The main method. + * + * @param args the arguments + */ + public static void main(final String[] args) { // NOSONAR + /* + * NOTE: arguments are validated by the constructor, thus sonar is disabled. + */ + + new Main(args); + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyActivator.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyActivator.java new file mode 100644 index 000000000..760f8267d --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyActivator.java @@ -0,0 +1,57 @@ +/*- + * ============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.main.startstop; + +import java.util.concurrent.atomic.AtomicReference; +import lombok.Getter; +import org.onap.policy.clamp.controlloop.participant.policy.main.handler.PolicyHandler; +import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; +import org.onap.policy.common.utils.services.ServiceManagerContainer; + +/** + * This class activates the policy participant. + * + */ +public class ParticipantPolicyActivator extends ServiceManagerContainer { + @Getter + private final ParticipantPolicyParameters parameters; + + /** + * Instantiate the activator for the policy participant. + * + * @param parameters the parameters for the policy participant + */ + public ParticipantPolicyActivator(final ParticipantPolicyParameters parameters) { + this.parameters = parameters; + + final AtomicReference<PolicyHandler> policyHandler = new AtomicReference<>(); + + // @formatter:off + addAction("Policy Handler", + () -> policyHandler.set(new PolicyHandler(parameters)), + () -> policyHandler.get().close()); + + addAction("Policy Providers", + () -> policyHandler.get().startProviders(), + () -> policyHandler.get().stopProviders()); + // @formatter:on + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyCommandLineArguments.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyCommandLineArguments.java new file mode 100644 index 000000000..af7a189f6 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/startstop/ParticipantPolicyCommandLineArguments.java @@ -0,0 +1,145 @@ +/*- + * ============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.main.startstop; + +import java.util.Arrays; +import javax.ws.rs.core.Response; +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.clamp.controlloop.common.startstop.CommonCommandLineArguments; +import org.onap.policy.common.utils.resources.ResourceUtils; + +/** + * This class reads and handles command line parameters for the policy participant. + * + */ +public class ParticipantPolicyCommandLineArguments { + private static final String FILE_MESSAGE_PREAMBLE = " file \""; + private static final int HELP_LINE_LENGTH = 120; + + private final Options options; + private final CommonCommandLineArguments commonCommandLineArguments; + + @Getter() + @Setter() + private String configurationFilePath = null; + + /** + * Construct the options for the policy participant. + */ + public ParticipantPolicyCommandLineArguments() { + options = new Options(); + commonCommandLineArguments = new CommonCommandLineArguments(options); + } + + /** + * Construct the options for the CLI editor and parse in the given arguments. + * + * @param args The command line arguments + */ + public ParticipantPolicyCommandLineArguments(final String[] args) { + // Set up the options with the default constructor + this(); + + // Parse the arguments + try { + parse(args); + } catch (final ControlLoopException e) { + throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, + "parse error on policy participant parameters", e); + } + } + + /** + * Parse the command line options. + * + * @param args The command line arguments + * @return a string with a message for help and version, or null if there is no message + * @throws ControlLoopException on command argument errors + */ + public String parse(final String[] args) throws ControlLoopException { + // Clear all our arguments + setConfigurationFilePath(null); + CommandLine commandLine = null; + try { + commandLine = new DefaultParser().parse(options, args); + } catch (final ParseException e) { + throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, + "invalid command line arguments specified : " + e.getMessage()); + } + + // Arguments left over after Commons CLI does its stuff + final String[] remainingArgs = commandLine.getArgs(); + + if (remainingArgs.length > 0) { + throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, + "too many command line arguments specified : " + Arrays.toString(args)); + } + + if (commandLine.hasOption('h')) { + return commonCommandLineArguments.help(Main.class.getName(), options); + } + + if (commandLine.hasOption('v')) { + return commonCommandLineArguments.version(); + } + + if (commandLine.hasOption('c')) { + setConfigurationFilePath(commandLine.getOptionValue('c')); + } + + return null; + } + + /** + * Validate the command line options. + * + * @throws ControlLoopException on command argument validation errors + */ + public void validate() throws ControlLoopException { + commonCommandLineArguments.validate(configurationFilePath); + } + + /** + * Gets the full expanded configuration file path. + * + * @return the configuration file path + */ + public String getFullConfigurationFilePath() { + return ResourceUtils.getFilePath4Resource(getConfigurationFilePath()); + } + + /** + * Check set configuration file path. + * + * @return true, if check set configuration file path + */ + public boolean checkSetConfigurationFilePath() { + return !StringUtils.isEmpty(configurationFilePath); + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/resources/META-INF/persistence.xml b/participant/participant-impl/participant-impl-policy/src/main/resources/META-INF/persistence.xml new file mode 100644 index 000000000..46db712b6 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,79 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============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========================================================= +--> +<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> + <persistence-unit name="ToscaConceptTest" transaction-type="RESOURCE_LOCAL"> + <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> + + <class>org.onap.policy.models.base.PfConceptKey</class> + <class>org.onap.policy.models.dao.converters.CDataConditioner</class> + <class>org.onap.policy.models.dao.converters.Uuid2String</class> + <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class> + <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup</class> + <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics</class> + <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityAssignment</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityAssignments</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityType</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityTypes</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeType</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTypes</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaParameter</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRelationshipType</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRelationshipTypes</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRequirement</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRequirements</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate</class> + <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaTrigger</class> + <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop</class> + <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoopElement</class> + <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipant</class> + <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics</class> + <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics</class> + <properties> + <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> + <property name="eclipselink.ddl-generation.output-mode" value="database" /> + <property name="eclipselink.logging.level" value="INFO" /> + + <!-- property name="eclipselink.logging.level" value="ALL" /> + <property name="eclipselink.logging.level.jpa" value="ALL" /> + <property name="eclipselink.logging.level.ddl" value="ALL" /> + <property name="eclipselink.logging.level.connection" value="ALL" /> + <property name="eclipselink.logging.level.sql" value="ALL" /> + <property name="eclipselink.logging.level.transaction" value="ALL" /> + <property name="eclipselink.logging.level.sequencing" value="ALL" /> + <property name="eclipselink.logging.level.server" value="ALL" /> + <property name="eclipselink.logging.level.query" value="ALL" /> + <property name="eclipselink.logging.level.properties" value="ALL" /--> + </properties> + <shared-cache-mode>NONE</shared-cache-mode> + </persistence-unit> +</persistence> + 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 new file mode 100644 index 000000000..e6b3c8eb1 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/resources/config/PolicyParticipantConfig.json @@ -0,0 +1,31 @@ +{ + "name":"ParticipantParameterGroup", + "participantStatusParameters":{ + "timeIntervalMs":10000, + "description":"Participant Status", + "participantId":{ + "name": "PolicyParticipant0", + "version":"1.0.0" + }, + "participantType":{ + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version":"2.3.1" + }, + "participantDefinition":{ + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version":"2.3.1" + } + }, + "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" + }] + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/main/resources/version.txt b/participant/participant-impl/participant-impl-policy/src/main/resources/version.txt new file mode 100644 index 000000000..dbd67585f --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/main/resources/version.txt @@ -0,0 +1,4 @@ +ONAP Tosca defined control loop Participant +Version: ${project.version} +Built (UTC): ${maven.build.timestamp} +ONAP https://wiki.onap.org |