0dfea32d68ec4e13829a0bb5c260642520754eda
[policy/models.git] / models-interactions / model-actors / actor.appc / src / test / java / org / onap / policy / controlloop / actor / appc / AppcServiceProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * AppcServiceProviderTest
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.appc;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.time.Instant;
30 import java.util.HashMap;
31 import java.util.UUID;
32
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.appc.Request;
37 import org.onap.policy.appc.Response;
38 import org.onap.policy.appc.ResponseCode;
39 import org.onap.policy.appc.util.Serialization;
40 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
41 import org.onap.policy.controlloop.ControlLoopEventStatus;
42 import org.onap.policy.controlloop.ControlLoopOperation;
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.Target;
47 import org.onap.policy.controlloop.policy.TargetType;
48 import org.onap.policy.simulators.Util;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class AppcServiceProviderTest {
53
54     private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
55
56     private static final String MODIFY_CONFIG = "ModifyConfig";
57
58     private static final String JSON_OUTPUT = "JSON Output: \n";
59
60     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
61
62     private static final VirtualControlLoopEvent onsetEvent;
63     private static final ControlLoopOperation operation;
64     private static final Policy policy;
65
66     private static final String KEY1 = "my-keyA";
67     private static final String KEY2 = "my-keyB";
68     private static final String SUBKEY = "sub-key";
69
70     private static final String VALUE1 = "'my-value'".replace('\'', '"');
71     private static final String VALUE2 = "{'sub-key':20}".replace('\'', '"');
72     private static final String SUBVALUE = "20";
73
74     static {
75         /*
76          * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of VM.
77          */
78         onsetEvent = new VirtualControlLoopEvent();
79         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
80         onsetEvent.setRequestId(UUID.randomUUID());
81         onsetEvent.setClosedLoopEventClient("tca.instance00001");
82         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
83         onsetEvent.setTarget("generic-vnf.vnf-name");
84         onsetEvent.setFrom("DCAE");
85         onsetEvent.setClosedLoopAlarmStart(Instant.now());
86         onsetEvent.setAai(new HashMap<>());
87         onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
88         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
89
90         /* Construct an operation with an APPC actor and ModifyConfig operation. */
91         operation = new ControlLoopOperation();
92         operation.setActor("APPC");
93         operation.setOperation(MODIFY_CONFIG);
94         operation.setTarget("VNF");
95         operation.setEnd(Instant.now());
96         operation.setSubRequestId("1");
97
98         /* Construct a policy specifying to modify configuration. */
99         policy = new Policy();
100         policy.setName("Modify Packet Generation Config");
101         policy.setDescription("Upon getting the trigger event, modify packet gen config");
102         policy.setActor("APPC");
103         policy.setTarget(new Target(TargetType.VNF));
104         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
105         policy.setRecipe(MODIFY_CONFIG);
106         policy.setPayload(null);
107         policy.setRetry(2);
108         policy.setTimeout(300);
109
110     }
111
112     /**
113      * Set up before test class.
114      *
115      * @throws Exception if the A&AI simulator cannot be started
116      */
117     @BeforeClass
118     public static void setUpSimulator() throws Exception {
119         Util.buildAaiSim();
120     }
121
122     /**
123      * Tear down after test class.
124      */
125     @AfterClass
126     public static void tearDownSimulator() {
127         HttpServletServerFactoryInstance.getServerFactory().destroy();
128     }
129
130     @Test
131     public void testConstructModifyConfigRequest() {
132         policy.setPayload(new HashMap<>());
133         policy.getPayload().put(KEY1, VALUE1);
134         policy.getPayload().put(KEY2, VALUE2);
135
136         Request appcRequest;
137         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
138
139         /* The service provider must return a non null APPC request */
140         assertNotNull(appcRequest);
141
142         /* A common header is required and cannot be null */
143         assertNotNull(appcRequest.getCommonHeader());
144         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
145
146         /* An action is required and cannot be null */
147         assertNotNull(appcRequest.getAction());
148         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
149
150         /* A payload is required and cannot be null */
151         assertNotNull(appcRequest.getPayload());
152         assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
153         assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
154         assertTrue(appcRequest.getPayload().containsKey(KEY1));
155         assertTrue(appcRequest.getPayload().containsKey(KEY2));
156
157         logger.debug("APPC Request: \n" + appcRequest.toString());
158
159         /* Print out request as json to make sure serialization works */
160         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
161         logger.debug(JSON_OUTPUT + jsonRequest);
162
163         /* The JSON string must contain the following fields */
164         assertTrue(jsonRequest.contains("CommonHeader"));
165         assertTrue(jsonRequest.contains("Action"));
166         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
167         assertTrue(jsonRequest.contains("Payload"));
168         assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
169         assertTrue(jsonRequest.contains(KEY1));
170         assertTrue(jsonRequest.contains(KEY2));
171         assertTrue(jsonRequest.contains(SUBKEY));
172         assertTrue(jsonRequest.contains(SUBVALUE));
173         assertFalse(jsonRequest.contains(SUBVALUE + ".0"));
174
175         Response appcResponse = new Response(appcRequest);
176         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
177         appcResponse.getStatus().setDescription("AppC success");
178         /* Print out request as json to make sure serialization works */
179         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
180         logger.debug(JSON_OUTPUT + jsonResponse);
181     }
182
183     @Test
184     public void testConstructModifyConfigRequest_NullPayload() {
185
186         Request appcRequest;
187         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
188
189         /* The service provider must return a non null APPC request */
190         assertNotNull(appcRequest);
191
192         /* A common header is required and cannot be null */
193         assertNotNull(appcRequest.getCommonHeader());
194         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
195
196         /* An action is required and cannot be null */
197         assertNotNull(appcRequest.getAction());
198         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
199
200         /* A payload is required and cannot be null */
201         assertNotNull(appcRequest.getPayload());
202         assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
203         assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
204
205         logger.debug("APPC Request: \n" + appcRequest.toString());
206
207         /* Print out request as json to make sure serialization works */
208         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
209         logger.debug(JSON_OUTPUT + jsonRequest);
210
211         /* The JSON string must contain the following fields */
212         assertTrue(jsonRequest.contains("CommonHeader"));
213         assertTrue(jsonRequest.contains("Action"));
214         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
215         assertTrue(jsonRequest.contains("Payload"));
216         assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
217
218         Response appcResponse = new Response(appcRequest);
219         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
220         appcResponse.getStatus().setDescription("AppC success");
221         /* Print out request as json to make sure serialization works */
222         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
223         logger.debug(JSON_OUTPUT + jsonResponse);
224     }
225
226     @Test
227     public void testMethods() {
228         AppcActorServiceProvider sp = new AppcActorServiceProvider();
229
230         assertEquals("APPC", sp.actor());
231         assertEquals(4, sp.recipes().size());
232         assertEquals("VM", sp.recipeTargets("Restart").get(0));
233         assertEquals(0, sp.recipePayloads("Restart").size());
234     }
235 }