blob: b516dbd77f8a1271e709c76edc3b499b3784d3e9 [file] [log] [blame]
package org.eclipse.stem.cloud.controller;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
@RestController
public class ModelController {
private static final Logger log = LoggerFactory.getLogger(ModelController.class);
@Autowired
@Lazy
private EurekaClient eurekaClient;
@Autowired
RestTemplate restTemplate;
@Value("${spring.application.name}")
private String appName;
@GetMapping(value = "/core/cloud/scenarios", produces = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public void scanScenarios(OutputStream out) throws IOException {
coreCall("engine/cloud/models?treetype=Scenario",out);
}
@GetMapping(value = "/core/cloud/graphs", produces = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public void scanGraphs(OutputStream out) throws IOException {
coreCall("engine/cloud/models?treetype=graph",out);
}
@GetMapping(value = "/core/cloud/models", produces = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public void scanModels(OutputStream out) throws IOException {
coreCall("engine/cloud/models?treetype=model",out);
}
@GetMapping(value = "/core/cloud/predicate", produces = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public void scanPredicate(OutputStream out) throws IOException {
coreCall("engine/cloud/models?treetype=predicate",out);
}
@GetMapping(value = "/core/cloud/sequencer", produces = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public void scanSequencer(OutputStream out) throws IOException {
coreCall("engine/cloud/models?treetype=sequencer",out);
}
@GetMapping(value = "/core/cloud/trigger", produces = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public void scanTrigger(OutputStream out) throws IOException {
coreCall("engine/cloud/models?treetype=trigger",out);
}
@GetMapping(value = "/core/cloud/experiments", produces = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public void scanExperiments(OutputStream out) throws IOException {
coreCall("engine/cloud/models?treetype=experiments",out);
}
@GetMapping(value = "/core/cloud/newproject", produces = "application/json")
@ResponseStatus(value = HttpStatus.OK)
public void newproject(OutputStream out, @RequestParam("name") String name) throws IOException {
coreCall("engine/cloud/models?treetype=newproject&projectName="+name,out);
}
private String serverIPAddr(){
eurekaClient.getApplication(appName).shuffleAndStoreInstances(true);
InstanceInfo instance = eurekaClient.getApplication(appName).getInstances().get(0);
return instance.getHomePageUrl();
}
private String prepareFullPath(String subPath){
return serverIPAddr()+subPath;
}
private void coreCall(String subPath, OutputStream out) throws IOException {
String url =prepareFullPath(subPath);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute(httpGet);
try {
HttpEntity entity = response.getEntity();
// Return its response
entity.writeTo(out);
} finally {
response.close();
}
}
}