519e1c92b1ce9934c37fbed20e68de9c9d59997f
[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.Vserver;
28 import org.onap.so.client.aai.AAIObjectType;
29 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
30 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
31 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.BeanUtils;
35 import org.springframework.stereotype.Component;
36
37 import com.fasterxml.jackson.core.JsonProcessingException;
38
39 @Component
40 public class AuditVServer extends AbstractAudit {
41         private static final Logger logger = LoggerFactory.getLogger(AuditVServer.class);
42
43         public Optional<AAIObjectAuditList> auditVservers(Set<Vserver> vServersToAudit, String tenantId, String cloudOwner, String cloudRegion) {
44                 if (vServersToAudit == null || vServersToAudit.isEmpty()){
45                         return Optional.empty();
46                 }
47                 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
48                 vServersToAudit.stream().forEach(vserver -> {
49                         try {
50                                 logger.debug("Vserver to Audit: {}",objectMapper.getMapper().writeValueAsString(vserver));
51                         } catch (JsonProcessingException e) {
52                                 
53                         }
54                 });
55                 AAIObjectAuditList auditList = new AAIObjectAuditList();                
56                 vServersToAudit.stream()
57                                 .forEach(vServer -> auditList.getAuditList().addAll(doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion).getAuditList()));
58                 return Optional.of(auditList);
59         }
60
61         private AAIObjectAuditList doesVServerExistInAAI(Vserver vServer, String tenantId, String cloudOwner, String cloudRegion) {
62                 AAIObjectAuditList auditList = new AAIObjectAuditList();
63                 AAIObjectAudit vServerAudit = new AAIObjectAudit();
64                 AAIResourceUri vserverURI = AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, cloudOwner, cloudRegion,
65                                 tenantId, vServer.getVserverId());
66                 Vserver vServerShallow = new Vserver();
67                 BeanUtils.copyProperties(vServer,vServerShallow,"LInterfaces");
68                 boolean vServerExists = getAaiClient().exists(vserverURI);
69                 logger.info("v-server {} exists: {}", vServer.getVserverId(), vServerExists);
70                 vServerAudit.setAaiObject(vServerShallow);
71                 vServerAudit.setDoesObjectExist(vServerExists);
72                 vServerAudit.setResourceURI(vserverURI.build());
73                 vServerAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName());
74                 auditList.getAuditList().add(vServerAudit);
75                 if (vServer.getLInterfaces() != null) {
76                         vServer.getLInterfaces().getLInterface().stream().forEach(lInterface -> auditList.getAuditList().addAll(doesLinterfaceExistinAAI(lInterface,
77                                                         vServer.getVserverId(), tenantId, cloudOwner, cloudRegion).getAuditList()));
78                 }
79                 return auditList;
80         }
81
82         private AAIObjectAuditList doesLinterfaceExistinAAI(LInterface lInterface, String vServerId, String tenantId,
83                         String cloudOwner, String cloudRegion) {
84                 AAIObjectAuditList auditList = new AAIObjectAuditList();
85                 AAIObjectAudit lInterfaceAudit = new AAIObjectAudit();
86                 AAIResourceUri linterfaceURI = AAIUriFactory
87                                 .createResourceUri(AAIObjectType.L_INTERFACE, cloudOwner, cloudRegion, tenantId, vServerId, lInterface.getInterfaceName());
88                 Optional<LInterface> queriedLInterface = getAaiClient().get(LInterface.class, linterfaceURI);
89                 if (queriedLInterface.isPresent()) {
90                                 lInterfaceAudit.setDoesObjectExist(true);
91                                 lInterface.setInterfaceName(lInterface.getInterfaceName());     
92                 }
93                 LInterface lInterfaceShallow = new LInterface();
94                 BeanUtils.copyProperties(lInterface,lInterfaceShallow,"LInterfaces");
95                 lInterfaceAudit.setAaiObject(lInterface);
96                 lInterfaceAudit.setResourceURI(linterfaceURI.build());
97                 lInterfaceAudit.setAaiObjectType(AAIObjectType.L_INTERFACE.typeName());
98                 auditList.getAuditList().add(lInterfaceAudit);
99                 logger.info("l-interface id:{} name: {} exists: {} ", lInterface.getInterfaceId(), lInterface.getInterfaceName(),
100                                 lInterfaceAudit.isDoesObjectExist());
101
102                 if (lInterface.getLInterfaces() != null) {
103                         lInterface.getLInterfaces().getLInterface().stream()
104                                         .forEach(subInterface -> auditList.getAuditList().add(doesSubInterfaceExistinAAI(subInterface,
105                                                         lInterface.getInterfaceName(), vServerId, tenantId, cloudOwner, cloudRegion)));
106                 }
107                 logger.debug("l-interface {} does not contain any sub-iterfaces, skipping audit of sub-interfaces", lInterface.getInterfaceId());
108
109                 return auditList;
110         }
111
112         private AAIObjectAudit doesSubInterfaceExistinAAI(LInterface subInterface, String linterfaceName, String vServerId,
113                         String tenantId, String cloudOwner, String cloudRegion) {
114                 logger.info("checking if sub-l-interface {} , linterfaceName: {} vserverId: {}  exists",
115                                 subInterface.getInterfaceName(), linterfaceName, vServerId);
116                 AAIObjectAudit subInterfaceAudit = new AAIObjectAudit();
117                 
118                 
119                 AAIResourceUri subInterfaceURI = AAIUriFactory.createResourceUri(AAIObjectType.SUB_L_INTERFACE, cloudOwner,
120                                 cloudRegion, tenantId, vServerId, linterfaceName, subInterface.getInterfaceName());
121                 subInterfaceAudit.setResourceURI(subInterfaceURI.build());
122                 boolean doesExist = getAaiClient().exists(subInterfaceURI);
123                 logger.info("sub-l-interface-id:{} exists: {}", subInterface.getInterfaceId(), doesExist);
124                 subInterfaceAudit.setAaiObject(subInterface);
125                 subInterfaceAudit.setDoesObjectExist(doesExist);
126                 subInterfaceAudit.setAaiObjectType(AAIObjectType.SUB_L_INTERFACE.typeName());
127                 return subInterfaceAudit;
128         }
129 }