2 * ============LICENSE_START=======================================================
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
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.tasks.audit;
23 import java.util.List;
24 import java.util.Optional;
26 import org.onap.aai.domain.yang.LInterface;
27 import org.onap.aai.domain.yang.VfModule;
28 import org.onap.aai.domain.yang.VfModules;
29 import org.onap.aai.domain.yang.Vserver;
30 import org.onap.aaiclient.client.aai.AAIObjectPlurals;
31 import org.onap.aaiclient.client.aai.AAIObjectType;
32 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
33 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri;
34 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
35 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
36 import org.onap.aaiclient.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
37 import org.onap.so.objects.audit.AAIObjectAudit;
38 import org.onap.so.objects.audit.AAIObjectAuditList;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.BeanUtils;
42 import org.springframework.stereotype.Component;
43 import com.fasterxml.jackson.core.JsonProcessingException;
46 public class AuditVServer extends AbstractAudit {
47 private static final Logger logger = LoggerFactory.getLogger(AuditVServer.class);
49 public void auditVservers(AAIObjectAuditList aaiObjectAuditList) {
51 aaiObjectAuditList.getAuditList().forEach(aaiObjectAudit -> {
52 boolean vserverExist = getAaiClient().exists(AAIUriFactory
53 .createResourceFromExistingURI(AAIObjectType.VSERVER, aaiObjectAudit.getResourceURI()));
54 aaiObjectAudit.setDoesObjectExist(vserverExist);
58 public Optional<AAIObjectAuditList> auditVserversThroughRelationships(String genericVnfId, String vfModuleName) {
59 AAIObjectAuditList auditList = new AAIObjectAuditList();
60 AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VF_MODULE, genericVnfId)
61 .queryParam("vf-module-name", vfModuleName);
62 Optional<AAIResultWrapper> wrapper = getAaiClient().getFirstWrapper(VfModules.class, VfModule.class, uri);
63 if (wrapper.isPresent() && wrapper.get().getRelationships().isPresent()) {
64 List<AAIResourceUri> relatedVservers =
65 wrapper.get().getRelationships().get().getRelatedUris(AAIObjectType.VSERVER);
66 if (!relatedVservers.isEmpty()) {
67 relatedVservers.forEach(vserverUri -> {
68 Optional<Vserver> vserver = getAaiClient().get(vserverUri).asBean(Vserver.class);
69 Vserver vServerShallow = new Vserver();
70 BeanUtils.copyProperties(vserver, vServerShallow);
71 AAIObjectAudit vServerAudit = new AAIObjectAudit();
72 vServerAudit.setAaiObject(vServerShallow);
73 vServerAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName());
74 vServerAudit.setDoesObjectExist(true);
75 auditList.getAuditList().add(vServerAudit);
79 return Optional.of(auditList);
82 public Optional<AAIObjectAuditList> auditVservers(Set<Vserver> vServersToAudit, String tenantId, String cloudOwner,
84 if (vServersToAudit == null || vServersToAudit.isEmpty()) {
85 return Optional.empty();
87 GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
88 vServersToAudit.stream().forEach(vserver -> {
90 logger.debug("Vserver to Audit: {}", objectMapper.getMapper().writeValueAsString(vserver));
91 } catch (JsonProcessingException e) {
92 logger.error("Json parse exception: ", e);
96 AAIObjectAuditList auditList = new AAIObjectAuditList();
97 vServersToAudit.stream().forEach(vServer -> auditList.getAuditList()
98 .addAll(doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion).getAuditList()));
99 return Optional.of(auditList);
102 private AAIObjectAuditList doesVServerExistInAAI(Vserver vServer, String tenantId, String cloudOwner,
103 String cloudRegion) {
104 AAIObjectAuditList auditList = new AAIObjectAuditList();
105 AAIObjectAudit vServerAudit = new AAIObjectAudit();
106 AAIResourceUri vserverURI = AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, cloudOwner, cloudRegion,
107 tenantId, vServer.getVserverId());
108 Vserver vServerShallow = new Vserver();
109 BeanUtils.copyProperties(vServer, vServerShallow, "LInterfaces");
110 boolean vServerExists = getAaiClient().exists(vserverURI);
111 logger.info("v-server {} exists: {}", vServer.getVserverId(), vServerExists);
112 vServerAudit.setAaiObject(vServerShallow);
113 vServerAudit.setDoesObjectExist(vServerExists);
114 vServerAudit.setResourceURI(vserverURI.build());
115 vServerAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName());
116 auditList.getAuditList().add(vServerAudit);
117 if (vServer.getLInterfaces() != null) {
118 vServer.getLInterfaces().getLInterface().stream().forEach(lInterface -> auditList.getAuditList().addAll(
119 doesLinterfaceExistinAAI(lInterface, vServer.getVserverId(), tenantId, cloudOwner, cloudRegion)
125 private AAIObjectAuditList doesLinterfaceExistinAAI(LInterface lInterface, String vServerId, String tenantId,
126 String cloudOwner, String cloudRegion) {
127 AAIObjectAuditList auditList = new AAIObjectAuditList();
128 AAIObjectAudit lInterfaceAudit = new AAIObjectAudit();
129 AAIResourceUri linterfaceURI = AAIUriFactory.createResourceUri(AAIObjectType.L_INTERFACE, cloudOwner,
130 cloudRegion, tenantId, vServerId, lInterface.getInterfaceName());
131 Optional<LInterface> queriedLInterface = getAaiClient().get(LInterface.class, linterfaceURI);
132 if (queriedLInterface.isPresent()) {
133 lInterfaceAudit.setDoesObjectExist(true);
134 lInterface.setInterfaceName(lInterface.getInterfaceName());
136 lInterfaceAudit.setAaiObject(lInterface);
137 lInterfaceAudit.setResourceURI(linterfaceURI.build());
138 lInterfaceAudit.setAaiObjectType(AAIObjectType.L_INTERFACE.typeName());
139 auditList.getAuditList().add(lInterfaceAudit);
140 logger.info("l-interface id:{} name: {} exists: {} ", lInterface.getInterfaceId(),
141 lInterface.getInterfaceName(), lInterfaceAudit.isDoesObjectExist());
143 if (lInterface.getLInterfaces() != null) {
144 lInterface.getLInterfaces().getLInterface().stream()
145 .forEach(subInterface -> auditList.getAuditList().add(doesSubInterfaceExistinAAI(subInterface,
146 lInterface.getInterfaceName(), vServerId, tenantId, cloudOwner, cloudRegion)));
148 logger.debug("l-interface {} does not contain any sub-iterfaces, skipping audit of sub-interfaces",
149 lInterface.getInterfaceId());
154 private AAIObjectAudit doesSubInterfaceExistinAAI(LInterface subInterface, String linterfaceName, String vServerId,
155 String tenantId, String cloudOwner, String cloudRegion) {
156 logger.info("checking if sub-l-interface {} , linterfaceName: {} vserverId: {} exists",
157 subInterface.getInterfaceName(), linterfaceName, vServerId);
158 AAIObjectAudit subInterfaceAudit = new AAIObjectAudit();
161 AAIResourceUri subInterfaceURI = AAIUriFactory.createResourceUri(AAIObjectType.SUB_L_INTERFACE, cloudOwner,
162 cloudRegion, tenantId, vServerId, linterfaceName, subInterface.getInterfaceName());
163 subInterfaceAudit.setResourceURI(subInterfaceURI.build());
164 boolean doesExist = getAaiClient().exists(subInterfaceURI);
165 logger.info("sub-l-interface-id:{} exists: {}", subInterface.getInterfaceId(), doesExist);
166 subInterfaceAudit.setAaiObject(subInterface);
167 subInterfaceAudit.setDoesObjectExist(doesExist);
168 subInterfaceAudit.setAaiObjectType(AAIObjectType.SUB_L_INTERFACE.typeName());
169 return subInterfaceAudit;