/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019-2020 Nordix Foundation.
+ * Copyright (C) 2019-2021 Nordix Foundation.
+ * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property.
+ * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.pap.main.comm;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
import java.text.ParseException;
-import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Date;
import java.util.List;
-
+import java.util.stream.Collectors;
import org.junit.Test;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
+import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpHealthStatus;
import org.onap.policy.models.pdp.enums.PdpState;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.pap.main.parameters.CommonTestData;
+import org.onap.policy.pap.main.parameters.PdpParameters;
import org.onap.policy.pap.main.rest.e2e.End2EndBase;
/**
public class PdpHeartbeatListenerTest extends End2EndBase {
private static final String POLICY_VERSION = "1.0.0";
- private static final String POLICY_NAME = "onap.policies.controlloop.operational.Apex.SampleDomain";
+ private static final String POLICY_NAME = "onap.policies.controlloop.operational.common.apex.SampleDomain";
private static final String APEX_TYPE = "apex";
private static final String DEFAULT_GROUP = "defaultGroup";
private static final String PDP_NAME = "pdp_1";
private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
private static final String TOPIC = "my-topic";
- private Date timeStamp;
+ private Instant timeStamp;
private PdpHeartbeatListener pdpHeartbeatListener;
@Test
public void testPdpHeartbeatListener() throws CoderException, PfModelException {
addGroups("PdpGroups.json");
- pdpHeartbeatListener = new PdpHeartbeatListener();
+ pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters(), true);
// Testing pdp registration success case
final PdpStatus status1 = new PdpStatus();
status1.setPdpGroup(DEFAULT_GROUP);
status1.setPdpType(APEX_TYPE);
status1.setHealthy(PdpHealthStatus.HEALTHY);
- final List<ToscaPolicyIdentifier> idents1 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents1 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status1.setPolicies(idents1);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status1);
verifyPdpGroup(DEFAULT_GROUP, 1);
status2.setPdpType(APEX_TYPE);
status2.setHealthy(PdpHealthStatus.HEALTHY);
status2.setPdpSubgroup(APEX_TYPE);
- final List<ToscaPolicyIdentifier> idents2 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents2 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status2.setPolicies(idents2);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status2);
verifyPdpGroup(DEFAULT_GROUP, 1);
status3.setPdpType(APEX_TYPE);
status3.setHealthy(PdpHealthStatus.HEALTHY);
status3.setPdpSubgroup(APEX_TYPE);
- final List<ToscaPolicyIdentifier> idents3 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents3 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status3.setPolicies(idents3);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status3);
verifyPdpGroup(DEFAULT_GROUP, 2);
status4.setPdpGroup("wrongGroup");
status4.setPdpType(APEX_TYPE);
status4.setHealthy(PdpHealthStatus.HEALTHY);
- final List<ToscaPolicyIdentifier> idents4 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents4 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status4.setPolicies(idents4);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status4);
verifyPdpGroup(DEFAULT_GROUP, 2);
status5.setPdpType(APEX_TYPE);
status5.setHealthy(PdpHealthStatus.HEALTHY);
status5.setPdpSubgroup(APEX_TYPE);
- final List<ToscaPolicyIdentifier> idents5 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents5 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status5.setPolicies(idents5);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status5);
verifyPdpGroup(DEFAULT_GROUP, 2);
status6.setPdpType(APEX_TYPE);
status6.setHealthy(PdpHealthStatus.HEALTHY);
status6.setPdpSubgroup(APEX_TYPE);
- final List<ToscaPolicyIdentifier> idents6 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION),
- new ToscaPolicyIdentifier("onap.restart.tca", POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents6 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION),
+ new ToscaConceptIdentifier("onap.restart.tca", POLICY_VERSION));
status6.setPolicies(idents6);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status6);
verifyPdpGroup(DEFAULT_GROUP, 2);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status7);
verifyPdpGroup(DEFAULT_GROUP, 2);
+ // Testing old message for pdp_1 - should have no effect
+ final PdpStatus status7b = new PdpStatus();
+ status7b.setTimestampMs(System.currentTimeMillis() - PdpParameters.DEFAULT_MAX_AGE_MS - 1);
+ status7b.setName(PDP_NAME);
+ status7b.setState(PdpState.TERMINATED);
+ status7b.setPdpGroup(DEFAULT_GROUP);
+ status7b.setPdpType(APEX_TYPE);
+ status7b.setPdpSubgroup(APEX_TYPE);
+ status7b.setHealthy(PdpHealthStatus.HEALTHY);
+ final List<ToscaConceptIdentifier> idents7b =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
+ status7b.setPolicies(idents7b);
+ pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status7b);
+ verifyPdpGroup(DEFAULT_GROUP, 2);
+
// Testing pdp termination case for pdp_1
final PdpStatus status8 = new PdpStatus();
status8.setName(PDP_NAME);
status8.setPdpType(APEX_TYPE);
status8.setPdpSubgroup(APEX_TYPE);
status8.setHealthy(PdpHealthStatus.HEALTHY);
- final List<ToscaPolicyIdentifier> idents8 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents8 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status8.setPolicies(idents8);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status8);
verifyPdpGroup(DEFAULT_GROUP, 1);
status9.setPdpType(APEX_TYPE);
status9.setPdpSubgroup(APEX_TYPE);
status9.setHealthy(PdpHealthStatus.HEALTHY);
- final List<ToscaPolicyIdentifier> idents9 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents9 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status9.setPolicies(idents9);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status9);
verifyPdpGroup(DEFAULT_GROUP, 0);
+
+ // Test policy lists updated in createUpdate
+ ToscaPolicy polA = new ToscaPolicy();
+ polA.setName("pol-a-1.1.1");
+ polA.setVersion("1.1.1");
+ ToscaPolicy polB = new ToscaPolicy();
+ polB.setName("pol-b-1.1.1");
+ polB.setVersion("1.1.1");
+ List<ToscaPolicy> policies = new ArrayList<>();
+ policies.add(polA);
+ policies.add(polB);
+ final CommonTestData testData = new CommonTestData();
+ PdpParameters params = testData.getPapParameterGroup(1).getPdpParameters();
+ List<ToscaConceptIdentifier> polsUndep = policies.stream().map(ToscaPolicy::getIdentifier)
+ .collect(Collectors.toList());
+ PdpStatusMessageHandler handler = new PdpStatusMessageHandler(params, true);
+ PdpUpdate update10 = handler.createPdpUpdateMessage(
+ status3.getPdpGroup(), new PdpSubGroup(), "pdp_2",
+ policies, polsUndep);
+ assertSame(update10.getPoliciesToBeDeployed(), policies);
+ assertSame(update10.getPoliciesToBeUndeployed(), polsUndep);
+ assertThat(update10.getPoliciesToBeDeployed()).isInstanceOf(List.class);
}
@Test
public void testPdpStatistics() throws CoderException, PfModelException, ParseException {
addGroups("PdpGroups.json");
- pdpHeartbeatListener = new PdpHeartbeatListener();
- timeStamp = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
+ pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters(), true);
+ timeStamp = Instant.parse("2021-02-12T17:48:01.029211400Z");
// init default pdp group
final PdpStatus status1 = new PdpStatus();
status1.setPdpGroup(DEFAULT_GROUP);
status1.setPdpType(APEX_TYPE);
status1.setHealthy(PdpHealthStatus.HEALTHY);
- final List<ToscaPolicyIdentifier> idents1 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents1 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status1.setPolicies(idents1);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status1);
verifyPdpGroup(DEFAULT_GROUP, 1);
status2.setPdpType(APEX_TYPE);
status2.setHealthy(PdpHealthStatus.HEALTHY);
status2.setPdpSubgroup(APEX_TYPE);
- final List<ToscaPolicyIdentifier> idents2 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents2 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status2.setPolicies(idents2);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status2);
status3.setPdpType(APEX_TYPE);
status3.setHealthy(PdpHealthStatus.HEALTHY);
status3.setPdpSubgroup(APEX_TYPE);
- final List<ToscaPolicyIdentifier> idents3 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents3 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status3.setPolicies(idents3);
PdpStatistics pdpStatistics03 = new PdpStatistics();
status4.setPdpType(APEX_TYPE);
status4.setHealthy(PdpHealthStatus.HEALTHY);
status4.setPdpSubgroup(APEX_TYPE);
- final List<ToscaPolicyIdentifier> idents4 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents4 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status4.setPolicies(idents4);
status4.setStatistics(null);
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status4);
status5.setPdpType(APEX_TYPE);
status5.setHealthy(PdpHealthStatus.HEALTHY);
status5.setPdpSubgroup(APEX_TYPE);
- final List<ToscaPolicyIdentifier> idents5 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents5 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status5.setPolicies(idents5);
PdpStatistics pdpStatistics05 = new PdpStatistics();
status6.setPdpType(APEX_TYPE);
status6.setHealthy(PdpHealthStatus.HEALTHY);
status6.setPdpSubgroup(APEX_TYPE);
- final List<ToscaPolicyIdentifier> idents6 =
- Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+ final List<ToscaConceptIdentifier> idents6 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
status5.setPolicies(idents6);
PdpStatistics pdpStatistics06 = new PdpStatistics();
pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status5);
verifyPdpStatistics(null, DEFAULT_GROUP, null, 1);
+
+ // Test pdp statistics save disabled case, sending valid pdp status but count should still remain 1
+ pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters(), false);
+ timeStamp = Instant.parse("2021-02-12T17:48:05.029211400Z");
+ final PdpStatus status7 = new PdpStatus();
+ status7.setName(PDP_NAME);
+ status7.setState(PdpState.ACTIVE);
+ status7.setPdpGroup(DEFAULT_GROUP);
+ status7.setPdpType(APEX_TYPE);
+ status7.setHealthy(PdpHealthStatus.HEALTHY);
+ status7.setPdpSubgroup(APEX_TYPE);
+ final List<ToscaConceptIdentifier> idents7 =
+ Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
+ status7.setPolicies(idents7);
+
+ PdpStatistics pdpStatistics07 = new PdpStatistics();
+ pdpStatistics07.setPdpInstanceId(PDP_NAME);
+ pdpStatistics07.setPdpGroupName(DEFAULT_GROUP);
+ pdpStatistics07.setPdpSubGroupName(APEX_TYPE);
+ pdpStatistics07.setTimeStamp(timeStamp);
+ status7.setStatistics(pdpStatistics07);
+ pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status7);
+ verifyPdpStatistics(PDP_NAME, DEFAULT_GROUP, null, 1);
+
}
private void verifyPdpGroup(final String name, final int count) throws PfModelException {