5a107f814b61b36166d4f90639423eed77ea4e3f
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * AppcServiceProviderTest
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.appc;
22
23 import static org.junit.Assert.*;
24
25 import java.time.Instant;
26 import java.util.HashMap;
27 import java.util.UUID;
28
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.policy.aai.AAIGETVnfResponse;
33 import org.onap.policy.aai.util.AAIException;
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.controlloop.ControlLoopEventStatus;
39 import org.onap.policy.controlloop.ControlLoopOperation;
40 import org.onap.policy.controlloop.ControlLoopTargetType;
41 import org.onap.policy.controlloop.VirtualControlLoopEvent;
42 import org.onap.policy.controlloop.policy.Policy;
43 import org.onap.policy.controlloop.policy.Target;
44 import org.onap.policy.controlloop.policy.TargetType;
45 import org.onap.policy.drools.http.server.HttpServletServer;
46 import org.onap.policy.drools.system.PolicyEngine;
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 Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
54     
55     private static VirtualControlLoopEvent onsetEvent;
56     private static ControlLoopOperation operation;
57     private static Policy policy;
58     private static AAIGETVnfResponse aaiResponse;
59
60     static {
61         /* 
62          * Construct an onset with an AAI subtag containing
63          * generic-vnf.vnf-id and a target type of VM.
64          */
65         onsetEvent = new VirtualControlLoopEvent();
66         onsetEvent.closedLoopControlName = "closedLoopControlName-Test";
67         onsetEvent.requestID = UUID.randomUUID();
68         onsetEvent.closedLoopEventClient = "tca.instance00001";
69         onsetEvent.target_type = ControlLoopTargetType.VNF;
70         onsetEvent.target = "generic-vnf.vnf-name";
71         onsetEvent.from = "DCAE";
72         onsetEvent.closedLoopAlarmStart = Instant.now();
73         onsetEvent.AAI = new HashMap<>();
74         onsetEvent.AAI.put("generic-vnf.vnf-name", "fw0001vm001fw001");
75         onsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
76
77         /* Construct an operation with an APPC actor and ModifyConfig operation. */
78         operation = new ControlLoopOperation();
79         operation.actor = "APPC";
80         operation.operation = "ModifyConfig";
81         operation.target = "VNF";
82         operation.end = Instant.now();
83         operation.subRequestId = "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         /* Construct a mock A&AI response */
98         aaiResponse = new AAIGETVnfResponse();
99         aaiResponse.vnfID = "vnf01";
100         
101         /* Set environment properties */
102         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
103         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
104         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
105         
106     }
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     @AfterClass
118     public static void tearDownSimulator() {
119         HttpServletServer.factory.destroy();
120     }
121     
122     @Test
123     public void constructModifyConfigRequestTest() {
124         
125         Request appcRequest = null;
126                 try {
127                         appcRequest = APPCActorServiceProvider.constructRequest(onsetEvent, operation, policy, aaiResponse);
128                 } catch (AAIException e) {
129                         logger.warn(e.toString());
130                         fail("no vnfid found");
131                 }
132         
133         /* The service provider must return a non null APPC request */
134         assertNotNull(appcRequest);
135
136         /* A common header is required and cannot be null */
137         assertNotNull(appcRequest.getCommonHeader());
138         assertEquals(appcRequest.getCommonHeader().RequestID, onsetEvent.requestID);
139
140         /* An action is required and cannot be null */
141         assertNotNull(appcRequest.Action);
142         assertEquals(appcRequest.Action, "ModifyConfig");
143
144         /* A payload is required and cannot be null */
145         assertNotNull(appcRequest.getPayload());
146         assertTrue(appcRequest.getPayload().containsKey("generic-vnf.vnf-id"));
147         assertNotNull(appcRequest.getPayload().get("generic-vnf.vnf-id"));
148         assertTrue(appcRequest.getPayload().containsKey("pg-streams"));
149
150         logger.debug("APPC Request: \n" + appcRequest.toString());
151         
152         /* Print out request as json to make sure serialization works */
153         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
154         logger.debug("JSON Output: \n" + jsonRequest);
155         
156         /* The JSON string must contain the following fields */
157         assertTrue(jsonRequest.contains("CommonHeader"));
158         assertTrue(jsonRequest.contains("Action"));
159         assertTrue(jsonRequest.contains("ModifyConfig"));
160         assertTrue(jsonRequest.contains("Payload"));
161         assertTrue(jsonRequest.contains("generic-vnf.vnf-id"));
162         assertTrue(jsonRequest.contains("pg-streams"));
163         
164         Response appcResponse = new Response(appcRequest);
165         appcResponse.getStatus().Code = ResponseCode.SUCCESS.getValue();
166         appcResponse.getStatus().Description = "AppC success";
167         /* Print out request as json to make sure serialization works */
168         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
169         logger.debug("JSON Output: \n" + jsonResponse);
170     }
171
172 }