Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
5.4 kB
3
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.ericsson.msdp.spg.model.GeoPoint;
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.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Iterator;
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;

  public SelectNodesQueries(EaiRowMapper mapper, Long eaiType, String query, String queryA, String queryB, Map<Long, Long> aliasMapping, NodeIndexer<BasicNode> nodeIndexer) {
    this.mapper = mapper;
    this.eaiType = eaiType;
    this.query = query;
    this.queryA = queryA;
    this.queryB = queryB;
    this.aliasMapping = aliasMapping;
    this.nodeIndexer = nodeIndexer;
  }

  @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;
    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;
      }

      //all sites need to have regions. Security roles depends on it
//      if (networkNode instanceof SiteNode && ((SiteNode) networkNode).getRegion() == null) {
//        logger.warn("Site {} doesn't have an region assigned.", networkNode.getName());
//        ((SiteNode) networkNode).setRegion("UNKNOWN");
//      }
       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");
          }
//          } else if (((SiteNode) networkNode).getRegion().contains("Region")) {
//            logger.info("region*********{}", ((SiteNode) networkNode).getRegion());
//            System.out.println("region*********"+ ((SiteNode) networkNode).getRegion());
//            ((SiteNode) networkNode).setRegion("UNKNOWN");
//          }
      }
      nodeIndexer.index(networkNode);
    }
    nodeIndexer.flush();
    logger.info("Query for class type {} returned {} nodes.", mapper.getEaiClassName(), nodeIndexer.getCount());
    //python
    ProcessBuilder builder = new ProcessBuilder("python", "C:\\Users\\ensaani\\OneDrive - Ericsson\\Desktop\\NGRVA Projects\\pythontest.py", "2", "10"); // Replace with arguments
    Process process = builder.start();

    // Read and handle output from the Python script (optional)
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line= null;
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }

    
    
    
    
    
    
    return null;
  }
}
Leave a Comment