2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.clamp.acm.participant.intermediary.main.parameters;
 
  24 import java.time.Instant;
 
  25 import java.util.ArrayList;
 
  26 import java.util.LinkedHashMap;
 
  27 import java.util.List;
 
  29 import java.util.TreeMap;
 
  30 import java.util.UUID;
 
  31 import org.onap.policy.clamp.acm.participant.intermediary.handler.DummyParticipantParameters;
 
  32 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantIntermediaryParameters;
 
  33 import org.onap.policy.clamp.acm.participant.intermediary.parameters.Topics;
 
  34 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
 
  35 import org.onap.policy.clamp.models.acm.concepts.AcElementRestart;
 
  36 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
 
  37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
 
  38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
 
  39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
 
  40 import org.onap.policy.clamp.models.acm.concepts.LockState;
 
  41 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
 
  42 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
 
  43 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
 
  44 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange;
 
  45 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
 
  46 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
 
  47 import org.onap.policy.common.parameters.topic.TopicParameters;
 
  48 import org.onap.policy.common.utils.coder.Coder;
 
  49 import org.onap.policy.common.utils.coder.CoderException;
 
  50 import org.onap.policy.common.utils.coder.StandardCoder;
 
  51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
  52 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
 
  55  * Class to hold/create all parameters for test cases.
 
  57 public class CommonTestData {
 
  58     public static final String PARTICIPANT_GROUP_NAME = "AutomationCompositionParticipantGroup";
 
  59     public static final String DESCRIPTION = "Participant description";
 
  60     public static final long TIME_INTERVAL = 2000;
 
  61     public static final List<TopicParameters> TOPIC_PARAMS = List.of(getTopicParams());
 
  62     public static final List<TopicParameters> TOPIC_SOURCE_PARAMS = List.of(getTopicParams(), getSyncTopicParams());
 
  63     public static final Coder CODER = new StandardCoder();
 
  64     public static final UUID AC_ID_0 = UUID.randomUUID();
 
  65     public static final UUID AC_ID_1 = UUID.randomUUID();
 
  66     public static final UUID PARTCICIPANT_ID = UUID.randomUUID();
 
  67     public static final UUID REPLICA_ID = UUID.randomUUID();
 
  70      * Get ParticipantIntermediaryParameters.
 
  72      * @return ParticipantIntermediaryParameters
 
  74     public static ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
 
  76             return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
 
  77                     ParticipantIntermediaryParameters.class);
 
  78         } catch (final CoderException e) {
 
  79             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
 
  84      * Get ParticipantParameters.
 
  86      * @return ParticipantParameters
 
  88     public static DummyParticipantParameters getParticipantParameters() {
 
  90             return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
 
  91         } catch (final CoderException e) {
 
  92             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
 
  97      * Returns a property map for a Parameters map for test cases.
 
  99      * @param name name of the parameters
 
 100      * @return a property map suitable for constructing an object
 
 102     public static Map<String, Object> getParametersMap(final String name) {
 
 103         final Map<String, Object> map = new TreeMap<>();
 
 104         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
 
 109      * Returns a property map for a intermediaryParameters map for test cases.
 
 111      * @param name name of the parameters
 
 112      * @return a property map suitable for constructing an object
 
 114     public static Map<String, Object> getIntermediaryParametersMap(final String name) {
 
 115         final Map<String, Object> map = new TreeMap<>();
 
 116         map.put("name", name);
 
 117         map.put("participantId", getParticipantId());
 
 118         map.put("description", DESCRIPTION);
 
 119         map.put("reportingTimeIntervalMs", TIME_INTERVAL);
 
 120         map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
 
 121         map.put("topics", getTopics());
 
 122         var supportedElementType = new ParticipantSupportedElementType();
 
 123         supportedElementType.setTypeName("org.onap.policy.clamp.acm.HttpAutomationCompositionElement");
 
 124         supportedElementType.setTypeVersion("1.0.0");
 
 125         map.put("participantSupportedElementTypes", List.of(supportedElementType));
 
 131      * Returns a property map for a TopicParameters map for test cases.
 
 133      * @param isEmpty boolean value to represent that object created should be empty or not
 
 134      * @return a property map suitable for constructing an object
 
 136     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
 
 137         final Map<String, Object> map = new TreeMap<>();
 
 139             map.put("topicSources", TOPIC_SOURCE_PARAMS);
 
 140             map.put("topicSinks", TOPIC_PARAMS);
 
 146      * Returns topic parameters for test cases.
 
 148      * @return topic parameters
 
 150     public static TopicParameters getTopicParams() {
 
 151         final var topicParams = new TopicParameters();
 
 152         topicParams.setTopic("policy-acruntime-participant");
 
 153         topicParams.setTopicCommInfrastructure("NOOP");
 
 154         topicParams.setServers(List.of("localhost"));
 
 159      * Returns topic parameters for sync topic.
 
 160      * @return topicparamaters
 
 162     public static TopicParameters getSyncTopicParams() {
 
 163         final var topicParams = new TopicParameters();
 
 164         topicParams.setTopic("acm-ppnt-sync");
 
 165         topicParams.setTopicCommInfrastructure("NOOP");
 
 166         topicParams.setServers(List.of("localhost"));
 
 170     private static Topics getTopics() {
 
 171         return new Topics("policy-acruntime-participant", "acm-ppnt-sync");
 
 175      * Returns participantId for test cases.
 
 177      * @return participant Id
 
 179     public static UUID getParticipantId() {
 
 180         return PARTCICIPANT_ID;
 
 183     public static UUID getReplicaId() {
 
 187     public static ToscaConceptIdentifier getDefinition() {
 
 188         return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
 
 192      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
 
 194      * @return automationCompositionMap
 
 196     public static Map<UUID, AutomationComposition> getTestAutomationCompositionMap() {
 
 197         var automationCompositions = getTestAutomationCompositions();
 
 198         var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
 
 199         Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
 
 200         automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
 
 201         return automationCompositionMap;
 
 205      * Returns List of AutomationComposition for test cases.
 
 207      * @return AutomationCompositions
 
 209     public static AutomationCompositions getTestAutomationCompositions() {
 
 211             var automationCompositions =
 
 212                     new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
 
 213                             AutomationCompositions.class);
 
 214             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
 
 215             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
 
 216             return automationCompositions;
 
 217         } catch (Exception e) {
 
 218             throw new RuntimeException("cannot read TestAutomationCompositions.json");
 
 223      * Return a AutomationCompositionStateChange.
 
 225      * @param participantId the participantId
 
 226      * @param instanceId the AutomationComposition Id
 
 227      * @param deployOrder a DeployOrder
 
 228      * @param lockOrder a LockOrder
 
 229      * @return a AutomationCompositionStateChange
 
 231     public static AutomationCompositionStateChange getStateChange(UUID participantId, UUID instanceId,
 
 232             DeployOrder deployOrder, LockOrder lockOrder) {
 
 233         var stateChange = new AutomationCompositionStateChange();
 
 234         stateChange.setStartPhase(0);
 
 235         stateChange.setAutomationCompositionId(instanceId);
 
 236         stateChange.setParticipantId(participantId);
 
 237         stateChange.setMessageId(UUID.randomUUID());
 
 238         stateChange.setDeployOrderedState(deployOrder);
 
 239         stateChange.setLockOrderedState(lockOrder);
 
 240         stateChange.setTimestamp(Instant.ofEpochMilli(3000));
 
 245      * Create a ParticipantRestartAc.
 
 247      * @return a ParticipantRestartAc
 
 249     public static ParticipantRestartAc createParticipantRestartAc() {
 
 250         var participantRestartAc = new ParticipantRestartAc();
 
 251         participantRestartAc.setAutomationCompositionId(AC_ID_0);
 
 252         participantRestartAc.setDeployState(DeployState.DEPLOYED);
 
 253         participantRestartAc.setLockState(LockState.LOCKED);
 
 254         var acElementRestart = new AcElementRestart();
 
 255         acElementRestart.setDefinition(getDefinition());
 
 256         acElementRestart.setParticipantId(PARTCICIPANT_ID);
 
 257         acElementRestart.setDeployState(DeployState.DEPLOYED);
 
 258         acElementRestart.setLockState(LockState.LOCKED);
 
 259         acElementRestart.setOperationalState("OperationalState");
 
 260         acElementRestart.setUseState("UseState");
 
 261         acElementRestart.setProperties(Map.of("key", "value"));
 
 262         acElementRestart.setOutProperties(Map.of("keyOut", "valueOut"));
 
 263         acElementRestart.setId(UUID.randomUUID());
 
 264         participantRestartAc.getAcElementList().add(acElementRestart);
 
 265         return participantRestartAc;
 
 269      * Create a ParticipantDeploy from an AutomationComposition.
 
 271      * @param participantId the participantId
 
 272      * @param automationComposition the AutomationComposition
 
 273      * @return the ParticipantDeploy
 
 275     public static ParticipantDeploy createparticipantDeploy(UUID participantId,
 
 276             AutomationComposition automationComposition) {
 
 277         var participantDeploy = new ParticipantDeploy();
 
 278         participantDeploy.setParticipantId(participantId);
 
 279         for (var element : automationComposition.getElements().values()) {
 
 280             var acElement = new AcElementDeploy();
 
 281             acElement.setId(element.getId());
 
 282             acElement.setDefinition(element.getDefinition());
 
 283             acElement.setProperties(element.getProperties());
 
 284             acElement.setMigrationState(element.getMigrationState());
 
 285             participantDeploy.getAcElementList().add(acElement);
 
 287         return participantDeploy;
 
 291      * create a List of AutomationCompositionElementDefinition from an AutomationComposition.
 
 293      * @param automationComposition the AutomationComposition
 
 294      * @return the List of AutomationCompositionElementDefinition
 
 296     public static List<AutomationCompositionElementDefinition>
 
 297             createAutomationCompositionElementDefinitionList(AutomationComposition automationComposition) {
 
 298         List<AutomationCompositionElementDefinition> definitions = new ArrayList<>();
 
 299         for (var element : automationComposition.getElements().values()) {
 
 300             definitions.add(createAutomationCompositionElementDefinition(element.getDefinition()));
 
 306      * create a new example of AutomationCompositionElementDefinition.
 
 308      * @param definition the composition definition element id
 
 309      * @return the AutomationCompositionElementDefinition
 
 311     public static AutomationCompositionElementDefinition createAutomationCompositionElementDefinition(
 
 312             ToscaConceptIdentifier definition) {
 
 313         var acElementDefinition = new AutomationCompositionElementDefinition();
 
 314         acElementDefinition.setAcElementDefinitionId(definition);
 
 315         var nodeTemplate = new ToscaNodeTemplate();
 
 316         nodeTemplate.setProperties(Map.of("key", "value"));
 
 317         acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(nodeTemplate);
 
 318         return acElementDefinition;