2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021-2022 Nordix Foundation.
7 * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest.e2e;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
31 import java.util.List;
32 import java.util.concurrent.LinkedBlockingQueue;
33 import java.util.concurrent.TimeUnit;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.client.Invocation;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
41 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
44 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
45 import org.onap.policy.models.pap.concepts.PolicyNotification;
46 import org.onap.policy.models.pap.concepts.PolicyStatus;
47 import org.onap.policy.models.pdp.concepts.DeploymentGroup;
48 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
49 import org.onap.policy.models.pdp.concepts.PdpStatus;
50 import org.onap.policy.models.pdp.enums.PdpState;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.pap.main.rest.PdpGroupDeployControllerV1;
54 public class PdpGroupDeployTest extends End2EndBase {
55 private static final String DEPLOY_GROUP_ENDPOINT = "pdps/deployments/batch";
56 private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
57 private static final String DEPLOY_SUBGROUP = "pdpTypeA";
64 public void setUp() throws Exception {
65 addToscaPolicyTypes("monitoring.policy-type.yaml");
66 addToscaPolicies("monitoring.policy.yaml");
69 context = new End2EndContext();
73 public void testUpdateGroupPolicies() throws Exception {
75 addGroups("deployGroups.json");
77 PdpStatus status11 = new PdpStatus();
78 status11.setName("pdpAA_1");
79 status11.setState(PdpState.ACTIVE);
80 status11.setPdpGroup("deployGroups");
81 status11.setPdpType(DEPLOY_SUBGROUP);
82 status11.setPdpSubgroup(DEPLOY_SUBGROUP);
84 List<ToscaConceptIdentifier> idents = List.of(new ToscaConceptIdentifier("onap.restart.tca", "1.0.0"));
85 status11.setPolicies(idents);
87 PdpStatus status12 = new PdpStatus();
88 status12.setName("pdpAA_2");
89 status12.setState(PdpState.ACTIVE);
90 status12.setPdpGroup("deployGroups");
91 status12.setPdpType(DEPLOY_SUBGROUP);
92 status12.setPdpSubgroup(DEPLOY_SUBGROUP);
93 status12.setPolicies(idents);
95 context.addPdp("pdpAA_1", DEPLOY_SUBGROUP).addReply(status11);
96 context.addPdp("pdpAA_2", DEPLOY_SUBGROUP).addReply(status12);
97 context.addPdp("pdpAB_1", "pdpTypeA");
99 context.startThreads();
101 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
103 DeploymentGroups groups = loadJsonFile("deployGroupsReq.json", DeploymentGroups.class);
104 Entity<DeploymentGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
105 Response rawresp = invocationBuilder.post(entity);
106 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
107 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
108 assertNull(resp.getErrorDetails());
112 // one of the PDPs should not have handled any requests
113 assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
115 // repeat - should be OK
116 rawresp = invocationBuilder.post(entity);
117 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
118 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
119 assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
120 assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
121 assertNull(resp.getErrorDetails());
123 assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(2);
125 // repeat with unknown group - should fail
126 DeploymentGroup group = groups.getGroups().get(0);
127 group.setName("unknown-group");
128 rawresp = invocationBuilder.post(entity);
129 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
130 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
131 assertTrue(resp.getErrorDetails().contains("unknown group"));
135 public void testDeployPolicies() throws Exception {
136 addGroups("deployPolicies.json");
138 PdpStatus status11 = new PdpStatus();
139 status11.setName("pdpBA_1");
140 status11.setState(PdpState.ACTIVE);
141 status11.setPdpGroup("deployPolicies");
142 status11.setPdpType(DEPLOY_SUBGROUP);
143 status11.setPdpSubgroup(DEPLOY_SUBGROUP);
145 final ToscaConceptIdentifier identifier = new ToscaConceptIdentifier("onap.restart.tcaB", "1.0.0");
147 List<ToscaConceptIdentifier> identifiers = List.of(identifier);
148 status11.setPolicies(identifiers);
150 PdpStatus status12 = new PdpStatus();
151 status12.setName("pdpBA_2");
152 status12.setState(PdpState.ACTIVE);
153 status12.setPdpGroup("deployPolicies");
154 status12.setPdpType(DEPLOY_SUBGROUP);
155 status12.setPdpSubgroup(DEPLOY_SUBGROUP);
156 status12.setPolicies(identifiers);
158 context.addPdp("pdpBA_1", DEPLOY_SUBGROUP).addReply(status11);
159 context.addPdp("pdpBA_2", DEPLOY_SUBGROUP).addReply(status12);
160 context.addPdp("pdpBB_1", "pdpTypeB");
162 context.startThreads();
164 // arrange to catch notifications
165 LinkedBlockingQueue<String> notifications = new LinkedBlockingQueue<>();
166 NoopTopicSink notifier = NoopTopicFactories.getSinkFactory().get(getTopicPolicyNotification());
167 notifier.register((infra, topic, msg) -> notifications.add(msg));
169 assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isZero();
171 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
173 PdpDeployPolicies policies = loadJsonFile("deployPoliciesReq2.json", PdpDeployPolicies.class);
174 Entity<PdpDeployPolicies> entity = Entity.entity(policies, MediaType.APPLICATION_JSON);
175 Response rawresp = invocationBuilder.post(entity);
176 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
177 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
178 assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
179 assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
180 assertNull(resp.getErrorDetails());
184 // wait for the notification
185 String json = notifications.poll(5, TimeUnit.SECONDS);
186 PolicyNotification notify = new StandardCoder().decode(json, PolicyNotification.class);
187 assertNotNull(notify.getAdded());
188 assertNotNull(notify.getDeleted());
189 assertTrue(notify.getDeleted().isEmpty());
190 assertEquals(1, notify.getAdded().size());
192 PolicyStatus added = notify.getAdded().get(0);
193 assertEquals(2, added.getSuccessCount());
194 assertEquals(0, added.getFailureCount());
195 assertEquals(0, added.getIncompleteCount());
196 assertEquals(identifier, added.getPolicy());
198 // one of the PDPs should not have handled any requests
199 assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
201 // repeat - should be OK
202 rawresp = invocationBuilder.post(entity);
203 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
204 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
205 assertNull(resp.getErrorDetails());
207 assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(2);