Skip to content

Commit 83474f6

Browse files
continue implementation
1 parent 0532b0f commit 83474f6

10 files changed

Lines changed: 172 additions & 2 deletions

File tree

plugins/iac/nimble/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,12 @@
2828
<version>4.22.0.0</version>
2929
<relativePath>../../pom.xml</relativePath>
3030
</parent>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.yaml</groupId>
34+
<artifactId>snakeyaml</artifactId>
35+
<version>2.6</version>
36+
<scope>compile</scope>
37+
</dependency>
38+
</dependencies>
3139
</project>

plugins/iac/nimble/src/main/java/org/apache/cloudstack/service/NimbleManagerImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
6464
protected List<String> loadToscaProfile() {
6565
logger.info("Loading NIMBLE's TOSCA profile.");
6666
List<IacTemplatesProfile> profileElements = iacTemplatesProfileDao.listAll();
67+
6768
return profileElements.stream().map(element -> {
6869
String elementContent = getElementDefinition(element.getElementContentFilePath());
6970
return elementContent;
7071
}).collect(Collectors.toList());
7172
}
7273

73-
protected String getElementDefinition(String resource) {
74-
Path path = Paths.get(String.format("%s%s", NIMBLE_CONFIG_PATH, "tosca/profile/storage/volume.yaml"));
74+
protected String getElementDefinition(String elementContentFilePath) {
75+
Path path = Paths.get(String.format("%s%s", NIMBLE_CONFIG_PATH, elementContentFilePath));
7576
try {
7677
return Files.readString(path);
7778
} catch (IOException e) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.apache.cloudstack.tosca;
2+
3+
import org.yaml.snakeyaml.Yaml;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.Map;
10+
11+
public class ToscaParser {
12+
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.apache.cloudstack.tosca.loader;
2+
3+
import org.apache.cloudstack.tosca.model.ToscaNodeType;
4+
5+
import java.nio.file.Path;
6+
import java.util.Map;
7+
8+
public class ToscaYamlLoader {
9+
private final String NODE_TYPES_KEY = "node_types";
10+
private final String NODE_TYPES_PROPERTIES_KEY = "properties";
11+
private final String NODE_TYPES_ATTRIBUTES_KEY = "attributes";
12+
13+
public ToscaNodeType loadNodeType(Path path) {
14+
Object rawYaml = YamlUtils.loadYaml(path);
15+
Map<String, Object> yamlRoot = YamlUtils.asMap(rawYaml);
16+
17+
Map<String, Object> nodeTypes = YamlUtils.asMap(yamlRoot.get(NODE_TYPES_KEY));
18+
for (Map.Entry<String, Object> entry : nodeTypes.entrySet()) {
19+
String name = entry.getKey();
20+
Map<String, Object> nodeTypeBody = YamlUtils.asMap(entry.getValue());
21+
parseNodeTypeProperties(nodeTypeBody.get(NODE_TYPES_PROPERTIES_KEY));
22+
parseNodeTypeAttributes(nodeTypeBody.get(NODE_TYPES_ATTRIBUTES_KEY));
23+
return new ToscaNodeType(name, null, null);
24+
}
25+
26+
return null;
27+
}
28+
29+
private void parseNodeTypeProperties(Object rawProperties) {
30+
31+
}
32+
33+
private void parseNodeTypeAttributes(Object rawAttributes) {}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.apache.cloudstack.tosca.loader;
2+
3+
import org.yaml.snakeyaml.Yaml;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.Map;
10+
11+
public class YamlUtils {
12+
@SuppressWarnings("unchecked")
13+
public static Map<String, Object> asMap(Object rawObject) {
14+
if (!(rawObject instanceof Map)) {
15+
return Map.of();
16+
}
17+
18+
return (Map<String, Object>) rawObject;
19+
}
20+
21+
public static Object loadYaml(Path path) {
22+
Yaml yaml = new Yaml();
23+
24+
try (InputStream inputStream = Files.newInputStream(path)) {
25+
return yaml.load(inputStream);
26+
} catch (IOException exception) {
27+
return null;
28+
}
29+
}
30+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.apache.cloudstack.tosca.model;
2+
3+
public class ToscaAttributeDefinition extends ToscaFieldDefinition {
4+
public ToscaAttributeDefinition(String name, String description, ToscaPrimitiveType type) {
5+
super(name, description, type);
6+
}
7+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.apache.cloudstack.tosca.model;
2+
3+
public abstract class ToscaFieldDefinition {
4+
private String name;
5+
private String description;
6+
private ToscaPrimitiveType type;
7+
8+
public ToscaFieldDefinition(String name, String description, ToscaPrimitiveType type) {
9+
this.name = name;
10+
this.description = description;
11+
this.type = type;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public String getDescription() {
19+
return description;
20+
}
21+
22+
public ToscaPrimitiveType getType() {
23+
return type;
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.apache.cloudstack.tosca.model;
2+
3+
import java.util.Map;
4+
5+
public class ToscaNodeType {
6+
private String name;
7+
private Map<String, ToscaPropertyDefinition> properties;
8+
private Map<String, ToscaAttributeDefinition> attributes;
9+
10+
public ToscaNodeType(String name, Map<String, ToscaPropertyDefinition> properties, Map<String, ToscaAttributeDefinition> attributes) {
11+
this.name = name;
12+
this.properties = properties;
13+
this.attributes = attributes;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public Map<String, ToscaPropertyDefinition> getProperties() {
21+
return properties;
22+
}
23+
24+
public Map<String, ToscaAttributeDefinition> getAttributes() {
25+
return attributes;
26+
}
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.apache.cloudstack.tosca.model;
2+
3+
public enum ToscaPrimitiveType {
4+
STRING, INTEGER, FLOAT, BOOLEAN, LIST
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.apache.cloudstack.tosca.model;
2+
3+
public class ToscaPropertyDefinition extends ToscaFieldDefinition {
4+
private boolean required;
5+
private Object validation;
6+
7+
public ToscaPropertyDefinition(String name, String description, ToscaPrimitiveType type, boolean required, Object validation) {
8+
super(name, description, type);
9+
this.required = required;
10+
this.validation = validation;
11+
}
12+
13+
public boolean isRequired() {
14+
return required;
15+
}
16+
17+
public Object getValidation() {
18+
return validation;
19+
}
20+
}

0 commit comments

Comments
 (0)