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;
28 import java.time.Instant;
29 import java.util.HashMap;
30 import java.util.UUID;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.appc.Request;
35 import org.onap.policy.appc.Response;
36 import org.onap.policy.appc.ResponseCode;
37 import org.onap.policy.appc.util.Serialization;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.controlloop.ControlLoopEventStatus;
40 import org.onap.policy.controlloop.ControlLoopOperation;
41 import org.onap.policy.controlloop.ControlLoopTargetType;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.policy.Policy;
44 import org.onap.policy.controlloop.policy.Target;
45 import org.onap.policy.controlloop.policy.TargetType;
46 import org.onap.policy.simulators.Util;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class AppcServiceProviderTest {
52 private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
54 private static final String MODIFY_CONFIG = "ModifyConfig";
56 private static final String JSON_OUTPUT = "JSON Output: \n";
58 private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
60 private static final VirtualControlLoopEvent onsetEvent;
61 private static final ControlLoopOperation operation;
62 private static final Policy policy;
64 private static final String KEY1 = "my-keyA";
65 private static final String KEY2 = "my-keyB";
66 private static final String SUBKEY = "sub-key";
68 private static final String VALUE1 = "'my-value'".replace('\'', '"');
69 private static final String VALUE2 = "{'sub-key':20}".replace('\'', '"');
70 private static final String SUBVALUE = "20";
74 * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
77 onsetEvent = new VirtualControlLoopEvent();
78 onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
79 onsetEvent.setRequestId(UUID.randomUUID());
80 onsetEvent.setClosedLoopEventClient("tca.instance00001");
81 onsetEvent.setTargetType(ControlLoopTargetType.VNF);
82 onsetEvent.setTarget("generic-vnf.vnf-name");
83 onsetEvent.setFrom("DCAE");
84 onsetEvent.setClosedLoopAlarmStart(Instant.now());
85 onsetEvent.setAai(new HashMap<>());
86 onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
87 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
89 /* Construct an operation with an APPC actor and ModifyConfig operation. */
90 operation = new ControlLoopOperation();
91 operation.setActor("APPC");
92 operation.setOperation(MODIFY_CONFIG);
93 operation.setTarget("VNF");
94 operation.setEnd(Instant.now());
95 operation.setSubRequestId("1");
97 /* Construct a policy specifying to modify configuration. */
98 policy = new Policy();
99 policy.setName("Modify Packet Generation Config");
100 policy.setDescription("Upon getting the trigger event, modify packet gen config");
101 policy.setActor("APPC");
102 policy.setTarget(new Target(TargetType.VNF));
103 policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
104 policy.setRecipe(MODIFY_CONFIG);
105 policy.setPayload(null);
107 policy.setTimeout(300);
112 * Set up before test class.
113 * @throws Exception if the A&AI simulator cannot be started
116 public static void setUpSimulator() throws Exception {
121 * Tear down after test class.
124 public static void tearDownSimulator() {
125 HttpServletServerFactoryInstance.getServerFactory().destroy();
129 public void constructModifyConfigRequestTest() {
130 policy.setPayload(new HashMap<>());
131 policy.getPayload().put(KEY1, VALUE1);
132 policy.getPayload().put(KEY2, VALUE2);
135 appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
137 /* The service provider must return a non null APPC request */
138 assertNotNull(appcRequest);
140 /* A common header is required and cannot be null */
141 assertNotNull(appcRequest.getCommonHeader());
142 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
144 /* An action is required and cannot be null */
145 assertNotNull(appcRequest.getAction());
146 assertEquals(MODIFY_CONFIG, appcRequest.getAction());
148 /* A payload is required and cannot be null */
149 assertNotNull(appcRequest.getPayload());
150 assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
151 assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
152 assertTrue(appcRequest.getPayload().containsKey(KEY1));
153 assertTrue(appcRequest.getPayload().containsKey(KEY2));
155 logger.debug("APPC Request: \n" + appcRequest.toString());
157 /* Print out request as json to make sure serialization works */
158 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
159 logger.debug(JSON_OUTPUT + jsonRequest);
161 /* The JSON string must contain the following fields */
162 assertTrue(jsonRequest.contains("CommonHeader"));
163 assertTrue(jsonRequest.contains("Action"));
164 assertTrue(jsonRequest.contains(MODIFY_CONFIG));
165 assertTrue(jsonRequest.contains("Payload"));
166 assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
167 assertTrue(jsonRequest.contains(KEY1));
168 assertTrue(jsonRequest.contains(KEY2));
169 assertTrue(jsonRequest.contains(SUBKEY));
170 assertTrue(jsonRequest.contains(SUBVALUE));
172 Response appcResponse = new Response(appcRequest);
173 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
174 appcResponse.getStatus().setDescription("AppC success");
175 /* Print out request as json to make sure serialization works */
176 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
177 logger.debug(JSON_OUTPUT + jsonResponse);
181 public void constructModifyConfigRequestTest_NullPayload() {
184 appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
186 /* The service provider must return a non null APPC request */
187 assertNotNull(appcRequest);
189 /* A common header is required and cannot be null */
190 assertNotNull(appcRequest.getCommonHeader());
191 assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
193 /* An action is required and cannot be null */
194 assertNotNull(appcRequest.getAction());
195 assertEquals(MODIFY_CONFIG, appcRequest.getAction());
197 /* A payload is required and cannot be null */
198 assertNotNull(appcRequest.getPayload());
199 assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
200 assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
202 logger.debug("APPC Request: \n" + appcRequest.toString());
204 /* Print out request as json to make sure serialization works */
205 String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
206 logger.debug(JSON_OUTPUT + jsonRequest);
208 /* The JSON string must contain the following fields */
209 assertTrue(jsonRequest.contains("CommonHeader"));
210 assertTrue(jsonRequest.contains("Action"));
211 assertTrue(jsonRequest.contains(MODIFY_CONFIG));
212 assertTrue(jsonRequest.contains("Payload"));
213 assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
215 Response appcResponse = new Response(appcRequest);
216 appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
217 appcResponse.getStatus().setDescription("AppC success");
218 /* Print out request as json to make sure serialization works */
219 String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
220 logger.debug(JSON_OUTPUT + jsonResponse);
224 public void testMethods() {
225 AppcActorServiceProvider sp = new AppcActorServiceProvider();
227 assertEquals("APPC", sp.actor());
228 assertEquals(4, sp.recipes().size());
229 assertEquals("VM", sp.recipeTargets("Restart").get(0));
230 assertEquals(0, sp.recipePayloads("Restart").size());