Untitled

 avatar
unknown
plain_text
9 months ago
8.4 kB
8
Indexable
package com.ericsson.msdp.spg.repositories;

import com.ericsson.msdp.spg.model.Attribute;
import com.ericsson.msdp.spg.model.BasicNode;
import com.ericsson.msdp.spg.model.SiteNode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.FieldValueList;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.function.Consumer;

@Slf4j
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class EaiRowMapper {

  public static final String ID_FIELD_NAME = "SPG_ID_NODE_FIELD_NAME";
  @JsonIgnore
  static final Map<String, String> keyMap = Map.of("L", "LONG_VALUE", "S", "STRING_VALUE", "I", "INTEGER_VALUE", "D",
      "DOUBLE_VALUE", "B", "BOOLEAN_VALUE");
  /**
   * Not null only if there is a need to override the setting from EAI
   * NIM_NGO_NODE tables.
   */
  private String idFieldName;
  private String nameFieldName;
  private String subtypeFieldName;
  private String subtypeAttrName;
  private String defaultSubtype;
  private int physicalNode = -1;
  private String typeName;
  private String vendorFieldName;
  private String nodeAttrName = "Cloud Node Role";

  private String statusFieldName;

  private String ipAttrName;
  private String cloudNodeName = "Cloud Node";
  // original equipment manufacturer
  private String oemAttrName = "Cloud Node OEM";
  private List<Map<String, String>> attributesNames = new ArrayList<>();
  private List<String> dynAttributesNames = new LinkedList<>();
  private String eaiClassName;
  private List<String> aliasedClassNames;
  private String whereFilter;
  private String regionName;
  private String stateProv;
  private String city;

  public <T extends BasicNode> T mapMain(FieldValueList dlRow, T node) {
    FieldValue id = dlRow.get(ID_FIELD_NAME);
    node.setEaiId(id.getLongValue());
    node.setNodeType(typeName);
    node.setName(dlRow.get(nameFieldName).getStringValue());
    if (physicalNode > -1) {
      node.setPhysical(physicalNode == 1);
    }
    node.setCloudNode(false);
    node.setOriginalEquipmentManufacturer("Other");

    if (defaultSubtype != null) {
      node.setSubType(defaultSubtype);
    } else {
      node.setSubType(typeName);
    }
    if (subtypeFieldName != null) {
      FieldValue value = dlRow.get(subtypeFieldName);
      if (!value.isNull()) {
        node.setSubType(sanitizeSubType(value));
      }
    }
    try {
      if (!StringUtils.isEmpty(vendorFieldName)) {
        FieldValue value = dlRow.get(vendorFieldName);
        if (!value.isNull()) {
          node.setVendor(value.getStringValue());
        }
      }
    } catch (IllegalArgumentException e) {
      // nop
    }

    try {
      if (!StringUtils.isEmpty(statusFieldName)) {
        FieldValue value = dlRow.get(statusFieldName);
        if (!value.isNull()) {
          node.setStatus(value.getStringValue());
        }
      }
    } catch (IllegalArgumentException e) {
      // nop
    }

    for (Map<String, String> attr : attributesNames) {
      FieldValue value = dlRow.get(attr.get("source"));
      if (!value.isNull()) {
        node.getAttributes().add(new Attribute(attr.get("target"), value.getStringValue()));
      }
    }

    if (node instanceof SiteNode) {
      try {
        FieldValue value = dlRow.get("NPA_NXX");
        System.out.println("================ set region if block ==============> " + value);
        if (!value.isNull()) {
          // ((SiteNode) node).setRegion(value.getStringValue());
          ((SiteNode) node).setNpaNxx(value.getStringValue());
        }
      } catch (Exception e) {
        log.debug("Missing field?", e);
      }
    }
    if (node instanceof SiteNode) {
      try {
        FieldValue value = dlRow.get("STATE_PROV");
        if (!value.isNull()) {
          ((SiteNode) node).setStateProv(value.getStringValue());
        }
      } catch (Exception e) {
        log.debug("Missing field?", e);
      }
    }
    if (node instanceof SiteNode) {
      try {
        FieldValue value = dlRow.get("CITY");
        if (!value.isNull()) {
          ((SiteNode) node).setCity(value.getStringValue());
        }
      } catch (Exception e) {
        log.debug("Missing field?", e);
      }
    }
    // System.out.println("=================== dlRow field list ================> " + dlRow);
    return node;
  }

  private String sanitizeSubType(FieldValue value) {
    return sanitizeSubType(value.getStringValue());
  }

  private String sanitizeSubType(String v1) {
    v1 = v1.toLowerCase(Locale.US).trim();
    try {
      int firstSpace = v1.indexOf(" ");
      if (firstSpace < 0) {
        firstSpace = v1.length();
      }
      if (v1.startsWith("network-cloud||")) {
        return v1.substring("network-cloud||".length(), firstSpace);
      }
      if (v1.startsWith("network||")) {
        return v1.substring("network||".length(), firstSpace);
      }
    } catch (Exception e) {
      // nop;
    }
    return v1;
  }

  public <T extends BasicNode> T mapAttr(FieldValueList dlRow, T node) {
    if (dlRow.get("ATTRIBUTE_LABEL").isNull()) {
      log.warn("[BUG] Attribute label missing for node {}. This should not happen.", node.getId());
      return node;
    }

    if (setDefinedField(dlRow, vendorFieldName, s -> node.setVendor(capitalize(s))))
      return node;
    if (setDefinedField(dlRow, nodeAttrName, s -> node.setRole(capitalize(s))))
      return node;
    if (setDefinedField(dlRow, oemAttrName, s -> node.setOriginalEquipmentManufacturer(capitalize(s))))
      return node;
    if (setDefinedField(dlRow, cloudNodeName,
        v -> node.setCloudNode("yes".equalsIgnoreCase(v) || "y".equalsIgnoreCase(v)))) {
      if (physicalNode > -1) {
        node.setPhysical(physicalNode == 1);
      }
    }
    setDefinedField(dlRow, subtypeAttrName, s -> node.setSubType(sanitizeSubType(s)));
    if (setDefinedField(dlRow, ipAttrName, s -> node.setIpAddress(s)))
      return node;
    if (node instanceof SiteNode) {
      if (setDefinedField(dlRow, regionName, v -> ((SiteNode) node).setRegion(v)))
        return node;
    }

    // all the rest
    String label = dlRow.get("ATTRIBUTE_LABEL").getStringValue();
    for (String attr : dynAttributesNames) {
      if (attr.equals(label)) {
        FieldValue value = dlRow.get(keyMap.get(dlRow.get("TYPE").getStringValue()));
        if (!value.isNull()) {
          node.addOrUpdateOrAttr(label, value.getStringValue());
        }
      }
    }
    return node;
  }

  private boolean setDefinedField(FieldValueList dlRow, List<String> expectedLabelNames, Consumer<String> fieldSetter) {
    String label = dlRow.get("ATTRIBUTE_LABEL").getStringValue();
    for (var expectedLabelName : expectedLabelNames) {
      if (!StringUtils.isEmpty(expectedLabelName) && label.equalsIgnoreCase(expectedLabelName)) {
        if (!dlRow.get("STRING_VALUE").isNull()) {
          fieldSetter.accept(dlRow.get("STRING_VALUE").getStringValue());
        }
        return true;
      }
    }
    return false;
  }

  /**
   * Set the field of provided name if label match
   *
   * @param dlRow
   * @param expectedLabelName, expected label name, can be null
   * @return true if field matches.
   */
  private boolean setDefinedField(FieldValueList dlRow, String expectedLabelName, Consumer<String> fieldSetter) {
    return setDefinedField(dlRow, Collections.singletonList(expectedLabelName), fieldSetter);
  }

  private String capitalize(String string_value) {
    if (string_value == null) {
      return null;
    }

    if (string_value.length() > 4) {
      return StringUtils.capitalize(string_value.toLowerCase());
    } else {
      return StringUtils.capitalize(string_value);
    }
  }

  public List<String> getAllClassNames() {
    if (aliasedClassNames != null) {
      var list = new ArrayList<>(aliasedClassNames);
      list.add(eaiClassName);
      return list;
    } else {
      return Collections.singletonList(eaiClassName);
    }
  }
}
Editor is loading...
Leave a Comment