policy/drools-apps jdk11 upgrades
[policy/drools-applications.git] / controlloop / m2 / appclcm / src / test / java / appclcm / AppcLcmHealthCheckOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * m2/appclcm
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package appclcm;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28
29 import java.util.Properties;
30 import java.util.UUID;
31
32 import org.drools.core.WorkingMemory;
33
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37
38 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
39 import org.onap.policy.appclcm.AppcLcmInput;
40 import org.onap.policy.appclcm.util.Serialization;
41 import org.onap.policy.controlloop.ControlLoopEventStatus;
42 import org.onap.policy.controlloop.ControlLoopException;
43 import org.onap.policy.controlloop.ControlLoopTargetType;
44 import org.onap.policy.controlloop.VirtualControlLoopEvent;
45 import org.onap.policy.controlloop.policy.Policy;
46 import org.onap.policy.controlloop.policy.PolicyResult;
47 import org.onap.policy.controlloop.policy.Target;
48 import org.onap.policy.controlloop.policy.TargetType;
49 import org.onap.policy.drools.m2.lock.LockAdjunct;
50 import org.onap.policy.drools.system.PolicyEngineConstants;
51 import org.onap.policy.m2.appclcm.AppcLcmHealthCheckOperation;
52 import org.onap.policy.m2.base.Transaction;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public class AppcLcmHealthCheckOperationTest {
57     private static Logger logger = LoggerFactory.getLogger(AppcLcmHealthCheckOperationTest.class);
58
59     public static Policy policy;
60     public static VirtualControlLoopEvent event;
61     public static Transaction transaction;
62     public static AppcLcmHealthCheckOperation operation;
63
64     /**
65      * Class-level setup.
66      */
67     @BeforeClass
68     public static void setup() {
69         PolicyEngineConstants.getManager().configure(new Properties());
70         PolicyEngineConstants.getManager().start();
71
72         policy = new Policy();
73         policy.setActor("APPCLCM");
74         policy.setTarget(new Target(TargetType.VM));
75
76         event = new VirtualControlLoopEvent();
77         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
78         event.setRequestId(UUID.randomUUID());
79         event.setTarget("vserver.vserver-name");
80         event.setTargetType(ControlLoopTargetType.VM);
81         event.getAai().put("vserver.is-closed-loop-disabled", "false");
82         event.getAai().put("complex.state", "NJ");
83         event.getAai().put("vserver.l-interface.interface-name", "89ee9ee6-1e96-4063-b690-aa5ca9f73b32");
84         event.getAai().put("vserver.l-interface.l3-interface-ipv4-address-list.l3-inteface-ipv4-address",
85             "135.144.3.49");
86         event.getAai().put("vserver.l-interface.l3-interface-ipv6-address-list.l3-inteface-ipv6-address", null);
87         event.getAai().put("vserver.in-maint", "N");
88         event.getAai().put("complex.city", "AAIDefault");
89         event.getAai().put("vserver.vserver-id", "aa7a24f9-8791-491f-b31a-c8ba5ad9e2aa");
90         event.getAai().put("vserver.l-interface.network-name", "vUSP_DPA3_OAM_3750");
91         event.getAai().put("vserver.vserver-name", "ctsf0002vm013");
92         event.getAai().put("generic-vnf.vnf-name", "ctsf0002v");
93         event.getAai().put("generic-vnf.vnf-id", "0f551f1b-e4e5-4ce2-84da-eda916e06e1c");
94         event.getAai().put("generic-vnf.service-id", "e433710f-9217-458d-a79d-1c7aff376d89");
95         event.getAai().put("vserver.selflink", "https://compute-aic.dpa3.cci.att.com:8774/v2/d0719b845a804b368f8ac0bba39e188b/servers/aa7a24f9-8791-491f-b31a-c8ba5ad9e2aa");
96         event.getAai().put("generic-vnf.vnf-type", "vUSP - vCTS");
97         event.getAai().put("tenant.tenant-id", "d0719b845a804b368f8ac0bba39e188b");
98         event.getAai().put("cloud-region.identity-url", "https://compute-aic.dpa3.cci.att.com:8774/");
99         event.getAai().put("vserver.prov-status", "PROV");
100         event.getAai().put("complex.physical-location-id", "LSLEILAA");
101
102         WorkingMemory wm = mock(WorkingMemory.class);
103         transaction = new Transaction(wm, "clvusptest", event.getRequestId(), null);
104
105     }
106
107     @AfterClass
108     public static void cleanup() {
109         transaction.cleanup();
110         PolicyEngineConstants.getManager().stop();
111     }
112
113     @Test
114     public void testGetVnfHealthCheckRequest() throws ControlLoopException {
115
116         policy.setRecipe("HEALTHCHECK");
117         policy.getTarget().setType(TargetType.VNF);
118         operation = new AppcLcmHealthCheckOperation(transaction, policy, event, 1);
119
120         Object request = operation.getRequest();
121         assertTrue(request instanceof AppcLcmDmaapWrapper);
122
123         AppcLcmDmaapWrapper dmaapRequest = (AppcLcmDmaapWrapper) request;
124         assertEquals("request", dmaapRequest.getType());
125         assertEquals("health-check", dmaapRequest.getRpcName());
126         assertNotNull(dmaapRequest.getBody());
127
128         AppcLcmInput appcRequest = dmaapRequest.getBody().getInput();
129         assertNotNull(appcRequest.getCommonHeader());
130         assertEquals("2.00", appcRequest.getCommonHeader().getApiVer());
131         assertEquals("POLICY", appcRequest.getCommonHeader().getOriginatorId());
132         assertNotNull(appcRequest.getAction());
133         assertEquals("HealthCheck", appcRequest.getAction());
134         assertNotNull(appcRequest.getActionIdentifiers());
135         assertEquals(event.getAai().get("generic-vnf.vnf-id"), appcRequest.getActionIdentifiers().get("vnf-id"));
136         assertNotNull(appcRequest.getPayload());
137         assertTrue(appcRequest.getPayload().contains("host-ip-address"));
138
139         logger.info("health-check request: {}", Serialization.gson.toJson(request, AppcLcmDmaapWrapper.class));
140
141     }
142
143     @Test
144     public void testIncomingHealthCheckMessageHealthyState() {
145         policy.setRecipe("HEALTHCHECK");
146         policy.getTarget().setType(TargetType.VNF);
147         operation = new AppcLcmHealthCheckOperation(transaction, policy, event, 1);
148
149         String lcmRespJson = "{\"body\":{\"output\":{\"common-header\":{\"timestamp\":\"2017-08-25T21:06:23.037Z\","
150             + "\"api-ver\":\"5.00\",\"originator-id\":\"POLICY\","
151             + "\"request-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200\",\"sub-request-id\":\"1\",\"flags\":{}},"
152             + "\"status\":{\"code\":400,\"message\":\"HealthCheckSuccessful\"},"
153             + "\"payload\":\"{\\\"identifier\\\":\\\"scoperepresented\\\",\\\"state\\\":\\\"healthy\\\","
154             + "\\\"time\\\":\\\"01-01-1000:0000\\\"}\"}},\"version\":\"2.0\",\"rpc-name\":\"health-check\","
155             + "\"correlation-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200-1\",\"type\":\"response\"}";
156         AppcLcmDmaapWrapper healthCheckResp = Serialization.gson.fromJson(lcmRespJson, AppcLcmDmaapWrapper.class);
157
158         operation.incomingMessage(healthCheckResp);
159         assertEquals(operation.getResult(), PolicyResult.SUCCESS);
160     }
161
162     @Test
163     public void testIncomingHealthCheckMessageUnhealthyState() {
164         policy.setRecipe("HEALTHCHECK");
165         policy.getTarget().setType(TargetType.VNF);
166         operation = new AppcLcmHealthCheckOperation(transaction, policy, event, 1);
167
168         String lcmRespJson = "{\"body\":{\"output\":{\"common-header\":{\"timestamp\":\"2017-08-25T21:06:23.037Z\","
169             + "\"api-ver\":\"5.00\",\"originator-id\":\"POLICY\","
170             + "\"request-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200\",\"sub-request-id\":\"1\",\"flags\":{}},"
171             + "\"status\":{\"code\":400,\"message\":\"VNF is unhealthy\"},"
172             + "\"payload\":\"{\\\"identifier\\\":\\\"scoperepresented\\\",\\\"state\\\":\\\"unhealthy\\\","
173             + "\\\"info\\\":\\\"Systemthresholdexceededdetails\\\",\\\"fault\\\":{\\\"cpuOverall\\\":0.80,"
174             + "\\\"cpuThreshold\\\":0.45},\\\"time\\\":\\\"01-01-1000:0000\\\"}\"}},\"version\":\"2.0\","
175             + "\"rpc-name\":\"health-check\",\"correlation-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200-1\","
176             + "\"type\":\"response\"}";
177         AppcLcmDmaapWrapper healthCheckResp = Serialization.gson.fromJson(lcmRespJson, AppcLcmDmaapWrapper.class);
178
179         operation.incomingMessage(healthCheckResp);
180         assertEquals(operation.getResult(), PolicyResult.FAILURE);
181     }
182
183     @Test
184     public void testIncomingHealthCheckMessageUnknownState() {
185         policy.setRecipe("HEALTHCHECK");
186         policy.getTarget().setType(TargetType.VNF);
187         operation = new AppcLcmHealthCheckOperation(transaction, policy, event, 1);
188
189         String lcmRespJson = "{\"body\":{\"output\":{\"common-header\":{\"timestamp\":\"2017-08-25T21:06:23.037Z\","
190             + "\"api-ver\":\"5.00\",\"originator-id\":\"POLICY\","
191             + "\"request-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200\",\"sub-request-id\":\"1\",\"flags\":{}},"
192             + "\"status\":{\"code\":400,\"message\":\"VNF is unhealthy\"},"
193             + "\"payload\":\"{\\\"identifier\\\":\\\"scoperepresented\\\",\\\"state\\\":\\\"unknown\\\","
194             + "\\\"info\\\":\\\"Systemthresholdexceededdetails\\\",\\\"fault\\\":{\\\"cpuOverall\\\":0.80,"
195             + "\\\"cpuThreshold\\\":0.45},\\\"time\\\":\\\"01-01-1000:0000\\\"}\"}},\"version\":\"2.0\","
196             + "\"rpc-name\":\"health-check\",\"correlation-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200-1\","
197             + "\"type\":\"response\"}";
198         AppcLcmDmaapWrapper healthCheckResp = Serialization.gson.fromJson(lcmRespJson, AppcLcmDmaapWrapper.class);
199
200         operation.incomingMessage(healthCheckResp);
201         assertEquals(operation.getResult(), PolicyResult.FAILURE_EXCEPTION);
202     }
203
204     @Test
205     public void testIncomingHealthCheckMessageNoState() {
206         policy.setRecipe("HEALTHCHECK");
207         policy.getTarget().setType(TargetType.VNF);
208         operation = new AppcLcmHealthCheckOperation(transaction, policy, event, 1);
209
210         String lcmRespJson = "{\"body\":{\"output\":{\"common-header\":{\"timestamp\":\"2017-08-25T21:06:23.037Z\","
211             + "\"api-ver\":\"5.00\",\"originator-id\":\"POLICY\","
212             + "\"request-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200\",\"sub-request-id\":\"1\",\"flags\":{}},"
213             + "\"status\":{\"code\":400,\"message\":\"VNF is unhealthy\"},"
214             + "\"payload\":\"{\\\"identifier\\\":\\\"scoperepresented\\\","
215             + "\\\"info\\\":\\\"Systemthresholdexceededdetails\\\",\\\"fault\\\":{\\\"cpuOverall\\\":0.80,"
216             + "\\\"cpuThreshold\\\":0.45},\\\"time\\\":\\\"01-01-1000:0000\\\"}\"}},\"version\":\"2.0\","
217             + "\"rpc-name\":\"health-check\",\"correlation-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200-1\","
218             + "\"type\":\"response\"}";
219         AppcLcmDmaapWrapper healthCheckResp = Serialization.gson.fromJson(lcmRespJson, AppcLcmDmaapWrapper.class);
220
221         operation.incomingMessage(healthCheckResp);
222         assertEquals(operation.getResult(), PolicyResult.FAILURE_EXCEPTION);
223     }
224
225     @Test
226     public void testIncomingHealthCheckMessageUnsuccessful() {
227         policy.setRecipe("HEALTHCHECK");
228         policy.getTarget().setType(TargetType.VNF);
229         operation = new AppcLcmHealthCheckOperation(transaction, policy, event, 1);
230
231         String lcmRespJson = "{\"body\":{\"output\":{\"common-header\":{\"timestamp\":\"2017-08-25T21:06:23.037Z\","
232             + "\"api-ver\":\"5.00\",\"originator-id\":\"POLICY\","
233             + "\"request-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200\",\"sub-request-id\":\"1\",\"flags\":{}},"
234             + "\"status\":{\"code\":401,\"message\":\"Could not complete HealthCheck\"},"
235             + "\"payload\":\"{\\\"identifier\\\":\\\"scoperepresented\\\","
236             + "\\\"info\\\":\\\"Systemthresholdexceededdetails\\\",\\\"fault\\\":{\\\"cpuOverall\\\":0.80,"
237             + "\\\"cpuThreshold\\\":0.45},\\\"time\\\":\\\"01-01-1000:0000\\\"}\"}},\"version\":\"2.0\","
238             + "\"rpc-name\":\"health-check\",\"correlation-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200-1\","
239             + "\"type\":\"response\"}";
240         AppcLcmDmaapWrapper healthCheckResp = Serialization.gson.fromJson(lcmRespJson, AppcLcmDmaapWrapper.class);
241
242         operation.incomingMessage(healthCheckResp);
243         assertEquals(operation.getResult(), PolicyResult.FAILURE);
244     }
245
246     @Test
247     public void testIncomingHealthCheckMessageNoPayload() {
248         policy.setRecipe("HEALTHCHECK");
249         policy.getTarget().setType(TargetType.VNF);
250         operation = new AppcLcmHealthCheckOperation(transaction, policy, event, 1);
251
252         String lcmRespJson = "{\"body\":{\"output\":{\"common-header\":{\"timestamp\":\"2017-08-25T21:06:23.037Z\","
253             + "\"api-ver\":\"5.00\",\"originator-id\":\"POLICY\","
254             + "\"request-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200\",\"sub-request-id\":\"1\",\"flags\":{}},"
255             + "\"status\":{\"code\":400,\"message\":\"VNF is unhealthy\"}}},\"version\":\"2.0\","
256             + "\"rpc-name\":\"health-check\",\"correlation-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200-1\","
257             + "\"type\":\"response\"}";
258         AppcLcmDmaapWrapper healthCheckResp = Serialization.gson.fromJson(lcmRespJson, AppcLcmDmaapWrapper.class);
259
260         operation.incomingMessage(healthCheckResp);
261         assertEquals(operation.getResult(), PolicyResult.FAILURE_EXCEPTION);
262     }
263
264     @Test
265     public void testIncomingHealthCheckMessageEmptyPayload() {
266         policy.setRecipe("HEALTHCHECK");
267         policy.getTarget().setType(TargetType.VNF);
268         operation = new AppcLcmHealthCheckOperation(transaction, policy, event, 1);
269
270         String lcmRespJson = "{\"body\":{\"output\":{\"common-header\":{\"timestamp\":\"2017-08-25T21:06:23.037Z\","
271             + "\"api-ver\":\"5.00\",\"originator-id\":\"POLICY\","
272             + "\"request-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200\",\"sub-request-id\":\"1\",\"flags\":{}},"
273             + "\"status\":{\"code\":400,\"message\":\"VNF is unhealthy\"},\"payload\":\"\"}},\"version\":\"2.0\","
274             + "\"rpc-name\":\"health-check\",\"correlation-id\":\"664be3d2-6c12-4f4b-a3e7-c349acced200-1\","
275             + "\"type\":\"response\"}";
276         AppcLcmDmaapWrapper healthCheckResp = Serialization.gson.fromJson(lcmRespJson, AppcLcmDmaapWrapper.class);
277
278         operation.incomingMessage(healthCheckResp);
279         assertEquals(operation.getResult(), PolicyResult.FAILURE_EXCEPTION);
280     }
281 }