fc57d50ef30c46d1b0f294774ef2772c0016bf13
[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-2018 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.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.time.Instant;
30 import java.util.HashMap;
31 import java.util.UUID;
32
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.appc.Request;
37 import org.onap.policy.appc.Response;
38 import org.onap.policy.appc.ResponseCode;
39 import org.onap.policy.appc.util.Serialization;
40 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
41 import org.onap.policy.controlloop.ControlLoopEventStatus;
42 import org.onap.policy.controlloop.ControlLoopOperation;
43 import org.onap.policy.controlloop.ControlLoopTargetType;
44 import org.onap.policy.controlloop.VirtualControlLoopEvent;
45 import org.onap.policy.controlloop.policy.Policy;
46 import org.onap.policy.controlloop.policy.Target;
47 import org.onap.policy.controlloop.policy.TargetType;
48 import org.onap.policy.simulators.Util;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class AppcServiceProviderTest {
53
54     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
55
56     private static final VirtualControlLoopEvent onsetEvent;
57     private static final ControlLoopOperation operation;
58     private static final Policy policy;
59
60     static {
61         /*
62          * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
63          * VM.
64          */
65         onsetEvent = new VirtualControlLoopEvent();
66         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
67         onsetEvent.setRequestId(UUID.randomUUID());
68         onsetEvent.setClosedLoopEventClient("tca.instance00001");
69         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
70         onsetEvent.setTarget("generic-vnf.vnf-name");
71         onsetEvent.setFrom("DCAE");
72         onsetEvent.setClosedLoopAlarmStart(Instant.now());
73         onsetEvent.setAai(new HashMap<>());
74         onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
75         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
76
77         /* Construct an operation with an APPC actor and ModifyConfig operation. */
78         operation = new ControlLoopOperation();
79         operation.setActor("APPC");
80         operation.setOperation("ModifyConfig");
81         operation.setTarget("VNF");
82         operation.setEnd(Instant.now());
83         operation.setSubRequestId("1");
84
85         /* Construct a policy specifying to modify configuration. */
86         policy = new Policy();
87         policy.setName("Modify Packet Generation Config");
88         policy.setDescription("Upon getting the trigger event, modify packet gen config");
89         policy.setActor("APPC");
90         policy.setTarget(new Target(TargetType.VNF));
91         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
92         policy.setRecipe("ModifyConfig");
93         policy.setPayload(null);
94         policy.setRetry(2);
95         policy.setTimeout(300);
96
97     }
98
99     /**
100      * Set up before test class.
101      */
102     @BeforeClass
103     public static void setUpSimulator() {
104         try {
105             Util.buildAaiSim();
106         } catch (Exception e) {
107             fail(e.getMessage());
108         }
109     }
110
111     /**
112      * Tear down after test class.
113      */
114     @AfterClass
115     public static void tearDownSimulator() {
116         HttpServletServer.factory.destroy();
117     }
118
119     @Test
120     public void constructModifyConfigRequestTest() {
121
122         Request appcRequest;
123         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
124
125         /* The service provider must return a non null APPC request */
126         assertNotNull(appcRequest);
127
128         /* A common header is required and cannot be null */
129         assertNotNull(appcRequest.getCommonHeader());
130         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
131
132         /* An action is required and cannot be null */
133         assertNotNull(appcRequest.getAction());
134         assertEquals("ModifyConfig", appcRequest.getAction());
135
136         /* A payload is required and cannot be null */
137         assertNotNull(appcRequest.getPayload());
138         assertTrue(appcRequest.getPayload().containsKey("generic-vnf.vnf-id"));
139         assertNotNull(appcRequest.getPayload().get("generic-vnf.vnf-id"));
140         assertTrue(appcRequest.getPayload().containsKey("pg-streams"));
141
142         logger.debug("APPC Request: \n" + appcRequest.toString());
143
144         /* Print out request as json to make sure serialization works */
145         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
146         logger.debug("JSON Output: \n" + jsonRequest);
147
148         /* The JSON string must contain the following fields */
149         assertTrue(jsonRequest.contains("CommonHeader"));
150         assertTrue(jsonRequest.contains("Action"));
151         assertTrue(jsonRequest.contains("ModifyConfig"));
152         assertTrue(jsonRequest.contains("Payload"));
153         assertTrue(jsonRequest.contains("generic-vnf.vnf-id"));
154         assertTrue(jsonRequest.contains("pg-streams"));
155
156         Response appcResponse = new Response(appcRequest);
157         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
158         appcResponse.getStatus().setDescription("AppC success");
159         /* Print out request as json to make sure serialization works */
160         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
161         logger.debug("JSON Output: \n" + jsonResponse);
162     }
163
164     @Test
165     public void testMethods() {
166         AppcActorServiceProvider sp = new AppcActorServiceProvider();
167
168         assertEquals("APPC", sp.actor());
169         assertEquals(4, sp.recipes().size());
170         assertEquals("VM", sp.recipeTargets("Restart").get(0));
171         assertEquals(0, sp.recipePayloads("Restart").size());
172     }
173 }