Merge "Merge Casablanca"
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / audit / AuditVServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 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
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.so.adapters.audit;
22
23 import java.util.Optional;
24 import java.util.Set;
25
26 import org.onap.aai.domain.yang.LInterface;
27 import org.onap.aai.domain.yang.LInterfaces;
28 import org.onap.aai.domain.yang.Vserver;
29 import org.onap.so.client.aai.AAIObjectPlurals;
30 import org.onap.so.client.aai.AAIObjectType;
31 import org.onap.so.client.aai.entities.AAIResultWrapper;
32 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
33 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class AuditVServer extends AbstractAudit {
40         private static final Logger logger = LoggerFactory.getLogger(AuditVServer.class);
41
42         public boolean auditVservers(Set<Vserver> vServersToAudit, String tenantId, String cloudOwner, String cloudRegion) {
43                 if (vServersToAudit == null || vServersToAudit.isEmpty()){
44                         return false;
45                 }
46                 return vServersToAudit.stream()
47                                 .filter(vServer -> !doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion)).findFirst()
48                                 .map(v -> false).orElse(true);
49         }
50
51         private boolean doesVServerExistInAAI(Vserver vServer, String tenantId, String cloudOwner, String cloudRegion) {
52                 AAIResourceUri vserverURI = AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, cloudOwner, cloudRegion,
53                                 tenantId, vServer.getVserverId());
54                 boolean vServerExists = getAaiClient().exists(vserverURI);
55                 boolean doesExist = getAaiClient().exists(vserverURI);
56                 logger.info("v-server {} exists: {}", vServer.getVserverId(), doesExist);
57                 boolean allNeutronNetworksExist = true;
58                 if (vServerExists && vServer.getLInterfaces() != null) {
59                         allNeutronNetworksExist = vServer.getLInterfaces()
60                                         .getLInterface().stream().filter(lInterface -> !doesLinterfaceExistinAAI(lInterface,
61                                                         vServer.getVserverId(), tenantId, cloudOwner, cloudRegion))
62                                         .findFirst().map(v -> false).orElse(true);
63                 }
64                 return vServerExists && allNeutronNetworksExist;
65         }
66
67         private boolean doesLinterfaceExistinAAI(LInterface lInterface, String vServerId, String tenantId,
68                         String cloudOwner, String cloudRegion) {
69                 boolean doesLInterfaceExist = false;
70                 boolean doSubInterfacesExist = true;
71                 AAIResourceUri linterfaceURI = AAIUriFactory
72                                 .createResourceUri(AAIObjectPlurals.L_INTERFACE, cloudOwner, cloudRegion, tenantId, vServerId)
73                                 .queryParam("interface-id", lInterface.getInterfaceId());
74                 Optional<LInterfaces> queriedLInterface = getAaiClient().get(LInterfaces.class, linterfaceURI);
75                 if (queriedLInterface.isPresent()) {
76                         if (queriedLInterface.get().getLInterface().size() > 1) {
77                                 logger.error("Non-Unique LInterface Found stopping audit, L-Interface Id: " +lInterface.getInterfaceId());
78                                 doesLInterfaceExist = false;
79                         } else {
80                                 doesLInterfaceExist = true;
81                                 lInterface.setInterfaceName(queriedLInterface.get().getLInterface().get(0).getInterfaceName());
82                         }
83                 }
84                 logger.info("l-interface id:{} name: {} exists: {}", lInterface.getInterfaceId(), lInterface.getInterfaceName(),
85                                 doesLInterfaceExist);
86
87                 if (doesLInterfaceExist && lInterface.getLInterfaces() != null) {
88                         doSubInterfacesExist = lInterface.getLInterfaces().getLInterface()
89                                         .stream().filter(subInterface -> !doesSubInterfaceExistinAAI(subInterface,
90                                                         lInterface.getInterfaceName(), vServerId, tenantId, cloudOwner, cloudRegion))
91                                         .findFirst().map(v -> false).orElse(true);
92                 } else
93                         logger.debug("l-interface {} does not contain any sub-iterfaces", lInterface.getInterfaceId());
94
95                 return doesLInterfaceExist && doSubInterfacesExist;
96         }
97
98         private boolean doesSubInterfaceExistinAAI(LInterface subInterface, String linterfaceName, String vServerId,
99                         String tenantId, String cloudOwner, String cloudRegion) {
100                 logger.info("checking if sub-l-interface {} , linterfaceName: {} vserverId: {}  exists",
101                                 subInterface.getInterfaceId(), linterfaceName, vServerId);
102
103                 AAIResourceUri linterfaceURI = AAIUriFactory.createResourceUri(AAIObjectPlurals.SUB_L_INTERFACE, cloudOwner,
104                                 cloudRegion, tenantId, vServerId, linterfaceName)
105                                 .queryParam("interface-id", subInterface.getInterfaceId());
106
107                 boolean doesExist = getAaiClient().exists(linterfaceURI);
108                 logger.info("sub-l-interface {} exists: {}", subInterface.getInterfaceId(), doesExist);
109                 return doesExist;
110         }
111 }