org.openstack4j.openstack.heat.utils.Environment Maven / Gradle / Ivy
package org.openstack4j.openstack.heat.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
public class Environment {
private String envContent;
private Map files = new HashMap();
private URL baseUrl;
public Environment(URL environmentRes) throws JsonParseException, IOException, URISyntaxException{
setEnvContent(Resources.toString(environmentRes, Charsets.UTF_8));
setBaseUrl(TemplateUtils.baseUrl(environmentRes.toString()));
getFileContent();
}
public Environment(String environmentLoc)
throws JsonParseException, MalformedURLException,
UnsupportedEncodingException, IOException, URISyntaxException {
this(TemplateUtils.normaliseFilePathToUrl(environmentLoc));
}
@SuppressWarnings("unchecked")
private Map getResourceRegistry(){
Yaml yaml = new Yaml();
Map content = (Map) yaml.load(getEnvContent());
return (Map) content.get("resource_registry");
}
/*
* Processing the template files located in the resource_registry part in the environment file
*/
private void getFileContent()
throws JsonParseException, MalformedURLException, UnsupportedEncodingException, IOException, URISyntaxException {
Map rr = getResourceRegistry();
if (rr == null ) return;
if(rr.get("base_url") != null ) setBaseUrl(new URL(rr.get("base_url")));
for(String resourceType: rr.keySet()) {
if(resourceType == "base_url") continue;
URL tplUrl = new URL(baseUrl, rr.get(resourceType));
Template tpl = new Template(tplUrl);
files.put(rr.get(resourceType), tpl.getTplContent());
Map fileFromTemplate = tpl.getFiles();
for(String file : fileFromTemplate.keySet()) {
if(! files.containsKey(file)) {
files.put(file, fileFromTemplate.get(file));
}
}
}
}
public String getEnvContent() {
return envContent;
}
public void setEnvContent(String envContent) {
this.envContent = envContent;
}
public Map getFiles() {
return files;
}
public void setFiles(Map files) {
this.files = files;
}
public URL getBaseUrl() {
return baseUrl;
}
public void setBaseUrl(URL baseUrl) {
this.baseUrl = baseUrl;
}
}