7ab368f6f2411b66c187e245c26f6d76c1f8182a
[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.util.AAIException;
33 import org.onap.policy.appc.Request;
34 import org.onap.policy.appc.Response;
35 import org.onap.policy.appc.ResponseCode;
36 import org.onap.policy.appc.util.Serialization;
37 import org.onap.policy.controlloop.ControlLoopEventStatus;
38 import org.onap.policy.controlloop.ControlLoopOperation;
39 import org.onap.policy.controlloop.ControlLoopTargetType;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.policy.Policy;
42 import org.onap.policy.controlloop.policy.Target;
43 import org.onap.policy.controlloop.policy.TargetType;
44 import org.onap.policy.drools.http.server.HttpServletServer;
45 import org.onap.policy.drools.system.PolicyEngine;
46 import org.onap.policy.simulators.Util;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class AppcServiceProviderTest {
51
52     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
53     
54     private static VirtualControlLoopEvent onsetEvent;
55     private static ControlLoopOperation operation;
56     private static Policy policy;
57
58     static {
59         /* 
60          * Construct an onset with an AAI subtag containing
61          * generic-vnf.vnf-id and a target type of VM.
62          */
63         onsetEvent = new VirtualControlLoopEvent();
64         onsetEvent.closedLoopControlName = "closedLoopControlName-Test";
65         onsetEvent.requestID = UUID.randomUUID();
66         onsetEvent.closedLoopEventClient = "tca.instance00001";
67         onsetEvent.target_type = ControlLoopTargetType.VNF;
68         onsetEvent.target = "generic-vnf.vnf-id";
69         onsetEvent.from = "DCAE";
70         onsetEvent.closedLoopAlarmStart = Instant.now();
71         onsetEvent.AAI = new HashMap<>();
72         onsetEvent.AAI.put("generic-vnf.vnf-id", "fw0001vm001fw001");
73         onsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
74
75         /* Construct an operation with an APPC actor and ModifyConfig operation. */
76         operation = new ControlLoopOperation();
77         operation.actor = "APPC";
78         operation.operation = "ModifyConfig";
79         operation.target = "VNF";
80         operation.end = Instant.now();
81         operation.subRequestId = "1";
82
83         /* Construct a policy specifying to modify configuration. */
84         policy = new Policy();
85         policy.setName("Modify Packet Generation Config");
86         policy.setDescription("Upon getting the trigger event, modify packet gen config");
87         policy.setActor("APPC");
88         policy.setTarget(new Target(TargetType.VNF));
89         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
90         policy.setRecipe("ModifyConfig");
91         policy.setPayload(null);
92         policy.setRetry(2);
93         policy.setTimeout(300);
94         
95         /* Set environment properties */
96         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
97         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
98         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
99         
100     }
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     @AfterClass
112     public static void tearDownSimulator() {
113         HttpServletServer.factory.destroy();
114     }
115     
116     @Test
117     public void constructModifyConfigRequestTest() {
118         
119         Request appcRequest = null;
120                 try {
121                         appcRequest = APPCActorServiceProvider.constructRequest(onsetEvent, operation, policy);
122                 } catch (AAIException e) {
123                         logger.warn(e.toString());
124                         fail("no vnfid found");
125                 }
126         
127         /* The service provider must return a non null APPC request */
128         assertNotNull(appcRequest);
129
130         /* A common header is required and cannot be null */
131         assertNotNull(appcRequest.getCommonHeader());
132         assertEquals(appcRequest.getCommonHeader().RequestID, onsetEvent.requestID);
133
134         /* An action is required and cannot be null */
135         assertNotNull(appcRequest.Action);
136         assertEquals(appcRequest.Action, "ModifyConfig");
137
138         /* A payload is required and cannot be null */
139         assertNotNull(appcRequest.getPayload());
140         assertTrue(appcRequest.getPayload().containsKey("generic-vnf.vnf-id"));
141         assertNotNull(appcRequest.getPayload().get("generic-vnf.vnf-id"));
142         assertTrue(appcRequest.getPayload().containsKey("pg-streams"));
143
144         logger.debug("APPC Request: \n" + appcRequest.toString());
145         
146         /* Print out request as json to make sure serialization works */
147         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
148         logger.debug("JSON Output: \n" + jsonRequest);
149         
150         /* The JSON string must contain the following fields */
151         assertTrue(jsonRequest.contains("CommonHeader"));
152         assertTrue(jsonRequest.contains("Action"));
153         assertTrue(jsonRequest.contains("ModifyConfig"));
154         assertTrue(jsonRequest.contains("Payload"));
155         assertTrue(jsonRequest.contains("generic-vnf.vnf-id"));
156         assertTrue(jsonRequest.contains("pg-streams"));
157         
158         Response appcResponse = new Response(appcRequest);
159         appcResponse.getStatus().Code = ResponseCode.SUCCESS.getValue();
160         appcResponse.getStatus().Description = "AppC success";
161         /* Print out request as json to make sure serialization works */
162         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
163         logger.debug("JSON Output: \n" + jsonResponse);
164     }
165
166 }