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.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.util.Serialization;
41 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
42 import org.onap.policy.controlloop.ControlLoopEventStatus;
43 import org.onap.policy.controlloop.ControlLoopOperation;
44 import org.onap.policy.controlloop.ControlLoopTargetType;
45 import org.onap.policy.controlloop.VirtualControlLoopEvent;
46 import org.onap.policy.controlloop.actor.test.BasicActor;
47 import org.onap.policy.controlloop.policy.Policy;
48 import org.onap.policy.controlloop.policy.Target;
49 import org.onap.policy.controlloop.policy.TargetType;
50 import org.onap.policy.simulators.Util;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class AppcServiceProviderTest extends BasicActor {
56 private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
58 private static final String MODIFY_CONFIG = "ModifyConfig";
60 private static final String JSON_OUTPUT = "JSON Output: \n";
62 private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
64 private static final VirtualControlLoopEvent onsetEvent;
65 private static final ControlLoopOperation operation;
66 private static final Policy policy;
68 private static final String KEY1 = "my-keyA";
69 private static final String KEY2 = "my-keyB";
70 private static final String SUBKEY = "sub-key";
72 private static final String VALUE1 = "'my-value'".replace('\'', '"');
73 private static final String VALUE2 = "{'sub-key':20}".replace('\'', '"');
74 private static final String SUBVALUE = "20";
78 * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a
81 onsetEvent = new VirtualControlLoopEvent();
82 onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
83 onsetEvent.setRequestId(UUID.randomUUID());
84 onsetEvent.setClosedLoopEventClient("tca.instance00001");
85 onsetEvent.setTargetType(ControlLoopTargetType.VNF);
86 onsetEvent.setTarget("generic-vnf.vnf-name");
87 onsetEvent.setFrom("DCAE");
88 onsetEvent.setClosedLoopAlarmStart(Instant.now());
89 onsetEvent.setAai(new HashMap<>());
90 onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
91 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
93 /* Construct an operation with an APPC actor and ModifyConfig operation. */
94 operation = new ControlLoopOperation();
95 operation.setActor("APPC");
96 operation.setOperation(MODIFY_CONFIG);
97 operation.setTarget("VNF");
98 operation.setEnd(Instant.now());
99 operation.setSubRequestId("1");
101 /* Construct a policy specifying to modify configuration. */
102 policy = new Policy();
103 policy.setName("Modify Packet Generation Config");
104 policy.setDescription("Upon getting the trigger event, modify packet gen config");
105 policy.setActor("APPC");
106 policy.setTarget(new Target(TargetType.VNF));
107 policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
108 policy.setRecipe(MODIFY_CONFIG);
109 policy.setPayload(null);
111 policy.setTimeout(300);
116 * Set up before test class.
118 * @throws Exception if the A&AI simulator cannot be started
121 public static void setUpSimulator() throws Exception {
126 * Tear down after test class.
129 public static void tearDownSimulator() {
130 HttpServletServerFactoryInstance.getServerFactory().destroy();
134 public void testConstructor() {
135 AppcActorServiceProvider prov = new AppcActorServiceProvider();
137 // verify that it has the operators we expect
138 var expected = Arrays.asList(ModifyConfigOperation.NAME).stream().sorted().collect(Collectors.toList());
139 var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
141 assertEquals(expected.toString(), actual.toString());
145 public void testActorService() {
146 // verify that it all plugs into the ActorService
147 verifyActorService(AppcActorServiceProvider.NAME, "service.yaml");
151 public void testConstructModifyConfigRequest() {
152 policy.setPayload(new HashMap<>());
153 policy.getPayload().put(KEY1, VALUE1);
154 policy.getPayload().put(KEY2, VALUE2);
157 appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
159 /* The service provider must return a non null APPC request */
160 assertNotNull(appcRequest);
162 /* A common header is required and cannot be null */
163 assertNotNull(appcRequest.getCommonHeader());
164 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
166 /* An action is required and cannot be null */
167 assertNotNull(appcRequest.getAction());
168 assertEquals(MODIFY_CONFIG, appcRequest.getAction());
170 /* A payload is required and cannot be null */
171 assertNotNull(appcRequest.getPayload());
172 assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
173 assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
174 assertTrue(appcRequest.getPayload().containsKey(KEY1));
175 assertTrue(appcRequest.getPayload().containsKey(KEY2));
177 logger.debug("APPC Request: \n" + appcRequest.toString());
179 /* Print out request as json to make sure serialization works */
180 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
181 logger.debug(JSON_OUTPUT + jsonRequest);
183 /* The JSON string must contain the following fields */
184 assertTrue(jsonRequest.contains("CommonHeader"));
185 assertTrue(jsonRequest.contains("Action"));
186 assertTrue(jsonRequest.contains(MODIFY_CONFIG));
187 assertTrue(jsonRequest.contains("Payload"));
188 assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
189 assertTrue(jsonRequest.contains(KEY1));
190 assertTrue(jsonRequest.contains(KEY2));
191 assertTrue(jsonRequest.contains(SUBKEY));
192 assertTrue(jsonRequest.contains(SUBVALUE));
193 assertFalse(jsonRequest.contains(SUBVALUE + ".0"));
195 Response appcResponse = new Response(appcRequest);
196 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
197 appcResponse.getStatus().setDescription("AppC success");
198 /* Print out request as json to make sure serialization works */
199 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
200 logger.debug(JSON_OUTPUT + jsonResponse);
204 public void testConstructModifyConfigRequest_NullPayload() {
207 appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
209 /* The service provider must return a non null APPC request */
210 assertNotNull(appcRequest);
212 /* A common header is required and cannot be null */
213 assertNotNull(appcRequest.getCommonHeader());
214 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
216 /* An action is required and cannot be null */
217 assertNotNull(appcRequest.getAction());
218 assertEquals(MODIFY_CONFIG, appcRequest.getAction());
220 /* A payload is required and cannot be null */
221 assertNotNull(appcRequest.getPayload());
222 assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
223 assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
225 logger.debug("APPC Request: \n" + appcRequest.toString());
227 /* Print out request as json to make sure serialization works */
228 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
229 logger.debug(JSON_OUTPUT + jsonRequest);
231 /* The JSON string must contain the following fields */
232 assertTrue(jsonRequest.contains("CommonHeader"));
233 assertTrue(jsonRequest.contains("Action"));
234 assertTrue(jsonRequest.contains(MODIFY_CONFIG));
235 assertTrue(jsonRequest.contains("Payload"));
236 assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
238 Response appcResponse = new Response(appcRequest);
239 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
240 appcResponse.getStatus().setDescription("AppC success");
241 /* Print out request as json to make sure serialization works */
242 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
243 logger.debug(JSON_OUTPUT + jsonResponse);
247 public void testMethods() {
248 AppcActorServiceProvider sp = new AppcActorServiceProvider();
250 assertEquals("APPC", sp.actor());
251 assertEquals(4, sp.recipes().size());
252 assertEquals("VM", sp.recipeTargets("Restart").get(0));
253 assertEquals(0, sp.recipePayloads("Restart").size());