Adding deployment metrics to PAP
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / e2e / PdpGroupDeployTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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-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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest.e2e;
24
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;
30
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.concurrent.LinkedBlockingQueue;
34 import java.util.concurrent.TimeUnit;
35 import javax.ws.rs.client.Entity;
36 import javax.ws.rs.client.Invocation;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import org.junit.Before;
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;
55
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";
60
61     /**
62      * Sets up.
63      */
64     @Override
65     @Before
66     public void setUp() throws Exception {
67         addToscaPolicyTypes("monitoring.policy-type.yaml");
68         addToscaPolicies("monitoring.policy.yaml");
69         super.setUp();
70
71         context = new End2EndContext();
72     }
73
74     @Test
75     public void testUpdateGroupPolicies() throws Exception {
76
77         addGroups("deployGroups.json");
78
79         PdpStatus status11 = new PdpStatus();
80         status11.setName("pdpAA_1");
81         status11.setState(PdpState.ACTIVE);
82         status11.setPdpGroup("deployGroups");
83         status11.setPdpType(DEPLOY_SUBGROUP);
84         status11.setPdpSubgroup(DEPLOY_SUBGROUP);
85
86         List<ToscaConceptIdentifier> idents = Arrays.asList(new ToscaConceptIdentifier("onap.restart.tca", "1.0.0"));
87         status11.setPolicies(idents);
88
89         PdpStatus status12 = new PdpStatus();
90         status12.setName("pdpAA_2");
91         status12.setState(PdpState.ACTIVE);
92         status12.setPdpGroup("deployGroups");
93         status12.setPdpType(DEPLOY_SUBGROUP);
94         status12.setPdpSubgroup(DEPLOY_SUBGROUP);
95         status12.setPolicies(idents);
96
97         context.addPdp("pdpAA_1", DEPLOY_SUBGROUP).addReply(status11);
98         context.addPdp("pdpAA_2", DEPLOY_SUBGROUP).addReply(status12);
99         context.addPdp("pdpAB_1", "pdpTypeA");
100
101         context.startThreads();
102
103         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT);
104
105         DeploymentGroups groups = loadJsonFile("deployGroupsReq.json", DeploymentGroups.class);
106         Entity<DeploymentGroups> entity = Entity.entity(groups, MediaType.APPLICATION_JSON);
107         Response rawresp = invocationBuilder.post(entity);
108         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
109         assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
110         assertNull(resp.getErrorDetails());
111
112         context.await();
113
114         // one of the PDPs should not have handled any requests
115         assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
116
117         // repeat - should be OK
118         rawresp = invocationBuilder.post(entity);
119         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
120         assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
121         assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
122         assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
123         assertNull(resp.getErrorDetails());
124
125         assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(2);
126
127         // repeat with unknown group - should fail
128         DeploymentGroup group = groups.getGroups().get(0);
129         group.setName("unknown-group");
130         rawresp = invocationBuilder.post(entity);
131         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
132         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus());
133         assertTrue(resp.getErrorDetails().contains("unknown group"));
134     }
135
136     @Test
137     public void testDeployPolicies() throws Exception {
138         addGroups("deployPolicies.json");
139
140         PdpStatus status11 = new PdpStatus();
141         status11.setName("pdpBA_1");
142         status11.setState(PdpState.ACTIVE);
143         status11.setPdpGroup("deployPolicies");
144         status11.setPdpType(DEPLOY_SUBGROUP);
145         status11.setPdpSubgroup(DEPLOY_SUBGROUP);
146
147         final ToscaConceptIdentifier ident = new ToscaConceptIdentifier("onap.restart.tcaB", "1.0.0");
148
149         List<ToscaConceptIdentifier> idents = Arrays.asList(ident);
150         status11.setPolicies(idents);
151
152         PdpStatus status12 = new PdpStatus();
153         status12.setName("pdpBA_2");
154         status12.setState(PdpState.ACTIVE);
155         status12.setPdpGroup("deployPolicies");
156         status12.setPdpType(DEPLOY_SUBGROUP);
157         status12.setPdpSubgroup(DEPLOY_SUBGROUP);
158         status12.setPolicies(idents);
159
160         context.addPdp("pdpBA_1", DEPLOY_SUBGROUP).addReply(status11);
161         context.addPdp("pdpBA_2", DEPLOY_SUBGROUP).addReply(status12);
162         context.addPdp("pdpBB_1", "pdpTypeB");
163
164         context.startThreads();
165
166         // arrange to catch notifications
167         LinkedBlockingQueue<String> notifications = new LinkedBlockingQueue<>();
168         NoopTopicSink notifier = NoopTopicFactories.getSinkFactory().get(PapConstants.TOPIC_POLICY_NOTIFICATION);
169         notifier.register((infra, topic, msg) -> {
170             notifications.add(msg);
171         });
172
173         assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(0);
174
175         Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
176
177         PdpDeployPolicies policies = loadJsonFile("deployPoliciesReq2.json", PdpDeployPolicies.class);
178         Entity<PdpDeployPolicies> entity = Entity.entity(policies, MediaType.APPLICATION_JSON);
179         Response rawresp = invocationBuilder.post(entity);
180         PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class);
181         assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
182         assertEquals(PdpGroupDeployControllerV1.DEPLOYMENT_RESPONSE_MSG, resp.getMessage());
183         assertEquals(PdpGroupDeployControllerV1.POLICY_STATUS_URI, resp.getUri());
184         assertNull(resp.getErrorDetails());
185
186         context.await();
187
188         // wait for the notification
189         String json = notifications.poll(5, TimeUnit.SECONDS);
190         PolicyNotification notify = new StandardCoder().decode(json, PolicyNotification.class);
191         assertNotNull(notify.getAdded());
192         assertNotNull(notify.getDeleted());
193         assertTrue(notify.getDeleted().isEmpty());
194         assertEquals(1, notify.getAdded().size());
195
196         PolicyStatus added = notify.getAdded().get(0);
197         assertEquals(2, added.getSuccessCount());
198         assertEquals(0, added.getFailureCount());
199         assertEquals(0, added.getIncompleteCount());
200         assertEquals(ident, added.getPolicy());
201
202         // one of the PDPs should not have handled any requests
203         assertEquals(1, context.getPdps().stream().filter(pdp -> pdp.getHandled().isEmpty()).count());
204
205         // repeat - should be OK
206         rawresp = invocationBuilder.post(entity);
207         resp = rawresp.readEntity(PdpGroupDeployResponse.class);
208         assertEquals(Response.Status.ACCEPTED.getStatusCode(), rawresp.getStatus());
209         assertNull(resp.getErrorDetails());
210
211         assertThat(meterRegistry.counter(deploymentsCounterName, deploymentSuccessTag).count()).isEqualTo(2);
212     }
213 }