use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.
the class VCDataService method syncCustomerAttrsData.
private void syncCustomerAttrsData(SDDCSoftwareConfig vcInfo) {
restClient.setServiceKey(serviceKeyConfig.getServiceKey());
try (VsphereClient vsphereClient = connectVsphere(vcInfo)) {
ServerMapping[] mappings = null;
try {
mappings = restClient.getServerMappingsByVC(vcInfo.getId()).getBody();
} catch (HttpClientErrorException clientError) {
if (clientError.getRawStatusCode() != HttpStatus.NOT_FOUND.value()) {
throw clientError;
}
mappings = new ServerMapping[0];
}
HashMap<String, ServerMapping> mobIdDictionary = new HashMap<String, ServerMapping>();
for (ServerMapping mapping : mappings) {
mobIdDictionary.put(mapping.getVcMobID(), mapping);
}
List<ServerMapping> validMapping = new ArrayList<ServerMapping>();
Collection<HostSystem> hosts = vsphereClient.getAllHost();
Map<String, HostSystem> hostDictionary = new HashMap<String, HostSystem>();
for (HostSystem host : hosts) {
String mobId = host._getRef().getValue();
String hostName = host.getName();
if (mobIdDictionary.containsKey(mobId)) {
ServerMapping serverMapping = mobIdDictionary.get(mobId);
if (!serverMapping.getVcHostName().equals(hostName)) {
// need to update the hostname.
serverMapping.setVcHostName(hostName);
restClient.saveServerMapping(serverMapping);
}
if (serverMapping.getAsset() != null) {
validMapping.add(serverMapping);
} else {
// check the hostNameIP mapping
String ipaddress = IPAddressUtil.getIPAddress(hostName);
if (null != ipaddress) {
AssetIPMapping[] ipMappings = restClient.getHostnameIPMappingByIP(ipaddress).getBody();
if (null != ipMappings && ipMappings.length > 0) {
// update the mapping
String assetName = ipMappings[0].getAssetname();
Asset asset = restClient.getAssetByName(assetName).getBody();
if (asset != null) {
serverMapping.setAsset(asset.getId());
restClient.saveServerMapping(serverMapping);
validMapping.add(serverMapping);
feedAssetMetricsFormulars(asset);
}
} else {
// seems we don't have the ip hostname mapping. Notify infoblox to check the ip
logger.info("Notify infoblox to check ip: " + ipaddress);
publisher.publish(null, ipaddress);
}
}
}
hostDictionary.put(mobId, host);
} else {
ServerMapping newMapping = new ServerMapping();
newMapping.setVcHostName(hostName);
newMapping.setVcID(vcInfo.getId());
newMapping.setVcMobID(mobId);
newMapping.setVcInstanceUUID(vsphereClient.getVCUUID());
String ipaddress = IPAddressUtil.getIPAddress(hostName);
logger.info(String.format("hostName %s, ipaddress: %s", hostName, ipaddress));
// publish message to queue.
if (null != ipaddress) {
logger.info("Notify infoblox");
publisher.publish(null, hostName);
AssetIPMapping[] ipMappings = restClient.getHostnameIPMappingByIP(ipaddress).getBody();
if (null != ipMappings && ipMappings.length > 0) {
// update the mapping
String assetName = ipMappings[0].getAssetname();
Asset asset = restClient.getAssetByName(assetName).getBody();
if (asset != null) {
newMapping.setAsset(asset.getId());
feedAssetMetricsFormulars(asset);
}
}
}
restClient.saveServerMapping(newMapping);
}
}
// feed meta data to VC.
Asset[] assets = restClient.getAssetsByVCID(vcInfo.getId()).getBody();
Map<String, Asset> assetDictionary = new HashMap<String, Asset>();
for (Asset asset : assets) {
assetDictionary.put(asset.getId(), asset);
}
feedData(assetDictionary, validMapping, hostDictionary);
validClusterHostsLocationAntiaffinity(vcInfo, assetDictionary, validMapping);
} catch (ConnectionException connectionException) {
checkAndUpdateIntegrationStatus(vcInfo, connectionException.getMessage());
return;
} catch (ExecutionException executionException) {
if (executionException.getCause() instanceof InvalidLogin) {
logger.error("Failed to push data to " + vcInfo.getServerURL(), executionException);
IntegrationStatus integrationStatus = vcInfo.getIntegrationStatus();
if (integrationStatus == null) {
integrationStatus = new IntegrationStatus();
}
integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
integrationStatus.setDetail("Invalid username or password.");
integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
updateIntegrationStatus(vcInfo);
return;
}
} catch (Exception exception) {
logger.error("Failed to push data to " + vcInfo.getServerURL(), exception);
}
}
use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.
the class VROAsyncJob method syncVROMetricData.
private void syncVROMetricData(SDDCSoftwareConfig config) {
if (!config.checkIsActive()) {
return;
}
VROConfig vro = new VROConfig(config);
long currentTime = System.currentTimeMillis();
MetricClient metricClient = null;
List<ResourceDto> hosts = null;
try {
metricClient = new MetricClient(vro, publisher);
hosts = metricClient.getHostSystemsResources();
} catch (AuthException authException) {
logger.error("Failed to connect to VROps manager ", authException);
IntegrationStatus integrationStatus = config.getIntegrationStatus();
if (integrationStatus == null) {
integrationStatus = new IntegrationStatus();
}
integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
integrationStatus.setDetail(authException.getMessage());
integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
updateIntegrationStatus(config);
return;
} catch (Exception e) {
if (e.getCause() instanceof UndeclaredThrowableException) {
UndeclaredThrowableException undeclaredThrowableException = (UndeclaredThrowableException) e.getCause();
if (undeclaredThrowableException.getUndeclaredThrowable().getCause() instanceof ConnectException) {
checkAndUpdateIntegrationStatus(config, undeclaredThrowableException.getUndeclaredThrowable().getCause().getMessage());
return;
}
}
throw e;
}
// read all host/asset maaping from apiserver
Map<String, Asset> assetDictionary = new HashMap<String, Asset>();
try {
restClient.setServiceKey(serviceKeyConfig.getServiceKey());
Asset[] servers = restClient.getAssetsByVRO(vro.getId()).getBody();
for (Asset server : servers) {
assetDictionary.put(server.getId(), server);
}
} catch (HttpClientErrorException clientError) {
if (clientError.getRawStatusCode() != HTTP_NOTFOUND) {
throw clientError;
}
}
ServerMapping[] mappings = null;
try {
mappings = restClient.getServerMappingsByVRO(vro.getId()).getBody();
} catch (HttpClientErrorException clientError) {
if (clientError.getRawStatusCode() != HTTP_NOTFOUND) {
throw clientError;
}
mappings = new ServerMapping[0];
}
HashMap<String, ServerMapping> entityNameDictionary = new HashMap<String, ServerMapping>();
HashMap<String, ServerMapping> objectIDDictionary = new HashMap<String, ServerMapping>();
for (ServerMapping mapping : mappings) {
entityNameDictionary.put(mapping.getVroVMEntityName(), mapping);
objectIDDictionary.put(mapping.getVroVMEntityObjectID(), mapping);
// vcIDDictionary.put(mapping.getVroVMEntityVCID(), mapping);
}
List<ServerMapping> validMapping = new ArrayList<ServerMapping>();
for (ResourceDto host : hosts) {
// There are several things that can be used as a identifier of a host.
// the first is the Name
// beside that there are three other identifier
// VMEntityName -- 10.112.113.160
// VMEntityObjectID host-81 the mobID of the host.
// VMEntityVCID this is the uuid of the vc object.
// currently we only add new servers and don't delete servers
List<ResourceIdentifier> identifiers = host.getResourceKey().getResourceIdentifiers();
boolean newHost = true;
String entityName = "";
String objectID = "";
String vcID = "";
ServerMapping refer = null;
for (ResourceIdentifier identifier : identifiers) {
if (!newHost) {
break;
}
switch(identifier.getIdentifierType().getName()) {
case EntityName:
entityName = identifier.getValue();
if (entityNameDictionary.containsKey(entityName)) {
newHost = false;
refer = entityNameDictionary.get(entityName);
}
break;
case VMEntityObjectID:
objectID = identifier.getValue();
if (objectIDDictionary.containsKey(objectID)) {
newHost = false;
refer = objectIDDictionary.get(objectID);
}
break;
case VMEntityVCID:
vcID = identifier.getValue();
break;
default:
break;
}
}
if (newHost) {
// try to notify the other system.
String ipaddress = IPAddressUtil.getIPAddress(entityName);
if (null != ipaddress) {
publisher.publish(null, ipaddress);
ipaddress = entityName;
}
ServerMapping newMapping = new ServerMapping();
newMapping.setVroID(vro.getId());
newMapping.setVroResourceName(host.getResourceKey().getName());
newMapping.setVroVMEntityName(entityName);
newMapping.setVroVMEntityObjectID(objectID);
newMapping.setVroVMEntityVCID(vcID);
newMapping.setVroResourceID(host.getIdentifier().toString());
AssetIPMapping[] ipMappings = restClient.getHostnameIPMappingByIP(ipaddress).getBody();
if (null != ipMappings && ipMappings.length > 0) {
// update the mapping
String assetName = ipMappings[0].getAssetname();
Asset asset = restClient.getAssetByName(assetName).getBody();
if (asset != null) {
newMapping.setAsset(asset.getId());
}
}
restClient.saveServerMapping(newMapping);
validMapping.add(newMapping);
} else {
if (refer.getAsset() == null) {
String ipaddress = IPAddressUtil.getIPAddress(refer.getVroVMEntityName());
AssetIPMapping[] ipMappings = restClient.getHostnameIPMappingByIP(ipaddress).getBody();
if (null != ipMappings && ipMappings.length > 0) {
// update the mapping
String assetName = ipMappings[0].getAssetname();
Asset asset = restClient.getAssetByName(assetName).getBody();
if (asset != null) {
refer.setAsset(asset.getId());
restClient.saveServerMapping(refer);
validMapping.add(refer);
}
} else {
if (null != ipaddress) {
logger.info("Notify Infoblox to query the assetname");
publisher.publish(null, ipaddress);
}
}
} else {
validMapping.add(refer);
}
}
}
// validMapping.stream().map(ServerMapping::getAssetID).collect(Collectors.toSet());
if (validMapping.isEmpty()) {
logger.info("No Mapping find.Sync nothing for this execution.");
return;
}
Long latencyFactor = latencyFactorMap.get(config.getServerURL());
if (latencyFactor == null) {
latencyFactor = 24L;
}
Long lastUpdateTimeStamp = lastUpdateTimeMap.get(config.getServerURL());
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = currentTime - FIVE_MINUTES * latencyFactor;
}
boolean hasNewData = false;
logger.info(String.format("Start prepare data.%s, lastUpdateTime:%s, latencyFactor:%s", executionCount, lastUpdateTimeStamp, latencyFactor));
long newUpdateTimeStamp = lastUpdateTimeStamp;
for (ServerMapping mapping : validMapping) {
if (mapping.getAsset() != null) {
MetricData[] sensorDatas = restClient.getServerRelatedSensorDataByServerID(mapping.getAsset(), lastUpdateTimeStamp, FIVE_MINUTES * latencyFactor).getBody();
Asset server = restClient.getAssetByID(mapping.getAsset()).getBody();
List<String> pdus = server.getPdus();
StatContents contents = new StatContents();
StatContent frontTemp = new StatContent();
List<Double> frontValues = new ArrayList<Double>();
List<Long> frontTimes = new ArrayList<Long>();
StatContent backTemp = new StatContent();
List<Double> backValues = new ArrayList<Double>();
List<Long> backTimes = new ArrayList<Long>();
StatContent pduAMPSValue = new StatContent();
List<Double> pduAMPSValues = new ArrayList<Double>();
List<Long> pduAMPSTimes = new ArrayList<Long>();
StatContent pduRealtimeVoltage = new StatContent();
List<Double> voltageValues = new ArrayList<Double>();
List<Long> voltageTimes = new ArrayList<Long>();
StatContent pduRealtimePower = new StatContent();
List<Double> powerValues = new ArrayList<Double>();
List<Long> powerTimes = new ArrayList<Long>();
StatContent frontHumidityPercent = new StatContent();
List<Double> frontHumidityValues = new ArrayList<Double>();
List<Long> frontHumidityTimes = new ArrayList<Long>();
StatContent backHumidityPercent = new StatContent();
List<Double> backHumidityValues = new ArrayList<Double>();
List<Long> backHumidityTimes = new ArrayList<Long>();
StatContent currentLoad = new StatContent();
List<Double> currentLoadValues = new ArrayList<Double>();
List<Long> currentLoadTimes = new ArrayList<Long>();
StatContent powerLoad = new StatContent();
List<Double> powerLoadValues = new ArrayList<Double>();
List<Long> powerLoadTimes = new ArrayList<Long>();
String pduId = null;
String currentMetricName = MetricName.SERVER_CONNECTED_PDU_CURRENT;
String powerMetricName = MetricName.SERVER_CONNECTED_PDU_POWER;
String voltageMetricName = MetricName.SERVER_VOLTAGE;
String currentLoadMetricName = MetricName.PDU_CURRENT_LOAD;
String powerLoadMetricName = MetricName.PDU_POWER_LOAD;
if (pdus != null && !pdus.isEmpty()) {
pduId = pdus.get(0);
currentMetricName = String.format(MetricKeyName.SERVER_CONNECTED_PDUX_TOTAL_CURRENT, pduId);
powerMetricName = String.format(MetricKeyName.SERVER_CONNECTED_PDUX_TOTAL_POWER, pduId);
currentLoadMetricName = String.format(MetricKeyName.SERVER_CONNECTED_PDUX_CURRENT_LOAD, pduId);
powerLoadMetricName = String.format(MetricKeyName.SERVER_CONNECTED_PDUX_POWER_LOAD, pduId);
}
for (MetricData data : sensorDatas) {
if (data.getTimeStamp() > newUpdateTimeStamp) {
newUpdateTimeStamp = data.getTimeStamp();
hasNewData = true;
}
String metricName = data.getMetricName();
if (metricName.equals(currentMetricName)) {
pduAMPSValues.add(data.getValueNum());
pduAMPSTimes.add(data.getTimeStamp());
continue;
} else if (metricName.equals(powerMetricName)) {
powerValues.add(data.getValueNum());
powerTimes.add(data.getTimeStamp());
continue;
} else if (metricName.equals(voltageMetricName)) {
voltageValues.add(data.getValueNum());
voltageTimes.add(data.getTimeStamp());
continue;
} else if (metricName.equals(currentLoadMetricName)) {
currentLoadValues.add(data.getValueNum());
currentLoadTimes.add(data.getTimeStamp());
continue;
} else if (metricName.equals(powerLoadMetricName)) {
powerLoadValues.add(data.getValueNum());
powerLoadTimes.add(data.getTimeStamp());
continue;
} else if (metricName.contains(MetricName.SERVER_FRONT_HUMIDITY)) {
frontHumidityValues.add(data.getValueNum());
frontHumidityTimes.add(data.getTimeStamp());
continue;
} else if (metricName.contains(MetricName.SERVER_FRONT_TEMPERATURE)) {
frontValues.add(data.getValueNum());
frontTimes.add(data.getTimeStamp());
continue;
} else if (metricName.contains(MetricName.SERVER_BACK_TEMPREATURE)) {
backValues.add(data.getValueNum());
backTimes.add(data.getTimeStamp());
continue;
} else if (metricName.contains(MetricName.SERVER_BACK_HUMIDITY)) {
backHumidityValues.add(data.getValueNum());
backHumidityTimes.add(data.getTimeStamp());
continue;
}
}
if (!frontValues.isEmpty()) {
frontTemp.setStatKey(VROConsts.ENVRIONMENT_FRONT_TEMPERATURE_METRIC);
frontTemp.setData(frontValues.stream().mapToDouble(Double::valueOf).toArray());
frontTemp.setTimestamps(frontTimes.stream().mapToLong(Long::valueOf).toArray());
contents.addStatContent(frontTemp);
}
if (!backValues.isEmpty()) {
backTemp.setStatKey(VROConsts.ENVRIONMENT_BACK_TEMPERATURE_METRIC);
backTemp.setData(backValues.stream().mapToDouble(Double::valueOf).toArray());
backTemp.setTimestamps(backTimes.stream().mapToLong(Long::valueOf).toArray());
contents.addStatContent(backTemp);
}
if (!pduAMPSValues.isEmpty()) {
pduAMPSValue.setStatKey(VROConsts.ENVRIONMENT_PDU_AMPS_METRIC);
pduAMPSValue.setData(pduAMPSValues.stream().mapToDouble(Double::valueOf).toArray());
pduAMPSValue.setTimestamps(pduAMPSTimes.stream().mapToLong(Long::valueOf).toArray());
contents.addStatContent(pduAMPSValue);
}
if (!voltageValues.isEmpty()) {
pduRealtimeVoltage.setStatKey(VROConsts.ENVRIONMENT_PDU_VOLTS_METRIC);
pduRealtimeVoltage.setData(voltageValues.stream().mapToDouble(Double::valueOf).toArray());
pduRealtimeVoltage.setTimestamps(voltageTimes.stream().mapToLong(Long::valueOf).toArray());
contents.addStatContent(pduRealtimeVoltage);
}
if (!powerValues.isEmpty()) {
pduRealtimePower.setStatKey(VROConsts.ENVRIONMENT_PDU_POWER_METRIC);
pduRealtimePower.setData(powerValues.stream().mapToDouble(Double::valueOf).toArray());
pduRealtimePower.setTimestamps(powerTimes.stream().mapToLong(Long::valueOf).toArray());
contents.addStatContent(pduRealtimePower);
}
if (!frontHumidityValues.isEmpty()) {
frontHumidityPercent.setStatKey(VROConsts.ENVRIONMENT_FRONT_HUMIDITY_METRIC);
frontHumidityPercent.setData(frontHumidityValues.stream().mapToDouble(Double::valueOf).toArray());
frontHumidityPercent.setTimestamps(frontHumidityTimes.stream().mapToLong(Long::valueOf).toArray());
contents.addStatContent(frontHumidityPercent);
}
if (!backHumidityValues.isEmpty()) {
backHumidityPercent.setStatKey(VROConsts.ENVRIONMENT_BACK_HUMIDITY_METRIC);
backHumidityPercent.setData(backHumidityValues.stream().mapToDouble(Double::valueOf).toArray());
backHumidityPercent.setTimestamps(backHumidityTimes.stream().mapToLong(Long::valueOf).toArray());
contents.addStatContent(backHumidityPercent);
}
if (!currentLoadValues.isEmpty()) {
currentLoad.setStatKey(VROConsts.ENVRIONMENT_PDU_AMPS_LOAD_METRIC);
currentLoad.setData(currentLoadValues.stream().mapToDouble(Double::valueOf).toArray());
currentLoad.setTimestamps(currentLoadTimes.stream().mapToLong(Long::valueOf).toArray());
contents.addStatContent(currentLoad);
}
if (!powerLoadValues.isEmpty()) {
powerLoad.setStatKey(VROConsts.ENVRIONMENT_PDU_POWER_LOAD_METRIC);
powerLoad.setData(powerLoadValues.stream().mapToDouble(Double::valueOf).toArray());
powerLoad.setTimestamps(powerLoadTimes.stream().mapToLong(Long::valueOf).toArray());
contents.addStatContent(powerLoad);
}
if (!contents.getStatContents().isEmpty()) {
logger.info("Push data to VRO: " + config.getServerURL());
metricClient.addStats(null, UUID.fromString(mapping.getVroResourceID()), contents, false);
}
// UPDATE THE PROPERTIES
if (executionCount % 1000 == 0) {
PropertyContents propertyContents = new PropertyContents();
packagingPropertyContent(assetDictionary.get(mapping.getAsset()), propertyContents);
metricClient.addProperties(null, UUID.fromString(mapping.getVroResourceID()), propertyContents);
}
}
}
if (hasNewData) {
Long factor = (currentTime - newUpdateTimeStamp) / FIVE_MINUTES + 1;
if (factor > 24) {
factor = 24L;
} else if (factor < 0) {
factor = 1L;
}
latencyFactorMap.put(config.getServerURL(), factor);
if (newUpdateTimeStamp > currentTime) {
newUpdateTimeStamp = currentTime;
}
lastUpdateTimeMap.put(config.getServerURL(), newUpdateTimeStamp);
} else {
latencyFactor = latencyFactor + 1;
if (latencyFactor > 24) {
latencyFactor = 24L;
}
latencyFactorMap.put(config.getServerURL(), latencyFactor);
}
executionCount++;
logger.info("Finished Sync metric data for VRO: " + config.getServerURL());
}
use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.
the class AssetControllerTest method createHostNameAndIPMappingFailureExample.
@Test
public void createHostNameAndIPMappingFailureExample() throws Exception {
expectedEx.expect(WormholeRequestException.class);
expectedEx.expectMessage("Invalid value: '10.15' for field: 'ip'");
AssetIPMapping mapping = new AssetIPMapping();
mapping.setAssetname("cloud-sha1-esx2");
mapping.setIp("10.15");
mapping.setMacAddress("00:50:56:be:60:62");
MvcResult result = this.mockMvc.perform(post("/v1/assets/mapping/hostnameip").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(mapping))).andExpect(status().is4xxClientError()).andReturn();
if (result.getResolvedException() != null) {
throw result.getResolvedException();
}
}
use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.
the class AssetControllerTest method testParseAssetIPMapingByString.
@Test
public void testParseAssetIPMapingByString() {
String contentString = "\t" + "\t" + "192.168.1.1" + " " + "\t" + " " + "cloud_server1";
AssetIPMapping mapping = AssetService.parseAssetIPMapingByString(contentString);
TestCase.assertEquals("192.168.1.1", mapping.getIp());
TestCase.assertEquals("cloud_server1", mapping.getAssetname());
}
use of com.vmware.flowgate.common.model.AssetIPMapping in project flowgate by vmware.
the class AssetControllerTest method createAssetIPMapping.
AssetIPMapping createAssetIPMapping() {
AssetIPMapping assetipmapping = new AssetIPMapping();
assetipmapping.setId(UUID.randomUUID().toString());
assetipmapping.setAssetname("assetname");
assetipmapping.setMacAddress("00:50:56:be:60:62");
assetipmapping.setIp("127.0.0.1");
return assetipmapping;
}
Aggregations