2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.audit;
23 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.Mockito.doReturn;
29 import java.util.ArrayList;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Optional;
34 import java.util.stream.Collectors;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.aai.domain.yang.LInterface;
43 import org.onap.aai.domain.yang.LInterfaces;
44 import org.onap.aai.domain.yang.Vserver;
45 import org.onap.so.openstack.utils.MsoHeatUtils;
46 import org.onap.so.openstack.utils.MsoNeutronUtils;
47 import org.skyscreamer.jsonassert.JSONAssert;
49 import com.fasterxml.jackson.databind.DeserializationFeature;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.woorea.openstack.heat.model.Resource;
52 import com.woorea.openstack.heat.model.Resources;
53 import com.woorea.openstack.heat.model.Stack;
54 import com.woorea.openstack.quantum.model.Port;
57 @RunWith(MockitoJUnitRunner.Silent.class)
58 public class HeatStackAuditTest extends HeatStackAudit {
61 private HeatStackAudit heatStackAudit = new HeatStackAudit();
64 private MsoHeatUtils msoHeatUtilsMock;
67 private MsoNeutronUtils neutronUtilsMock;
70 private AuditVServer auditVserver;
72 private static final String cloudRegion = "cloudRegion";
73 private static final String tenantId = "tenantId";
75 private Resources resources = new Resources();
77 private ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
79 private ObjectMapper stackObjectMapper = new ObjectMapper().configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
81 private List<Optional<Port>> portList = new ArrayList<>();
84 public void setup() throws Exception{
85 resources= objectMapper.readValue(new File("src/test/resources/GetResources.json"), Resources.class);
86 Port neutronPort1 =stackObjectMapper.readValue(new File("src/test/resources/NeutronPort1.json"), Port.class);
87 doReturn(Optional.of(neutronPort1)).when(neutronUtilsMock).getNeutronPort("7ee06d9d-3d18-411c-9d3e-aec930f70413", cloudRegion,tenantId);
88 Port neutronPort2 = stackObjectMapper.readValue(new File("src/test/resources/NeutronPort2.json"), Port.class);
89 doReturn(Optional.of(neutronPort2)).when(neutronUtilsMock).getNeutronPort("27391d94-33af-474a-927d-d409249e8fd3", cloudRegion,tenantId);
90 Port neutronPort3 = stackObjectMapper.readValue(new File("src/test/resources/NeutronPort3.json"), Port.class);
91 doReturn(Optional.of(neutronPort3)).when(neutronUtilsMock).getNeutronPort("fdeedf37-c01e-4ab0-bdd6-8d5fc4913943", cloudRegion,tenantId);
92 Port neutronPort4 = stackObjectMapper.readValue(new File("src/test/resources/NeutronPort4.json"), Port.class);
93 doReturn(Optional.of(neutronPort4)).when(neutronUtilsMock).getNeutronPort("8d93f63e-e972-48c7-ad98-b2122da47315", cloudRegion,tenantId);
94 Port neutronPort5 = stackObjectMapper.readValue(new File("src/test/resources/NeutronPort5.json"), Port.class);
95 doReturn(Optional.of(neutronPort5)).when(neutronUtilsMock).getNeutronPort("0594a2f2-7ea4-42eb-abc2-48ea49677fca", cloudRegion,tenantId);
96 Port neutronPort6 = stackObjectMapper.readValue(new File("src/test/resources/NeutronPort6.json"), Port.class);
97 doReturn(Optional.of(neutronPort6)).when(neutronUtilsMock).getNeutronPort("00bb8407-650e-48b5-b919-33b88d6f8fe3", cloudRegion,tenantId);
100 portList.add(Optional.empty());
101 portList.add(Optional.of(neutronPort1));
102 portList.add(Optional.of(neutronPort2));
103 portList.add(Optional.of(neutronPort3));
104 portList.add(Optional.of(neutronPort4));
105 portList.add(Optional.of(neutronPort5));
106 portList.add(Optional.of(neutronPort6));
110 public void extract_proper_path_Test(){
111 Optional<String> actualResult = extractStackPathFromHref("https://orchestration.com:8004/v1/stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81");
112 assertEquals("/stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81", actualResult.get());
116 public void extract_proper_resources_path_Test(){
117 Optional<String> actualResult = extractResourcePathFromHref("https://orchestration.com:8004/v1/stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81");
118 assertEquals("/stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81/resources", actualResult.get());
122 public void extract_invalid_uri_Test(){
123 Optional<String> actualResult = extractStackPathFromHref("orchestrn.com:8004/v18b44d60a6f94bdcb2738f9e//stacks/test_stack/f711be16-2654-4a09-b89d-0511fda20e81");
124 assertEquals(false, actualResult.isPresent());
128 public void createVserverSet_Test() throws Exception{
129 List<Resource> novaResources = resources.getList().stream()
130 .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList());
132 List<Resource> resourceGroups = resources.getList().stream()
133 .filter(p -> "OS::Heat::ResourceGroup".equals(p.getType())).collect(Collectors.toList());
135 Set<Vserver> expectedVservers = new HashSet<>();
136 Vserver vServer1= new Vserver();
137 vServer1.setVserverId("92272b67-d23f-42ca-87fa-7b06a9ec81f3");
138 LInterfaces vServer1Linterfaces = new LInterfaces();
139 vServer1.setLInterfaces(vServer1Linterfaces);
141 LInterface ssc_1_trusted_port_0 = new LInterface();
142 ssc_1_trusted_port_0.setInterfaceId("7ee06d9d-3d18-411c-9d3e-aec930f70413");
143 vServer1.getLInterfaces().getLInterface().add(ssc_1_trusted_port_0);
147 LInterface ssc_1_mgmt_port_1 = new LInterface();
148 ssc_1_mgmt_port_1.setInterfaceId("fdeedf37-c01e-4ab0-bdd6-8d5fc4913943");
149 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_1);
151 LInterface ssc_1_mgmt_port_0 = new LInterface();
152 ssc_1_mgmt_port_0.setInterfaceId("8d93f63e-e972-48c7-ad98-b2122da47315");
153 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_0);
155 LInterface ssc_1_service2_port_0 = new LInterface();
156 ssc_1_service2_port_0.setLInterfaces(new LInterfaces());
157 ssc_1_service2_port_0.setInterfaceId("0594a2f2-7ea4-42eb-abc2-48ea49677fca");
158 vServer1.getLInterfaces().getLInterface().add(ssc_1_service2_port_0);
160 LInterface service2_sub_interface_1 = new LInterface();
161 service2_sub_interface_1.setInterfaceId("2bbfa345-33bb-495a-94b2-fb514ee1cffc");
162 ssc_1_service2_port_0.getLInterfaces().getLInterface().add(service2_sub_interface_1);
164 LInterface ssc_1_int_ha_port_0 = new LInterface();
165 ssc_1_int_ha_port_0.setInterfaceId("00bb8407-650e-48b5-b919-33b88d6f8fe3");
166 vServer1.getLInterfaces().getLInterface().add(ssc_1_int_ha_port_0);
169 LInterface ssc_1_service1_port_0 = new LInterface();
170 ssc_1_service1_port_0.setInterfaceId("27391d94-33af-474a-927d-d409249e8fd3");
171 vServer1.getLInterfaces().getLInterface().add(ssc_1_service1_port_0);
172 ssc_1_service1_port_0.setLInterfaces(new LInterfaces());
174 LInterface service1_sub_interface_0 = new LInterface();
175 service1_sub_interface_0.setInterfaceId("d54dfd09-75c6-4e04-b204-909455b8f933");
176 ssc_1_service1_port_0.getLInterfaces().getLInterface().add(service1_sub_interface_0);
178 LInterface service1_sub_interface_1 = new LInterface();
179 service1_sub_interface_1.setInterfaceId("f7a998c0-8939-4b07-bf4a-0862e9c325e1");
180 ssc_1_service1_port_0.getLInterfaces().getLInterface().add(service1_sub_interface_1);
182 LInterface service1_sub_interface_2 = new LInterface();
183 service1_sub_interface_2.setInterfaceId("621c1fea-60b8-44ee-aede-c01b8b1aaa70");
184 ssc_1_service1_port_0.getLInterfaces().getLInterface().add(service1_sub_interface_2);
187 expectedVservers.add(vServer1);
190 Resources service1QueryResponse = objectMapper.readValue(new File("src/test/resources/Service1ResourceGroupResponse.json"), Resources.class);
191 doReturn(service1QueryResponse).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz/31d0647a-6043-49a4-81b6-ccab29380672/resources", cloudRegion, tenantId, Resources.class);
193 Resources service2QueryResponse =objectMapper.readValue(new File("src/test/resources/Service2ResourceGroupResponse.json"), Resources.class);
194 doReturn(service2QueryResponse).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service2_port_0_subinterfaces-hlzdigtimzst/447a9b41-714e-434b-b1d0-6cce8d9f0f0c/resources", cloudRegion, tenantId, Resources.class);
197 Stack service2StackQuerySubInt = stackObjectMapper.readValue(new File("src/test/resources/Service2SubInterface0.json"), Stack.class);
198 doReturn(service2StackQuerySubInt).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service2_port_0_subinterfaces-hlzdigtimzst-0-upfi5nhurk7y/f711be16-2654-4a09-b89d-0511fda20e81", cloudRegion,tenantId, Stack.class);
199 Resources service2ResourceQuerySubInt = objectMapper.readValue(new File("src/test/resources/Service2SubInterface1Resources.json"), Resources.class);
200 doReturn(service2ResourceQuerySubInt).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service2_port_0_subinterfaces-hlzdigtimzst-0-upfi5nhurk7y/f711be16-2654-4a09-b89d-0511fda20e81/resources", cloudRegion,tenantId, Resources.class);
202 Stack service1StackQuerySubInt1 =stackObjectMapper.readValue(new File("src/test/resources/Service1SubInterface0.json"), Stack.class);
203 doReturn(service1StackQuerySubInt1).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-1-fmn5laetg5cs/0d9cd813-2ae1-46c0-9ebb-48081f6cffbb", cloudRegion,tenantId, Stack.class);
204 Resources service1ResourceQuerySubInt1 = objectMapper.readValue(new File("src/test/resources/Service1SubInterface0Resources.json"), Resources.class);
205 doReturn(service1ResourceQuerySubInt1).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-1-fmn5laetg5cs/0d9cd813-2ae1-46c0-9ebb-48081f6cffbb/resources", cloudRegion,tenantId, Resources.class);
208 Stack service1StackQuerySubInt2 =stackObjectMapper.readValue(new File("src/test/resources/Service1SubInterface1.json"), Stack.class);
209 doReturn(service1StackQuerySubInt2).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-0-yghihziaf36m/b7019dd0-2ee9-4447-bdef-ac25676b205a", cloudRegion,tenantId, Stack.class);
210 Resources service1ResourceQuerySubInt2 = objectMapper.readValue(new File("src/test/resources/Service1SubInterface1Resources.json"), Resources.class);
211 doReturn(service1ResourceQuerySubInt2).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-0-yghihziaf36m/b7019dd0-2ee9-4447-bdef-ac25676b205a/resources", cloudRegion,tenantId, Resources.class);
213 Stack service1StackQuerySubInt3 =stackObjectMapper.readValue(new File("src/test/resources/Service1SubInterface2.json"), Stack.class);
214 doReturn(service1StackQuerySubInt3).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-2-y3ndsavmsymv/bd0fc728-cbde-4301-a581-db56f494675c", cloudRegion,tenantId, Stack.class);
215 Resources service1ResourceQuerySubInt3 = objectMapper.readValue(new File("src/test/resources/Service1SubInterface2Resources.json"), Resources.class);
216 doReturn(service1ResourceQuerySubInt3).when(msoHeatUtilsMock).executeHeatClientRequest("/stacks/tsbc0005vm002ssc001-ssc_1_subint_service1_port_0_subinterfaces-dtmxjmny7yjz-2-y3ndsavmsymv/bd0fc728-cbde-4301-a581-db56f494675c/resources", cloudRegion,tenantId, Resources.class);
218 Set<Vserver> vServersToAudit = heatStackAudit.createVserverSet(resources, novaResources,portList);
219 Set<Vserver> vserversWithSubInterfaces = heatStackAudit.processSubInterfaces(cloudRegion,tenantId,resourceGroups, vServersToAudit);
221 String actualValue = objectMapper.writeValueAsString(vserversWithSubInterfaces);
222 String expectedValue = objectMapper.writeValueAsString(expectedVservers);
224 JSONAssert.assertEquals(expectedValue, actualValue, false);
228 public void auditHeatStackNoServers_Test() throws Exception{
231 Resources getResource = objectMapper.readValue(new File("src/test/resources/Service1ResourceGroupResponse.json"), Resources.class);
232 doReturn(getResource).when(msoHeatUtilsMock).queryStackResources(cloudRegion, tenantId, "heatStackName");
234 boolean actual = heatStackAudit.auditHeatStackCreate(cloudRegion, "cloudOwner", tenantId, "heatStackName");
235 assertEquals(true, actual);
240 public void findInterfaceInformation_Test(){
241 List<Resource> novaResources = resources.getList().stream()
242 .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList());
243 Set<Vserver> expectedVservers = new HashSet<>();
244 Vserver vServer1= new Vserver();
245 vServer1.setVserverId("92272b67-d23f-42ca-87fa-7b06a9ec81f3");
246 LInterfaces vServer1Linterfaces = new LInterfaces();
247 vServer1.setLInterfaces(vServer1Linterfaces);
249 LInterface ssc_1_trusted_port_0 = new LInterface();
250 ssc_1_trusted_port_0.setInterfaceId("7ee06d9d-3d18-411c-9d3e-aec930f70413");
251 vServer1.getLInterfaces().getLInterface().add(ssc_1_trusted_port_0);
253 LInterface ssc_1_service1_port_0 = new LInterface();
254 ssc_1_service1_port_0.setInterfaceId("27391d94-33af-474a-927d-d409249e8fd3");
255 vServer1.getLInterfaces().getLInterface().add(ssc_1_service1_port_0);
257 LInterface ssc_1_mgmt_port_1 = new LInterface();
258 ssc_1_mgmt_port_1.setInterfaceId("fdeedf37-c01e-4ab0-bdd6-8d5fc4913943");
259 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_1);
261 LInterface ssc_1_mgmt_port_0 = new LInterface();
262 ssc_1_mgmt_port_0.setInterfaceId("8d93f63e-e972-48c7-ad98-b2122da47315");
263 vServer1.getLInterfaces().getLInterface().add(ssc_1_mgmt_port_0);
265 LInterface ssc_1_service2_port_0 = new LInterface();
266 ssc_1_service2_port_0.setInterfaceId("0594a2f2-7ea4-42eb-abc2-48ea49677fca");
267 vServer1.getLInterfaces().getLInterface().add(ssc_1_service2_port_0);
269 LInterface ssc_1_int_ha_port_0 = new LInterface();
270 ssc_1_int_ha_port_0.setInterfaceId("00bb8407-650e-48b5-b919-33b88d6f8fe3");
271 vServer1.getLInterfaces().getLInterface().add(ssc_1_int_ha_port_0);
273 expectedVservers.add(vServer1);
275 Set<Vserver> actualVservers = heatStackAudit.createVserverSet(resources, novaResources,portList);
277 assertThat(actualVservers, sameBeanAs(expectedVservers));