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 org.openo.commontosca.catalog.common.ToolUtil;
20 import com.google.gson.Gson;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import com.google.gson.annotations.SerializedName;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
31 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() {
136 if (this.metadata == null) {
137 return new HashMap<>();
142 public void setMetadata(Map<String, String> metadata) {
143 this.metadata = metadata;
146 public JsonObject getPlans() {
150 public void setPlans(JsonObject plans) {
154 public List<Plan> getPlanList() {
155 return jsonObject2PlanList(this.plans);
158 private List<Plan> jsonObject2PlanList(JsonObject plans) {
160 return new ArrayList<>();
162 List<Plan> retList = new ArrayList<>();
163 Iterator<Entry<String, JsonElement>> iterator = plans.entrySet().iterator();
164 while (iterator.hasNext()) {
165 Plan ret = new Plan();
166 Entry<String, JsonElement> next = iterator.next();
167 ret.setName(next.getKey());
168 ret.setValue(new Gson().fromJson(next.getValue(), Plan.PlanValue.class));
174 public class TopologyTemplate {
175 private String description;
176 private List<Input> inputs;
177 private List<Output> outputs;
178 private List<NodeTemplate> nodeTemplates;
179 private SubstitutionMapping substitutionMappings;
181 public String getDescription() {
185 public void setDescription(String description) {
186 this.description = description;
189 public List<Input> getInputs() {
193 public void setInputs(List<Input> inputs) {
194 this.inputs = inputs;
197 public List<Output> getOutputs() {
201 public void setOutputs(List<Output> outputs) {
202 this.outputs = outputs;
205 public List<NodeTemplate> getNodeTemplates() {
206 return nodeTemplates;
209 public void setNodeTemplates(List<NodeTemplate> nodeTemplates) {
210 this.nodeTemplates = nodeTemplates;
213 public SubstitutionMapping getSubstitutionMappings() {
214 return substitutionMappings;
217 public void setSubstitutionMappings(SubstitutionMapping substitutionMappings) {
218 this.substitutionMappings = substitutionMappings;
224 private String description;
225 private String defaultValue;
226 private boolean required;
228 public String getName() {
232 public void setName(String name) {
236 public String getType() {
240 public void setType(String type) {
244 public String getDescription() {
248 public void setDescription(String description) {
249 this.description = description;
252 public String getDefault() {
256 public void setDefault(String defaultValue) {
257 this.defaultValue = defaultValue;
260 public boolean isRequired() {
264 public void setRequired(boolean required) {
265 this.required = required;
269 public class Output {
271 private String description;
272 private Object value;
274 public String getName() {
278 public void setName(String name) {
282 public String getDescription() {
286 public void setDescription(String description) {
287 this.description = description;
290 public Object getValue() {
294 public void setValue(Object value) {
299 public class NodeTemplate {
301 private String nodeType;
302 private JsonObject properties;
303 private JsonObject[] requirements;
304 private JsonObject capabilities;
305 private List<Relationship> relationships;
307 public String getName() {
311 public void setName(String name) {
315 public String getNodeType() {
319 public void setNodeType(String nodeType) {
320 this.nodeType = nodeType;
323 public JsonObject getProperties() {
327 public void setProperties(JsonObject properties) {
328 this.properties = properties;
331 public Map<String, Object> getPropertyList() {
332 return jsonObject2Properties(properties);
335 private Map<String, Object> jsonObject2Properties(JsonObject properties) {
336 Map<String, Object> ret = new HashMap<>();
337 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
338 while (iterator.hasNext()) {
339 Entry<String, JsonElement> next = iterator.next();
340 ret.put(next.getKey(), ToolUtil.getAsString(next.getValue()));
345 public JsonObject[] getRequirements() {
349 public void setRequirements(JsonObject[] requirements) {
350 this.requirements = requirements;
353 public JsonObject getCapabilities() {
357 public void setCapabilities(JsonObject capabilities) {
358 this.capabilities = capabilities;
361 public List<Relationship> getRelationships() {
362 return relationships;
365 public void setRelationships(List<Relationship> relationships) {
366 this.relationships = relationships;
371 * @return NodeTemplateScalable
373 public NodeTemplateScalable getScalable() {
374 if (capabilities == null) {
377 JsonElement scaleableJson = capabilities.get("scalable");
378 if (scaleableJson == null || !scaleableJson.isJsonObject()) {
381 JsonElement propertyJson = scaleableJson.getAsJsonObject().get("properties");
382 if (propertyJson == null || !propertyJson.isJsonObject()) {
386 NodeTemplateScalable scalable = new NodeTemplateScalable();
387 scalable.setMin_instances(
388 ToolUtil.getAsString(propertyJson.getAsJsonObject().get("min_instances")));
389 scalable.setMax_instances(
390 ToolUtil.getAsString(propertyJson.getAsJsonObject().get("max_instances")));
391 scalable.setDefault_instances(
392 ToolUtil.getAsString(propertyJson.getAsJsonObject().get("default_instances")));
396 public class Relationship {
397 private String targetNodeName;
399 private String sourceNodeName;
401 public String getTargetNodeName() {
402 return targetNodeName;
405 public void setTargetNodeName(String targetNodeName) {
406 this.targetNodeName = targetNodeName;
409 public String getType() {
413 public void setType(String type) {
417 public String getSourceNodeName() {
418 return sourceNodeName;
421 public void setSourceNodeName(String sourceNodeName) {
422 this.sourceNodeName = sourceNodeName;
426 public class NodeTemplateScalable {
427 private String minInstances;
428 private String maxInstances;
429 private String defaultInstances;
431 public String getMin_instances() {
435 public void setMin_instances(String minInstances) {
436 this.minInstances = minInstances;
439 public String getMax_instances() {
443 public void setMax_instances(String maxInstances) {
444 this.maxInstances = maxInstances;
447 public String getDefault_instances() {
448 return defaultInstances;
451 public void setDefault_instances(String defaultInstances) {
452 this.defaultInstances = defaultInstances;
457 public class SubstitutionMapping {
458 private String nodeType;
459 private JsonObject requirements;
460 private JsonObject capabilities;
461 private JsonObject properties;
463 public String getNodeType() {
467 public void setNodeType(String nodeType) {
468 this.nodeType = nodeType;
471 public JsonObject getRequirements() {
475 public void setRequirements(JsonObject requirements) {
476 this.requirements = requirements;
479 public Map<String, String[]> getRequirementList() {
480 return jsonObjects2Requirements(this.requirements);
483 private Map<String, String[]> jsonObjects2Requirements(JsonObject requirements) {
484 Map<String, String[]> ret = new HashMap<String, String[]>();
486 Iterator<Entry<String, JsonElement>> iterator = requirements.entrySet().iterator();
487 while (iterator.hasNext()) {
488 Entry<String, JsonElement> next = iterator.next();
489 if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
490 ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())});
494 if (next.getValue().isJsonArray()) {
495 String[] value = parseListValue((JsonArray) next.getValue());
496 ret.put(next.getKey(), value);
503 private String[] parseListValue(JsonArray jsonArray) {
504 String[] value = new String[jsonArray.size()];
505 for (int i = 0, size = jsonArray.size(); i < size; i++) {
506 value[i] = ToolUtil.getAsString(jsonArray.get(i));
511 public JsonObject getCapabilities() {
515 public void setCapabilities(JsonObject capabilities) {
516 this.capabilities = capabilities;
519 public Map<String, String[]> getCapabilityList() {
520 return jsonObject2Capabilities(this.capabilities);
523 private Map<String, String[]> jsonObject2Capabilities(JsonObject capabilities) {
524 Map<String, String[]> ret = new HashMap<String, String[]>();
526 Iterator<Entry<String, JsonElement>> iterator = capabilities.entrySet().iterator();
527 while (iterator.hasNext()) {
528 Entry<String, JsonElement> next = iterator.next();
529 if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
530 ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())});
534 if (next.getValue().isJsonArray()) {
535 String[] value = parseListValue((JsonArray) next.getValue());
536 ret.put(next.getKey(), value);
543 public JsonObject getProperties() {
547 public void setProperties(JsonObject properties) {
548 this.properties = properties;
551 public Map<String, Object> getPropertyList() {
552 return jsonObject2Properties(properties);
555 private Map<String, Object> jsonObject2Properties(JsonObject properties) {
556 Map<String, Object> ret = new HashMap<>();
557 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
558 while (iterator.hasNext()) {
559 Entry<String, JsonElement> next = iterator.next();
560 ret.put(next.getKey(), ToolUtil.getAsString(next.getValue()));
568 public class RelationshipType {
570 private RelationshipValue value;
572 public String getType() {
576 public void setType(String type) {
580 public RelationshipValue getValue() {
584 public void setValue(RelationshipValue value) {
588 public class RelationshipValue {
589 private String derivedFrom;
590 private String[] validTargetTypes;
592 public String getDerivedFrom() {
596 public void setDerivedFrom(String derivedFrom) {
597 this.derivedFrom = derivedFrom;
600 public String[] getValid_target_types() {
601 return validTargetTypes;
604 public void setValid_target_types(String[] validTargetTypes) {
605 this.validTargetTypes = validTargetTypes;
611 public class NodeType {
613 private NodeTypeValue value;
615 public String getType() {
619 public void setType(String type) {
623 public NodeTypeValue getValue() {
627 public void setValue(NodeTypeValue value) {
631 public class NodeTypeValue {
632 private String derivedFrom;
633 private JsonObject properties;
634 private JsonObject[] requirements;
635 private JsonObject capabilities;
637 public String getDerivedFrom() {
641 public void setDerived_from(String derivedFrom) {
642 this.derivedFrom = derivedFrom;
645 public JsonObject getProperties() {
649 public void setProperties(JsonObject properties) {
650 this.properties = properties;
653 public List<NodeTypeProperty> getPropertyList() {
654 return jsonObject2Properties(properties);
657 private List<NodeTypeProperty> jsonObject2Properties(JsonObject properties) {
658 List<NodeTypeProperty> propertieList = new ArrayList<NodeTypeProperty>();
659 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
660 while (iterator.hasNext()) {
661 NodeTypeProperty type = new NodeTypeProperty();
662 Entry<String, JsonElement> next = iterator.next();
663 type.setKey(next.getKey());
664 type.setValue(new Gson().fromJson(next.getValue(), JsonObject.class));
665 propertieList.add(type);
667 return propertieList;
670 public class NodeTypeProperty {
672 private JsonObject value;
674 public String getKey() {
678 public void setKey(String key) {
684 * @return default value
686 public String getDefaultValue() {
687 JsonElement defaultValue = value.get("default");
688 if (defaultValue == null || defaultValue.isJsonObject()) {
692 return ToolUtil.getAsString(defaultValue);
695 public JsonObject getValue() {
699 public void setValue(JsonObject value) {
704 public JsonObject[] getRequirements() {
708 public void setRequirements(JsonObject[] requirements) {
709 this.requirements = requirements;
712 public JsonObject getCapabilities() {
716 public void setCapabilities(JsonObject capabilities) {
717 this.capabilities = capabilities;
724 private PlanValue value;
726 public String getName() {
730 public void setName(String name) {
734 public String getDescription() {
735 return value.getDescription();
738 public String getReference() {
739 return value.getReference();
742 public String getPlanLanguage() {
743 return value.getPlanLanguage();
746 public List<PlanValue.PlanInput> getInputList() {
747 return value.getInputList();
750 public PlanValue getValue() {
754 public void setValue(PlanValue value) {
758 public class PlanValue {
759 private String description;
760 private String reference;
761 private String planLanguage;
762 private JsonObject inputs;
764 public String getDescription() {
768 public void setDescription(String description) {
769 this.description = description;
772 public String getReference() {
776 public void setReference(String reference) {
777 this.reference = reference;
780 public String getPlanLanguage() {
784 public void setPlanLanguage(String planLanguage) {
785 this.planLanguage = planLanguage;
788 public JsonObject getInputs() {
792 public void setInputs(JsonObject inputs) {
793 this.inputs = inputs;
796 public List<PlanInput> getInputList() {
797 return jsonObject2PlanInputList(inputs);
801 private List<PlanInput> jsonObject2PlanInputList(JsonObject inputs) {
802 List<PlanInput> retList = new ArrayList<PlanInput>();
803 Iterator<Entry<String, JsonElement>> iterator = inputs.entrySet().iterator();
804 while (iterator.hasNext()) {
805 PlanInput ret = new PlanInput();
806 Entry<String, JsonElement> next = iterator.next();
807 ret.setName(next.getKey());
808 ret.setValue(new Gson().fromJson(next.getValue(), PlanInput.PlanInputValue.class));
814 public class PlanInput {
816 private PlanInputValue value;
818 public String getName() {
822 public void setName(String name) {
826 public String getType() {
827 return value.getType();
830 public String getDescription() {
831 return value.getDescription();
834 public String getDefault() {
835 return value.getDefaultValue();
838 public boolean isRequired() {
839 return value.isRequired();
842 public PlanInputValue getValue() {
846 public void setValue(PlanInputValue value) {
850 public class PlanInputValue {
852 private String description;
853 @SerializedName("default")
854 private String defaultValue;
855 private boolean required;
857 public String getType() {
861 public void setType(String type) {
865 public String getDescription() {
869 public void setDescription(String description) {
870 this.description = description;
873 public String getDefaultValue() {
877 public void setDefaultValue(String defaultValue) {
878 this.defaultValue = defaultValue;
881 public boolean isRequired() {
885 public void setRequired(boolean required) {
886 this.required = required;