2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest.e2e;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.concurrent.LinkedBlockingQueue;
32 import java.util.concurrent.TimeUnit;
33 import javax.ws.rs.client.Entity;
34 import javax.ws.rs.client.Invocation;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
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.PapConstants;
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";
60 * Starts Main and adds policies to the DB.
62 * @throws Exception if an error occurs
65 public static void setUpBeforeClass() throws Exception {
66 End2EndBase.setUpBeforeClass();
68 addToscaPolicyTypes("monitoring.policy-type.yaml");
69 addToscaPolicies("monitoring.policy.yaml");
77 public void setUp() throws Exception {
80 context = new End2EndContext();
84 public void testUpdateGroupPolicies() throws Exception {
86 addGroups("deployGroups.json");
88 PdpStatus status11 = new PdpStatus();
89 status11.setName("pdpAA_1");
90 status11.setState(PdpState.ACTIVE);
91 status11.setPdpGroup("deployGroups");
92 status11.setPdpType(DEPLOY_SUBGROUP);
93 status11.setPdpSubgroup(DEPLOY_SUBGROUP);
95 List<ToscaConceptIdentifier> idents = Arrays.asList(new ToscaConceptIdentifier("onap.restart.tca", "1.0.0"));
96 status11.setPolicies(idents);
98 PdpStatus status12 = new PdpStatus();
99 status12.setName("pdpAA_2");
100 status12.setState(PdpState.ACTIVE);
101 status12.setPdpGroup("deployGroups");
102 status12.setPdpType(DEPLOY_SUBGROUP);
103 status12.setPdpSubgroup(DEPLOY_SUBGROUP);
104 status12.setPolicies(idents);
106 context.addPdp("pdpAA_1", DEPLOY_SUBGROUP).addReply(status11);
107 context.addPdp("pdpAA_2", DEPLOY_SUBGROUP).addReply(status12);
108 context.addPdp("pdpAB_1", "pdpTypeA");
110 context.startThreads();
112 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
114 DeploymentGroups groups = loadJsonFile("deployGroupsReq.json", DeploymentGroups.class);
115 Entity<DeploymentGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
116 Response rawresp = invocationBuilder.post(entity);
117 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
118 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
119 assertNull(resp.getErrorDetails());
123 // one of the PDPs should not have handled any requests
124 assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
126 // repeat - should be OK
127 rawresp = invocationBuilder.post(entity);
128 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
129 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
130 assertNull(resp.getErrorDetails());
132 // repeat with unknown group - should fail
133 DeploymentGroup group = groups.getGroups().get(0);
134 group.setName("unknown-group");
135 rawresp = invocationBuilder.post(entity);
136 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
137 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
138 assertTrue(resp.getErrorDetails().contains("unknown group"));
142 public void testDeployPolicies() throws Exception {
143 addGroups("deployPolicies.json");
145 PdpStatus status11 = new PdpStatus();
146 status11.setName("pdpBA_1");
147 status11.setState(PdpState.ACTIVE);
148 status11.setPdpGroup("deployPolicies");
149 status11.setPdpType(DEPLOY_SUBGROUP);
150 status11.setPdpSubgroup(DEPLOY_SUBGROUP);
152 final ToscaConceptIdentifier ident = new ToscaConceptIdentifier("onap.restart.tcaB", "1.0.0");
154 List<ToscaConceptIdentifier> idents = Arrays.asList(ident);
155 status11.setPolicies(idents);
157 PdpStatus status12 = new PdpStatus();
158 status12.setName("pdpBA_2");
159 status12.setState(PdpState.ACTIVE);
160 status12.setPdpGroup("deployPolicies");
161 status12.setPdpType(DEPLOY_SUBGROUP);
162 status12.setPdpSubgroup(DEPLOY_SUBGROUP);
163 status12.setPolicies(idents);
165 context.addPdp("pdpBA_1", DEPLOY_SUBGROUP).addReply(status11);
166 context.addPdp("pdpBA_2", DEPLOY_SUBGROUP).addReply(status12);
167 context.addPdp("pdpBB_1", "pdpTypeB");
169 context.startThreads();
171 // arrange to catch notifications
172 LinkedBlockingQueue<String> notifications = new LinkedBlockingQueue<>();
173 NoopTopicSink notifier = NoopTopicFactories.getSinkFactory().get(PapConstants.TOPIC_POLICY_NOTIFICATION);
174 notifier.register((infra, topic, msg) -> {
175 notifications.add(msg);
178 Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
180 PdpDeployPolicies policies = loadJsonFile("deployPoliciesReq2.json", PdpDeployPolicies.class);
181 Entity<PdpDeployPolicies> entity = Entity.entity(policies, MediaType.APPLICATION_JSON);
182 Response rawresp = invocationBuilder.post(entity);
183 PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
184 System.out.println(resp.getErrorDetails());
185 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
186 assertNull(resp.getErrorDetails());
190 // wait for the notification
191 String json = notifications.poll(5, TimeUnit.SECONDS);
192 PolicyNotification notify = new StandardCoder().decode(json, PolicyNotification.class);
193 assertNotNull(notify.getAdded());
194 assertNotNull(notify.getDeleted());
195 assertTrue(notify.getDeleted().isEmpty());
196 assertEquals(1, notify.getAdded().size());
198 PolicyStatus added = notify.getAdded().get(0);
199 assertEquals(2, added.getSuccessCount());
200 assertEquals(0, added.getFailureCount());
201 assertEquals(0, added.getIncompleteCount());
202 assertEquals(ident, added.getPolicy());
204 // one of the PDPs should not have handled any requests
205 assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
207 // repeat - should be OK
208 rawresp = invocationBuilder.post(entity);
209 resp = rawresp.readEntity(PdpGroupDeployResponse.class);
210 assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
211 assertNull(resp.getErrorDetails());