Skip to content

Commit e0d8905

Browse files
start validation
1 parent 930724f commit e0d8905

7 files changed

Lines changed: 386 additions & 3 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.tosca.model;
18+
19+
import org.apache.cloudstack.tosca.functions.ToscaFunction;
20+
21+
public class ToscaInputDefinition extends ToscaFieldDefinition {
22+
private final Object defaultValue;
23+
private final ToscaFunction.ToscaBooleanFunction validation;
24+
25+
public ToscaInputDefinition(String name, String description, ToscaTypeDefinition type, Object defaultValue, ToscaFunction.ToscaBooleanFunction validation) {
26+
super(name, description, type);
27+
this.defaultValue = defaultValue;
28+
this.validation = validation;
29+
}
30+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.tosca.model;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
public class ToscaNodeTemplate {
23+
private final String name;
24+
private final ToscaNodeType type;
25+
private final Map<String, ToscaProperty> properties = new HashMap<>();
26+
private final Map<String, Object> attributes = new HashMap<>();
27+
28+
public ToscaNodeTemplate(String name, ToscaNodeType type) {
29+
this.name = name;
30+
this.type = type;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void addProperty(String name, ToscaProperty property) {
38+
properties.put(name, property);
39+
}
40+
41+
public void addAttribute(String name, Object value) {
42+
attributes.put(name, value);
43+
}
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.tosca.model;
18+
19+
public class ToscaProperty {
20+
private final ToscaPropertyDefinition definition;
21+
private final Object rawValue;
22+
private final Object evaluatedValue;
23+
24+
public ToscaProperty(ToscaPropertyDefinition definition, Object rawValue, Object evaluatedValue) {
25+
this.definition = definition;
26+
this.rawValue = rawValue;
27+
this.evaluatedValue = evaluatedValue;
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.tosca.model;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
import java.util.Set;
22+
23+
public class ToscaServiceTemplate {
24+
private final Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>();
25+
private final Map<String, ToscaInputDefinition> inputs = new HashMap<>();
26+
private final Map<String, Set<ToscaNodeTemplate>> dependencies = new HashMap<>();
27+
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/tosca/model/ToscaTypeDefinition.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
2020

21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.Set;
24+
2125
public class ToscaTypeDefinition {
2226
public enum Kind {
2327
PRIMITIVE, COLLECTION, DATA_TYPE
@@ -69,6 +73,54 @@ public ToscaDataTypeDefinition getDataType() {
6973
return dataType;
7074
}
7175

76+
public boolean isCompatibleWith(Object value) {
77+
if (kind == Kind.PRIMITIVE) {
78+
return isCompatibleWithPrimitive(value);
79+
}
80+
81+
if (kind == Kind.COLLECTION) {
82+
return isCompatibleWithCollection(value);
83+
}
84+
85+
return isCompatibleWithDataType(value);
86+
}
87+
88+
private boolean isCompatibleWithPrimitive(Object value) {
89+
switch (primitiveType) {
90+
case STRING: return value instanceof String;
91+
case INTEGER: return value instanceof Integer;
92+
case FLOAT: return value instanceof Double;
93+
case BOOLEAN: return value instanceof Boolean;
94+
default: return false;
95+
}
96+
}
97+
98+
private boolean isCompatibleWithCollection(Object value) {
99+
switch (collectionType) {
100+
case LIST:
101+
if (!(value instanceof List)) return false;
102+
return ((List<?>) value).stream().allMatch(entrySchema::isCompatibleWith);
103+
case MAP:
104+
if (!(value instanceof Map)) return false;
105+
return ((Map<?, ?>) value).values().stream().allMatch(entrySchema::isCompatibleWith);
106+
default:
107+
return false;
108+
}
109+
}
110+
111+
private boolean isCompatibleWithDataType(Object value) {
112+
if (!(value instanceof Map)) return false;
113+
Map<?, ?> valueMap = (Map<?, ?>) value;
114+
Set<String> dataTypesFields = dataType.getProperties().keySet();
115+
if (!dataTypesFields.containsAll(valueMap.keySet())) return false;
116+
117+
return dataType.getProperties().values().stream().allMatch(property -> {
118+
if (!property.isRequired() && !valueMap.containsKey(property.getName())) return true;
119+
120+
return property.getType().isCompatibleWith(valueMap.get(property.getName()));
121+
});
122+
}
123+
72124
@Override
73125
public String toString() {
74126
if (kind == Kind.PRIMITIVE) {

0 commit comments

Comments
 (0)