2 * Copyright 2016 ZTE Corporation.
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.
16 package org.openo.commontosca.catalog.model.parser.yaml.zte.entity;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
23 import java.util.Map.Entry;
25 import lombok.AllArgsConstructor;
27 import lombok.NoArgsConstructor;
29 import org.openo.commontosca.catalog.common.ToolUtil;
31 import com.google.gson.Gson;
32 import com.google.gson.JsonArray;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonObject;
35 import com.google.gson.annotations.SerializedName;
40 public class ParseYamlResult {
41 private String toscaDefinitionsVersion;
42 private String description;
43 private JsonObject nodeTypes;
44 private JsonObject capabilityTypes;
45 private JsonObject relationshipTypes;
46 private JsonObject policyTypes;
47 private TopologyTemplate topologyTemplate;
48 private Map<String, String> metadata;
49 private JsonObject plans;
51 private String rawData;
53 public String getToscaDefinitionsVersion() {
54 return toscaDefinitionsVersion;
57 public void setToscaDefinitionsVersion(String toscaDefinitionsVersion) {
58 this.toscaDefinitionsVersion = toscaDefinitionsVersion;
61 public String getDescription() {
65 public void setDescription(String description) {
66 this.description = description;
69 public JsonObject getNodeTypes() {
73 public void setNodeTypes(JsonObject nodeTypes) {
74 this.nodeTypes = nodeTypes;
77 public List<NodeType> getNodeTypeList() {
78 return jsonObject2NodeTypes(nodeTypes);
81 private ArrayList<NodeType> jsonObject2NodeTypes(JsonObject nodeTypes) {
82 ArrayList<NodeType> nodeTypeList = new ArrayList<NodeType>();
83 Iterator<Entry<String, JsonElement>> iterator = nodeTypes.entrySet().iterator();
84 while (iterator.hasNext()) {
85 NodeType type = new NodeType();
86 Entry<String, JsonElement> next = iterator.next();
87 type.setType(next.getKey());
88 type.setValue(new Gson().fromJson(next.getValue(), NodeType.NodeTypeValue.class));
89 nodeTypeList.add(type);
94 public JsonObject getCapabilityTypes() {
95 return capabilityTypes;
98 public void setCapabilityTypes(JsonObject capabilityTypes) {
99 this.capabilityTypes = capabilityTypes;
102 public JsonObject getRelationshipTypes() {
103 return relationshipTypes;
106 public void setRelationshipTypes(JsonObject relationshipTypes) {
107 this.relationshipTypes = relationshipTypes;
110 public List<RelationshipType> getRelationshipTypeList() {
111 return jsonObject2RelationshipTypes(relationshipTypes);
114 private ArrayList<RelationshipType> jsonObject2RelationshipTypes(JsonObject relationshipTypes) {
115 ArrayList<RelationshipType> relationshipTypeList = new ArrayList<RelationshipType>();
116 Iterator<Entry<String, JsonElement>> iterator = relationshipTypes.entrySet().iterator();
117 while (iterator.hasNext()) {
118 RelationshipType type = new RelationshipType();
119 Entry<String, JsonElement> next = iterator.next();
120 type.setType(next.getKey());
121 type.setValue(new Gson().fromJson(next.getValue(), RelationshipType.RelationshipValue.class));
122 relationshipTypeList.add(type);
124 return relationshipTypeList;
127 public JsonObject getPolicyTypes() {
131 public void setPolicyTypes(JsonObject policyTypes) {
132 this.policyTypes = policyTypes;
135 public TopologyTemplate getTopologyTemplate() {
136 return topologyTemplate;
139 public void setTopologyTemplate(TopologyTemplate topologyTemplate) {
140 this.topologyTemplate = topologyTemplate;
143 public Map<String, String> getMetadata() {
144 if (this.metadata == null) {
145 return new HashMap<>();
150 public void setMetadata(Map<String, String> metadata) {
151 this.metadata = metadata;
154 public JsonObject getPlans() {
158 public void setPlans(JsonObject plans) {
162 public List<Plan> getPlanList() {
163 return jsonObject2PlanList(this.plans);
166 private List<Plan> jsonObject2PlanList(JsonObject plans) {
168 return new ArrayList<>();
170 List<Plan> retList = new ArrayList<>();
171 Iterator<Entry<String, JsonElement>> iterator = plans.entrySet().iterator();
172 while (iterator.hasNext()) {
173 Plan ret = new Plan();
174 Entry<String, JsonElement> next = iterator.next();
175 ret.setName(next.getKey());
176 ret.setValue(new Gson().fromJson(next.getValue(), Plan.PlanValue.class));
182 public class TopologyTemplate {
183 private String description;
184 private List<Input> inputs;
185 private List<Output> outputs;
186 private List<NodeTemplate> nodeTemplates;
187 private SubstitutionMapping substitutionMappings;
189 public String getDescription() {
193 public void setDescription(String description) {
194 this.description = description;
197 public List<Input> getInputs() {
201 public void setInputs(List<Input> inputs) {
202 this.inputs = inputs;
205 public List<Output> getOutputs() {
209 public void setOutputs(List<Output> outputs) {
210 this.outputs = outputs;
213 public List<NodeTemplate> getNodeTemplates() {
214 return nodeTemplates;
217 public void setNodeTemplates(List<NodeTemplate> nodeTemplates) {
218 this.nodeTemplates = nodeTemplates;
221 public SubstitutionMapping getSubstitutionMappings() {
222 return substitutionMappings;
225 public void setSubstitutionMappings(SubstitutionMapping substitutionMappings) {
226 this.substitutionMappings = substitutionMappings;
232 private String description;
233 private String defaultValue;
234 private boolean required;
236 public String getName() {
240 public void setName(String name) {
244 public String getType() {
248 public void setType(String type) {
252 public String getDescription() {
256 public void setDescription(String description) {
257 this.description = description;
260 public String getDefault() {
264 public void setDefault(String defaultValue) {
265 this.defaultValue = defaultValue;
268 public boolean isRequired() {
272 public void setRequired(boolean required) {
273 this.required = required;
277 public class Output {
279 private String description;
280 private Object value;
282 public String getName() {
286 public void setName(String name) {
290 public String getDescription() {
294 public void setDescription(String description) {
295 this.description = description;
298 public Object getValue() {
302 public void setValue(Object value) {
307 public class NodeTemplate {
309 private String nodeType;
310 private JsonObject properties;
311 private JsonObject[] requirements;
312 private JsonObject capabilities;
313 private List<Relationship> relationships;
315 public String getName() {
319 public void setName(String name) {
323 public String getNodeType() {
327 public void setNodeType(String nodeType) {
328 this.nodeType = nodeType;
331 public JsonObject getProperties() {
335 public void setProperties(JsonObject properties) {
336 this.properties = properties;
339 public Map<String, Object> getPropertyList() {
340 return jsonObject2Properties(properties);
343 private Map<String, Object> jsonObject2Properties(JsonObject properties) {
344 Map<String, Object> ret = new HashMap<>();
345 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
346 while (iterator.hasNext()) {
347 Entry<String, JsonElement> next = iterator.next();
348 ret.put(next.getKey(), ToolUtil.getAsString(next.getValue()));
353 public JsonObject[] getRequirements() {
357 public void setRequirements(JsonObject[] requirements) {
358 this.requirements = requirements;
361 public JsonObject getCapabilities() {
365 public void setCapabilities(JsonObject capabilities) {
366 this.capabilities = capabilities;
369 public List<Relationship> getRelationships() {
370 return relationships;
373 public void setRelationships(List<Relationship> relationships) {
374 this.relationships = relationships;
379 * @return NodeTemplateScalable
381 public NodeTemplateScalable getScalable() {
382 if (capabilities == null) {
385 JsonElement scaleableJson = capabilities.get("scalable");
386 if (scaleableJson == null || !scaleableJson.isJsonObject()) {
389 JsonElement propertyJson = scaleableJson.getAsJsonObject().get("properties");
390 if (propertyJson == null || !propertyJson.isJsonObject()) {
394 NodeTemplateScalable scalable = new NodeTemplateScalable();
395 scalable.setMin_instances(
396 ToolUtil.getAsString(propertyJson.getAsJsonObject().get("min_instances")));
397 scalable.setMax_instances(
398 ToolUtil.getAsString(propertyJson.getAsJsonObject().get("max_instances")));
399 scalable.setDefault_instances(
400 ToolUtil.getAsString(propertyJson.getAsJsonObject().get("default_instances")));
404 public class Relationship {
405 private String targetNodeName;
407 private String sourceNodeName;
409 public String getTargetNodeName() {
410 return targetNodeName;
413 public void setTargetNodeName(String targetNodeName) {
414 this.targetNodeName = targetNodeName;
417 public String getType() {
421 public void setType(String type) {
425 public String getSourceNodeName() {
426 return sourceNodeName;
429 public void setSourceNodeName(String sourceNodeName) {
430 this.sourceNodeName = sourceNodeName;
434 public class NodeTemplateScalable {
435 private String minInstances;
436 private String maxInstances;
437 private String defaultInstances;
439 public String getMin_instances() {
443 public void setMin_instances(String minInstances) {
444 this.minInstances = minInstances;
447 public String getMax_instances() {
451 public void setMax_instances(String maxInstances) {
452 this.maxInstances = maxInstances;
455 public String getDefault_instances() {
456 return defaultInstances;
459 public void setDefault_instances(String defaultInstances) {
460 this.defaultInstances = defaultInstances;
465 public class SubstitutionMapping {
466 private String node_type;
467 private JsonObject requirements;
468 private JsonObject capabilities;
470 public String getNode_type() {
474 public void setNode_type(String node_type) {
475 this.node_type = node_type;
478 public JsonObject getRequirements() {
482 public void setRequirements(JsonObject requirements) {
483 this.requirements = requirements;
486 public Map<String, String[]> getRequirementList() {
487 return jsonObjects2Requirements(this.requirements);
490 private Map<String, String[]> jsonObjects2Requirements(JsonObject requirements) {
491 if (requirements == null) {
492 return new HashMap<String, String[]>();
495 Map<String, String[]> ret = new HashMap<String, String[]>();
496 Iterator<Entry<String, JsonElement>> iterator = requirements.entrySet().iterator();
497 while (iterator.hasNext()) {
498 Entry<String, JsonElement> next = iterator.next();
499 if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
500 ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())});
504 if (next.getValue().isJsonArray()) {
505 String[] value = parseListValue((JsonArray) next.getValue());
506 ret.put(next.getKey(), value);
513 private String[] parseListValue(JsonArray jsonArray) {
514 String[] value = new String[jsonArray.size()];
515 for (int i = 0, size = jsonArray.size(); i < size; i++) {
516 value[i] = ToolUtil.getAsString(jsonArray.get(i));
521 public JsonObject getCapabilities() {
525 public void setCapabilities(JsonObject capabilities) {
526 this.capabilities = capabilities;
529 public Map<String, String[]> getCapabilityList() {
530 return jsonObject2Capabilities(this.capabilities);
533 private Map<String, String[]> jsonObject2Capabilities(JsonObject capabilities) {
534 if (capabilities == null) {
535 return new HashMap<String, String[]>();
538 Map<String, String[]> ret = new HashMap<String, String[]>();
539 Iterator<Entry<String, JsonElement>> iterator = capabilities.entrySet().iterator();
540 while (iterator.hasNext()) {
541 Entry<String, JsonElement> next = iterator.next();
542 if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
543 ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())});
547 if (next.getValue().isJsonArray()) {
548 String[] value = parseListValue((JsonArray) next.getValue());
549 ret.put(next.getKey(), value);
559 public class RelationshipType {
561 private RelationshipValue value;
563 public String getType() {
567 public void setType(String type) {
571 public RelationshipValue getValue() {
575 public void setValue(RelationshipValue value) {
579 public class RelationshipValue {
580 private String derivedFrom;
581 private String[] validTargetTypes;
583 public String getDerivedFrom() {
587 public void setDerivedFrom(String derivedFrom) {
588 this.derivedFrom = derivedFrom;
591 public String[] getValid_target_types() {
592 return validTargetTypes;
595 public void setValid_target_types(String[] validTargetTypes) {
596 this.validTargetTypes = validTargetTypes;
602 public class NodeType {
604 private NodeTypeValue value;
606 public String getType() {
610 public void setType(String type) {
614 public NodeTypeValue getValue() {
618 public void setValue(NodeTypeValue value) {
622 public class NodeTypeValue {
623 private String derivedFrom;
624 private JsonObject properties;
625 private JsonObject[] requirements;
626 private JsonObject capabilities;
628 public String getDerivedFrom() {
632 public void setDerived_from(String derivedFrom) {
633 this.derivedFrom = derivedFrom;
636 public JsonObject getProperties() {
640 public void setProperties(JsonObject properties) {
641 this.properties = properties;
644 public List<NodeTypeProperty> getPropertyList() {
645 return jsonObject2Properties(properties);
648 private List<NodeTypeProperty> jsonObject2Properties(JsonObject properties) {
649 List<NodeTypeProperty> propertieList = new ArrayList<NodeTypeProperty>();
650 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
651 while (iterator.hasNext()) {
652 NodeTypeProperty type = new NodeTypeProperty();
653 Entry<String, JsonElement> next = iterator.next();
654 type.setKey(next.getKey());
655 type.setValue(new Gson().fromJson(next.getValue(), JsonObject.class));
656 propertieList.add(type);
658 return propertieList;
661 public class NodeTypeProperty {
663 private JsonObject value;
665 public String getKey() {
669 public void setKey(String key) {
675 * @return default value
677 public String getDefaultValue() {
678 JsonElement defaultValue = value.get("default");
679 if (defaultValue == null || defaultValue.isJsonObject()) {
683 return ToolUtil.getAsString(defaultValue);
686 public JsonObject getValue() {
690 public void setValue(JsonObject value) {
695 public JsonObject[] getRequirements() {
699 public void setRequirements(JsonObject[] requirements) {
700 this.requirements = requirements;
703 public JsonObject getCapabilities() {
707 public void setCapabilities(JsonObject capabilities) {
708 this.capabilities = capabilities;
715 private PlanValue value;
717 public String getName() {
721 public void setName(String name) {
725 public String getDescription() {
726 return value.getDescription();
729 public String getReference() {
730 return value.getReference();
733 public String getPlanLanguage() {
734 return value.getPlanLanguage();
737 public List<PlanValue.PlanInput> getInputList() {
738 return value.getInputList();
741 public PlanValue getValue() {
745 public void setValue(PlanValue value) {
749 public class PlanValue {
750 private String description;
751 private String reference;
752 private String planLanguage;
753 private JsonObject inputs;
755 public String getDescription() {
759 public void setDescription(String description) {
760 this.description = description;
763 public String getReference() {
767 public void setReference(String reference) {
768 this.reference = reference;
771 public String getPlanLanguage() {
775 public void setPlanLanguage(String planLanguage) {
776 this.planLanguage = planLanguage;
779 public JsonObject getInputs() {
783 public void setInputs(JsonObject inputs) {
784 this.inputs = inputs;
787 public List<PlanInput> getInputList() {
788 return jsonObject2PlanInputList(inputs);
792 private List<PlanInput> jsonObject2PlanInputList(JsonObject inputs) {
793 List<PlanInput> retList = new ArrayList<PlanInput>();
794 Iterator<Entry<String, JsonElement>> iterator = inputs.entrySet().iterator();
795 while (iterator.hasNext()) {
796 PlanInput ret = new PlanInput();
797 Entry<String, JsonElement> next = iterator.next();
798 ret.setName(next.getKey());
799 ret.setValue(new Gson().fromJson(next.getValue(), PlanInput.PlanInputValue.class));
805 public class PlanInput {
807 private PlanInputValue value;
809 public String getName() {
813 public void setName(String name) {
817 public String getType() {
818 return value.getType();
821 public String getDescription() {
822 return value.getDescription();
825 public String getDefault() {
826 return value.getDefaultValue();
829 public boolean isRequired() {
830 return value.isRequired();
833 public PlanInputValue getValue() {
837 public void setValue(PlanInputValue value) {
841 public class PlanInputValue {
843 private String description;
844 @SerializedName("default")
845 private String defaultValue;
846 private boolean required;
848 public String getType() {
852 public void setType(String type) {
856 public String getDescription() {
860 public void setDescription(String description) {
861 this.description = description;
864 public String getDefaultValue() {
868 public void setDefaultValue(String defaultValue) {
869 this.defaultValue = defaultValue;
872 public boolean isRequired() {
876 public void setRequired(boolean required) {
877 this.required = required;