Merge "Add missing lines for CCVPN"
[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         lInterfaceAudit.setAaiObject(lInterface);
95         lInterfaceAudit.setResourceURI(linterfaceURI.build());
96         lInterfaceAudit.setAaiObjectType(AAIObjectType.L_INTERFACE.typeName());
97         auditList.getAuditList().add(lInterfaceAudit);
98         logger.info("l-interface id:{} name: {} exists: {} ", lInterface.getInterfaceId(),
99                 lInterface.getInterfaceName(), lInterfaceAudit.isDoesObjectExist());
100
101         if (lInterface.getLInterfaces() != null) {
102             lInterface.getLInterfaces().getLInterface().stream()
103                     .forEach(subInterface -> auditList.getAuditList().add(doesSubInterfaceExistinAAI(subInterface,
104                             lInterface.getInterfaceName(), vServerId, tenantId, cloudOwner, cloudRegion)));
105         }
106         logger.debug("l-interface {} does not contain any sub-iterfaces, skipping audit of sub-interfaces",
107                 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 }