Merge "ONAP code for AAI, consolidate name and get query"
[policy/drools-applications.git] / controlloop / common / eventmanager / src / test / java / org / onap / policy / controlloop / eventmanager / ControlLoopEventManagerTest.java
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.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.time.Instant;
29 import java.util.HashMap;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.UUID;
33
34 import org.junit.Test;
35 import org.onap.policy.aai.AAIGETVserverResponse;
36 import org.onap.policy.aai.AAIGETVnfResponse;
37 import org.onap.policy.aai.RelatedToPropertyItem;
38 import org.onap.policy.aai.Relationship;
39 import org.onap.policy.aai.RelationshipData;
40 import org.onap.policy.aai.RelationshipDataItem;
41 import org.onap.policy.aai.RelationshipList;
42 import org.onap.policy.aai.AAIManager;
43 import org.onap.policy.aai.RelatedToProperty;
44 import org.onap.policy.appc.Request;
45 import org.onap.policy.appc.Response;
46 import org.onap.policy.appc.ResponseCode;
47 import org.onap.policy.appc.ResponseValue;
48 import org.onap.policy.controlloop.ControlLoopEventStatus;
49
50 import org.onap.policy.controlloop.VirtualControlLoopEvent;
51 import org.onap.policy.controlloop.ControlLoopException;
52 import org.onap.policy.controlloop.Util;
53 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
54 import org.onap.policy.controlloop.policy.PolicyResult;
55 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
56 import org.onap.policy.controlloop.processor.ControlLoopProcessorTest;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 public class ControlLoopEventManagerTest {
61         private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManagerTest.class);
62         
63         private static VirtualControlLoopEvent onset;
64         static {
65                 onset = new VirtualControlLoopEvent();
66                 onset.closedLoopControlName = "ControlLoop-vUSP"; 
67                 onset.requestID = UUID.randomUUID();
68                 onset.target = "VM_NAME";
69                 onset.closedLoopAlarmStart = Instant.now();
70                 onset.AAI = new HashMap<String, String>();
71                 onset.AAI.put("cloud-region.identity-url", "foo");
72                 onset.AAI.put("vserver.selflink", "bar");
73                 onset.AAI.put("generic-vnf.vnf-id", "83f674e8-7555-44d7-9a39-bdc3770b0491");
74                 onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
75         }
76         
77         @Test
78         public void testGetAAIInfo() {
79                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
80                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
81                 try {
82                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
83                         onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET; 
84                          
85                         String user = "POLICY";
86                         String password = "POLICY";
87                         String vnfID = "83f674e8-7555-44d7-9a39-bdc3770b0491";
88                         String url = "https://aai-ext1.test.att.com:8443/aai/v11/network/generic-vnfs/generic-vnf/";                    
89                         AAIGETVnfResponse response = getQueryByVnfID2(url, user, password, onset.requestID, vnfID);  
90                         assertNotNull(response);
91                         logger.info("testGetAAIInfo test result is " + (response == null ? "null" : "not null"));
92                 } catch (Exception e) {
93                         fail(e.getMessage());
94                         logger.error("testGetAAIInfo Exception: ", e);
95                 }
96         }
97
98         @Test
99         public void testIsClosedLoopDisabled() {
100                 //
101                 // Load up the policy
102                 //
103                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
104                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
105                 
106                 try {
107                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
108                         onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET; 
109                         
110                         logger.info("testIsClosedLoopDisabled --");
111                         AAIManager manager = new AAIManager(); 
112                         String user = "POLICY";
113                         String password = "POLICY";
114                         String vnfID = "83f674e8-7555-44d7-9a39-bdc3770b0491";
115                         String url = "https://aai-ext1.test.att.com:8443/aai/v11/network/generic-vnfs/generic-vnf/"; 
116                         AAIGETVnfResponse response = getQueryByVnfID2(url, user, password, onset.requestID, vnfID); 
117                         assertNotNull(response);
118                         boolean disabled = eventManager.isClosedLoopDisabled(response);
119                         logger.info("QueryByVnfID - isClosedLoopDisabled: " + disabled); 
120
121                         String vnfName = "lll_vnf_010317";
122                         url = "https://aai-ext1.test.att.com:8443/aai/v11/network/generic-vnfs/generic-vnf?vnf-name="; 
123                         response = getQueryByVnfName2(url, user, password, onset.requestID, vnfName); 
124                         assertNotNull(response);
125                         disabled = eventManager.isClosedLoopDisabled(response);
126                         logger.info("QueryByVnfName2 - isClosedLoopDisabled: " + disabled); 
127
128                         String vserverName = "USMSO1SX7NJ0103UJZZ01-vjunos0";
129                         url = "https://aai-ext1.test.att.com:8443//aai/v11/nodes/vservers?vserver-name="; 
130                         AAIGETVserverResponse response2 = getQueryByVserverName2(url, user, password, onset.requestID, vserverName); 
131                         assertNotNull(response);
132                         disabled = eventManager.isClosedLoopDisabled(response);
133                         logger.info("QueryByVserverName - isClosedLoopDisabled: " + disabled); 
134                 } catch (Exception e) {
135                         fail(e.getMessage());
136                 }
137         }
138         
139         // Simulate a response 
140         public static AAIGETVnfResponse getQueryByVnfID2(String urlGet, String username, String password, UUID requestID, String key) {
141                 AAIGETVnfResponse response = new AAIGETVnfResponse();
142                 
143                 response.vnfID = "83f674e8-7555-44d7-9a39-bdc3770b0491"; 
144                 response.vnfName = "lll_vnf_010317";
145                 response.vnfType = "Basa-122216-Service/VidVsamp12BaseVolume 1";
146                 response.serviceId = "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb";
147                 response.orchestrationStatus = "Created";
148                 response.inMaint = "false";
149                 response.isClosedLoopDisabled = "false";
150                 response.resourceVersion = "1494001988835";
151                 response.modelInvariantId = "f18be3cd-d446-456e-9109-121d9b62feaa";
152                   
153         RelationshipList relationshipList = new RelationshipList();
154         Relationship     relationship = new Relationship();
155         RelationshipData relationshipData = new RelationshipData();
156         RelationshipDataItem relationshipDataItem = new RelationshipDataItem();
157         
158         relationshipDataItem.relationshipKey   = "customer.global-customer-id"; 
159         relationshipDataItem.relationshipValue = "MSO_1610_ST"; 
160         relationshipData.relationshipData.add(relationshipDataItem);
161         
162         relationshipDataItem.relationshipKey   = "service-subscription.service-type"; 
163         relationshipDataItem.relationshipValue = "MSO-dev-service-type"; 
164         relationshipData.relationshipData.add(relationshipDataItem);
165         
166         relationshipDataItem.relationshipKey   = "service-instance.service-instance-id"; 
167         relationshipDataItem.relationshipValue = "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"; 
168         relationshipData.relationshipData.add(relationshipDataItem);
169         
170         RelatedToProperty relatedToProperty = new RelatedToProperty();
171         RelatedToPropertyItem item = new RelatedToPropertyItem();  
172         item.propertyKey = "service-instance.service-instance-name"; 
173         item.propertyValue = "lll_svc_010317"; 
174         relatedToProperty.relatedTo.add(item);
175         
176         relationship.relatedTo = "service-instance";
177         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";
178         relationship.relationshipData = relationshipData;
179         relationship.relatedToProperty = relatedToProperty;
180        
181         relationshipList.relationshipList.add(relationship);
182         response.relationshipList = relationshipList; 
183
184                 return response;
185         }
186
187         public static AAIGETVnfResponse getQueryByVnfName2(String urlGet, String username, String password, UUID requestID, String key) {
188                 AAIGETVnfResponse response = new AAIGETVnfResponse();
189                 
190                 response.vnfID = "83f674e8-7555-44d7-9a39-bdc3770b0491"; 
191                 response.vnfName = "lll_vnf_010317";
192                 response.vnfType = "Basa-122216-Service/VidVsamp12BaseVolume 1";
193                 response.serviceId = "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb";
194                 response.orchestrationStatus = "Created";
195                 response.inMaint = "false";
196                 response.isClosedLoopDisabled = "false";
197                 response.resourceVersion = "1494001988835";
198                 response.modelInvariantId = "f18be3cd-d446-456e-9109-121d9b62feaa";
199                   
200         RelationshipList relationshipList = new RelationshipList();
201         Relationship     relationship = new Relationship();
202         RelationshipData relationshipData = new RelationshipData();
203         RelationshipDataItem relationshipDataItem = new RelationshipDataItem();
204         
205         relationshipDataItem.relationshipKey   = "customer.global-customer-id"; 
206         relationshipDataItem.relationshipValue = "MSO_1610_ST"; 
207         relationshipData.relationshipData.add(relationshipDataItem);
208         
209         relationshipDataItem.relationshipKey   = "service-subscription.service-type"; 
210         relationshipDataItem.relationshipValue = "MSO-dev-service-type"; 
211         relationshipData.relationshipData.add(relationshipDataItem);
212         
213         relationshipDataItem.relationshipKey   = "service-instance.service-instance-id"; 
214         relationshipDataItem.relationshipValue = "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"; 
215         relationshipData.relationshipData.add(relationshipDataItem);
216         
217         RelatedToProperty relatedToProperty = new RelatedToProperty();
218         RelatedToPropertyItem item = new RelatedToPropertyItem();  
219         item.propertyKey = "service-instance.service-instance-name"; 
220         item.propertyValue = "lll_svc_010317"; 
221         relatedToProperty.relatedTo.add(item);
222         
223         relationship.relatedTo = "service-instance";
224         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";
225         relationship.relationshipData = relationshipData;
226         relationship.relatedToProperty = relatedToProperty;
227        
228         relationshipList.relationshipList.add(relationship);
229         response.relationshipList = relationshipList; 
230
231                 return response;
232         }
233
234         public static AAIGETVserverResponse getQueryByVserverName2(String urlGet, String username, String password, UUID requestID, String key) {
235                 AAIGETVserverResponse response = new AAIGETVserverResponse();
236                 
237                 response.vserverID = "d0668d4f-c25e-4a1b-87c4-83845c01efd8"; 
238                 response.vserverName = "USMSO1SX7NJ0103UJZZ01-vjunos0"; 
239                 response.vserverName2 = "vjunos0"; 
240                 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"; 
241                 response.inMaint = "false"; 
242                 response.isClosedLoopDisabled = "false"; 
243                 response.resourceVersion = "1494001931513"; 
244                   
245         RelationshipList relationshipList = new RelationshipList();
246         Relationship     relationship = new Relationship();
247         RelationshipData relationshipData = new RelationshipData();
248         RelationshipDataItem relationshipDataItem = new RelationshipDataItem();
249         
250         relationshipDataItem.relationshipKey   = "customer.global-customer-id"; 
251         relationshipDataItem.relationshipValue = "MSO_1610_ST"; 
252         relationshipData.relationshipData.add(relationshipDataItem);
253         
254         relationshipDataItem.relationshipKey   = "service-subscription.service-type"; 
255         relationshipDataItem.relationshipValue = "MSO-dev-service-type"; 
256         relationshipData.relationshipData.add(relationshipDataItem);
257         
258         relationshipDataItem.relationshipKey   = "service-instance.service-instance-id"; 
259         relationshipDataItem.relationshipValue = "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970"; 
260         relationshipData.relationshipData.add(relationshipDataItem);
261         
262         RelatedToProperty relatedToProperty = new RelatedToProperty();
263         RelatedToPropertyItem item = new RelatedToPropertyItem();  
264         item.propertyKey = "service-instance.service-instance-name"; 
265         item.propertyValue = "lll_svc_010317"; 
266         relatedToProperty.relatedTo.add(item);
267         
268         relationship.relatedTo = "service-instance";
269         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";
270         relationship.relationshipData = relationshipData;
271         relationship.relatedToProperty = relatedToProperty;
272        
273         relationshipList.relationshipList.add(relationship);
274         response.relationshipList = relationshipList; 
275
276                 return response;
277         }
278 }