/**
   * Set up properties for the given virtual application object representing a NetScaler loading
   * balance. and its discovery extension. Create the GslbServiceAgent to capture the lbvserver and
   * app services that are configured on this target and are bound to the specified GSLB. Also
   * create the transaction commodity sold by the vApp.
   *
   * @param gslb VirtualApplication for the load balancing server
   * @param specificTarget NetScaler appliance target
   * @param vServerExt NetScalerGslbVirtualAppDiscExt Discovery extension for the virtual
   *     application
   * @param localName gslb server name to be set as local name in the discovery extension
   * @param vAppUuid UUID created for the virtual application
   * @param vAppType load balancing service protocol
   * @param classname static string "gslbvserver" to set as class name in the discovery extension
   * @param lbvServers list of lbvservers configured on the gslb on this target
   * @return modified vApp object
   */
  private VirtualApplication createGslbVirtualApplication(
      VirtualApplication gslb,
      VMTTarget specificTarget,
      NetScalerGslbVirtualAppDiscExt vServerExt,
      String localName,
      String vAppUuid,
      String vAppType,
      String classname,
      List<String> lbvServers) {
    // set properties in the virtual app and the discovery extension
    // and create the transaction commodity sold
    gslb =
        createVirtualApplication(
            gslb,
            specificTarget,
            vServerExt,
            localName, // service name as the local name
            "", // there is no IP and port associated with the GSLB
            0, // there is no port for the GSLB service
            vAppUuid, // uuid for the gslb vapp containing its service name only
            vAppType, // service type
            NetScalerUtil.GSLB_VSERVER // class name for the poller
            );

    // agent to represent this service on this target
    String agentName =
        NetScalerUtil.GSLB_AGENT_PREFIX + gslb.getName() + "-" + target.getNameOrAddress();
    GslbServiceAgent lbAgent =
        (GslbServiceAgent)
            createObject(MediationPackage.eINSTANCE.getGslbServiceAgent(), agentName);
    lbAgent.setDisplayName(gslb.getName() + "-" + target.getNameOrAddress());
    // discovery extension for the agent
    NetScalerGslbServiceAgentDiscExt agentDiscExt =
        (NetScalerGslbServiceAgentDiscExt)
            lbAgent.createExtension(
                DiscoveryExtensionsPackage.eINSTANCE.getNetScalerGslbServiceAgentDiscExt());
    // we set the GSLB server name as the local name
    setExtProps(
        agentDiscExt,
        specificTarget,
        NetScalerUtil.LB_VSERVER, // class name attribute in the extension used during polling
        agentName, // display name attribute in the extension
        localName, // local name attribute
        localName, // id attribute
        logPrefix);

    // attach to the vApp
    gslb.getAgents().add(lbAgent);
    agentDiscExt.getLbvServers().addAll(lbvServers);
    if (logger.isDebugEnabled())
      logger.debug(
          logPrefix
              + gslb.toVMTString()
              + " : create glsb agent on target "
              + target.getNameOrAddress());

    return gslb;
  }
  /**
   * Update the Gslb Agent attached to the Gslb application that the lbvserver is bound to
   *
   * @param vServerIP
   */
  private void updateGslbVAppAgent(String vServerIP, Integer vServerPort) {

    // the gslb vApp that this lbvserver is bound to
    VirtualApplication gslb = lbvServerVIPToGslbVAppMap.get(vServerIP);
    // if (logger.isDebugEnabled())
    logger.info(
        logPrefix
            + "LB virtual server at VIP "
            + vServerIP
            + " on target "
            + target.getNameOrAddress()
            + " is bound to  GSLB "
            + gslb.toVMTString());
    // port for the GSLB service
    gslb.setPort(vServerPort);

    // gslb agent for this target
    GslbServiceAgent lbAgent = NetScalerUtil.getTargetAgent(gslb, target);
    if (lbAgent == null) {
      logger.warn("null gslb service agent for gslb " + gslb.toVMTString());
      // TODO: create it ?
    }
    NetScalerGslbServiceAgentDiscExt lbAgentDiscExt =
        (NetScalerGslbServiceAgentDiscExt) lbAgent.getDiscoveryExtension();
    Map<String, String> vipToServiceName = lbAgentDiscExt.getVipToLbServiceName();
    if (vipToServiceName == null) {
      vipToServiceName = new HashMap<String, String>();
      lbAgentDiscExt.setVipToLbServiceName(vipToServiceName);
    }
    vipToServiceName.put(vServerIP, lbvserver_ip2serviceName.get(vServerIP));

    Map<String, Map<String, String>> vipToAppServices = lbAgentDiscExt.getVipToAppServices();
    if (vipToAppServices == null) {
      vipToAppServices = new HashMap<String, Map<String, String>>();
      lbAgentDiscExt.setVipToAppServices(vipToAppServices);
    }
    vipToAppServices.put(vServerIP, ip2NameIPPort);

    if (logger.isDebugEnabled()) {
      logger.debug(
          logPrefix
              + " added vipToServiceName Map : "
              + lbAgentDiscExt.getVipToLbServiceName()
              + " for "
              + lbAgent.toVMTString());
      logger.debug(
          logPrefix
              + " added vipToAppServices Map : "
              + lbAgentDiscExt.getVipToAppServices()
              + " for "
              + lbAgent.toVMTString());
    }

    // create IP objects for the app service endpoints.
    for (String ip : ip2NameIPPort.keySet()) {
      IP serviceIP = DiscoveryUtil.createOrGetIPObj(ip, logPrefix);
      if (logger.isDebugEnabled())
        logger.debug(
            target.getNameOrAddress()
                + " created IP : "
                + serviceIP.toVMTString()
                + " for "
                + ip
                + " :: Name/IP/Port : "
                + ip2NameIPPort.get(ip));
    }
  }