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 nodeType;
467 private JsonObject requirements;
468 private JsonObject capabilities;
469 private JsonObject properties;
471 public String getNodeType() {
475 public void setNodeType(String nodeType) {
476 this.nodeType = nodeType;
479 public JsonObject getRequirements() {
483 public void setRequirements(JsonObject requirements) {
484 this.requirements = requirements;
487 public Map<String, String[]> getRequirementList() {
488 return jsonObjects2Requirements(this.requirements);
491 private Map<String, String[]> jsonObjects2Requirements(JsonObject requirements) {
492 Map<String, String[]> ret = new HashMap<String, String[]>();
494 Iterator<Entry<String, JsonElement>> iterator = requirements.entrySet().iterator();
495 while (iterator.hasNext()) {
496 Entry<String, JsonElement> next = iterator.next();
497 if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
498 ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())});
502 if (next.getValue().isJsonArray()) {
503 String[] value = parseListValue((JsonArray) next.getValue());
504 ret.put(next.getKey(), value);
511 private String[] parseListValue(JsonArray jsonArray) {
512 String[] value = new String[jsonArray.size()];
513 for (int i = 0, size = jsonArray.size(); i < size; i++) {
514 value[i] = ToolUtil.getAsString(jsonArray.get(i));
519 public JsonObject getCapabilities() {
523 public void setCapabilities(JsonObject capabilities) {
524 this.capabilities = capabilities;
527 public Map<String, String[]> getCapabilityList() {
528 return jsonObject2Capabilities(this.capabilities);
531 private Map<String, String[]> jsonObject2Capabilities(JsonObject capabilities) {
532 Map<String, String[]> ret = new HashMap<String, String[]>();
534 Iterator<Entry<String, JsonElement>> iterator = capabilities.entrySet().iterator();
535 while (iterator.hasNext()) {
536 Entry<String, JsonElement> next = iterator.next();
537 if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
538 ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())});
542 if (next.getValue().isJsonArray()) {
543 String[] value = parseListValue((JsonArray) next.getValue());
544 ret.put(next.getKey(), value);
551 public JsonObject getProperties() {
555 public void setProperties(JsonObject properties) {
556 this.properties = properties;
559 public Map<String, Object> getPropertyList() {
560 return jsonObject2Properties(properties);
563 private Map<String, Object> jsonObject2Properties(JsonObject properties) {
564 Map<String, Object> ret = new HashMap<>();
565 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
566 while (iterator.hasNext()) {
567 Entry<String, JsonElement> next = iterator.next();
568 ret.put(next.getKey(), ToolUtil.getAsString(next.getValue()));
576 public class RelationshipType {
578 private RelationshipValue value;
580 public String getType() {
584 public void setType(String type) {
588 public RelationshipValue getValue() {
592 public void setValue(RelationshipValue value) {
596 public class RelationshipValue {
597 private String derivedFrom;
598 private String[] validTargetTypes;
600 public String getDerivedFrom() {
604 public void setDerivedFrom(String derivedFrom) {
605 this.derivedFrom = derivedFrom;
608 public String[] getValid_target_types() {
609 return validTargetTypes;
612 public void setValid_target_types(String[] validTargetTypes) {
613 this.validTargetTypes = validTargetTypes;
619 public class NodeType {
621 private NodeTypeValue value;
623 public String getType() {
627 public void setType(String type) {
631 public NodeTypeValue getValue() {
635 public void setValue(NodeTypeValue value) {
639 public class NodeTypeValue {
640 private String derivedFrom;
641 private JsonObject properties;
642 private JsonObject[] requirements;
643 private JsonObject capabilities;
645 public String getDerivedFrom() {
649 public void setDerived_from(String derivedFrom) {
650 this.derivedFrom = derivedFrom;
653 public JsonObject getProperties() {
657 public void setProperties(JsonObject properties) {
658 this.properties = properties;
661 public List<NodeTypeProperty> getPropertyList() {
662 return jsonObject2Properties(properties);
665 private List<NodeTypeProperty> jsonObject2Properties(JsonObject properties) {
666 List<NodeTypeProperty> propertieList = new ArrayList<NodeTypeProperty>();
667 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
668 while (iterator.hasNext()) {
669 NodeTypeProperty type = new NodeTypeProperty();
670 Entry<String, JsonElement> next = iterator.next();
671 type.setKey(next.getKey());
672 type.setValue(new Gson().fromJson(next.getValue(), JsonObject.class));
673 propertieList.add(type);
675 return propertieList;
678 public class NodeTypeProperty {
680 private JsonObject value;
682 public String getKey() {
686 public void setKey(String key) {
692 * @return default value
694 public String getDefaultValue() {
695 JsonElement defaultValue = value.get("default");
696 if (defaultValue == null || defaultValue.isJsonObject()) {
700 return ToolUtil.getAsString(defaultValue);
703 public JsonObject getValue() {
707 public void setValue(JsonObject value) {
712 public JsonObject[] getRequirements() {
716 public void setRequirements(JsonObject[] requirements) {
717 this.requirements = requirements;
720 public JsonObject getCapabilities() {
724 public void setCapabilities(JsonObject capabilities) {
725 this.capabilities = capabilities;
732 private PlanValue value;
734 public String getName() {
738 public void setName(String name) {
742 public String getDescription() {
743 return value.getDescription();
746 public String getReference() {
747 return value.getReference();
750 public String getPlanLanguage() {
751 return value.getPlanLanguage();
754 public List<PlanValue.PlanInput> getInputList() {
755 return value.getInputList();
758 public PlanValue getValue() {
762 public void setValue(PlanValue value) {
766 public class PlanValue {
767 private String description;
768 private String reference;
769 private String planLanguage;
770 private JsonObject inputs;
772 public String getDescription() {
776 public void setDescription(String description) {
777 this.description = description;
780 public String getReference() {
784 public void setReference(String reference) {
785 this.reference = reference;
788 public String getPlanLanguage() {
792 public void setPlanLanguage(String planLanguage) {
793 this.planLanguage = planLanguage;
796 public JsonObject getInputs() {
800 public void setInputs(JsonObject inputs) {
801 this.inputs = inputs;
804 public List<PlanInput> getInputList() {
805 return jsonObject2PlanInputList(inputs);
809 private List<PlanInput> jsonObject2PlanInputList(JsonObject inputs) {
810 List<PlanInput> retList = new ArrayList<PlanInput>();
811 Iterator<Entry<String, JsonElement>> iterator = inputs.entrySet().iterator();
812 while (iterator.hasNext()) {
813 PlanInput ret = new PlanInput();
814 Entry<String, JsonElement> next = iterator.next();
815 ret.setName(next.getKey());
816 ret.setValue(new Gson().fromJson(next.getValue(), PlanInput.PlanInputValue.class));
822 public class PlanInput {
824 private PlanInputValue value;
826 public String getName() {
830 public void setName(String name) {
834 public String getType() {
835 return value.getType();
838 public String getDescription() {
839 return value.getDescription();
842 public String getDefault() {
843 return value.getDefaultValue();
846 public boolean isRequired() {
847 return value.isRequired();
850 public PlanInputValue getValue() {
854 public void setValue(PlanInputValue value) {
858 public class PlanInputValue {
860 private String description;
861 @SerializedName("default")
862 private String defaultValue;
863 private boolean required;
865 public String getType() {
869 public void setType(String type) {
873 public String getDescription() {
877 public void setDescription(String description) {
878 this.description = description;
881 public String getDefaultValue() {
885 public void setDefaultValue(String defaultValue) {
886 this.defaultValue = defaultValue;
889 public boolean isRequired() {
893 public void setRequired(boolean required) {
894 this.required = required;