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