2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 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.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
29 import java.time.Instant;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.UUID;
33 import java.util.stream.Collectors;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.appc.Request;
38 import org.onap.policy.appc.Response;
39 import org.onap.policy.appc.ResponseCode;
40 import org.onap.policy.appc.ResponseStatus;
41 import org.onap.policy.appc.util.Serialization;
42 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
43 import org.onap.policy.controlloop.ControlLoopEventStatus;
44 import org.onap.policy.controlloop.ControlLoopOperation;
45 import org.onap.policy.controlloop.ControlLoopTargetType;
46 import org.onap.policy.controlloop.VirtualControlLoopEvent;
47 import org.onap.policy.controlloop.actor.test.BasicActor;
48 import org.onap.policy.controlloop.policy.Policy;
49 import org.onap.policy.controlloop.policy.Target;
50 import org.onap.policy.controlloop.policy.TargetType;
51 import org.onap.policy.simulators.Util;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class AppcActorTest extends BasicActor {
57 private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
59 private static final String MODIFY_CONFIG = "ModifyConfig";
61 private static final String JSON_OUTPUT = "JSON Output: \n";
63 private static final Logger logger = LoggerFactory.getLogger(AppcActorTest.class);
65 private static final VirtualControlLoopEvent onsetEvent;
66 private static final ControlLoopOperation operation;
67 private static final Policy policy;
69 private static final String KEY1 = "my-keyA";
70 private static final String KEY2 = "my-keyB";
71 private static final String SUBKEY = "sub-key";
73 private static final String VALUE1 = "'my-value'".replace('\'', '"');
74 private static final String VALUE2 = "{'sub-key':20}".replace('\'', '"');
75 private static final String SUBVALUE = "20";
79 * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a
82 onsetEvent = new VirtualControlLoopEvent();
83 onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
84 onsetEvent.setRequestId(UUID.randomUUID());
85 onsetEvent.setClosedLoopEventClient("tca.instance00001");
86 onsetEvent.setTargetType(ControlLoopTargetType.VNF);
87 onsetEvent.setTarget("generic-vnf.vnf-name");
88 onsetEvent.setFrom("DCAE");
89 onsetEvent.setClosedLoopAlarmStart(Instant.now());
90 onsetEvent.setAai(new HashMap<>());
91 onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
92 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
94 /* Construct an operation with an APPC actor and ModifyConfig operation. */
95 operation = new ControlLoopOperation();
96 operation.setActor("APPC");
97 operation.setOperation(MODIFY_CONFIG);
98 operation.setTarget("VNF");
99 operation.setEnd(Instant.now());
100 operation.setSubRequestId("1");
102 /* Construct a policy specifying to modify configuration. */
103 policy = new Policy();
104 policy.setName("Modify Packet Generation Config");
105 policy.setDescription("Upon getting the trigger event, modify packet gen config");
106 policy.setActor("APPC");
107 policy.setTarget(new Target(TargetType.VNF));
108 policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
109 policy.setRecipe(MODIFY_CONFIG);
110 policy.setPayload(null);
112 policy.setTimeout(300);
117 * Set up before test class.
119 * @throws Exception if the A&AI simulator cannot be started
122 public static void setUpSimulator() throws Exception {
127 * Tear down after test class.
130 public static void tearDownSimulator() {
131 HttpServletServerFactoryInstance.getServerFactory().destroy();
135 public void testConstructor() {
136 AppcActor prov = new AppcActor();
137 assertEquals(0, prov.getSequenceNumber());
139 // verify that it has the operators we expect
140 var expected = Arrays.asList(ModifyConfigOperation.NAME).stream().sorted().collect(Collectors.toList());
141 var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
143 assertEquals(expected.toString(), actual.toString());
147 public void testActorService() {
148 // verify that it all plugs into the ActorService
149 verifyActorService(AppcActor.NAME, "service.yaml");
153 public void testConstructModifyConfigRequest() {
154 policy.setPayload(new HashMap<>());
155 policy.getPayload().put(KEY1, VALUE1);
156 policy.getPayload().put(KEY2, VALUE2);
159 appcRequest = AppcActor.constructRequest(onsetEvent, operation, policy, "vnf01");
161 /* The service provider must return a non null APPC request */
162 assertNotNull(appcRequest);
164 /* A common header is required and cannot be null */
165 assertNotNull(appcRequest.getCommonHeader());
166 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
168 /* An action is required and cannot be null */
169 assertNotNull(appcRequest.getAction());
170 assertEquals(MODIFY_CONFIG, appcRequest.getAction());
172 /* A payload is required and cannot be null */
173 assertNotNull(appcRequest.getPayload());
174 assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
175 assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
176 assertTrue(appcRequest.getPayload().containsKey(KEY1));
177 assertTrue(appcRequest.getPayload().containsKey(KEY2));
179 logger.debug("APPC Request: \n" + appcRequest.toString());
181 /* Print out request as json to make sure serialization works */
182 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
183 logger.debug(JSON_OUTPUT + jsonRequest);
185 /* The JSON string must contain the following fields */
186 assertTrue(jsonRequest.contains("CommonHeader"));
187 assertTrue(jsonRequest.contains("Action"));
188 assertTrue(jsonRequest.contains(MODIFY_CONFIG));
189 assertTrue(jsonRequest.contains("Payload"));
190 assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
191 assertTrue(jsonRequest.contains(KEY1));
192 assertTrue(jsonRequest.contains(KEY2));
193 assertTrue(jsonRequest.contains(SUBKEY));
194 assertTrue(jsonRequest.contains(SUBVALUE));
195 assertFalse(jsonRequest.contains(SUBVALUE + ".0"));
197 Response appcResponse = new Response(appcRequest);
198 appcResponse.setStatus(new ResponseStatus());
199 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
200 appcResponse.getStatus().setDescription("AppC success");
201 /* Print out request as json to make sure serialization works */
202 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
203 logger.debug(JSON_OUTPUT + jsonResponse);
207 public void testConstructModifyConfigRequest_NullPayload() {
210 appcRequest = AppcActor.constructRequest(onsetEvent, operation, policy, "vnf01");
212 /* The service provider must return a non null APPC request */
213 assertNotNull(appcRequest);
215 /* A common header is required and cannot be null */
216 assertNotNull(appcRequest.getCommonHeader());
217 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
219 /* An action is required and cannot be null */
220 assertNotNull(appcRequest.getAction());
221 assertEquals(MODIFY_CONFIG, appcRequest.getAction());
223 /* A payload is required and cannot be null */
224 assertNotNull(appcRequest.getPayload());
225 assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
226 assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
228 logger.debug("APPC Request: \n" + appcRequest.toString());
230 /* Print out request as json to make sure serialization works */
231 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
232 logger.debug(JSON_OUTPUT + jsonRequest);
234 /* The JSON string must contain the following fields */
235 assertTrue(jsonRequest.contains("CommonHeader"));
236 assertTrue(jsonRequest.contains("Action"));
237 assertTrue(jsonRequest.contains(MODIFY_CONFIG));
238 assertTrue(jsonRequest.contains("Payload"));
239 assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
241 Response appcResponse = new Response(appcRequest);
242 appcResponse.setStatus(new ResponseStatus());
243 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
244 appcResponse.getStatus().setDescription("AppC success");
245 /* Print out request as json to make sure serialization works */
246 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
247 logger.debug(JSON_OUTPUT + jsonResponse);
251 public void testMethods() {
252 AppcActor sp = new AppcActor();
254 assertEquals("APPC", sp.actor());
255 assertEquals(4, sp.recipes().size());
256 assertEquals("VM", sp.recipeTargets("Restart").get(0));
257 assertEquals(0, sp.recipePayloads("Restart").size());