APPC Actor
[policy/models.git] / models-interactions / model-actors / actor.appc / src / test / java / org / onap / policy / controlloop / actor / appc / AppcServiceProviderTest.java
1 /*-
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.appc;
23
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;
28
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.policy.Policy;
47 import org.onap.policy.controlloop.policy.Target;
48 import org.onap.policy.controlloop.policy.TargetType;
49 import org.onap.policy.simulators.Util;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class AppcServiceProviderTest {
54
55     private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
56
57     private static final String MODIFY_CONFIG = "ModifyConfig";
58
59     private static final String JSON_OUTPUT = "JSON Output: \n";
60
61     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
62
63     private static final VirtualControlLoopEvent onsetEvent;
64     private static final ControlLoopOperation operation;
65     private static final Policy policy;
66
67     private static final String KEY1 = "my-keyA";
68     private static final String KEY2 = "my-keyB";
69     private static final String SUBKEY = "sub-key";
70
71     private static final String VALUE1 = "'my-value'".replace('\'', '"');
72     private static final String VALUE2 = "{'sub-key':20}".replace('\'', '"');
73     private static final String SUBVALUE = "20";
74
75     static {
76         /*
77          * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of VM.
78          */
79         onsetEvent = new VirtualControlLoopEvent();
80         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
81         onsetEvent.setRequestId(UUID.randomUUID());
82         onsetEvent.setClosedLoopEventClient("tca.instance00001");
83         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
84         onsetEvent.setTarget("generic-vnf.vnf-name");
85         onsetEvent.setFrom("DCAE");
86         onsetEvent.setClosedLoopAlarmStart(Instant.now());
87         onsetEvent.setAai(new HashMap<>());
88         onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
89         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
90
91         /* Construct an operation with an APPC actor and ModifyConfig operation. */
92         operation = new ControlLoopOperation();
93         operation.setActor("APPC");
94         operation.setOperation(MODIFY_CONFIG);
95         operation.setTarget("VNF");
96         operation.setEnd(Instant.now());
97         operation.setSubRequestId("1");
98
99         /* Construct a policy specifying to modify configuration. */
100         policy = new Policy();
101         policy.setName("Modify Packet Generation Config");
102         policy.setDescription("Upon getting the trigger event, modify packet gen config");
103         policy.setActor("APPC");
104         policy.setTarget(new Target(TargetType.VNF));
105         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
106         policy.setRecipe(MODIFY_CONFIG);
107         policy.setPayload(null);
108         policy.setRetry(2);
109         policy.setTimeout(300);
110
111     }
112
113     /**
114      * Set up before test class.
115      *
116      * @throws Exception if the A&AI simulator cannot be started
117      */
118     @BeforeClass
119     public static void setUpSimulator() throws Exception {
120         Util.buildAaiSim();
121     }
122
123     /**
124      * Tear down after test class.
125      */
126     @AfterClass
127     public static void tearDownSimulator() {
128         HttpServletServerFactoryInstance.getServerFactory().destroy();
129     }
130
131     @Test
132     public void testConstructor() {
133         AppcActorServiceProvider prov = new AppcActorServiceProvider();
134
135         // verify that it has the operators we expect
136         var expected = Arrays.asList(ModifyConfigOperation.NAME).stream().sorted().collect(Collectors.toList());
137         var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
138
139         assertEquals(expected.toString(), actual.toString());
140     }
141
142     @Test
143     public void testConstructModifyConfigRequest() {
144         policy.setPayload(new HashMap<>());
145         policy.getPayload().put(KEY1, VALUE1);
146         policy.getPayload().put(KEY2, VALUE2);
147
148         Request appcRequest;
149         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
150
151         /* The service provider must return a non null APPC request */
152         assertNotNull(appcRequest);
153
154         /* A common header is required and cannot be null */
155         assertNotNull(appcRequest.getCommonHeader());
156         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
157
158         /* An action is required and cannot be null */
159         assertNotNull(appcRequest.getAction());
160         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
161
162         /* A payload is required and cannot be null */
163         assertNotNull(appcRequest.getPayload());
164         assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
165         assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
166         assertTrue(appcRequest.getPayload().containsKey(KEY1));
167         assertTrue(appcRequest.getPayload().containsKey(KEY2));
168
169         logger.debug("APPC Request: \n" + appcRequest.toString());
170
171         /* Print out request as json to make sure serialization works */
172         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
173         logger.debug(JSON_OUTPUT + jsonRequest);
174
175         /* The JSON string must contain the following fields */
176         assertTrue(jsonRequest.contains("CommonHeader"));
177         assertTrue(jsonRequest.contains("Action"));
178         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
179         assertTrue(jsonRequest.contains("Payload"));
180         assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
181         assertTrue(jsonRequest.contains(KEY1));
182         assertTrue(jsonRequest.contains(KEY2));
183         assertTrue(jsonRequest.contains(SUBKEY));
184         assertTrue(jsonRequest.contains(SUBVALUE));
185         assertFalse(jsonRequest.contains(SUBVALUE + ".0"));
186
187         Response appcResponse = new Response(appcRequest);
188         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
189         appcResponse.getStatus().setDescription("AppC success");
190         /* Print out request as json to make sure serialization works */
191         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
192         logger.debug(JSON_OUTPUT + jsonResponse);
193     }
194
195     @Test
196     public void testConstructModifyConfigRequest_NullPayload() {
197
198         Request appcRequest;
199         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
200
201         /* The service provider must return a non null APPC request */
202         assertNotNull(appcRequest);
203
204         /* A common header is required and cannot be null */
205         assertNotNull(appcRequest.getCommonHeader());
206         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
207
208         /* An action is required and cannot be null */
209         assertNotNull(appcRequest.getAction());
210         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
211
212         /* A payload is required and cannot be null */
213         assertNotNull(appcRequest.getPayload());
214         assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
215         assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
216
217         logger.debug("APPC Request: \n" + appcRequest.toString());
218
219         /* Print out request as json to make sure serialization works */
220         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
221         logger.debug(JSON_OUTPUT + jsonRequest);
222
223         /* The JSON string must contain the following fields */
224         assertTrue(jsonRequest.contains("CommonHeader"));
225         assertTrue(jsonRequest.contains("Action"));
226         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
227         assertTrue(jsonRequest.contains("Payload"));
228         assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
229
230         Response appcResponse = new Response(appcRequest);
231         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
232         appcResponse.getStatus().setDescription("AppC success");
233         /* Print out request as json to make sure serialization works */
234         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
235         logger.debug(JSON_OUTPUT + jsonResponse);
236     }
237
238     @Test
239     public void testMethods() {
240         AppcActorServiceProvider sp = new AppcActorServiceProvider();
241
242         assertEquals("APPC", sp.actor());
243         assertEquals(4, sp.recipes().size());
244         assertEquals("VM", sp.recipeTargets("Restart").get(0));
245         assertEquals(0, sp.recipePayloads("Restart").size());
246     }
247 }