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
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.controlloop.actor.appc;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.time.Instant;
30 import java.util.HashMap;
31 import java.util.UUID;
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.HttpServletServer;
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;
52 public class AppcServiceProviderTest {
54 private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
56 private static final VirtualControlLoopEvent onsetEvent;
57 private static final ControlLoopOperation operation;
58 private static final Policy policy;
60 private static final String KEY1 = "my-keyA";
61 private static final String KEY2 = "my-keyB";
62 private static final String SUBKEY = "sub-key";
64 private static final String VALUE1 = "'my-value'".replace('\'', '"');
65 private static final String VALUE2 = "{'sub-key':20}".replace('\'', '"');
66 private static final String SUBVALUE = "20";
70 * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
73 onsetEvent = new VirtualControlLoopEvent();
74 onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
75 onsetEvent.setRequestId(UUID.randomUUID());
76 onsetEvent.setClosedLoopEventClient("tca.instance00001");
77 onsetEvent.setTargetType(ControlLoopTargetType.VNF);
78 onsetEvent.setTarget("generic-vnf.vnf-name");
79 onsetEvent.setFrom("DCAE");
80 onsetEvent.setClosedLoopAlarmStart(Instant.now());
81 onsetEvent.setAai(new HashMap<>());
82 onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
83 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
85 /* Construct an operation with an APPC actor and ModifyConfig operation. */
86 operation = new ControlLoopOperation();
87 operation.setActor("APPC");
88 operation.setOperation("ModifyConfig");
89 operation.setTarget("VNF");
90 operation.setEnd(Instant.now());
91 operation.setSubRequestId("1");
93 /* Construct a policy specifying to modify configuration. */
94 policy = new Policy();
95 policy.setName("Modify Packet Generation Config");
96 policy.setDescription("Upon getting the trigger event, modify packet gen config");
97 policy.setActor("APPC");
98 policy.setTarget(new Target(TargetType.VNF));
99 policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
100 policy.setRecipe("ModifyConfig");
101 policy.setPayload(null);
103 policy.setTimeout(300);
108 * Set up before test class.
111 public static void setUpSimulator() {
114 } catch (Exception e) {
115 fail(e.getMessage());
120 * Tear down after test class.
123 public static void tearDownSimulator() {
124 HttpServletServer.factory.destroy();
128 public void constructModifyConfigRequestTest() {
129 policy.setPayload(new HashMap<>());
130 policy.getPayload().put(KEY1, VALUE1);
131 policy.getPayload().put(KEY2, VALUE2);
134 appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
136 /* The service provider must return a non null APPC request */
137 assertNotNull(appcRequest);
139 /* A common header is required and cannot be null */
140 assertNotNull(appcRequest.getCommonHeader());
141 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
143 /* An action is required and cannot be null */
144 assertNotNull(appcRequest.getAction());
145 assertEquals("ModifyConfig", appcRequest.getAction());
147 /* A payload is required and cannot be null */
148 assertNotNull(appcRequest.getPayload());
149 assertTrue(appcRequest.getPayload().containsKey("generic-vnf.vnf-id"));
150 assertNotNull(appcRequest.getPayload().get("generic-vnf.vnf-id"));
151 assertTrue(appcRequest.getPayload().containsKey(KEY1));
152 assertTrue(appcRequest.getPayload().containsKey(KEY2));
154 logger.debug("APPC Request: \n" + appcRequest.toString());
156 /* Print out request as json to make sure serialization works */
157 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
158 logger.debug("JSON Output: \n" + jsonRequest);
160 /* The JSON string must contain the following fields */
161 assertTrue(jsonRequest.contains("CommonHeader"));
162 assertTrue(jsonRequest.contains("Action"));
163 assertTrue(jsonRequest.contains("ModifyConfig"));
164 assertTrue(jsonRequest.contains("Payload"));
165 assertTrue(jsonRequest.contains("generic-vnf.vnf-id"));
166 assertTrue(jsonRequest.contains(KEY1));
167 assertTrue(jsonRequest.contains(KEY2));
168 assertTrue(jsonRequest.contains(SUBKEY));
169 assertTrue(jsonRequest.contains(SUBVALUE));
171 Response appcResponse = new Response(appcRequest);
172 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
173 appcResponse.getStatus().setDescription("AppC success");
174 /* Print out request as json to make sure serialization works */
175 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
176 logger.debug("JSON Output: \n" + jsonResponse);
180 public void constructModifyConfigRequestTest_NullPayload() {
183 appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
185 /* The service provider must return a non null APPC request */
186 assertNotNull(appcRequest);
188 /* A common header is required and cannot be null */
189 assertNotNull(appcRequest.getCommonHeader());
190 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
192 /* An action is required and cannot be null */
193 assertNotNull(appcRequest.getAction());
194 assertEquals("ModifyConfig", appcRequest.getAction());
196 /* A payload is required and cannot be null */
197 assertNotNull(appcRequest.getPayload());
198 assertTrue(appcRequest.getPayload().containsKey("generic-vnf.vnf-id"));
199 assertNotNull(appcRequest.getPayload().get("generic-vnf.vnf-id"));
201 logger.debug("APPC Request: \n" + appcRequest.toString());
203 /* Print out request as json to make sure serialization works */
204 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
205 logger.debug("JSON Output: \n" + jsonRequest);
207 /* The JSON string must contain the following fields */
208 assertTrue(jsonRequest.contains("CommonHeader"));
209 assertTrue(jsonRequest.contains("Action"));
210 assertTrue(jsonRequest.contains("ModifyConfig"));
211 assertTrue(jsonRequest.contains("Payload"));
212 assertTrue(jsonRequest.contains("generic-vnf.vnf-id"));
214 Response appcResponse = new Response(appcRequest);
215 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
216 appcResponse.getStatus().setDescription("AppC success");
217 /* Print out request as json to make sure serialization works */
218 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
219 logger.debug("JSON Output: \n" + jsonResponse);
223 public void testMethods() {
224 AppcActorServiceProvider sp = new AppcActorServiceProvider();
226 assertEquals("APPC", sp.actor());
227 assertEquals(4, sp.recipes().size());
228 assertEquals("VM", sp.recipeTargets("Restart").get(0));
229 assertEquals(0, sp.recipePayloads("Restart").size());