Commit aabe213f authored by Mohamed Khalil Labidi's avatar Mohamed Khalil Labidi
Browse files

Merge branch 'debug-nc' into 'master'

Fix Hashmap to Json casting in updateNodeCandidate method

See merge request melodic/morphemic-preprocessor!10
parents 97062ef8 4b63a4d8
......@@ -38,7 +38,7 @@ public class PAConnectorIaasGateway {
public JSONArray getNodeCandidates(String nodeSourceName, String region, String imageReq) {
Validate.notNull(nodeSourceName, "nodeSourceName must not be null");
Validate.notNull(region, "region must not be null");
LOGGER.debug("Retrieving node candidates for cloud " + nodeSourceName + " region " +
LOGGER.info("Retrieving node candidates for cloud " + nodeSourceName + " region " +
region + " and imageReq " + imageReq);
JSONArray nodeCandidates = null;
......@@ -50,7 +50,7 @@ public class PAConnectorIaasGateway {
HttpURLConnection connection = (HttpURLConnection) requestUri.toURL().openConnection();
connection.setRequestMethod(HttpMethod.POST.toString());
LOGGER.debug("requestUri = " + requestUri.toString());
LOGGER.info("requestUri = " + requestUri.toString());
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
LOGGER.warn("Failed : HTTP error code : " + connection.getResponseCode());
......@@ -61,7 +61,7 @@ public class PAConnectorIaasGateway {
(connection.getInputStream())));
nodeCandidates = new JSONArray(new JSONTokener(br));
LOGGER.debug("Node candidates retrieved for successfully: " + nodeCandidates.toString());
LOGGER.info("Node candidates retrieved for successfully: " + nodeCandidates.toString());
connection.disconnect();
......@@ -71,7 +71,7 @@ public class PAConnectorIaasGateway {
@SneakyThrows
public JSONArray getImages(String nodeSourceName) {
Validate.notNull(nodeSourceName, "nodeSourceName must not be null");
LOGGER.debug("Retrieving images for cloud " + nodeSourceName);
LOGGER.info("Retrieving images for cloud " + nodeSourceName);
JSONArray images = null;
URIBuilder uriBuilder = new URIBuilder(new URL(paURL).toURI());
......@@ -80,7 +80,7 @@ public class PAConnectorIaasGateway {
HttpURLConnection connection = (HttpURLConnection) requestUri.toURL().openConnection();
connection.setRequestMethod(HttpMethod.GET.toString());
LOGGER.debug("requestUri = " + requestUri.toString());
LOGGER.info("requestUri = " + requestUri.toString());
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
LOGGER.warn("Failed : HTTP error code : " + connection.getResponseCode());
......@@ -91,7 +91,7 @@ public class PAConnectorIaasGateway {
(connection.getInputStream())));
images = new JSONArray(new JSONTokener(br));
LOGGER.debug("Images retrieved for cloud " + nodeSourceName + ". Images: " + images.toString());
LOGGER.info("Images retrieved for cloud " + nodeSourceName + ". Images: " + images.toString());
connection.disconnect();
......
......@@ -3,12 +3,14 @@ package org.activeeon.morphemic.service;
import org.activeeon.morphemic.infrastructure.deployment.PAConnectorIaasGateway;
import org.activeeon.morphemic.model.*;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
......@@ -131,6 +133,7 @@ public class NodeCandidateUtils {
}
private Location createLocation(JSONObject nodeCandidateJSON, PACloud paCloud) {
LOGGER.info("Creating location ...");
String locationId = paCloud.getCloudID() + "/" + nodeCandidateJSON.optString("region");
Location location = em.find(Location.class, locationId);
if (location == null) {
......@@ -142,7 +145,7 @@ public class NodeCandidateUtils {
location.setIsAssignable(true);
location.setGeoLocation(createGeoLocation(paCloud.getCloudProviderName(), location.getName()));
}
LOGGER.info("Location created: " + location.toString());
return location;
}
......@@ -205,6 +208,7 @@ public class NodeCandidateUtils {
}
public NodeCandidate createNodeCandidate(JSONObject nodeCandidateJSON, JSONObject imageJSON, PACloud paCloud) {
LOGGER.info("Creating node candidate ...");
NodeCandidate nodeCandidate = new NodeCandidate();
nodeCandidate.setNodeCandidateType(NodeCandidate.NodeCandidateTypeEnum.IAAS);
nodeCandidate.setPrice(nodeCandidateJSON.optDouble("price"));
......@@ -217,19 +221,34 @@ public class NodeCandidateUtils {
nodeCandidate.setPricePerInvocation((double) 0);
nodeCandidate.setMemoryPrice((double) 0);
nodeCandidate.setEnvironment(new Environment());
LOGGER.info("Node candidate created: " + nodeCandidate.toString());
return nodeCandidate;
}
private static JSONObject convertObjectToJson(Object object) {
JSONObject myJson = null;
try {
myJson = new JSONObject(new ObjectMapper().writeValueAsString(object));
} catch (IOException e) {
LOGGER.error("Error in casting Hashmap to JSON: " + Arrays.toString(e.getStackTrace()));
}
return myJson;
}
public void updateNodeCandidates(List<String> newCloudIds) {
em.getTransaction().begin();
newCloudIds.forEach(newCloudId -> {
PACloud paCloud = em.find(PACloud.class, newCloudId);
LOGGER.info("Getting blacklisted regions...");
List<String> blacklistedRegions = Arrays.asList(paCloud.getBlacklist().split(","));
LOGGER.info("Blacklisted regions: " + blacklistedRegions.toString());
LOGGER.info("Getting images from Proactive ...");
JSONArray images = connectorIaasGateway.getImages(paCloud.getDummyInfrastructureName());
List<JSONObject> consolidatedImages = images.toList().parallelStream().map(o -> (JSONObject) o).filter(record -> !blacklistedRegions.contains(record.get("location"))).collect(Collectors.toList());
LOGGER.info("Returned images: " + images.toString());
List<JSONObject> consolidatedImages = images.toList().parallelStream().map(NodeCandidateUtils::convertObjectToJson).filter(record -> !blacklistedRegions.contains(record.get("location"))).collect(Collectors.toList());
LOGGER.info("Consolidated images: " + consolidatedImages.toString());
//TODO: (Optimization) An images per region map structure <region,[image1,image2]> could be the best here.
// It can reduce the getNodeCandidates calls to PA.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment