Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
13 kB
1
Indexable
Never
package com.ericsson.msdp.spg.gcp.eai;

import com.ericsson.msdp.spg.gcp.GenericQuery;
import com.ericsson.msdp.spg.model.*;
import com.ericsson.msdp.spg.repositories.EaiRowMapper;
import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableResult;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Getter
class SelectNodesQueries implements GenericQuery<Void> {

	private static Logger logger = LoggerFactory.getLogger(SelectNodesQueries.class);
	private EaiRowMapper mapper;
	private long eaiType;

	private String query;
	private String queryA;
	private String queryB;

	private Map<Long, Long> aliasMapping;
	private NodeIndexer<BasicNode> nodeIndexer;
	private Map mainMap;
	private Long typeId;
	private Map mainMapForTypeId;

	public SelectNodesQueries(EaiRowMapper mapper, Long eaiType, String query, String queryA, String queryB,
			Map<Long, Long> aliasMapping, NodeIndexer<BasicNode> nodeIndexer, Map mainMap, Long typeId, Map mainMapForTypeId) {
		this.mapper = mapper;
		this.eaiType = eaiType;
		this.query = query;
		this.queryA = queryA;
		this.queryB = queryB;
		this.aliasMapping = aliasMapping;
		this.nodeIndexer = nodeIndexer;
		this.mainMap = mainMap;
		this.typeId = typeId;
		this.mainMapForTypeId = mainMapForTypeId;
	}
	//s2
	private void deduplicateByEquipmentIP(List<SiteNode> siteList, List<NetworkNode> networkList) {
	    Map<String, BasicNode> ipToNodeMap = new HashMap<>();
	    List<BasicNode> deduplicatedNodes = new ArrayList<>();

	     for (SiteNode siteNode : siteList) {
	    	// logger.info("Attribute def id {}", );
	        String equipmentIP = siteNode.getAttributes().stream()
	                .filter(attr -> attr.getName().equals("244"))//equalsIgnoreCase("Equipment IP Address")
	                .map(Attribute::getValue)
	                .findFirst()
	                .orElse(null);
	        
//	        List<String> equipmentIP = siteNode.getAttributes().stream()
//	        		  .filter(attr -> attr.getName().equals("244") 
//	        		          || attr.getName().equalsIgnoreCase("Equipment IP Address"))
//	        		  .map(Attribute::getValue)
//	        		  .collect(Collectors.toList());

	        if (equipmentIP != null) {
	        	logger.info("Node with Equipment IP Address: {}", equipmentIP);
	            if (ipToNodeMap.containsKey(equipmentIP)) {
	                BasicNode existingNode = ipToNodeMap.get(equipmentIP);
	                if (existingNode.getName().matches("\\d+\\.\\d+\\.\\d+\\.\\d+")) {
	                    existingNode.setName(siteNode.getName());
	                }
	            } else {
	                ipToNodeMap.put(equipmentIP, siteNode);
	                deduplicatedNodes.add(siteNode);
	            }
	        } else {
	            deduplicatedNodes.add(siteNode);
	        }
	    }

	    for (NetworkNode networkNode : networkList) {
	        String equipmentIP = networkNode.getAttributes().stream()
	                .filter(attr -> attr.getName().equals("244"))
	                .map(Attribute::getValue)
	                .findFirst()
	                .orElse(null);

	        if (equipmentIP != null) {
	            if (ipToNodeMap.containsKey(equipmentIP)) {
	                BasicNode existingNode = ipToNodeMap.get(equipmentIP);
	                if (existingNode.getName().matches("\\d+\\.\\d+\\.\\d+\\.\\d+")) {
	                    existingNode.setName(networkNode.getName());
	                }
	            } else {
	                ipToNodeMap.put(equipmentIP, networkNode);
	                deduplicatedNodes.add(networkNode);
	            }
	        } else {
	            deduplicatedNodes.add(networkNode);
	        }
	    }

	    siteList.clear();
	    siteList.addAll(deduplicatedNodes.stream().filter(node -> node instanceof SiteNode).map(node -> (SiteNode) node).collect(Collectors.toList()));

	    networkList.clear();
	    networkList.addAll(deduplicatedNodes.stream().filter(node -> node instanceof NetworkNode).map(node -> (NetworkNode) node).collect(Collectors.toList()));
	}
	private boolean deduplicate;

	public void setDeduplicate(boolean deduplicate) {
	    this.deduplicate = deduplicate;
	}

	@Override
	public Void execute(Function<QueryJobConfiguration.Builder, TableResult> fun) {

		Iterator<FieldValueList> attributes = fun.apply(QueryJobConfiguration.newBuilder(queryA)).iterateAll()
				.iterator();
		FieldValueList link = null;
		Iterator<FieldValueList> links = null;
		if (StringUtils.isNotEmpty(queryB)) {
			links = fun.apply(QueryJobConfiguration.newBuilder(queryB)).iterateAll().iterator();
			link = links.hasNext() ? links.next() : null;
		}

		FieldValueList attr = attributes.hasNext() ? attributes.next() : null;
		List<SiteNode> networkNodeList = new ArrayList<SiteNode>();
		List<NetworkNode> networkNodeList2 = new ArrayList<NetworkNode>();
		Map localSiteMap = new HashMap();
		Map localNetworkMap = new HashMap();
		//Map localMapForSiteId = new HashMap();
		List<SiteNode> siteList = null;
		List<NetworkNode> networkList = null;
		//List<String> eaiIdWithDuplicateNodes = new ArrayList<String>();
		for (FieldValueList row : fun.apply(QueryJobConfiguration.newBuilder(query)).iterateAll()) {
			var networkNode = mapper.mapMain(row, nodeIndexer.newNode());
			networkNode.mapId(Id.of(eaiType, networkNode.getEaiId()));
			if (networkNode instanceof NetworkNode) {
				((NetworkNode) networkNode).setSite(
						SiteId.of(Id.of(row.get("SITE_TYPE").getLongValue(), row.get("SITE_ID").getLongValue())));
				while (link != null && link.get("SOURCE_OBJECT_ID").getLongValue() == networkNode.getEaiId()) {
					FieldValue targetObjectType = link.get("TARGET_OBJECT_TYPE");
					FieldValue targetObjectId = link.get("TARGET_OBJECT_ID");
					Long targetType = aliasMapping.getOrDefault(targetObjectType.getLongValue(),
							targetObjectType.getLongValue());
					((NetworkNode) networkNode).getLinks()
							.add(Id.of(targetType, targetObjectId.getLongValue()).toString());
					link = links.hasNext() ? links.next() : null;
				}
			}
			if (networkNode instanceof SiteNode) {
				try {
					((SiteNode) networkNode).setLocation(
							GeoPoint.of(row.get("LATITUDE").getDoubleValue(), row.get("LONGITUDE").getDoubleValue()));
				} catch (Exception e) {
					logger.warn("Site {} has no geo location available", networkNode.getName());
				}
			}
			while (attr != null && attr.get("OWNING_OBJECT_ID").getLongValue() == networkNode.getEaiId()) {
				mapper.mapAttr(attr, networkNode);
				attr = attributes.hasNext() ? attributes.next() : null;
			}
			if (networkNode instanceof SiteNode) {
				if (((SiteNode) networkNode).getRegion() == null) {
					logger.info("Site {} doesn't have an region assigned.", networkNode.getName());
					System.out.println("Site  doesn't have an region assigned." + networkNode.getName());
					((SiteNode) networkNode).setRegion("UNKNOWN");
				}
			}
			if (networkNode instanceof SiteNode) {
				networkNodeList.add((SiteNode) networkNode);
			} else if (networkNode instanceof NetworkNode) {
				networkNodeList2.add((NetworkNode) networkNode);
			}
		}
		//s2list 
		if (networkNodeList != null && !networkNodeList.isEmpty()) {

			for (SiteNode networkNodeVar : networkNodeList) {
				if (!mainMap.containsKey(networkNodeVar.getName().toString())) {
					mainMap.put(networkNodeVar.getName().toString(), networkNodeVar);
					localSiteMap.put(networkNodeVar.getName().toString(), networkNodeVar);
					//mainMapForTypeId.put(networkNodeVar.getName(),typeId);
				} else if (!networkNodeVar.getCloudNode().booleanValue()) {
					//eaiIdWithDuplicateNodes.add(networkNodeVar.getId());
					//localMapForSiteId.put(networkNodeVar.getName(),mainMapForTypeId.get(networkNodeVar.getName().toString()));

				} else if (networkNodeVar.getCloudNode().booleanValue()) {
					SiteNode siteNodeTemp = null;
					NetworkNode networkNodeTemp = null;
					try {
						siteNodeTemp = (SiteNode) mainMap.get(networkNodeVar.getName().toString());
					} catch (Exception e) {
						networkNodeTemp = (NetworkNode) mainMap.get(networkNodeVar.getName().toString());
					}
					if(siteNodeTemp != null ? !(siteNodeTemp.getCloudNode().booleanValue()) : !(networkNodeTemp.getCloudNode().booleanValue()))
						mainMapForTypeId.put(siteNodeTemp != null ? siteNodeTemp.getName().toString() : networkNodeTemp.getName().toString(),typeId);
					mainMap.put(networkNodeVar.getName().toString(), networkNodeVar);
					localSiteMap.put(networkNodeVar.getName().toString(), networkNodeVar);
					//eaiIdWithDuplicateNodes.add(siteNodeTemp != null ? siteNodeTemp.getId() : networkNodeTemp.getId());
				}
			}
			siteList = new ArrayList<SiteNode>(localSiteMap.values());
		}

		if (networkNodeList2 != null && !networkNodeList2.isEmpty()) {
			for (NetworkNode networkNodeVar : networkNodeList2) {
				if (!mainMap.containsKey(networkNodeVar.getName().toString())) {
					mainMap.put(networkNodeVar.getName().toString(), networkNodeVar);
					localNetworkMap.put(networkNodeVar.getName().toString(), networkNodeVar);
					if ("APX-HYD-VIJ3-TDC-IXRD3L-PACO-LEF30".equals(networkNodeVar.getName().toString())) {
						logger.info("APX-HYD-VIJ3-TDC-IXRD3L-PACO-LEF30 node found 1.");
					}
				} else if (!networkNodeVar.getCloudNode().booleanValue()) {
					//eaiIdWithDuplicateNodes.add(networkNodeVar.getId());
					//localMapForSiteId.put(networkNodeVar.getName(),mainMapForTypeId.get(networkNodeVar.getName().toString()));

				} else if (networkNodeVar.getCloudNode().booleanValue()) {
					SiteNode siteNodeTemp = null;
					NetworkNode networkNodeTemp = null;
					try {
						siteNodeTemp = (SiteNode) mainMap.get(networkNodeVar.getName().toString());
					} catch (Exception e) {
						networkNodeTemp = (NetworkNode) mainMap.get(networkNodeVar.getName().toString());
					}
					if(siteNodeTemp != null ? !(siteNodeTemp.getCloudNode().booleanValue()) : !(networkNodeTemp.getCloudNode().booleanValue()))
						mainMapForTypeId.put(siteNodeTemp != null ? siteNodeTemp.getName().toString() : networkNodeTemp.getName().toString(),typeId);
					mainMap.put(networkNodeVar.getName().toString(), networkNodeVar);
					localNetworkMap.put(networkNodeVar.getName().toString(), networkNodeVar);
					//eaiIdWithDuplicateNodes.add(siteNodeTemp != null ? siteNodeTemp.getId() : networkNodeTemp.getId());
					if ("APX-HYD-VIJ3-TDC-IXRD3L-PACO-LEF30".equals(networkNodeVar.getName().toString())) {
						logger.info("APX-HYD-VIJ3-TDC-IXRD3L-PACO-LEF30 node found 2.");
						logger.info("APX-HYD-VIJ3-TDC-IXRD3L-PACO-LEF30 node found 2."
								+ mainMap.get(networkNodeVar.getName().toString()));
					}
				}
			}
			networkList = new ArrayList<NetworkNode>(localNetworkMap.values());
		}
		if (networkNodeList != null && !networkNodeList.isEmpty()) {
			//logger.info("duplicate node eai id count" + eaiIdWithDuplicateNodes.size());
			for (SiteNode networkNodeVar : siteList) {
				if ("APX-HYD-VIJ3-TDC-IXRD3L-PACO-LEF30".equals(networkNodeVar.getName().toString())) {
					logger.info("APX-HYD-VIJ3-TDC-IXRD3L-PACO-LEF30 node found.");
				}
				nodeIndexer.index(networkNodeVar);
			}
		}
		if (networkNodeList2 != null && !networkNodeList2.isEmpty()) {
			//logger.info("duplicate node eai id count2" + eaiIdWithDuplicateNodes.size());
			for (NetworkNode networkNodeVar : networkList) {
				if ("APX-HYD-VIJ3-TDC-IXRD3L-PACO-LEF30".equals(networkNodeVar.getName().toString())) {
					logger.info("APX-HYD-VIJ3-TDC-IXRD3L-PACO-LEF30 node found.");
				}
				nodeIndexer.index(networkNodeVar);
			}
		}
		//s2
		if (networkNodeList != null && !networkNodeList.isEmpty()) {
		    siteList = new ArrayList<>(localSiteMap.values());
		    deduplicateByEquipmentIP(siteList, null);
		}

		if (networkNodeList2 != null && !networkNodeList2.isEmpty()) {
		    networkList = new ArrayList<>(localNetworkMap.values());
		    deduplicateByEquipmentIP(null, networkList);
		}
		if (deduplicate) {
		    deduplicateByEquipmentIP(siteList, networkList);
		}
		nodeIndexer.flush();
		logger.info("Query for class type {} returned {} nodes.", mapper.getEaiClassName(), nodeIndexer.getCount());
		return null;
	}
}

Leave a Comment