Move code to common, avoid duplication 93/125393/2
authorSirisha_Manchikanti <sirisha.manchikanti@est.tech>
Wed, 27 Oct 2021 07:24:48 +0000 (08:24 +0100)
committerSirisha_Manchikanti <sirisha.manchikanti@est.tech>
Wed, 27 Oct 2021 11:48:30 +0000 (12:48 +0100)
This commit moves duplicate code from runtime and participant
to common

Issue-ID: POLICY-3542
Signed-off-by: Sirisha_Manchikanti <sirisha.manchikanti@est.tech>
Change-Id: Ia7cea1d2c88031c72af77338e242f8981e536c21

common/pom.xml
common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java [new file with mode: 0644]
participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java
participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java
runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java
runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java

index 75239ec..c60ed6c 100644 (file)
     <name>${project.artifactId}</name>
     <description>Common utilities and code for the TOSCA Control Loop system</description>
 
+    <dependencies>
+        <dependency>
+            <groupId>org.onap.policy.clamp</groupId>
+            <artifactId>policy-clamp-models</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
     <build>
         <plugins>
             <!-- Builds examples jar -->
diff --git a/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java b/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java
new file mode 100644 (file)
index 0000000..4b0e41f
--- /dev/null
@@ -0,0 +1,150 @@
+/*-
+ * ============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.common.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
+
+/**
+ * Utility functions used in controlloop-runtime and participants.
+ *
+ */
+public class CommonUtils {
+    private static final String POLICY_TYPE_ID = "policy_type_id";
+    private static final String POLICY_ID = "policy_id";
+
+    /**
+     * Prepare participant updates map.
+     *
+     * @param clElement controlloop element
+     * @param participantUpdates list of participantUpdates
+     */
+    public static void prepareParticipantUpdate(ControlLoopElement clElement,
+        List<ParticipantUpdates> participantUpdates) {
+        if (participantUpdates.isEmpty()) {
+            participantUpdates.add(getControlLoopElementList(clElement));
+            return;
+        }
+
+        var participantExists = false;
+        for (ParticipantUpdates participantUpdate : participantUpdates) {
+            if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
+                participantUpdate.setControlLoopElementList(List.of(clElement));
+                participantExists = true;
+            }
+        }
+        if (!participantExists) {
+            participantUpdates.add(getControlLoopElementList(clElement));
+        }
+    }
+
+    private static ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) {
+        var participantUpdate = new ParticipantUpdates();
+        participantUpdate.setParticipantId(clElement.getParticipantId());
+        participantUpdate.setControlLoopElementList(List.of(clElement));
+        return participantUpdate;
+    }
+
+    /**
+     * Set the Policy information in the service template for the controlloopelement.
+     *
+     * @param clElement controlloop element
+     * @param toscaServiceTemplate ToscaServiceTemplate
+     */
+    public static void setServiceTemplatePolicyInfo(ControlLoopElement clElement,
+            ToscaServiceTemplate toscaServiceTemplate) {
+        // Pass respective PolicyTypes or Policies as part of toscaServiceTemplateFragment
+        if (toscaServiceTemplate.getPolicyTypes() == null
+            && toscaServiceTemplate.getToscaTopologyTemplate().getPolicies() == null) {
+            return;
+        }
+        ToscaServiceTemplate toscaServiceTemplateFragment = new ToscaServiceTemplate();
+        toscaServiceTemplateFragment.setPolicyTypes(toscaServiceTemplate.getPolicyTypes());
+        ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
+        toscaTopologyTemplate.setPolicies(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies());
+        toscaServiceTemplateFragment.setToscaTopologyTemplate(toscaTopologyTemplate);
+        toscaServiceTemplateFragment.setDataTypes(toscaServiceTemplate.getDataTypes());
+        clElement.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
+    }
+
+    /**
+     * Prepare ParticipantDefinitionUpdate to set in the message.
+     *
+     * @param clParticipantType controlloop element
+     * @param entryKey key for the entry
+     * @param entryValue value relates to toscaNodeTemplate
+     * @param participantDefinitionUpdates list of participantDefinitionUpdates
+     * @param commonPropertiesMap common properties map
+     */
+    public static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
+            ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates,
+            Map<String, ToscaNodeType> commonPropertiesMap) {
+
+        var clDefinition = new ControlLoopElementDefinition();
+        clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
+        clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
+        if (commonPropertiesMap != null) {
+            ToscaNodeType nodeType = commonPropertiesMap.get(entryValue.getType());
+            if (nodeType != null) {
+                clDefinition.setCommonPropertiesMap(nodeType.getProperties());
+            }
+        }
+
+        List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
+
+        if (participantDefinitionUpdates.isEmpty()) {
+            participantDefinitionUpdates
+                    .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
+        } else {
+            var participantExists = false;
+            for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
+                if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
+                    participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
+                    participantExists = true;
+                }
+            }
+            if (!participantExists) {
+                participantDefinitionUpdates.add(
+                        getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
+            }
+        }
+    }
+
+    private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
+            ToscaConceptIdentifier clParticipantType,
+            List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
+        var participantDefinition = new ParticipantDefinition();
+        participantDefinition.setParticipantType(clParticipantType);
+        controlLoopElementDefinitionList.add(clDefinition);
+        participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
+        return participantDefinition;
+    }
+}
index 25da5a3..d517ef6 100644 (file)
@@ -33,9 +33,9 @@ import java.util.Set;
 import java.util.UUID;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.onap.policy.clamp.controlloop.common.utils.CommonUtils;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
-import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
 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.models.controlloop.concepts.ParticipantDefinition;
@@ -53,7 +53,6 @@ import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,8 +63,6 @@ public final class TestListenerUtils {
     private static final Coder CODER = new StandardCoder();
     static CommonTestData commonTestData = new CommonTestData();
     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
-    private static final String POLICY_TYPE_ID = "policy_type_id";
-    private static final String POLICY_ID = "policy_id";
 
     /**
      * Method to create a controlLoop from a yaml file.
@@ -174,65 +171,13 @@ public final class TestListenerUtils {
 
         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
         for (ControlLoopElement element : elements.values()) {
-            populateToscaNodeTemplateFragment(element, toscaServiceTemplate);
-            prepareParticipantUpdateForControlLoop(element, participantUpdates);
+            CommonUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate);
+            CommonUtils.prepareParticipantUpdate(element, participantUpdates);
         }
         clUpdateMsg.setParticipantUpdatesList(participantUpdates);
         return clUpdateMsg;
     }
 
-    private static void populateToscaNodeTemplateFragment(ControlLoopElement clElement,
-            ToscaServiceTemplate toscaServiceTemplate) {
-        ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates()
-                .get(clElement.getDefinition().getName());
-        // If the ControlLoopElement has policy_type_id or policy_id, identify it as a PolicyControlLoopElement
-        // and pass respective PolicyTypes or Policies as part of toscaServiceTemplateFragment
-        if ((toscaNodeTemplate.getProperties().get(POLICY_TYPE_ID) != null)
-                || (toscaNodeTemplate.getProperties().get(POLICY_ID) != null)) {
-            // ControlLoopElement for policy framework, send policies and policyTypes to participants
-            if ((toscaServiceTemplate.getPolicyTypes() != null)
-                    || (toscaServiceTemplate.getToscaTopologyTemplate().getPolicies() != null)) {
-                ToscaServiceTemplate toscaServiceTemplateFragment = new ToscaServiceTemplate();
-                toscaServiceTemplateFragment.setPolicyTypes(toscaServiceTemplate.getPolicyTypes());
-
-                ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
-                toscaTopologyTemplate.setPolicies(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies());
-                toscaServiceTemplateFragment.setToscaTopologyTemplate(toscaTopologyTemplate);
-
-                toscaServiceTemplateFragment.setDataTypes(toscaServiceTemplate.getDataTypes());
-
-                clElement.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
-            }
-        }
-    }
-
-    private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement,
-            List<ParticipantUpdates> participantUpdates) {
-        if (participantUpdates.isEmpty()) {
-            participantUpdates.add(getControlLoopElementList(clElement));
-        } else {
-            boolean participantExists = false;
-            for (ParticipantUpdates participantUpdate : participantUpdates) {
-                if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
-                    participantUpdate.getControlLoopElementList().add(clElement);
-                    participantExists = true;
-                }
-            }
-            if (!participantExists) {
-                participantUpdates.add(getControlLoopElementList(clElement));
-            }
-        }
-    }
-
-    private static ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) {
-        ParticipantUpdates participantUpdate = new ParticipantUpdates();
-        List<ControlLoopElement> controlLoopElementList = new ArrayList<>();
-        participantUpdate.setParticipantId(clElement.getParticipantId());
-        controlLoopElementList.add(clElement);
-        participantUpdate.setControlLoopElementList(controlLoopElementList);
-        return participantUpdate;
-    }
-
     /**
      * Method to create participantUpdateMsg.
      *
@@ -259,10 +204,10 @@ public final class TestListenerUtils {
                 .getNodeTemplates().entrySet()) {
             if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
                     toscaServiceTemplate)) {
-                var clParticipantType =
-                        ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
-                prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
-                        toscaInputEntry.getValue(), participantDefinitionUpdates);
+                CommonUtils.prepareParticipantDefinitionUpdate(
+                        ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()),
+                        toscaInputEntry.getKey(), toscaInputEntry.getValue(),
+                        participantDefinitionUpdates, null);
             }
         }
 
@@ -270,42 +215,6 @@ public final class TestListenerUtils {
         return participantUpdateMsg;
     }
 
-    private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
-            ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
-
-        var clDefinition = new ControlLoopElementDefinition();
-        clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
-        clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
-        List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
-
-        if (participantDefinitionUpdates.isEmpty()) {
-            participantDefinitionUpdates
-                    .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
-        } else {
-            boolean participantExists = false;
-            for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
-                if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
-                    participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
-                    participantExists = true;
-                }
-            }
-            if (!participantExists) {
-                participantDefinitionUpdates.add(
-                        getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
-            }
-        }
-    }
-
-    private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
-            ToscaConceptIdentifier clParticipantType,
-            List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
-        ParticipantDefinition participantDefinition = new ParticipantDefinition();
-        participantDefinition.setParticipantType(clParticipantType);
-        controlLoopElementDefinitionList.add(clDefinition);
-        participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
-        return participantDefinition;
-    }
-
     /**
      * Method to create ControlLoopUpdate using the arguments passed.
      *
index bd3316a..c984824 100644 (file)
@@ -33,9 +33,9 @@ import java.util.Set;
 import java.util.UUID;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.onap.policy.clamp.controlloop.common.utils.CommonUtils;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
-import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
 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.models.controlloop.concepts.ParticipantDefinition;
@@ -166,39 +166,12 @@ public final class TestListenerUtils {
 
         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
         for (ControlLoopElement element : elements.values()) {
-            prepareParticipantUpdateForControlLoop(element, participantUpdates);
+            CommonUtils.prepareParticipantUpdate(element, participantUpdates);
         }
         clUpdateMsg.setParticipantUpdatesList(participantUpdates);
         return clUpdateMsg;
     }
 
-    private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement,
-            List<ParticipantUpdates> participantUpdates) {
-        if (participantUpdates.isEmpty()) {
-            participantUpdates.add(getControlLoopElementList(clElement));
-        } else {
-            boolean participantExists = false;
-            for (ParticipantUpdates participantUpdate : participantUpdates) {
-                if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
-                    participantUpdate.getControlLoopElementList().add(clElement);
-                    participantExists = true;
-                }
-            }
-            if (!participantExists) {
-                participantUpdates.add(getControlLoopElementList(clElement));
-            }
-        }
-    }
-
-    private static ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) {
-        ParticipantUpdates participantUpdate = new ParticipantUpdates();
-        List<ControlLoopElement> controlLoopElementList = new ArrayList<>();
-        participantUpdate.setParticipantId(clElement.getParticipantId());
-        controlLoopElementList.add(clElement);
-        participantUpdate.setControlLoopElementList(controlLoopElementList);
-        return participantUpdate;
-    }
-
     /**
      * Method to create participantUpdateMsg.
      *
@@ -226,8 +199,8 @@ public final class TestListenerUtils {
                     toscaServiceTemplate)) {
                 var clParticipantType =
                         ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
-                prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
-                        toscaInputEntry.getValue(), participantDefinitionUpdates);
+                CommonUtils.prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
+                        toscaInputEntry.getValue(), participantDefinitionUpdates, null);
             }
         }
 
@@ -235,42 +208,6 @@ public final class TestListenerUtils {
         return participantUpdateMsg;
     }
 
-    private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
-            ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
-
-        var clDefinition = new ControlLoopElementDefinition();
-        clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
-        clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
-        List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
-
-        if (participantDefinitionUpdates.isEmpty()) {
-            participantDefinitionUpdates
-                    .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
-        } else {
-            boolean participantExists = false;
-            for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
-                if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
-                    participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
-                    participantExists = true;
-                }
-            }
-            if (!participantExists) {
-                participantDefinitionUpdates.add(
-                        getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
-            }
-        }
-    }
-
-    private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
-            ToscaConceptIdentifier clParticipantType,
-            List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
-        ParticipantDefinition participantDefinition = new ParticipantDefinition();
-        participantDefinition.setParticipantType(clParticipantType);
-        controlLoopElementDefinitionList.add(clDefinition);
-        participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
-        return participantDefinition;
-    }
-
     /**
      * Method to create ControlLoopUpdate using the arguments passed.
      *
index 74d9872..06fbcd6 100644 (file)
@@ -27,15 +27,14 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 import lombok.AllArgsConstructor;
+import org.onap.policy.clamp.controlloop.common.utils.CommonUtils;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.provider.PolicyModelsProvider;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
@@ -48,8 +47,6 @@ import org.springframework.stereotype.Component;
 public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<ControlLoopUpdate> {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopUpdatePublisher.class);
-    private static final String POLICY_TYPE_ID = "policy_type_id";
-    private static final String POLICY_ID = "policy_id";
     private final PolicyModelsProvider modelsProvider;
 
     /**
@@ -83,59 +80,12 @@ public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<Con
 
         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
         for (ControlLoopElement element : controlLoop.getElements().values()) {
-            ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate
-                .getToscaTopologyTemplate().getNodeTemplates().get(element.getDefinition().getName());
-            // If the ControlLoopElement has policy_type_id or policy_id, identify it as a PolicyControlLoopElement
-            // and pass respective PolicyTypes or Policies as part of toscaServiceTemplateFragment
-            if ((toscaNodeTemplate.getProperties().get(POLICY_TYPE_ID) != null)
-                    || (toscaNodeTemplate.getProperties().get(POLICY_ID) != null)) {
-                // ControlLoopElement for policy framework, send policies and policyTypes to participants
-                if ((toscaServiceTemplate.getPolicyTypes() != null)
-                        || (toscaServiceTemplate.getToscaTopologyTemplate().getPolicies() != null)) {
-                    ToscaServiceTemplate toscaServiceTemplateFragment = new ToscaServiceTemplate();
-                    toscaServiceTemplateFragment.setPolicyTypes(toscaServiceTemplate.getPolicyTypes());
-
-                    ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
-                    toscaTopologyTemplate.setPolicies(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies());
-                    toscaServiceTemplateFragment.setToscaTopologyTemplate(toscaTopologyTemplate);
-
-                    toscaServiceTemplateFragment.setDataTypes(toscaServiceTemplate.getDataTypes());
-
-                    element.setToscaServiceTemplateFragment(toscaServiceTemplateFragment);
-                }
-            }
-            prepareParticipantUpdate(element, participantUpdates);
+            CommonUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate);
+            CommonUtils.prepareParticipantUpdate(element, participantUpdates);
         }
         controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates);
 
         LOGGER.debug("ControlLoopUpdate message sent {}", controlLoopUpdateMsg);
         super.send(controlLoopUpdateMsg);
     }
-
-    private void prepareParticipantUpdate(ControlLoopElement clElement,
-        List<ParticipantUpdates> participantUpdates) {
-        if (participantUpdates.isEmpty()) {
-            participantUpdates.add(getControlLoopElementList(clElement));
-        } else {
-            var participantExists = false;
-            for (ParticipantUpdates participantUpdate : participantUpdates) {
-                if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
-                    participantUpdate.getControlLoopElementList().add(clElement);
-                    participantExists = true;
-                }
-            }
-            if (!participantExists) {
-                participantUpdates.add(getControlLoopElementList(clElement));
-            }
-        }
-    }
-
-    private ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) {
-        var participantUpdate = new ParticipantUpdates();
-        List<ControlLoopElement> controlLoopElementList = new ArrayList<>();
-        participantUpdate.setParticipantId(clElement.getParticipantId());
-        controlLoopElementList.add(clElement);
-        participantUpdate.setControlLoopElementList(controlLoopElementList);
-        return participantUpdate;
-    }
 }
index 5d879dc..c6788f1 100644 (file)
@@ -27,14 +27,13 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import lombok.AllArgsConstructor;
-import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
+import org.onap.policy.clamp.controlloop.common.utils.CommonUtils;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils;
 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider;
 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 import org.slf4j.Logger;
@@ -99,10 +98,10 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par
         for (var toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
             if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
                     toscaServiceTemplate)) {
-                var clParticipantType =
-                        ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
-                prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
-                        toscaInputEntry.getValue(), participantDefinitionUpdates, commonPropertiesMap);
+                CommonUtils.prepareParticipantDefinitionUpdate(
+                    ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()),
+                    toscaInputEntry.getKey(), toscaInputEntry.getValue(),
+                    participantDefinitionUpdates, commonPropertiesMap);
             }
         }
 
@@ -113,48 +112,6 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par
         return true;
     }
 
-    private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
-            ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates,
-            Map<String, ToscaNodeType> commonPropertiesMap) {
-
-        var clDefinition = new ControlLoopElementDefinition();
-        clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
-        clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
-        ToscaNodeType nodeType = commonPropertiesMap.get(entryValue.getType());
-        if (nodeType != null) {
-            clDefinition.setCommonPropertiesMap(nodeType.getProperties());
-        }
-
-        List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
-
-        if (participantDefinitionUpdates.isEmpty()) {
-            participantDefinitionUpdates
-                    .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
-        } else {
-            var participantExists = false;
-            for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
-                if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
-                    participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
-                    participantExists = true;
-                }
-            }
-            if (!participantExists) {
-                participantDefinitionUpdates.add(
-                        getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
-            }
-        }
-    }
-
-    private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
-            ToscaConceptIdentifier clParticipantType,
-            List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
-        var participantDefinition = new ParticipantDefinition();
-        participantDefinition.setParticipantType(clParticipantType);
-        controlLoopElementDefinitionList.add(clDefinition);
-        participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
-        return participantDefinition;
-    }
-
     /**
      * Send ParticipantUpdate to Participant after that commissioning has been removed.
      */