if audit fails write sub interface data to a ai
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / audit / AuditVServer.java
index c81dac7..519e1c9 100644 (file)
@@ -24,97 +24,106 @@ import java.util.Optional;
 import java.util.Set;
 
 import org.onap.aai.domain.yang.LInterface;
-import org.onap.aai.domain.yang.LInterfaces;
 import org.onap.aai.domain.yang.Vserver;
-import org.onap.so.client.aai.AAIObjectPlurals;
 import org.onap.so.client.aai.AAIObjectType;
-import org.onap.so.client.aai.entities.AAIResultWrapper;
 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
+import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Component;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
+
 @Component
 public class AuditVServer extends AbstractAudit {
        private static final Logger logger = LoggerFactory.getLogger(AuditVServer.class);
 
-       public boolean auditAllVserversDoExist(Set<Vserver> vServersToAudit, String tenantId, String cloudOwner, String cloudRegion) {
+       public Optional<AAIObjectAuditList> auditVservers(Set<Vserver> vServersToAudit, String tenantId, String cloudOwner, String cloudRegion) {
                if (vServersToAudit == null || vServersToAudit.isEmpty()){
-                       return false;
+                       return Optional.empty();
                }
-               return vServersToAudit.stream()
-                               .filter(vServer -> !doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion,true)).findFirst()
-                               .map(v -> false).orElse(true);
-       }
-       
-       public boolean auditAllVserversDoNotExist(Set<Vserver> vServersToAudit, String tenantId, String cloudOwner, String cloudRegion) {
-               if (vServersToAudit == null || vServersToAudit.isEmpty()){
-                       return true;
-               }
-               return vServersToAudit.stream()
-                               .filter(vServer -> doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion,false)).findFirst()
-                               .map(v -> false).orElse(true);
+               GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
+               vServersToAudit.stream().forEach(vserver -> {
+                       try {
+                               logger.debug("Vserver to Audit: {}",objectMapper.getMapper().writeValueAsString(vserver));
+                       } catch (JsonProcessingException e) {
+                               
+                       }
+               });
+               AAIObjectAuditList auditList = new AAIObjectAuditList();                
+               vServersToAudit.stream()
+                               .forEach(vServer -> auditList.getAuditList().addAll(doesVServerExistInAAI(vServer, tenantId, cloudOwner, cloudRegion).getAuditList()));
+               return Optional.of(auditList);
        }
 
-       private boolean doesVServerExistInAAI(Vserver vServer, String tenantId, String cloudOwner, String cloudRegion, boolean checkLinterfaces) {
+       private AAIObjectAuditList doesVServerExistInAAI(Vserver vServer, String tenantId, String cloudOwner, String cloudRegion) {
+               AAIObjectAuditList auditList = new AAIObjectAuditList();
+               AAIObjectAudit vServerAudit = new AAIObjectAudit();
                AAIResourceUri vserverURI = AAIUriFactory.createResourceUri(AAIObjectType.VSERVER, cloudOwner, cloudRegion,
                                tenantId, vServer.getVserverId());
+               Vserver vServerShallow = new Vserver();
+               BeanUtils.copyProperties(vServer,vServerShallow,"LInterfaces");
                boolean vServerExists = getAaiClient().exists(vserverURI);
-               boolean doesExist = getAaiClient().exists(vserverURI);
-               logger.info("v-server {} exists: {}", vServer.getVserverId(), doesExist);
-               boolean allNeutronNetworksExist = true;
-               if (vServerExists && vServer.getLInterfaces() != null && checkLinterfaces) {
-                       allNeutronNetworksExist = vServer.getLInterfaces()
-                                       .getLInterface().stream().filter(lInterface -> !doesLinterfaceExistinAAI(lInterface,
-                                                       vServer.getVserverId(), tenantId, cloudOwner, cloudRegion))
-                                       .findFirst().map(v -> false).orElse(true);
+               logger.info("v-server {} exists: {}", vServer.getVserverId(), vServerExists);
+               vServerAudit.setAaiObject(vServerShallow);
+               vServerAudit.setDoesObjectExist(vServerExists);
+               vServerAudit.setResourceURI(vserverURI.build());
+               vServerAudit.setAaiObjectType(AAIObjectType.VSERVER.typeName());
+               auditList.getAuditList().add(vServerAudit);
+               if (vServer.getLInterfaces() != null) {
+                       vServer.getLInterfaces().getLInterface().stream().forEach(lInterface -> auditList.getAuditList().addAll(doesLinterfaceExistinAAI(lInterface,
+                                                       vServer.getVserverId(), tenantId, cloudOwner, cloudRegion).getAuditList()));
                }
-               return vServerExists && allNeutronNetworksExist;
+               return auditList;
        }
 
-       private boolean doesLinterfaceExistinAAI(LInterface lInterface, String vServerId, String tenantId,
+       private AAIObjectAuditList doesLinterfaceExistinAAI(LInterface lInterface, String vServerId, String tenantId,
                        String cloudOwner, String cloudRegion) {
-               boolean doesLInterfaceExist = false;
-               boolean doSubInterfacesExist = true;
+               AAIObjectAuditList auditList = new AAIObjectAuditList();
+               AAIObjectAudit lInterfaceAudit = new AAIObjectAudit();
                AAIResourceUri linterfaceURI = AAIUriFactory
-                               .createResourceUri(AAIObjectPlurals.L_INTERFACE, cloudOwner, cloudRegion, tenantId, vServerId)
-                               .queryParam("interface-id", lInterface.getInterfaceId());
-               Optional<LInterfaces> queriedLInterface = getAaiClient().get(LInterfaces.class, linterfaceURI);
+                               .createResourceUri(AAIObjectType.L_INTERFACE, cloudOwner, cloudRegion, tenantId, vServerId, lInterface.getInterfaceName());
+               Optional<LInterface> queriedLInterface = getAaiClient().get(LInterface.class, linterfaceURI);
                if (queriedLInterface.isPresent()) {
-                       if (queriedLInterface.get().getLInterface().size() > 1) {
-                               logger.error("Non-Unique LInterface Found stopping audit, L-Interface Id: " +lInterface.getInterfaceId());
-                               doesLInterfaceExist = false;
-                       } else {
-                               doesLInterfaceExist = true;
-                               lInterface.setInterfaceName(queriedLInterface.get().getLInterface().get(0).getInterfaceName());
-                       }
+                               lInterfaceAudit.setDoesObjectExist(true);
+                               lInterface.setInterfaceName(lInterface.getInterfaceName());     
                }
-               logger.info("l-interface id:{} name: {} exists: {}", lInterface.getInterfaceId(), lInterface.getInterfaceName(),
-                               doesLInterfaceExist);
+               LInterface lInterfaceShallow = new LInterface();
+               BeanUtils.copyProperties(lInterface,lInterfaceShallow,"LInterfaces");
+               lInterfaceAudit.setAaiObject(lInterface);
+               lInterfaceAudit.setResourceURI(linterfaceURI.build());
+               lInterfaceAudit.setAaiObjectType(AAIObjectType.L_INTERFACE.typeName());
+               auditList.getAuditList().add(lInterfaceAudit);
+               logger.info("l-interface id:{} name: {} exists: {} ", lInterface.getInterfaceId(), lInterface.getInterfaceName(),
+                               lInterfaceAudit.isDoesObjectExist());
 
-               if (doesLInterfaceExist && lInterface.getLInterfaces() != null) {
-                       doSubInterfacesExist = lInterface.getLInterfaces().getLInterface()
-                                       .stream().filter(subInterface -> !doesSubInterfaceExistinAAI(subInterface,
-                                                       lInterface.getInterfaceName(), vServerId, tenantId, cloudOwner, cloudRegion))
-                                       .findFirst().map(v -> false).orElse(true);
-               } else
-                       logger.debug("l-interface {} does not contain any sub-iterfaces", lInterface.getInterfaceId());
+               if (lInterface.getLInterfaces() != null) {
+                       lInterface.getLInterfaces().getLInterface().stream()
+                                       .forEach(subInterface -> auditList.getAuditList().add(doesSubInterfaceExistinAAI(subInterface,
+                                                       lInterface.getInterfaceName(), vServerId, tenantId, cloudOwner, cloudRegion)));
+               }
+               logger.debug("l-interface {} does not contain any sub-iterfaces, skipping audit of sub-interfaces", lInterface.getInterfaceId());
 
-               return doesLInterfaceExist && doSubInterfacesExist;
+               return auditList;
        }
 
-       private boolean doesSubInterfaceExistinAAI(LInterface subInterface, String linterfaceName, String vServerId,
+       private AAIObjectAudit doesSubInterfaceExistinAAI(LInterface subInterface, String linterfaceName, String vServerId,
                        String tenantId, String cloudOwner, String cloudRegion) {
                logger.info("checking if sub-l-interface {} , linterfaceName: {} vserverId: {}  exists",
-                               subInterface.getInterfaceId(), linterfaceName, vServerId);
-
-               AAIResourceUri linterfaceURI = AAIUriFactory.createResourceUri(AAIObjectPlurals.SUB_L_INTERFACE, cloudOwner,
-                               cloudRegion, tenantId, vServerId, linterfaceName)
-                               .queryParam("interface-id", subInterface.getInterfaceId());
-
-               boolean doesExist = getAaiClient().exists(linterfaceURI);
-               logger.info("sub-l-interface {} exists: {}", subInterface.getInterfaceId(), doesExist);
-               return doesExist;
+                               subInterface.getInterfaceName(), linterfaceName, vServerId);
+               AAIObjectAudit subInterfaceAudit = new AAIObjectAudit();
+               
+               
+               AAIResourceUri subInterfaceURI = AAIUriFactory.createResourceUri(AAIObjectType.SUB_L_INTERFACE, cloudOwner,
+                               cloudRegion, tenantId, vServerId, linterfaceName, subInterface.getInterfaceName());
+               subInterfaceAudit.setResourceURI(subInterfaceURI.build());
+               boolean doesExist = getAaiClient().exists(subInterfaceURI);
+               logger.info("sub-l-interface-id:{} exists: {}", subInterface.getInterfaceId(), doesExist);
+               subInterfaceAudit.setAaiObject(subInterface);
+               subInterfaceAudit.setDoesObjectExist(doesExist);
+               subInterfaceAudit.setAaiObjectType(AAIObjectType.SUB_L_INTERFACE.typeName());
+               return subInterfaceAudit;
        }
 }