Send ToscaServiceTemplateFragment with policies, policy-types 58/123758/2
authorSirisha_Manchikanti <sirisha.manchikanti@est.tech>
Tue, 31 Aug 2021 10:25:34 +0000 (11:25 +0100)
committerSirisha_Manchikanti <sirisha.manchikanti@est.tech>
Wed, 1 Sep 2021 15:48:18 +0000 (16:48 +0100)
ToscaServiceTemplateFragment contains policies, policy-types and
respective datatypes to be sent to Policy participant, and there on to
Policy Framework to create new policies or policy-types that doesnot
exist in the database.
https://wiki.onap.org/display/DW/The+CLAMP+Policy+Framework+Participant

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

16 files changed:
models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/concepts/ControlLoopElement.java
models/src/main/java/org/onap/policy/clamp/controlloop/models/messages/dmaap/participant/ParticipantUpdate.java
participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/rest/TestListenerUtils.java
participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java
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/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlConfiguration.java
participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlHttpMessageConverter.java
participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/endtoend/ParticipantSimulatorTest.java
participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java
participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/ParticipantIntermediaryApi.java
participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/impl/ParticipantIntermediaryApiImpl.java
participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.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
runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java
runtime-controlloop/src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml

index b99759e..67bcb53 100644 (file)
@@ -31,6 +31,7 @@ import lombok.ToString;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfUtils;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
 /**
  * Class to represent a control loop instance.
@@ -57,6 +58,8 @@ public class ControlLoopElement {
     @NonNull
     private ControlLoopOrderedState orderedState = ControlLoopOrderedState.UNINITIALISED;
 
+    private ToscaServiceTemplate toscaServiceTemplateFragment;
+
     private String description;
 
     private ClElementStatistics clElementStatistics;
@@ -77,6 +80,7 @@ public class ControlLoopElement {
         this.participantId = new ToscaConceptIdentifier(otherElement.participantId);
         this.state = otherElement.state;
         this.orderedState = otherElement.orderedState;
+        this.toscaServiceTemplateFragment = otherElement.toscaServiceTemplateFragment;
         this.description = otherElement.description;
         this.clElementStatistics = otherElement.clElementStatistics;
         this.commonPropertiesMap = PfUtils.mapMap(otherElement.commonPropertiesMap, UnaryOperator.identity());
index 5c1b679..51cce53 100644 (file)
@@ -41,8 +41,6 @@ public class ParticipantUpdate extends ParticipantMessage {
     // A list of updates to ParticipantDefinitions
     private List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
 
-    private ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
-
     /**
      * Constructor for instantiating ParticipantUpdate class with message name.
      *
@@ -61,6 +59,5 @@ public class ParticipantUpdate extends ParticipantMessage {
 
         this.participantDefinitionUpdates = PfUtils.mapList(source.participantDefinitionUpdates,
             ParticipantDefinition::new);
-        this.toscaServiceTemplate = source.toscaServiceTemplate;
     }
 }
index b17ae43..4f8dcf1 100644 (file)
@@ -130,7 +130,7 @@ public class ControlLoopElementHandler implements ControlLoopElementListener {
             throws PfModelException {
         intermediaryApi.updateControlLoopElementState(element.getId(), element.getOrderedState(),
                 ControlLoopState.PASSIVE, ParticipantMessageType.CONTROL_LOOP_UPDATE);
-        ToscaServiceTemplate controlLoopDefinition = intermediaryApi.getToscaServiceTemplate();
+        ToscaServiceTemplate controlLoopDefinition = element.getToscaServiceTemplateFragment();
         if (controlLoopDefinition.getToscaTopologyTemplate() != null) {
             if (controlLoopDefinition.getPolicyTypes() != null) {
                 for (ToscaPolicyType policyType : controlLoopDefinition.getPolicyTypes().values()) {
index 4c87e08..52ec98e 100644 (file)
@@ -50,6 +50,7 @@ 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;
 
@@ -60,6 +61,8 @@ public class TestListenerUtils {
     static CommonTestData commonTestData = new CommonTestData();
     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
     private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
+    private static final String POLICY_TYPE_ID = "policy_type_id";
+    private static final String POLICY_ID = "policy_id";
 
     private TestListenerUtils() {}
 
@@ -147,6 +150,7 @@ public class TestListenerUtils {
 
         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
+        TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
@@ -176,12 +180,38 @@ public class TestListenerUtils {
 
         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
         for (ControlLoopElement element : elements.values()) {
+            populateToscaNodeTemplateFragment(element, toscaServiceTemplate);
             prepareParticipantUpdateForControlLoop(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()) {
@@ -248,7 +278,6 @@ public class TestListenerUtils {
         }
 
         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
-        participantUpdateMsg.setToscaServiceTemplate(toscaServiceTemplate);
         return participantUpdateMsg;
     }
 
index e84a7fc..16da5cf 100644 (file)
@@ -22,7 +22,9 @@ package org.onap.policy.clamp.controlloop.participant.simulator.config;
 
 import java.util.List;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.http.MediaType;
 import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.http.converter.StringHttpMessageConverter;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
 @Configuration
@@ -30,6 +32,11 @@ public class YamlConfiguration implements WebMvcConfigurer {
 
     @Override
     public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
-        converters.add(new YamlHttpMessageConverter<>());
+        converters.add(new YamlHttpMessageConverter<>("yaml"));
+        converters.add(new YamlHttpMessageConverter<>("json"));
+
+        StringHttpMessageConverter converter = new StringHttpMessageConverter();
+        converter.setSupportedMediaTypes(List.of(MediaType.TEXT_PLAIN));
+        converters.add(converter);
     }
 }
index 3e10598..d9a72ce 100644 (file)
 package org.onap.policy.clamp.controlloop.participant.simulator.config;
 
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.nio.charset.StandardCharsets;
+import javax.ws.rs.core.Response;
+import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
 import org.springframework.http.HttpInputMessage;
 import org.springframework.http.HttpOutputMessage;
 import org.springframework.http.MediaType;
 import org.springframework.http.converter.AbstractHttpMessageConverter;
 import org.springframework.http.converter.HttpMessageNotReadableException;
 import org.springframework.http.converter.HttpMessageNotWritableException;
-import org.yaml.snakeyaml.Yaml;
 
 public class YamlHttpMessageConverter<T> extends AbstractHttpMessageConverter<T> {
 
-    public YamlHttpMessageConverter() {
-        super(new MediaType("application", "yaml"));
+    private Coder coder;
+
+    public YamlHttpMessageConverter(String type) {
+        super(new MediaType("application", type, StandardCharsets.UTF_8));
+        this.coder = "json".equals(type) ? new StandardCoder() : new StandardYamlCoder();
     }
 
     @Override
@@ -47,16 +56,20 @@ public class YamlHttpMessageConverter<T> extends AbstractHttpMessageConverter<T>
     @Override
     protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
             throws IOException, HttpMessageNotReadableException {
-        var yaml = new Yaml();
-        return yaml.loadAs(inputMessage.getBody(), clazz);
+        try (var is = new InputStreamReader(inputMessage.getBody(), StandardCharsets.UTF_8)) {
+            return coder.decode(is, clazz);
+        } catch (CoderException e) {
+            throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e);
+        }
     }
 
     @Override
     protected void writeInternal(T t, HttpOutputMessage outputMessage)
             throws IOException, HttpMessageNotWritableException {
-        var yaml = new Yaml();
         try (var writer = new OutputStreamWriter(outputMessage.getBody(), StandardCharsets.UTF_8)) {
-            yaml.dump(t, writer);
+            coder.encode(writer, t);
+        } catch (CoderException e) {
+            throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e);
         }
     }
 }
index a9dea17..14f5126 100644 (file)
@@ -25,10 +25,20 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.GenericType;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import org.glassfish.jersey.client.ClientProperties;
+import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
@@ -45,21 +55,13 @@ import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.C
 import org.onap.policy.clamp.controlloop.participant.simulator.main.rest.AbstractRestController;
 import org.onap.policy.clamp.controlloop.participant.simulator.main.rest.TestListenerUtils;
 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
-import org.onap.policy.common.utils.coder.Coder;
-import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.gson.GsonMessageBodyHandler;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
-import org.springframework.boot.test.web.client.TestRestTemplate;
 import org.springframework.boot.web.server.LocalServerPort;
-import org.springframework.core.ParameterizedTypeReference;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
 import org.springframework.test.context.TestPropertySource;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
 
@@ -73,8 +75,6 @@ class ParticipantSimulatorTest {
     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
     private static final String TOPIC = "my-topic";
 
-    public static final Coder coder = new StandardCoder();
-
     @Value("${spring.security.user.name}")
     private String user;
 
@@ -84,9 +84,6 @@ class ParticipantSimulatorTest {
     @LocalServerPort
     private int randomServerPort;
 
-    @Autowired
-    private TestRestTemplate restTemplate;
-
     @Autowired
     private ParticipantIntermediaryApi participantIntermediaryApi;
 
@@ -100,11 +97,9 @@ class ParticipantSimulatorTest {
         synchronized (lockit) {
             if (!check) {
                 check = true;
-                ControlLoopUpdateListener clUpdateListener =
-                        new ControlLoopUpdateListener(participantHandler);
+                ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler);
 
-                ControlLoopUpdate controlLoopUpdateMsg =
-                        TestListenerUtils.createControlLoopUpdateMsg();
+                ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
                 clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg);
 
             }
@@ -116,11 +111,18 @@ class ParticipantSimulatorTest {
     }
 
     void testSwagger(String endPoint) {
-        HttpEntity<Void> request = new HttpEntity<>(null, createHttpHeaders());
-        ResponseEntity<String> response =
-                restTemplate.exchange(getPath("api-docs"), HttpMethod.GET, request, String.class);
-        assertThat(response.getStatusCodeValue()).isEqualTo(200);
-        assertTrue(response.getBody().contains("/onap/participantsim/v2/" + endPoint));
+        final Client client = ClientBuilder.newBuilder().build();
+
+        client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
+        client.register(GsonMessageBodyHandler.class);
+        client.register(HttpAuthenticationFeature.basic(user, password));
+
+        final WebTarget webTarget = client.target(getPath("api-docs"));
+
+        Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
+
+        assertThat(response.getStatus()).isEqualTo(200);
+        assertTrue(response.readEntity(String.class).contains("/onap/participantsim/v2/" + endPoint));
     }
 
     @Test
@@ -135,59 +137,49 @@ class ParticipantSimulatorTest {
 
     @Test
     void testProducerYaml() {
-        MediaType yamlMediaType = new MediaType("application", "yaml");
-        HttpHeaders headers = createHttpHeaders();
-        headers.setAccept(Collections.singletonList(yamlMediaType));
-        HttpEntity<Void> request = new HttpEntity<>(null, headers);
+        final Client client = ClientBuilder.newBuilder().build();
+
+        client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
+        client.register(GsonMessageBodyHandler.class);
+        client.register(HttpAuthenticationFeature.basic(user, password));
+
         String path = getPath(PARTICIPANTS_ENDPOINT + "/org.onap.PM_CDS_Blueprint/1");
+        final WebTarget webTarget = client.target(path);
 
-        ResponseEntity<String> response = restTemplate.exchange(path, HttpMethod.GET, request, String.class);
+        Response response = webTarget.request("application/yaml").get();
 
-        assertThat(response.getStatusCodeValue()).isEqualTo(200);
-        assertTrue(response.getHeaders().getContentType().isCompatibleWith(yamlMediaType));
+        assertThat(response.getStatus()).isEqualTo(200);
     }
 
     @Test
     void testQuery_Unauthorized() throws Exception {
-        String path = getPath(PARTICIPANTS_ENDPOINT + "/org.onap.PM_CDS_Blueprint/1");
+        String path = PARTICIPANTS_ENDPOINT + "/org.onap.PM_CDS_Blueprint/1";
 
-        // authorized call
-        ResponseEntity<String> response =
-                restTemplate.exchange(path, HttpMethod.GET, new HttpEntity<>(null, createHttpHeaders()), String.class);
-        assertThat(response.getStatusCodeValue()).isEqualTo(200);
+        Response response = performRequest(path, true, null).get();
+        assertThat(response.getStatus()).isEqualTo(200);
 
         // unauthorized call
-        response = restTemplate.exchange(path, HttpMethod.GET, new HttpEntity<>(null, new HttpHeaders()), String.class);
-        assertThat(response.getStatusCodeValue()).isEqualTo(401);
+        response = performRequest(path, false, null).get();
+        assertThat(response.getStatus()).isEqualTo(401);
     }
 
-    private HttpHeaders createHttpHeaders() {
-        HttpHeaders headers = new HttpHeaders();
-        headers.setBasicAuth(user, password);
-        return headers;
-    }
+    private Invocation.Builder performRequest(String endpoint, boolean includeAuth, UUID uuid) {
+        final Client client = ClientBuilder.newBuilder().build();
 
-    protected <T> ResponseEntity<T> performGet(String endpoint, Class<T> responseType, UUID uuid) throws Exception {
-        HttpHeaders httpHeaders = createHttpHeaders();
+        client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
+        client.register(GsonMessageBodyHandler.class);
+        if (includeAuth) {
+            client.register(HttpAuthenticationFeature.basic(user, password));
+        }
+        Invocation.Builder builder = client.target(getPath(endpoint)).request(MediaType.APPLICATION_JSON);
         if (uuid != null) {
-            httpHeaders.add(AbstractRestController.REQUEST_ID_NAME, uuid.toString());
+            builder = builder.header(AbstractRestController.REQUEST_ID_NAME, uuid.toString());
         }
-        HttpEntity<Void> request = new HttpEntity<>(null, httpHeaders);
-        return restTemplate.exchange(getPath(endpoint), HttpMethod.GET, request, responseType);
+        return builder;
     }
 
-    protected <T> ResponseEntity<T> performGet(String endpoint, Class<T> responseType) throws Exception {
-        return performGet(endpoint, responseType, null);
-    }
-
-    protected <T, R> ResponseEntity<R> performPut(String path, T body, ParameterizedTypeReference<R> responseType,
-            UUID uuid) throws Exception {
-        HttpHeaders httpHeaders = createHttpHeaders();
-        if (uuid != null) {
-            httpHeaders.add(AbstractRestController.REQUEST_ID_NAME, uuid.toString());
-        }
-        HttpEntity<T> request = new HttpEntity<>(body, httpHeaders);
-        return restTemplate.exchange(getPath(path), HttpMethod.PUT, request, responseType);
+    private Response performGet(String endpoint, UUID uuid) throws Exception {
+        return performRequest(endpoint, true, uuid).get();
     }
 
     @Test
@@ -200,29 +192,30 @@ class ParticipantSimulatorTest {
         UUID uuid = UUID.randomUUID();
 
         // GET REST call for querying the participants
-        ResponseEntity<String> response = performGet(
+        Response response = performGet(
                 PARTICIPANTS_ENDPOINT + "/" + participant.getKey().getName() + "/" + participant.getKey().getVersion(),
-                String.class, uuid);
+                uuid);
         checkResponseEntity(response, 200, uuid);
 
-        Participant[] returnValue = coder.decode(response.getBody(), Participant[].class);
+        Participant[] returnValue = response.readEntity(Participant[].class);
         assertThat(returnValue).hasSize(1);
         // Verify the result of GET participants with what is stored
         assertEquals(participant.getDefinition(), returnValue[0].getDefinition());
     }
 
-    private <T> void checkResponseEntity(ResponseEntity<T> response, int status, UUID uuid) {
-        assertThat(response.getStatusCodeValue()).isEqualTo(status);
+    private <T> void checkResponseEntity(Response response, int status, UUID uuid) {
+        assertThat(response.getStatus()).isEqualTo(status);
         assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_MINOR_NAME)).isEqualTo("0");
         assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_PATCH_NAME)).isEqualTo("0");
         assertThat(getHeader(response.getHeaders(), AbstractRestController.VERSION_LATEST_NAME)).isEqualTo("1.0.0");
         assertThat(getHeader(response.getHeaders(), AbstractRestController.REQUEST_ID_NAME)).isEqualTo(uuid.toString());
     }
 
-    private String getHeader(HttpHeaders httpHeaders, String param) {
-        List<String> list = httpHeaders.get(param);
+    private String getHeader(MultivaluedMap<String, Object> httpHeaders, String param) {
+        List<Object> list = httpHeaders.get(param);
         assertThat(list).hasSize(1);
-        return list.get(0);
+        assertThat(list.get(0)).isNotNull();
+        return (String) list.get(0);
     }
 
     @Test
@@ -232,16 +225,19 @@ class ParticipantSimulatorTest {
         ToscaConceptIdentifier participantId = CommonTestData.getParticipantId();
 
         // GET REST call for querying the controlLoop elements
-        ResponseEntity<String> response =
-                performGet(ELEMENTS_ENDPOINT + "/" + participantId.getName() + "/" + participantId.getVersion(),
-                        String.class, uuid);
+        Response response =
+                performGet(ELEMENTS_ENDPOINT + "/" + participantId.getName() + "/" + participantId.getVersion(), uuid);
         checkResponseEntity(response, 200, uuid);
 
-        Map<?, ?> returnValue = coder.decode(response.getBody(), Map.class);
+        Map<?, ?> returnValue = response.readEntity(Map.class);
         // Verify the result of GET controlloop elements with what is stored
         assertThat(returnValue).isEmpty();
     }
 
+    private Response performPut(String endpoint, final Entity<?> entity, UUID uuid) throws Exception {
+        return performRequest(endpoint, true, uuid).put(entity);
+    }
+
     @Test
     void testUpdateParticipant() throws Exception {
         setUp();
@@ -253,11 +249,11 @@ class ParticipantSimulatorTest {
         UUID uuid = UUID.randomUUID();
 
         // PUT REST call for updating Participant
-        ResponseEntity<TypedSimpleResponse<Participant>> response = performPut(PARTICIPANTS_ENDPOINT,
-                participants.get(0), new ParameterizedTypeReference<TypedSimpleResponse<Participant>>() {}, uuid);
+        Response response = performPut(PARTICIPANTS_ENDPOINT, Entity.json(participants.get(0)), uuid);
         checkResponseEntity(response, 200, uuid);
 
-        TypedSimpleResponse<Participant> resp = response.getBody();
+        TypedSimpleResponse<Participant> resp =
+                response.readEntity(new GenericType<TypedSimpleResponse<Participant>>() {});
         assertNotNull(resp.getResponse());
         // Verify the response and state returned by PUT REST call for updating participants
         assertEquals(participants.get(0).getDefinition(), resp.getResponse().getDefinition());
@@ -275,13 +271,12 @@ class ParticipantSimulatorTest {
         ControlLoopElement controlLoopElement = controlLoopElements.get(uuid);
 
         controlLoopElement.setOrderedState(ControlLoopOrderedState.PASSIVE);
-
         // PUT REST call for updating ControlLoopElement
-        ResponseEntity<TypedSimpleResponse<ControlLoopElement>> response = performPut(ELEMENTS_ENDPOINT,
-                controlLoopElement, new ParameterizedTypeReference<TypedSimpleResponse<ControlLoopElement>>() {}, uuid);
+        Response response = performPut(ELEMENTS_ENDPOINT, Entity.json(controlLoopElement), uuid);
         checkResponseEntity(response, 200, uuid);
 
-        TypedSimpleResponse<ControlLoopElement> resp = response.getBody();
+        TypedSimpleResponse<ControlLoopElement> resp =
+                response.readEntity(new GenericType<TypedSimpleResponse<ControlLoopElement>>() {});
         assertNotNull(resp.getResponse());
         // Verify the response and state returned by PUT REST call for updating participants
         assertEquals(controlLoopElement.getDefinition(), resp.getResponse().getDefinition());
index ed0aea4..2ca14fd 100644 (file)
@@ -100,13 +100,6 @@ public interface ParticipantIntermediaryApi {
      */
     Map<UUID, ControlLoopElement> getControlLoopElements(String name, String version);
 
-    /**
-     * Get ToscaServiceTemplate from the intermediary API.
-     *
-     * @return the control loop element
-     */
-    ToscaServiceTemplate getToscaServiceTemplate();
-
     /**
      * Get control loop element from the intermediary API.
      *
index ded102f..f6d7cd1 100644 (file)
@@ -121,11 +121,6 @@ public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryAp
         return null;
     }
 
-    @Override
-    public ToscaServiceTemplate getToscaServiceTemplate() {
-        return participantHandler.getToscaServiceTemplate();
-    }
-
     @Override
     public ControlLoopElement updateControlLoopElementState(UUID id, ControlLoopOrderedState currentState,
             ControlLoopState newState, ParticipantMessageType messageType) {
index 49ad6da..56fddd6 100644 (file)
@@ -59,7 +59,6 @@ import org.onap.policy.clamp.controlloop.participant.intermediary.comm.Participa
 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
@@ -86,8 +85,6 @@ public class ParticipantHandler {
 
     private final List<ControlLoopElementDefinition> clElementDefsOnThisParticipant = new ArrayList<>();
 
-    private ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
-
     /**
      * Constructor, set the participant ID and sender.
      *
@@ -292,8 +289,7 @@ public class ParticipantHandler {
             return;
         }
 
-        toscaServiceTemplate = participantUpdateMsg.getToscaServiceTemplate();
-        if (toscaServiceTemplate != null) {
+        if (!participantUpdateMsg.getParticipantDefinitionUpdates().isEmpty()) {
             // This message is to commission the controlloop
             for (ParticipantDefinition participantDefinition : participantUpdateMsg.getParticipantDefinitionUpdates()) {
                 if (participantDefinition.getParticipantId().equals(participantType)) {
index 6df030d..d68a643 100644 (file)
@@ -31,6 +31,11 @@ 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;
@@ -43,6 +48,9 @@ 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;
 
     /**
      * Send ControlLoopUpdate to Participant.
@@ -54,9 +62,37 @@ public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<Con
         controlLoopUpdateMsg.setControlLoopId(controlLoop.getKey().asIdentifier());
         controlLoopUpdateMsg.setMessageId(UUID.randomUUID());
         controlLoopUpdateMsg.setTimestamp(Instant.now());
+        ToscaServiceTemplate toscaServiceTemplate;
+        try {
+            toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0);
+        } catch (PfModelException pfme) {
+            LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
+            return;
+        }
 
         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);
         }
         controlLoopUpdateMsg.setParticipantUpdatesList(participantUpdates);
index efd2625..1138236 100644 (file)
@@ -95,11 +95,9 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par
         if (commissionFlag) {
             // Commission the controlloop but sending participantdefinitions to participants
             message.setParticipantDefinitionUpdates(participantDefinitionUpdates);
-            message.setToscaServiceTemplate(toscaServiceTemplate);
         } else {
             // DeCommission the controlloop but deleting participantdefinitions on participants
             message.setParticipantDefinitionUpdates(null);
-            message.setToscaServiceTemplate(null);
         }
         LOGGER.debug("Participant Update sent {}", message);
         super.send(message);
index f1a2728..3bf0138 100644 (file)
@@ -206,7 +206,6 @@ class SupervisionMessagesTest extends CommonRestController {
         }
 
         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
-        participantUpdateMsg.setToscaServiceTemplate(toscaServiceTemplate);
         synchronized (lockit) {
             ParticipantUpdatePublisher participantUpdatePublisher =
                 new ParticipantUpdatePublisher(modelsProvider);
index dc09b46..c14af81 100644 (file)
@@ -177,6 +177,9 @@ topology_template:
         policy_type_id:
           name: onap.policies.monitoring.pm-subscription-handler
           version: 1.0.0
+        policy_id:
+          name: onap.policies.monitoring.pm-subscription-handler
+          version: 1.0.0
     org.onap.domain.pmsh.PMSD_OperationalPolicyControlLoopElement:
       version: 1.2.3
       type: org.onap.policy.clamp.controlloop.PolicyTypeControlLoopElement
@@ -190,6 +193,9 @@ topology_template:
         policy_type_id:
           name: onap.policies.operational.pm-subscription-handler
           version: 1.0.0
+        policy_id:
+          name: onap.policies.monitoring.pm-subscription-handler
+          version: 1.0.0
     org.onap.domain.pmsh.PMSD_CDS_ControlLoopElement:
       version: 1.2.3
       type: org.onap.policy.clamp.controlloop.ControlLoopElement