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