2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * Modifications Copyright (C) 2021 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.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
30 import java.util.Arrays;
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.BeforeClass;
40 import org.junit.Test;
41 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
42 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
45 import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
46 import org.onap.policy.models.pap.concepts.PolicyNotification;
47 import org.onap.policy.models.pap.concepts.PolicyStatus;
48 import org.onap.policy.models.pdp.concepts.DeploymentGroup;
49 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
50 import org.onap.policy.models.pdp.concepts.PdpStatus;
51 import org.onap.policy.models.pdp.enums.PdpState;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 import org.onap.policy.pap.main.PapConstants;
54 import org.onap.policy.pap.main.rest.PdpGroupDeployControllerV1;
56 public class PdpGroupDeployTest extends End2EndBase {
57 private static final String DEPLOY_GROUP_ENDPOINT = "pdps/deployments/batch";
58 private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies";
59 private static final String DEPLOY_SUBGROUP = "pdpTypeA";
62 * Starts Main and adds policies to the DB.
64 * @throws Exception if an error occurs
67 public static void setUpBeforeClass() throws Exception {
68 End2EndBase.setUpBeforeClass();
70 addToscaPolicyTypes("monitoring.policy-type.yaml");
71 addToscaPolicies("monitoring.policy.yaml");
79 public void setUp() throws Exception {
82 context = new End2EndContext();
86 public void testUpdateGroupPolicies() throws Exception {
88 addGroups("deployGroups.json");
90 PdpStatus status11 = new PdpStatus();
91 status11.setName("pdpAA_1");
92 status11.setState(PdpState.ACTIVE);
93 status11.setPdpGroup("deployGroups");
94 status11.setPdpType(DEPLOY_SUBGROUP);
95 status11.setPdpSubgroup(DEPLOY_SUBGROUP);
97 List<ToscaConceptIdentifier> idents = Arrays.asList(new ToscaConceptIdentifier("onap.restart.tca", "1.0.0"));
98 status11.setPolicies(idents);
100 PdpStatus status12 = new PdpStatus();
101 status12.setName("pdpAA_2");
102 status12.setState(PdpState.ACTIVE);
103 status12.setPdpGroup("deployGroups");
104 status12.setPdpType(DEPLOY_SUBGROUP);
105 status12.setPdpSubgroup(DEPLOY_SUBGROUP);
106 status12.setPolicies(idents);
108 context.addPdp("pdpAA_1", DEPLOY_SUBGROUP).addReply(status11);
109 context.addPdp("pdpAA_2", DEPLOY_SUBGROUP).addReply(status12);
110 context.addPdp("pdpAB_1", "pdpTypeA");
112 context.startThreads();
114 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
116 DeploymentGroups groups = loadJsonFile("deployGroupsReq.json", DeploymentGroups.class);
117 Entity<DeploymentGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
118 Response rawresp = invocationBuilder.post(entity);
119 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
120 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
121 assertNull(resp.getErrorDetails());
125 // one of the PDPs should not have handled any requests
126 assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
128 // repeat - should be OK
129 rawresp = invocationBuilder.post(entity);
130 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
131 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
132 assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
133 assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
134 assertNull(resp.getErrorDetails());
136 // repeat with unknown group - should fail
137 DeploymentGroup group = groups.getGroups().get(0);
138 group.setName("unknown-group");
139 rawresp = invocationBuilder.post(entity);
140 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
141 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
142 assertTrue(resp.getErrorDetails().contains("unknown group"));
146 public void testDeployPolicies() throws Exception {
147 addGroups("deployPolicies.json");
149 PdpStatus status11 = new PdpStatus();
150 status11.setName("pdpBA_1");
151 status11.setState(PdpState.ACTIVE);
152 status11.setPdpGroup("deployPolicies");
153 status11.setPdpType(DEPLOY_SUBGROUP);
154 status11.setPdpSubgroup(DEPLOY_SUBGROUP);
156 final ToscaConceptIdentifier ident = new ToscaConceptIdentifier("onap.restart.tcaB", "1.0.0");
158 List<ToscaConceptIdentifier> idents = Arrays.asList(ident);
159 status11.setPolicies(idents);
161 PdpStatus status12 = new PdpStatus();
162 status12.setName("pdpBA_2");
163 status12.setState(PdpState.ACTIVE);
164 status12.setPdpGroup("deployPolicies");
165 status12.setPdpType(DEPLOY_SUBGROUP);
166 status12.setPdpSubgroup(DEPLOY_SUBGROUP);
167 status12.setPolicies(idents);
169 context.addPdp("pdpBA_1", DEPLOY_SUBGROUP).addReply(status11);
170 context.addPdp("pdpBA_2", DEPLOY_SUBGROUP).addReply(status12);
171 context.addPdp("pdpBB_1", "pdpTypeB");
173 context.startThreads();
175 // arrange to catch notifications
176 LinkedBlockingQueue<String> notifications = new LinkedBlockingQueue<>();
177 NoopTopicSink notifier = NoopTopicFactories.getSinkFactory().get(PapConstants.TOPIC_POLICY_NOTIFICATION);
178 notifier.register((infra, topic, msg) -> {
179 notifications.add(msg);
182 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
184 PdpDeployPolicies policies = loadJsonFile("deployPoliciesReq2.json", PdpDeployPolicies.class);
185 Entity<PdpDeployPolicies> entity = Entity.entity(policies, MediaType.APPLICATION_JSON);
186 Response rawresp = invocationBuilder.post(entity);
187 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
188 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
189 assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
190 assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
191 assertNull(resp.getErrorDetails());
195 // wait for the notification
196 String json = notifications.poll(5, TimeUnit.SECONDS);
197 PolicyNotification notify = new StandardCoder().decode(json, PolicyNotification.class);
198 assertNotNull(notify.getAdded());
199 assertNotNull(notify.getDeleted());
200 assertTrue(notify.getDeleted().isEmpty());
201 assertEquals(1, notify.getAdded().size());
203 PolicyStatus added = notify.getAdded().get(0);
204 assertEquals(2, added.getSuccessCount());
205 assertEquals(0, added.getFailureCount());
206 assertEquals(0, added.getIncompleteCount());
207 assertEquals(ident, added.getPolicy());
209 // one of the PDPs should not have handled any requests
210 assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
212 // repeat - should be OK
213 rawresp = invocationBuilder.post(entity);
214 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
215 assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
216 assertNull(resp.getErrorDetails());