2 * Copyright 2016 [ZTE] and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openo.commontosca.catalog.model.parser.yaml.zte.entity;
19 import com.google.gson.Gson;
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
23 import com.google.gson.annotations.SerializedName;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
30 import java.util.Map.Entry;
33 public class ParseYamlResult {
34 private String toscaDefinitionsVersion;
35 private String description;
36 private JsonObject nodeTypes;
37 private JsonObject capabilityTypes;
38 private JsonObject relationshipTypes;
39 private JsonObject policyTypes;
40 private TopologyTemplate topologyTemplate;
41 private Map<String, String> metadata;
42 private JsonObject plans;
45 public String getToscaDefinitionsVersion() {
46 return toscaDefinitionsVersion;
49 public void setToscaDefinitionsVersion(String toscaDefinitionsVersion) {
50 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
53 public String getDescription() {
57 public void setDescription(String description) {
58 this.description = description;
61 public JsonObject getNodeTypes() {
65 public void setNodeTypes(JsonObject nodeTypes) {
66 this.nodeTypes = nodeTypes;
69 public List<NodeType> getNodeTypeList() {
70 return jsonObject2NodeTypes(nodeTypes);
73 private ArrayList<NodeType> jsonObject2NodeTypes(JsonObject nodeTypes) {
74 ArrayList<NodeType> nodeTypeList = new ArrayList<NodeType>();
75 Iterator<Entry<String, JsonElement>> iterator = nodeTypes.entrySet().iterator();
76 while (iterator.hasNext()) {
77 NodeType type = new NodeType();
78 Entry<String, JsonElement> next = iterator.next();
79 type.setType(next.getKey());
80 type.setValue(new Gson().fromJson(next.getValue(), NodeType.NodeTypeValue.class));
81 nodeTypeList.add(type);
86 public JsonObject getCapabilityTypes() {
87 return capabilityTypes;
90 public void setCapabilityTypes(JsonObject capabilityTypes) {
91 this.capabilityTypes = capabilityTypes;
94 public JsonObject getRelationshipTypes() {
95 return relationshipTypes;
98 public void setRelationshipTypes(JsonObject relationshipTypes) {
99 this.relationshipTypes = relationshipTypes;
102 public List<RelationshipType> getRelationshipTypeList() {
103 return jsonObject2RelationshipTypes(relationshipTypes);
106 private ArrayList<RelationshipType> jsonObject2RelationshipTypes(JsonObject relationshipTypes) {
107 ArrayList<RelationshipType> relationshipTypeList = new ArrayList<RelationshipType>();
108 Iterator<Entry<String, JsonElement>> iterator = relationshipTypes.entrySet().iterator();
109 while (iterator.hasNext()) {
110 RelationshipType type = new RelationshipType();
111 Entry<String, JsonElement> next = iterator.next();
112 type.setType(next.getKey());
113 type.setValue(new Gson().fromJson(next.getValue(), RelationshipType.RelationshipValue.class));
114 relationshipTypeList.add(type);
116 return relationshipTypeList;
119 public JsonObject getPolicyTypes() {
123 public void setPolicyTypes(JsonObject policyTypes) {
124 this.policyTypes = policyTypes;
127 public TopologyTemplate getTopologyTemplate() {
128 return topologyTemplate;
131 public void setTopologyTemplate(TopologyTemplate topologyTemplate) {
132 this.topologyTemplate = topologyTemplate;
135 public Map<String, String> getMetadata() {
139 public void setMetadata(Map<String, String> metadata) {
140 this.metadata = metadata;
143 public JsonObject getPlans() {
147 public void setPlans(JsonObject plans) {
151 public List<Plan> getPlanList() {
152 return jsonObject2PlanList(this.plans);
155 private List<Plan> jsonObject2PlanList(JsonObject plans) {
157 return new ArrayList<>();
159 List<Plan> retList = new ArrayList<>();
160 Iterator<Entry<String, JsonElement>> iterator = plans.entrySet().iterator();
161 while (iterator.hasNext()) {
162 Plan ret = new Plan();
163 Entry<String, JsonElement> next = iterator.next();
164 ret.setName(next.getKey());
165 ret.setValue(new Gson().fromJson(next.getValue(), Plan.PlanValue.class));
171 public class TopologyTemplate {
172 private String description;
173 private List<Input> inputs;
174 private List<Output> outputs;
175 private List<NodeTemplate> nodeTemplates;
176 private SubstitutionMapping substitutionMappings;
178 public String getDescription() {
182 public void setDescription(String description) {
183 this.description = description;
186 public List<Input> getInputs() {
190 public void setInputs(List<Input> inputs) {
191 this.inputs = inputs;
194 public List<Output> getOutputs() {
198 public void setOutputs(List<Output> outputs) {
199 this.outputs = outputs;
202 public List<NodeTemplate> getNodeTemplates() {
203 return nodeTemplates;
206 public void setNodeTemplates(List<NodeTemplate> nodeTemplates) {
207 this.nodeTemplates = nodeTemplates;
210 public SubstitutionMapping getSubstitutionMappings() {
211 return substitutionMappings;
214 public void setSubstitutionMappings(SubstitutionMapping substitutionMappings) {
215 this.substitutionMappings = substitutionMappings;
221 private String description;
222 private String defaultValue;
223 private boolean required;
225 public String getName() {
229 public void setName(String name) {
233 public String getType() {
237 public void setType(String type) {
241 public String getDescription() {
245 public void setDescription(String description) {
246 this.description = description;
249 public String getDefault() {
253 public void setDefault(String defaultValue) {
254 this.defaultValue = defaultValue;
257 public boolean isRequired() {
261 public void setRequired(boolean required) {
262 this.required = required;
266 public class Output {
268 private String description;
269 private Object value;
271 public String getName() {
275 public void setName(String name) {
279 public String getDescription() {
283 public void setDescription(String description) {
284 this.description = description;
287 public Object getValue() {
291 public void setValue(Object value) {
296 public class NodeTemplate {
298 private String nodeType;
299 private JsonObject properties;
300 private JsonObject[] requirements;
301 private JsonObject capabilities;
302 private List<Relationship> relationships;
304 public String getName() {
308 public void setName(String name) {
312 public String getNodeType() {
316 public void setNodeType(String nodeType) {
317 this.nodeType = nodeType;
320 public JsonObject getProperties() {
324 public void setProperties(JsonObject properties) {
325 this.properties = properties;
328 public Map<String, Object> getPropertyList() {
329 return jsonObject2Properties(properties);
332 private Map<String, Object> jsonObject2Properties(JsonObject properties) {
333 Map<String, Object> ret = new HashMap<>();
334 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
335 while (iterator.hasNext()) {
336 Entry<String, JsonElement> next = iterator.next();
337 ret.put(next.getKey(), next.getValue().getAsString());
342 public JsonObject[] getRequirements() {
346 public void setRequirements(JsonObject[] requirements) {
347 this.requirements = requirements;
350 public JsonObject getCapabilities() {
354 public void setCapabilities(JsonObject capabilities) {
355 this.capabilities = capabilities;
358 public List<Relationship> getRelationships() {
359 return relationships;
362 public void setRelationships(List<Relationship> relationships) {
363 this.relationships = relationships;
368 * @return NodeTemplateScalable
370 public NodeTemplateScalable getScalable() {
371 if (capabilities == null) {
374 JsonElement scaleableJson = capabilities.get("scalable");
375 if (scaleableJson == null || !scaleableJson.isJsonObject()) {
378 JsonElement propertyJson = scaleableJson.getAsJsonObject().get("properties");
379 if (propertyJson == null || !propertyJson.isJsonObject()) {
383 NodeTemplateScalable scalable = new NodeTemplateScalable();
385 .setMin_instances(propertyJson.getAsJsonObject().get("min_instances").getAsString());
387 .setMax_instances(propertyJson.getAsJsonObject().get("max_instances").getAsString());
388 scalable.setDefault_instances(
389 propertyJson.getAsJsonObject().get("default_instances").getAsString());
393 public class Relationship {
394 private String targetNodeName;
396 private String sourceNodeName;
398 public String getTargetNodeName() {
399 return targetNodeName;
402 public void setTargetNodeName(String targetNodeName) {
403 this.targetNodeName = targetNodeName;
406 public String getType() {
410 public void setType(String type) {
414 public String getSourceNodeName() {
415 return sourceNodeName;
418 public void setSourceNodeName(String sourceNodeName) {
419 this.sourceNodeName = sourceNodeName;
423 public class NodeTemplateScalable {
424 private String minInstances;
425 private String maxInstances;
426 private String defaultInstances;
428 public String getMin_instances() {
432 public void setMin_instances(String minInstances) {
433 this.minInstances = minInstances;
436 public String getMax_instances() {
440 public void setMax_instances(String maxInstances) {
441 this.maxInstances = maxInstances;
444 public String getDefault_instances() {
445 return defaultInstances;
448 public void setDefault_instances(String defaultInstances) {
449 this.defaultInstances = defaultInstances;
454 public class SubstitutionMapping {
455 private String nodeType;
456 private JsonObject requirements;
457 private JsonObject capabilities;
458 private JsonObject properties;
460 public String getNodeType() {
464 public void setNodeType(String nodeType) {
465 this.nodeType = nodeType;
468 public JsonObject getRequirements() {
472 public void setRequirements(JsonObject requirements) {
473 this.requirements = requirements;
476 public Map<String, String[]> getRequirementList() {
477 return jsonObjects2Requirements(this.requirements);
480 private Map<String, String[]> jsonObjects2Requirements(JsonObject requirements) {
481 Map<String, String[]> ret = new HashMap<String, String[]>();
483 Iterator<Entry<String, JsonElement>> iterator = requirements.entrySet().iterator();
484 while (iterator.hasNext()) {
485 Entry<String, JsonElement> next = iterator.next();
486 if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
487 ret.put(next.getKey(), new String[] {next.getValue().getAsString()});
491 if (next.getValue().isJsonArray()) {
492 String[] value = parseListValue((JsonArray) next.getValue());
493 ret.put(next.getKey(), value);
500 private String[] parseListValue(JsonArray jsonArray) {
501 String[] value = new String[jsonArray.size()];
502 for (int i = 0, size = jsonArray.size(); i < size; i++) {
503 value[i] = jsonArray.get(i).getAsString();
508 public JsonObject getCapabilities() {
512 public void setCapabilities(JsonObject capabilities) {
513 this.capabilities = capabilities;
516 public Map<String, String[]> getCapabilityList() {
517 return jsonObject2Capabilities(this.capabilities);
520 private Map<String, String[]> jsonObject2Capabilities(JsonObject capabilities) {
521 Map<String, String[]> ret = new HashMap<String, String[]>();
523 Iterator<Entry<String, JsonElement>> iterator = capabilities.entrySet().iterator();
524 while (iterator.hasNext()) {
525 Entry<String, JsonElement> next = iterator.next();
527 if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
528 ret.put(next.getKey(), new String[] {next.getValue().getAsString()});
532 if (next.getValue().isJsonArray()) {
533 String[] value = parseListValue((JsonArray) next.getValue());
534 ret.put(next.getKey(), value);
541 public JsonObject getProperties() {
545 public void setProperties(JsonObject properties) {
546 this.properties = properties;
549 public Map<String, Object> getPropertyList() {
550 return jsonObject2Properties(properties);
553 private Map<String, Object> jsonObject2Properties(JsonObject properties) {
554 Map<String, Object> ret = new HashMap<>();
555 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
556 while (iterator.hasNext()) {
557 Entry<String, JsonElement> next = iterator.next();
558 ret.put(next.getKey(), next.getValue().getAsString());
566 public class RelationshipType {
568 private RelationshipValue value;
570 public String getType() {
574 public void setType(String type) {
578 public RelationshipValue getValue() {
582 public void setValue(RelationshipValue value) {
586 public class RelationshipValue {
587 private String derivedFrom;
588 private String[] validTargetTypes;
590 public String getDerivedFrom() {
594 public void setDerivedFrom(String derivedFrom) {
595 this.derivedFrom = derivedFrom;
598 public String[] getValid_target_types() {
599 return validTargetTypes;
602 public void setValid_target_types(String[] validTargetTypes) {
603 this.validTargetTypes = validTargetTypes;
609 public class NodeType {
611 private NodeTypeValue value;
613 public String getType() {
617 public void setType(String type) {
621 public NodeTypeValue getValue() {
625 public void setValue(NodeTypeValue value) {
629 public class NodeTypeValue {
630 private String derivedFrom;
631 private JsonObject properties;
632 private JsonObject[] requirements;
633 private JsonObject capabilities;
635 public String getDerivedFrom() {
639 public void setDerived_from(String derivedFrom) {
640 this.derivedFrom = derivedFrom;
643 public JsonObject getProperties() {
647 public void setProperties(JsonObject properties) {
648 this.properties = properties;
651 public List<NodeTypeProperty> getPropertyList() {
652 return jsonObject2Properties(properties);
655 private List<NodeTypeProperty> jsonObject2Properties(JsonObject properties) {
656 List<NodeTypeProperty> propertieList = new ArrayList<NodeTypeProperty>();
657 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
658 while (iterator.hasNext()) {
659 NodeTypeProperty type = new NodeTypeProperty();
660 Entry<String, JsonElement> next = iterator.next();
661 type.setKey(next.getKey());
662 type.setValue(new Gson().fromJson(next.getValue(), JsonObject.class));
663 propertieList.add(type);
665 return propertieList;
668 public class NodeTypeProperty {
670 private JsonObject value;
672 public String getKey() {
676 public void setKey(String key) {
682 * @return default value
684 public String getDefaultValue() {
685 JsonElement defaultValue = value.get("default");
686 if (defaultValue == null || defaultValue.isJsonObject()) {
690 return defaultValue.getAsString();
693 public JsonObject getValue() {
697 public void setValue(JsonObject value) {
702 public JsonObject[] getRequirements() {
706 public void setRequirements(JsonObject[] requirements) {
707 this.requirements = requirements;
710 public JsonObject getCapabilities() {
714 public void setCapabilities(JsonObject capabilities) {
715 this.capabilities = capabilities;
722 private PlanValue value;
724 public String getName() {
728 public void setName(String name) {
732 public String getDescription() {
733 return value.getDescription();
736 public String getReference() {
737 return value.getReference();
740 public String getPlanLanguage() {
741 return value.getPlanLanguage();
744 public List<PlanValue.PlanInput> getInputList() {
745 return value.getInputList();
748 public PlanValue getValue() {
752 public void setValue(PlanValue value) {
756 public class PlanValue {
757 private String description;
758 private String reference;
759 private String planLanguage;
760 private JsonObject inputs;
762 public String getDescription() {
766 public void setDescription(String description) {
767 this.description = description;
770 public String getReference() {
774 public void setReference(String reference) {
775 this.reference = reference;
778 public String getPlanLanguage() {
782 public void setPlanLanguage(String planLanguage) {
783 this.planLanguage = planLanguage;
786 public JsonObject getInputs() {
790 public void setInputs(JsonObject inputs) {
791 this.inputs = inputs;
794 public List<PlanInput> getInputList() {
795 return jsonObject2PlanInputList(inputs);
799 private List<PlanInput> jsonObject2PlanInputList(JsonObject inputs) {
800 List<PlanInput> retList = new ArrayList<PlanInput>();
801 Iterator<Entry<String, JsonElement>> iterator = inputs.entrySet().iterator();
802 while (iterator.hasNext()) {
803 PlanInput ret = new PlanInput();
804 Entry<String, JsonElement> next = iterator.next();
805 ret.setName(next.getKey());
806 ret.setValue(new Gson().fromJson(next.getValue(), PlanInput.PlanInputValue.class));
812 public class PlanInput {
814 private PlanInputValue value;
816 public String getName() {
820 public void setName(String name) {
824 public String getType() {
825 return value.getType();
828 public String getDescription() {
829 return value.getDescription();
832 public String getDefault() {
833 return value.getDefaultValue();
836 public boolean isRequired() {
837 return value.isRequired();
840 public PlanInputValue getValue() {
844 public void setValue(PlanInputValue value) {
848 public class PlanInputValue {
850 private String description;
851 @SerializedName("default")
852 private String defaultValue;
853 private boolean required;
855 public String getType() {
859 public void setType(String type) {
863 public String getDescription() {
867 public void setDescription(String description) {
868 this.description = description;
871 public String getDefaultValue() {
875 public void setDefaultValue(String defaultValue) {
876 this.defaultValue = defaultValue;
879 public boolean isRequired() {
883 public void setRequired(boolean required) {
884 this.required = required;