223361505bef39ceb047f702242d8ed11807c8cf
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * unit test
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.eventmanager;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27
28 import java.time.Instant;
29 import java.util.HashMap;
30 import java.util.UUID;
31
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.aai.AAIGETVnfResponse;
36 import org.onap.policy.aai.AAIGETVserverResponse;
37 import org.onap.policy.aai.RelatedToProperty;
38 import org.onap.policy.aai.RelatedToPropertyItem;
39 import org.onap.policy.aai.Relationship;
40 import org.onap.policy.aai.RelationshipData;
41 import org.onap.policy.aai.RelationshipDataItem;
42 import org.onap.policy.aai.RelationshipList;
43 import org.onap.policy.controlloop.ControlLoopEventStatus;
44 import org.onap.policy.controlloop.ControlLoopException;
45 import org.onap.policy.controlloop.ControlLoopNotificationType;
46 import org.onap.policy.controlloop.Util;
47 import org.onap.policy.controlloop.VirtualControlLoopEvent;
48 import org.onap.policy.controlloop.VirtualControlLoopNotification;
49 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
50 import org.onap.policy.drools.http.server.HttpServletServer;
51 import org.onap.policy.drools.system.PolicyEngine; 
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class ControlLoopEventManagerTest {
56         private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManagerTest.class);
57         
58         private static VirtualControlLoopEvent onset;
59         static {
60                 onset = new VirtualControlLoopEvent();
61                 onset.closedLoopControlName = "ControlLoop-vUSP"; 
62                 onset.requestID = UUID.randomUUID();
63                 onset.target = "VM_NAME";
64                 onset.closedLoopAlarmStart = Instant.now();
65                 onset.AAI = new HashMap<String, String>();
66                 onset.AAI.put("cloud-region.identity-url", "foo");
67                 onset.AAI.put("vserver.selflink", "bar");
68                 onset.AAI.put("generic-vnf.vnf-id", "83f674e8-7555-44d7-9a39-bdc3770b0491");
69                 onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
70         }
71         
72         @BeforeClass
73         public static void setUpSimulator() {
74                 try {
75                         org.onap.policy.simulators.Util.buildAaiSim();
76                 } catch (Exception e) {
77                         fail(e.getMessage());
78                 }
79                 PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
80                 PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
81                 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
82         }
83
84         @AfterClass
85         public static void tearDownSimulator() {
86                 HttpServletServer.factory.destroy();
87         }
88         
89         @Test
90         public void testAAIVnfInfo() {
91                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
92                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
93                 try {                   
94                         AAIGETVnfResponse response = getQueryByVnfID2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/network/generic-vnfs/generic-vnf/", 
95                                         PolicyEngine.manager.getEnvironmentProperty("aai.username"), 
96                                         PolicyEngine.manager.getEnvironmentProperty("aai.password"), 
97                                         UUID.randomUUID(), "5e49ca06-2972-4532-9ed4-6d071588d792");
98                         assertNotNull(response);
99                         logger.info("testAAIVnfInfo test result is " + (response == null ? "null" : "not null"));
100                 } catch (Exception e) {
101                         logger.error("testAAIVnfInfo Exception: ", e);
102                         fail(e.getMessage());
103                 }
104         }
105         
106         @Test
107         public void testAAIVnfInfo2() {
108                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
109                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
110                 try {
111                         AAIGETVnfResponse response = getQueryByVnfName2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=", 
112                                         PolicyEngine.manager.getEnvironmentProperty("aai.username"), 
113                                         PolicyEngine.manager.getEnvironmentProperty("aai.password"), 
114                                         UUID.randomUUID(), "lll_vnf_010317");   
115                         assertNotNull(response);
116                         logger.info("testAAIVnfInfo2 test result is " + (response == null ? "null" : "not null"));
117                 } catch (Exception e) {
118                         logger.error("testAAIVnfInfo2 Exception: ", e);
119                         fail(e.getMessage());
120                 }
121         }
122         
123         @Test
124         public void testAAIVserver() {
125                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
126                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
127                 try {
128                         AAIGETVserverResponse response = getQueryByVserverName2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/nodes/vservers?vserver-name=", 
129                                         PolicyEngine.manager.getEnvironmentProperty("aai.username"), 
130                                         PolicyEngine.manager.getEnvironmentProperty("aai.password"), 
131                                         UUID.randomUUID(), "USMSO1SX7NJ0103UJZZ01-vjunos0");
132                         assertNotNull(response);
133                         logger.info("testAAIVserver test result is " + (response == null ? "null" : "not null"));
134                 } catch (Exception e) {
135                         logger.error("testAAIVserver Exception: ", e);
136                         fail(e.getMessage());
137                 }
138         }
139
140         @Test
141         public void testIsClosedLoopDisabled() {
142                 //
143                 // Load up the policy
144                 //
145                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
146                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
147                 
148                 try {
149                         logger.info("testIsClosedLoopDisabled --");
150                         AAIGETVnfResponse response = getQueryByVnfID2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/network/generic-vnfs/generic-vnf/", 
151                                         PolicyEngine.manager.getEnvironmentProperty("aai.username"), 
152                                         PolicyEngine.manager.getEnvironmentProperty("aai.password"), 
153                                         UUID.randomUUID(), "5e49ca06-2972-4532-9ed4-6d071588d792");
154                         assertNotNull(response);
155                         boolean disabled = ControlLoopEventManager.isClosedLoopDisabled(response);
156                         logger.info("QueryByVnfID - isClosedLoopDisabled: " + disabled); 
157
158                         response = getQueryByVnfName2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=", 
159                                         PolicyEngine.manager.getEnvironmentProperty("aai.username"), 
160                                         PolicyEngine.manager.getEnvironmentProperty("aai.password"), 
161                                         UUID.randomUUID(), "lll_vnf_010317");                   
162                         assertNotNull(response);
163                         disabled = ControlLoopEventManager.isClosedLoopDisabled(response);
164                         logger.info("QueryByVnfName - isClosedLoopDisabled: " + disabled); 
165
166                         AAIGETVserverResponse response2 = getQueryByVserverName2(PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/nodes/vservers?vserver-name=", 
167                                         PolicyEngine.manager.getEnvironmentProperty("aai.user"), 
168                                         PolicyEngine.manager.getEnvironmentProperty("aai.password"), 
169                                         UUID.randomUUID(), "USMSO1SX7NJ0103UJZZ01-vjunos0");
170                         assertNotNull(response2);
171                         disabled = ControlLoopEventManager.isClosedLoopDisabled(response2);
172                         logger.info("QueryByVserverName - isClosedLoopDisabled: " + disabled); 
173                 } catch (Exception e) {
174                         fail(e.getMessage());
175                 }
176         }
177         
178         @Test
179         public void abatemetCheckEventSyntaxTest() {
180                 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
181         event.closedLoopControlName = "abatementAAI";
182         event.requestID = UUID.randomUUID();
183         event.target = "generic-vnf.vnf-id";
184         event.closedLoopAlarmStart = Instant.now();
185         event.closedLoopEventStatus = ControlLoopEventStatus.ABATED;
186         ControlLoopEventManager manager = new ControlLoopEventManager(event.closedLoopControlName, event.requestID);
187         assertNull(manager.getVnfResponse());
188         assertNull(manager.getVserverResponse());
189         try {
190                         manager.checkEventSyntax(event);
191                 } catch (ControlLoopException e) {
192                         logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: "+e.getMessage());
193                         e.printStackTrace();
194                         fail("Exception in check event syntax");
195                 }
196         assertNull(manager.getVnfResponse());
197         assertNull(manager.getVserverResponse());
198         
199
200         event.AAI = new HashMap<>();
201         event.AAI.put("generic-vnf.vnf-name", "abatementTest");
202         try {
203                         manager.checkEventSyntax(event);
204                 } catch (ControlLoopException e) {
205                         logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: "+e.getMessage());
206                         e.printStackTrace();
207                         fail("Exception in check event syntax");
208                 }
209         assertNull(manager.getVnfResponse());
210         assertNull(manager.getVserverResponse());
211         }
212         
213         @Test
214         public void subsequentOnsetTest() {
215                 UUID requestId = UUID.randomUUID();
216                 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
217                 event.closedLoopControlName = "TwoOnsetTest";
218                 event.requestID = requestId;
219                 event.target = "generic-vnf.vnf-id";
220         event.closedLoopAlarmStart = Instant.now();
221         event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
222         event.AAI = new HashMap<>();
223         event.AAI.put("generic-vnf.vnf-name", "onsetOne");
224         
225         ControlLoopEventManager manager = new ControlLoopEventManager(event.closedLoopControlName, event.requestID);
226         VirtualControlLoopNotification notification = manager.activate(event);
227         
228         assertNotNull(notification);
229         assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
230         AAIGETVnfResponse response = manager.getVnfResponse();
231         assertNotNull(response);
232         assertNull(manager.getVserverResponse());
233         
234         VirtualControlLoopEvent event2 = new VirtualControlLoopEvent();
235                 event2.closedLoopControlName = "TwoOnsetTest";
236                 event2.requestID = requestId;
237                 event2.target = "generic-vnf.vnf-id";
238         event2.closedLoopAlarmStart = Instant.now();
239         event2.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
240         event2.AAI = new HashMap<>();
241         event2.AAI.put("generic-vnf.vnf-name", "onsetTwo");
242         
243         ControlLoopEventManager.NEW_EVENT_STATUS status = manager.onNewEvent(event2);
244         assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.SUBSEQUENT_ONSET, status);
245         AAIGETVnfResponse response2 = manager.getVnfResponse();
246         assertNotNull(response2);
247         // We should not have queried AAI, so the stored response should be the same
248         assertEquals(response, response2);
249         assertNull(manager.getVserverResponse());
250         }
251         
252         // Simulate a response 
253         public static AAIGETVnfResponse getQueryByVnfID2(String urlGet, String username, String password, UUID requestID, String key) {
254                 AAIGETVnfResponse response = new AAIGETVnfResponse();
255                 
256                 response.vnfID = "83f674e8-7555-44d7-9a39-bdc3770b0491"; 
257                 response.vnfName = "lll_vnf_010317";
258                 response.vnfType = "Basa-122216-Service/VidVsamp12BaseVolume 1";
259                 response.serviceId = "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb";
260                 response.orchestrationStatus = "Created";
261                 response.inMaint = "false";
262                 response.isClosedLoopDisabled = "false";
263                 response.resourceVersion = "1494001988835";
264                 response.modelInvariantId = "f18be3cd-d446-456e-9109-121d9b62feaa";
265                   
266         RelationshipList relationshipList = new RelationshipList();
267         Relationship     relationship = new Relationship();
268         RelationshipData relationshipData = new RelationshipData();
269         RelationshipDataItem relationshipDataItem = new RelationshipDataItem();
270         
271         relationshipDataItem.relationshipKey   = "customer.global-customer-id"; 
272         relationshipDataItem.relationshipValue = "MSO_1610_ST"; 
273         relationshipData.relationshipData.add(relationshipDataItem);
274         
275         relationshipDataItem.relationshipKey   = "service-subscription.service-type"; 
276         relationshipDataItem.relationshipValue = "MSO-dev-service-type"; 
277         relationshipData.relationshipData.add(relationshipDataItem);
278         
279         relationshipDataItem.relationshipKey   = "service-instance.service-instance-id"; 
280         relationshipDataItem.relationshipValue = "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"; 
281         relationshipData.relationshipData.add(relationshipDataItem);
282         
283         RelatedToProperty relatedToProperty = new RelatedToProperty();
284         RelatedToPropertyItem item = new RelatedToPropertyItem();  
285         item.propertyKey = "service-instance.service-instance-name"; 
286         item.propertyValue = "lll_svc_010317"; 
287         relatedToProperty.relatedTo.add(item);
288         
289         relationship.relatedTo = "service-instance";
290         relationship.relatedLink = "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription/MSO-dev-service-type/service-instances/service-instance/e1e9c97c-02c0-4919-9b4c-eb5d5ef68970";
291         relationship.relationshipData = relationshipData;
292         relationship.relatedToProperty = relatedToProperty;
293        
294         relationshipList.relationshipList.add(relationship);
295         response.relationshipList = relationshipList; 
296
297                 return response;
298         }
299
300         public static AAIGETVnfResponse getQueryByVnfName2(String urlGet, String username, String password, UUID requestID, String key) {
301                 AAIGETVnfResponse response = new AAIGETVnfResponse();
302                 
303                 response.vnfID = "83f674e8-7555-44d7-9a39-bdc3770b0491"; 
304                 response.vnfName = "lll_vnf_010317";
305                 response.vnfType = "Basa-122216-Service/VidVsamp12BaseVolume 1";
306                 response.serviceId = "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb";
307                 response.orchestrationStatus = "Created";
308                 response.inMaint = "false";
309                 response.isClosedLoopDisabled = "false";
310                 response.resourceVersion = "1494001988835";
311                 response.modelInvariantId = "f18be3cd-d446-456e-9109-121d9b62feaa";
312                   
313         RelationshipList relationshipList = new RelationshipList();
314         Relationship     relationship = new Relationship();
315         RelationshipData relationshipData = new RelationshipData();
316         RelationshipDataItem relationshipDataItem = new RelationshipDataItem();
317         
318         relationshipDataItem.relationshipKey   = "customer.global-customer-id"; 
319         relationshipDataItem.relationshipValue = "MSO_1610_ST"; 
320         relationshipData.relationshipData.add(relationshipDataItem);
321         
322         relationshipDataItem.relationshipKey   = "service-subscription.service-type"; 
323         relationshipDataItem.relationshipValue = "MSO-dev-service-type"; 
324         relationshipData.relationshipData.add(relationshipDataItem);
325         
326         relationshipDataItem.relationshipKey   = "service-instance.service-instance-id"; 
327         relationshipDataItem.relationshipValue = "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"; 
328         relationshipData.relationshipData.add(relationshipDataItem);
329         
330         RelatedToProperty relatedToProperty = new RelatedToProperty();
331         RelatedToPropertyItem item = new RelatedToPropertyItem();  
332         item.propertyKey = "service-instance.service-instance-name"; 
333         item.propertyValue = "lll_svc_010317"; 
334         relatedToProperty.relatedTo.add(item);
335         
336         relationship.relatedTo = "service-instance";
337         relationship.relatedLink = "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription/MSO-dev-service-type/service-instances/service-instance/e1e9c97c-02c0-4919-9b4c-eb5d5ef68970";
338         relationship.relationshipData = relationshipData;
339         relationship.relatedToProperty = relatedToProperty;
340        
341         relationshipList.relationshipList.add(relationship);
342         response.relationshipList = relationshipList; 
343
344                 return response;
345         }
346
347         public static AAIGETVserverResponse getQueryByVserverName2(String urlGet, String username, String password, UUID requestID, String key) {
348                 AAIGETVserverResponse response = new AAIGETVserverResponse();
349                 
350                 response.vserverID = "d0668d4f-c25e-4a1b-87c4-83845c01efd8"; 
351                 response.vserverName = "USMSO1SX7NJ0103UJZZ01-vjunos0"; 
352                 response.vserverName2 = "vjunos0"; 
353                 response.vserverSelflink = "https://aai-ext1.test.att.com:8443/aai/v7/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/USMSO1SX7NJ0103UJZZ01%3A%3AuCPE-VMS/vservers/vserver/d0668d4f-c25e-4a1b-87c4-83845c01efd8"; 
354                 response.inMaint = "false"; 
355                 response.isClosedLoopDisabled = "false"; 
356                 response.resourceVersion = "1494001931513"; 
357                   
358         RelationshipList relationshipList = new RelationshipList();
359         Relationship     relationship = new Relationship();
360         RelationshipData relationshipData = new RelationshipData();
361         RelationshipDataItem relationshipDataItem = new RelationshipDataItem();
362         
363         relationshipDataItem.relationshipKey   = "customer.global-customer-id"; 
364         relationshipDataItem.relationshipValue = "MSO_1610_ST"; 
365         relationshipData.relationshipData.add(relationshipDataItem);
366         
367         relationshipDataItem.relationshipKey   = "service-subscription.service-type"; 
368         relationshipDataItem.relationshipValue = "MSO-dev-service-type"; 
369         relationshipData.relationshipData.add(relationshipDataItem);
370         
371         relationshipDataItem.relationshipKey   = "service-instance.service-instance-id"; 
372         relationshipDataItem.relationshipValue = "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"; 
373         relationshipData.relationshipData.add(relationshipDataItem);
374         
375         RelatedToProperty relatedToProperty = new RelatedToProperty();
376         RelatedToPropertyItem item = new RelatedToPropertyItem();  
377         item.propertyKey = "service-instance.service-instance-name"; 
378         item.propertyValue = "lll_svc_010317"; 
379         relatedToProperty.relatedTo.add(item);
380         
381         relationship.relatedTo = "service-instance";
382         relationship.relatedLink = "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription/MSO-dev-service-type/service-instances/service-instance/e1e9c97c-02c0-4919-9b4c-eb5d5ef68970";
383         relationship.relationshipData = relationshipData;
384         relationship.relatedToProperty = relatedToProperty;
385        
386         relationshipList.relationshipList.add(relationship);
387         response.relationshipList = relationshipList; 
388
389                 return response;
390         }
391 }