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.HashMap;
31 import java.util.UUID;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.appc.Request;
36 import org.onap.policy.appc.Response;
37 import org.onap.policy.appc.ResponseCode;
38 import org.onap.policy.appc.util.Serialization;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
40 import org.onap.policy.controlloop.ControlLoopEventStatus;
41 import org.onap.policy.controlloop.ControlLoopOperation;
42 import org.onap.policy.controlloop.ControlLoopTargetType;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.policy.Policy;
45 import org.onap.policy.controlloop.policy.Target;
46 import org.onap.policy.controlloop.policy.TargetType;
47 import org.onap.policy.simulators.Util;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class AppcServiceProviderTest {
53 private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
55 private static final String MODIFY_CONFIG = "ModifyConfig";
57 private static final String JSON_OUTPUT = "JSON Output: \n";
59 private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
61 private static final VirtualControlLoopEvent onsetEvent;
62 private static final ControlLoopOperation operation;
63 private static final Policy policy;
65 private static final String KEY1 = "my-keyA";
66 private static final String KEY2 = "my-keyB";
67 private static final String SUBKEY = "sub-key";
69 private static final String VALUE1 = "'my-value'".replace('\'', '"');
70 private static final String VALUE2 = "{'sub-key':20}".replace('\'', '"');
71 private static final String SUBVALUE = "20";
75 * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
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);
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");
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);
108 policy.setTimeout(300);
113 * Set up before test class.
114 * @throws Exception if the A&AI simulator cannot be started
117 public static void setUpSimulator() throws Exception {
122 * Tear down after test class.
125 public static void tearDownSimulator() {
126 HttpServletServerFactoryInstance.getServerFactory().destroy();
130 public void constructModifyConfigRequestTest() {
131 policy.setPayload(new HashMap<>());
132 policy.getPayload().put(KEY1, VALUE1);
133 policy.getPayload().put(KEY2, VALUE2);
136 appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
138 /* The service provider must return a non null APPC request */
139 assertNotNull(appcRequest);
141 /* A common header is required and cannot be null */
142 assertNotNull(appcRequest.getCommonHeader());
143 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
145 /* An action is required and cannot be null */
146 assertNotNull(appcRequest.getAction());
147 assertEquals(MODIFY_CONFIG, appcRequest.getAction());
149 /* A payload is required and cannot be null */
150 assertNotNull(appcRequest.getPayload());
151 assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
152 assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
153 assertTrue(appcRequest.getPayload().containsKey(KEY1));
154 assertTrue(appcRequest.getPayload().containsKey(KEY2));
156 logger.debug("APPC Request: \n" + appcRequest.toString());
158 /* Print out request as json to make sure serialization works */
159 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
160 logger.debug(JSON_OUTPUT + jsonRequest);
162 /* The JSON string must contain the following fields */
163 assertTrue(jsonRequest.contains("CommonHeader"));
164 assertTrue(jsonRequest.contains("Action"));
165 assertTrue(jsonRequest.contains(MODIFY_CONFIG));
166 assertTrue(jsonRequest.contains("Payload"));
167 assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
168 assertTrue(jsonRequest.contains(KEY1));
169 assertTrue(jsonRequest.contains(KEY2));
170 assertTrue(jsonRequest.contains(SUBKEY));
171 assertTrue(jsonRequest.contains(SUBVALUE));
172 assertFalse(jsonRequest.contains(SUBVALUE + ".0"));
174 Response appcResponse = new Response(appcRequest);
175 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
176 appcResponse.getStatus().setDescription("AppC success");
177 /* Print out request as json to make sure serialization works */
178 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
179 logger.debug(JSON_OUTPUT + jsonResponse);
183 public void constructModifyConfigRequestTest_NullPayload() {
186 appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
188 /* The service provider must return a non null APPC request */
189 assertNotNull(appcRequest);
191 /* A common header is required and cannot be null */
192 assertNotNull(appcRequest.getCommonHeader());
193 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
195 /* An action is required and cannot be null */
196 assertNotNull(appcRequest.getAction());
197 assertEquals(MODIFY_CONFIG, appcRequest.getAction());
199 /* A payload is required and cannot be null */
200 assertNotNull(appcRequest.getPayload());
201 assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
202 assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
204 logger.debug("APPC Request: \n" + appcRequest.toString());
206 /* Print out request as json to make sure serialization works */
207 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
208 logger.debug(JSON_OUTPUT + jsonRequest);
210 /* The JSON string must contain the following fields */
211 assertTrue(jsonRequest.contains("CommonHeader"));
212 assertTrue(jsonRequest.contains("Action"));
213 assertTrue(jsonRequest.contains(MODIFY_CONFIG));
214 assertTrue(jsonRequest.contains("Payload"));
215 assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
217 Response appcResponse = new Response(appcRequest);
218 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
219 appcResponse.getStatus().setDescription("AppC success");
220 /* Print out request as json to make sure serialization works */
221 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
222 logger.debug(JSON_OUTPUT + jsonResponse);
226 public void testMethods() {
227 AppcActorServiceProvider sp = new AppcActorServiceProvider();
229 assertEquals("APPC", sp.actor());
230 assertEquals(4, sp.recipes().size());
231 assertEquals("VM", sp.recipeTargets("Restart").get(0));
232 assertEquals(0, sp.recipePayloads("Restart").size());