List of usage examples for java.util Formatter Formatter
public Formatter(OutputStream os, String csn) throws UnsupportedEncodingException
From source file:com.jaspersoft.jasperserver.util.test.BaseServiceSetupTestNG.java
public String msg(String text, Object... info) { StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb, LocaleContextHolder.getLocale()); formatter.format(text, info);/*w w w .j a va2 s .com*/ return sb.toString(); }
From source file:com.cloud.network.resource.NitroError.java
@SuppressWarnings("static-access") private synchronized boolean enableAutoScaleConfig(LoadBalancerTO loadBalancerTO, boolean isCleanUp) throws Exception { String vmGroupIdentifier = generateAutoScaleVmGroupIdentifier(loadBalancerTO); String srcIp = loadBalancerTO.getSrcIp(); int srcPort = loadBalancerTO.getSrcPort(); String nsVirtualServerName = generateNSVirtualServerName(srcIp, srcPort); String serviceGroupName = generateAutoScaleServiceGroupName(loadBalancerTO); String profileName = generateAutoScaleProfileName(vmGroupIdentifier); String timerName = generateAutoScaleTimerName(vmGroupIdentifier); String scaleDownActionName = generateAutoScaleScaleDownActionName(vmGroupIdentifier); String scaleUpActionName = generateAutoScaleScaleUpActionName(vmGroupIdentifier); String mtName = generateSnmpMetricTableName(vmGroupIdentifier); String monitorName = generateSnmpMonitorName(vmGroupIdentifier); AutoScaleVmGroupTO vmGroupTO = loadBalancerTO.getAutoScaleVmGroupTO(); AutoScaleVmProfileTO profileTO = vmGroupTO.getProfile(); List<AutoScalePolicyTO> policies = vmGroupTO.getPolicies(); int interval = vmGroupTO.getInterval(); profileTO.getCounterParamList();//from w w w .j a v a2 s . c o m String snmpCommunity = null; int snmpPort = DEFAULT_SNMP_PORT; long cur_prirotiy = 1; // get the session persistence parameters List<Pair<String, String>> paramsList = profileTO.getCounterParamList(); for (Pair<String, String> param : paramsList) { if ("snmpcommunity".equalsIgnoreCase(param.first())) { snmpCommunity = param.second(); } else if ("snmpport".equalsIgnoreCase(param.first())) { snmpPort = Integer.parseInt(param.second()); } } try { // Set min and max autoscale members; // add lb vserver lb http 10.102.31.100 80 -minAutoscaleMinMembers 3 -maxAutoscaleMembers 10 int minAutoScaleMembers = vmGroupTO.getMinMembers(); int maxAutoScaleMembers = vmGroupTO.getMaxMembers(); lbvserver vserver = new lbvserver(); try { vserver.set_name(nsVirtualServerName); vserver.set_minautoscalemembers(minAutoScaleMembers); vserver.set_maxautoscalemembers(maxAutoScaleMembers); lbvserver.update(_netscalerService, vserver); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } /* AutoScale Config */ // Add AutoScale Profile // add autoscale profile lb_asprofile CLOUDSTACK -url -http:// 10.102.31.34:8080/client/api- -apiKey abcdef // -sharedSecret xyzabc String apiKey = profileTO.getAutoScaleUserApiKey(); String secretKey = profileTO.getAutoScaleUserSecretKey(); String url = profileTO.getCloudStackApiUrl(); autoscaleprofile autoscaleProfile = new autoscaleprofile(); try { autoscaleProfile.set_name(profileName); autoscaleProfile.set_type("CLOUDSTACK"); autoscaleProfile.set_apikey(apiKey); autoscaleProfile.set_sharedsecret(secretKey); autoscaleProfile.set_url(url); autoscaleprofile.add(_netscalerService, autoscaleProfile); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } // Add Timer nstimer timer = new nstimer(); try { timer.set_name(timerName); timer.set_interval(interval); nstimer.add(_netscalerService, timer); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } // AutoScale Actions Integer scaleUpQuietTime = null; Integer scaleDownQuietTime = null; for (AutoScalePolicyTO autoScalePolicyTO : policies) { if (scaleUpQuietTime == null) { if (isScaleUpPolicy(autoScalePolicyTO)) { scaleUpQuietTime = autoScalePolicyTO.getQuietTime(); if (scaleDownQuietTime != null) { break; } } } if (scaleDownQuietTime == null) { if (isScaleDownPolicy(autoScalePolicyTO)) { scaleDownQuietTime = autoScalePolicyTO.getQuietTime(); if (scaleUpQuietTime != null) { break; } } } } // Add AutoScale ScaleUp action // add autoscale action lb_scaleUpAction provision -vserver lb -profilename lb_asprofile -params // -lbruleid=1234&command=deployvm&zoneid=10&templateid=5&serviceofferingid=3- -quiettime 300 com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleUpAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction(); try { scaleUpAction.set_name(scaleUpActionName); scaleUpAction.set_type("SCALE_UP"); // TODO: will this be called provision? scaleUpAction.set_vserver(nsVirtualServerName); // Actions Vserver, the one that is autoscaled, with CS // now both are same. Not exposed in API. scaleUpAction.set_profilename(profileName); if (scaleUpQuietTime != null) { scaleUpAction.set_quiettime(scaleUpQuietTime); } String scaleUpParameters = "command=deployVirtualMachine" + "&" + ApiConstants.ZONE_ID + "=" + profileTO.getZoneId() + "&" + ApiConstants.SERVICE_OFFERING_ID + "=" + profileTO.getServiceOfferingId() + "&" + ApiConstants.TEMPLATE_ID + "=" + profileTO.getTemplateId() + "&" + ApiConstants.DISPLAY_NAME + "=" + profileTO.getVmName() + "&" + ((profileTO.getNetworkId() == null) ? "" : (ApiConstants.NETWORK_IDS + "=" + profileTO.getNetworkId() + "&")) + ((profileTO.getOtherDeployParams() == null) ? "" : (profileTO.getOtherDeployParams() + "&")) + "lbruleid=" + loadBalancerTO.getUuid(); scaleUpAction.set_parameters(scaleUpParameters); autoscaleaction.add(_netscalerService, scaleUpAction); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction scaleDownAction = new com.citrix.netscaler.nitro.resource.config.autoscale.autoscaleaction(); Integer destroyVmGracePeriod = profileTO.getDestroyVmGraceperiod(); try { scaleDownAction.set_name(scaleDownActionName); scaleDownAction.set_type("SCALE_DOWN"); // TODO: will this be called de-provision? scaleDownAction.set_vserver(nsVirtualServerName); // TODO: no global option as of now through Nitro. // Testing cannot be done. scaleDownAction.set_profilename(profileName); scaleDownAction.set_quiettime(scaleDownQuietTime); String scaleDownParameters = "command=destroyVirtualMachine" + "&" + "lbruleid=" + loadBalancerTO.getUuid(); scaleDownAction.set_parameters(scaleDownParameters); scaleDownAction.set_vmdestroygraceperiod(destroyVmGracePeriod); autoscaleaction.add(_netscalerService, scaleDownAction); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } /* Create min member policy */ String minMemberPolicyName = generateAutoScaleMinPolicyName(vmGroupIdentifier); String minMemberPolicyExp = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.LT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MINAUTOSCALEMEMBERS)"; addAutoScalePolicy(timerName, minMemberPolicyName, cur_prirotiy++, minMemberPolicyExp, scaleUpActionName, interval, interval, isCleanUp); /* Create max member policy */ String maxMemberPolicyName = generateAutoScaleMaxPolicyName(vmGroupIdentifier); String maxMemberPolicyExp = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.GT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MAXAUTOSCALEMEMBERS)"; addAutoScalePolicy(timerName, maxMemberPolicyName, cur_prirotiy++, maxMemberPolicyExp, scaleDownActionName, interval, interval, isCleanUp); /* Create Counters */ HashMap<String, Integer> snmpMetrics = new HashMap<String, Integer>(); for (AutoScalePolicyTO autoScalePolicyTO : policies) { List<ConditionTO> conditions = autoScalePolicyTO.getConditions(); String policyExpression = ""; int snmpCounterNumber = 0; for (ConditionTO conditionTO : conditions) { CounterTO counterTO = conditionTO.getCounter(); String counterName = counterTO.getName(); String operator = conditionTO.getRelationalOperator(); long threshold = conditionTO.getThreshold(); StringBuilder conditionExpression = new StringBuilder(); Formatter formatter = new Formatter(conditionExpression, Locale.US); if (counterTO.getSource().equals("snmp")) { counterName = generateSnmpMetricName(counterName); if (snmpMetrics.size() == 0) { // Create Metric Table //add lb metricTable lb_metric_table lbmetrictable metricTable = new lbmetrictable(); try { metricTable.set_metrictable(mtName); lbmetrictable.add(_netscalerService, metricTable); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } // Create Monitor // add lb monitor lb_metric_table_mon LOAD -destPort 161 -snmpCommunity public -metricTable // lb_metric_table -interval <policy_interval == 80% > lbmonitor monitor = new lbmonitor(); try { monitor.set_monitorname(monitorName); monitor.set_type("LOAD"); monitor.set_destport(snmpPort); monitor.set_snmpcommunity(snmpCommunity); monitor.set_metrictable(mtName); monitor.set_interval((int) (interval * 0.8)); lbmonitor.add(_netscalerService, monitor); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } // Bind monitor to servicegroup. // bind lb monitor lb_metric_table_mon lb_autoscaleGroup -passive servicegroup_lbmonitor_binding servicegroup_monitor_binding = new servicegroup_lbmonitor_binding(); try { servicegroup_monitor_binding.set_servicegroupname(serviceGroupName); servicegroup_monitor_binding.set_monitor_name(monitorName); // Use the monitor for autoscaling purpose only. // Don't mark service members down when metric breaches threshold servicegroup_monitor_binding.set_passive(true); servicegroup_lbmonitor_binding.add(_netscalerService, servicegroup_monitor_binding); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } } boolean newMetric = !snmpMetrics.containsKey(counterName); if (newMetric) { snmpMetrics.put(counterName, snmpCounterNumber++); } if (newMetric) { // bind lb metricTable lb_metric_table mem 1.3.6.1.4.1.2021.11.9.0 String counterOid = counterTO.getValue(); lbmetrictable_metric_binding metrictable_metric_binding = new lbmetrictable_metric_binding(); try { metrictable_metric_binding.set_metrictable(mtName); metrictable_metric_binding.set_metric(counterName); metrictable_metric_binding.set_Snmpoid(counterOid); lbmetrictable_metric_binding.add(_netscalerService, metrictable_metric_binding); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } // bind lb monitor lb_metric_table_mon -metric cpu -metricThreshold 1 lbmonitor_metric_binding monitor_metric_binding = new lbmonitor_metric_binding(); ; try { monitor_metric_binding.set_monitorname(monitorName); monitor_metric_binding.set_metric(counterName); /* * Setting it to max to make sure traffic is not affected due to 'LOAD' monitoring. * For Ex. if CPU is tracked and CPU is greater than 80, it is still < than Integer.MAX_VALUE * so traffic will continue to flow. */ monitor_metric_binding.set_metricthreshold(Integer.MAX_VALUE); lbmonitor_metric_binding.add(_netscalerService, monitor_metric_binding); } catch (Exception e) { // Ignore Exception on cleanup if (!isCleanUp) throw e; } } // SYS.VSERVER("abcd").SNMP_TABLE(0).AVERAGE_VALUE.GT(80) int counterIndex = snmpMetrics.get(counterName); // TODO: temporary fix. later on counter name // will be added as a param to SNMP_TABLE. formatter.format("SYS.VSERVER(\"%s\").SNMP_TABLE(%d).AVERAGE_VALUE.%s(%d)", nsVirtualServerName, counterIndex, operator, threshold); } else if (counterTO.getSource().equals("netscaler")) { //SYS.VSERVER("abcd").RESPTIME.GT(10) formatter.format("SYS.VSERVER(\"%s\").%s.%s(%d)", nsVirtualServerName, counterTO.getValue(), operator, threshold); } if (policyExpression.length() != 0) { policyExpression += " && "; } policyExpression += conditionExpression; } policyExpression = "(" + policyExpression + ")"; String policyId = Long.toString(autoScalePolicyTO.getId()); String policyName = generateAutoScalePolicyName(vmGroupIdentifier, policyId); String action = null; if (isScaleUpPolicy(autoScalePolicyTO)) { action = scaleUpActionName; String scaleUpCondition = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.LT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MAXAUTOSCALEMEMBERS)"; policyExpression = scaleUpCondition + " && " + policyExpression; } else { action = scaleDownActionName; String scaleDownCondition = "SYS.VSERVER(\"" + nsVirtualServerName + "\").ACTIVESERVICES.GT(SYS.VSERVER(\"" + nsVirtualServerName + "\").MINAUTOSCALEMEMBERS)"; policyExpression = scaleDownCondition + " && " + policyExpression; } addAutoScalePolicy(timerName, policyName, cur_prirotiy++, policyExpression, action, autoScalePolicyTO.getDuration(), interval, isCleanUp); } } catch (Exception ex) { if (!isCleanUp) { // Normal course, exception has occurred disableAutoScaleConfig(loadBalancerTO, true); throw ex; } else { // Programming error. Exception should never be thrown afterall. throw ex; } } return true; }