Use new addTopic() method in models
[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.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;
50
51 public class AppcServiceProviderTest {
52
53     private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
54
55     private static final String MODIFY_CONFIG = "ModifyConfig";
56
57     private static final String JSON_OUTPUT = "JSON Output: \n";
58
59     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
60
61     private static final VirtualControlLoopEvent onsetEvent;
62     private static final ControlLoopOperation operation;
63     private static final Policy policy;
64
65     private static final String KEY1 = "my-keyA";
66     private static final String KEY2 = "my-keyB";
67     private static final String SUBKEY = "sub-key";
68
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";
72
73     static {
74         /*
75          * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
76          * VM.
77          */
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);
89
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");
97
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);
107         policy.setRetry(2);
108         policy.setTimeout(300);
109
110     }
111
112     /**
113      * Set up before test class.
114      * @throws Exception if the A&AI simulator cannot be started
115      */
116     @BeforeClass
117     public static void setUpSimulator() throws Exception {
118         Util.buildAaiSim();
119     }
120
121     /**
122      * Tear down after test class.
123      */
124     @AfterClass
125     public static void tearDownSimulator() {
126         HttpServletServerFactoryInstance.getServerFactory().destroy();
127     }
128
129     @Test
130     public void constructModifyConfigRequestTest() {
131         policy.setPayload(new HashMap<>());
132         policy.getPayload().put(KEY1, VALUE1);
133         policy.getPayload().put(KEY2, VALUE2);
134
135         Request appcRequest;
136         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
137
138         /* The service provider must return a non null APPC request */
139         assertNotNull(appcRequest);
140
141         /* A common header is required and cannot be null */
142         assertNotNull(appcRequest.getCommonHeader());
143         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
144
145         /* An action is required and cannot be null */
146         assertNotNull(appcRequest.getAction());
147         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
148
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));
155
156         logger.debug("APPC Request: \n" + appcRequest.toString());
157
158         /* Print out request as json to make sure serialization works */
159         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
160         logger.debug(JSON_OUTPUT + jsonRequest);
161
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"));
173
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);
180     }
181
182     @Test
183     public void constructModifyConfigRequestTest_NullPayload() {
184
185         Request appcRequest;
186         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
187
188         /* The service provider must return a non null APPC request */
189         assertNotNull(appcRequest);
190
191         /* A common header is required and cannot be null */
192         assertNotNull(appcRequest.getCommonHeader());
193         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
194
195         /* An action is required and cannot be null */
196         assertNotNull(appcRequest.getAction());
197         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
198
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));
203
204         logger.debug("APPC Request: \n" + appcRequest.toString());
205
206         /* Print out request as json to make sure serialization works */
207         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
208         logger.debug(JSON_OUTPUT + jsonRequest);
209
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));
216
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);
223     }
224
225     @Test
226     public void testMethods() {
227         AppcActorServiceProvider sp = new AppcActorServiceProvider();
228
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());
233     }
234 }