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 com.google.gson.Gson;
 
  19 import com.google.gson.JsonArray;
 
  20 import com.google.gson.JsonElement;
 
  21 import com.google.gson.JsonObject;
 
  22 import com.google.gson.annotations.SerializedName;
 
  24 import java.util.ArrayList;
 
  25 import java.util.HashMap;
 
  26 import java.util.Iterator;
 
  27 import java.util.List;
 
  29 import java.util.Map.Entry;
 
  32 public class ParseYamlResult {
 
  33   private String toscaDefinitionsVersion;
 
  34   private String description;
 
  35   private JsonObject nodeTypes;
 
  36   private JsonObject capabilityTypes;
 
  37   private JsonObject relationshipTypes;
 
  38   private JsonObject policyTypes;
 
  39   private TopologyTemplate topologyTemplate;
 
  40   private Map<String, String> metadata;
 
  41   private JsonObject plans;
 
  44   public String getToscaDefinitionsVersion() {
 
  45     return toscaDefinitionsVersion;
 
  48   public void setToscaDefinitionsVersion(String toscaDefinitionsVersion) {
 
  49     this.toscaDefinitionsVersion = toscaDefinitionsVersion;
 
  52   public String getDescription() {
 
  56   public void setDescription(String description) {
 
  57     this.description = description;
 
  60   public JsonObject getNodeTypes() {
 
  64   public void setNodeTypes(JsonObject nodeTypes) {
 
  65     this.nodeTypes = nodeTypes;
 
  68   public List<NodeType> getNodeTypeList() {
 
  69     return jsonObject2NodeTypes(nodeTypes);
 
  72   private ArrayList<NodeType> jsonObject2NodeTypes(JsonObject nodeTypes) {
 
  73     ArrayList<NodeType> nodeTypeList = new ArrayList<NodeType>();
 
  74     Iterator<Entry<String, JsonElement>> iterator = nodeTypes.entrySet().iterator();
 
  75     while (iterator.hasNext()) {
 
  76       NodeType type = new NodeType();
 
  77       Entry<String, JsonElement> next = iterator.next();
 
  78       type.setType(next.getKey());
 
  79       type.setValue(new Gson().fromJson(next.getValue(), NodeType.NodeTypeValue.class));
 
  80       nodeTypeList.add(type);
 
  85   public JsonObject getCapabilityTypes() {
 
  86     return capabilityTypes;
 
  89   public void setCapabilityTypes(JsonObject capabilityTypes) {
 
  90     this.capabilityTypes = capabilityTypes;
 
  93   public JsonObject getRelationshipTypes() {
 
  94     return relationshipTypes;
 
  97   public void setRelationshipTypes(JsonObject relationshipTypes) {
 
  98     this.relationshipTypes = relationshipTypes;
 
 101   public List<RelationshipType> getRelationshipTypeList() {
 
 102     return jsonObject2RelationshipTypes(relationshipTypes);
 
 105   private ArrayList<RelationshipType> jsonObject2RelationshipTypes(JsonObject relationshipTypes) {
 
 106     ArrayList<RelationshipType> relationshipTypeList = new ArrayList<RelationshipType>();
 
 107     Iterator<Entry<String, JsonElement>> iterator = relationshipTypes.entrySet().iterator();
 
 108     while (iterator.hasNext()) {
 
 109       RelationshipType type = new RelationshipType();
 
 110       Entry<String, JsonElement> next = iterator.next();
 
 111       type.setType(next.getKey());
 
 112       type.setValue(new Gson().fromJson(next.getValue(), RelationshipType.RelationshipValue.class));
 
 113       relationshipTypeList.add(type);
 
 115     return relationshipTypeList;
 
 118   public JsonObject getPolicyTypes() {
 
 122   public void setPolicyTypes(JsonObject policyTypes) {
 
 123     this.policyTypes = policyTypes;
 
 126   public TopologyTemplate getTopologyTemplate() {
 
 127     return topologyTemplate;
 
 130   public void setTopologyTemplate(TopologyTemplate topologyTemplate) {
 
 131     this.topologyTemplate = topologyTemplate;
 
 134   public Map<String, String> getMetadata() {
 
 138   public void setMetadata(Map<String, String> metadata) {
 
 139     this.metadata = metadata;
 
 142   public JsonObject getPlans() {
 
 146   public void setPlans(JsonObject plans) {
 
 150   public List<Plan> getPlanList() {
 
 151     return jsonObject2PlanList(this.plans);
 
 154   private List<Plan> jsonObject2PlanList(JsonObject plans) {
 
 156       return new ArrayList<>();
 
 158     List<Plan> retList = new ArrayList<>();
 
 159     Iterator<Entry<String, JsonElement>> iterator = plans.entrySet().iterator();
 
 160     while (iterator.hasNext()) {
 
 161       Plan ret = new Plan();
 
 162       Entry<String, JsonElement> next = iterator.next();
 
 163       ret.setName(next.getKey());
 
 164       ret.setValue(new Gson().fromJson(next.getValue(), Plan.PlanValue.class));
 
 170   public class TopologyTemplate {
 
 171     private String description;
 
 172     private List<Input> inputs;
 
 173     private List<Output> outputs;
 
 174     private List<NodeTemplate> nodeTemplates;
 
 175     private SubstitutionMapping substitutionMappings;
 
 177     public String getDescription() {
 
 181     public void setDescription(String description) {
 
 182       this.description = description;
 
 185     public List<Input> getInputs() {
 
 189     public void setInputs(List<Input> inputs) {
 
 190       this.inputs = inputs;
 
 193     public List<Output> getOutputs() {
 
 197     public void setOutputs(List<Output> outputs) {
 
 198       this.outputs = outputs;
 
 201     public List<NodeTemplate> getNodeTemplates() {
 
 202       return nodeTemplates;
 
 205     public void setNodeTemplates(List<NodeTemplate> nodeTemplates) {
 
 206       this.nodeTemplates = nodeTemplates;
 
 209     public SubstitutionMapping getSubstitutionMappings() {
 
 210       return substitutionMappings;
 
 213     public void setSubstitutionMappings(SubstitutionMapping substitutionMappings) {
 
 214       this.substitutionMappings = substitutionMappings;
 
 220       private String description;
 
 221       private String defaultValue;
 
 222       private boolean required;
 
 224       public String getName() {
 
 228       public void setName(String name) {
 
 232       public String getType() {
 
 236       public void setType(String type) {
 
 240       public String getDescription() {
 
 244       public void setDescription(String description) {
 
 245         this.description = description;
 
 248       public String getDefault() {
 
 252       public void setDefault(String defaultValue) {
 
 253         this.defaultValue = defaultValue;
 
 256       public boolean isRequired() {
 
 260       public void setRequired(boolean required) {
 
 261         this.required = required;
 
 265     public class Output {
 
 267       private String description;
 
 268       private Object value;
 
 270       public String getName() {
 
 274       public void setName(String name) {
 
 278       public String getDescription() {
 
 282       public void setDescription(String description) {
 
 283         this.description = description;
 
 286       public Object getValue() {
 
 290       public void setValue(Object value) {
 
 295     public class NodeTemplate {
 
 297       private String nodeType;
 
 298       private JsonObject properties;
 
 299       private JsonObject[] requirements;
 
 300       private JsonObject capabilities;
 
 301       private List<Relationship> relationships;
 
 303       public String getName() {
 
 307       public void setName(String name) {
 
 311       public String getNodeType() {
 
 315       public void setNodeType(String nodeType) {
 
 316         this.nodeType = nodeType;
 
 319       public JsonObject getProperties() {
 
 323       public void setProperties(JsonObject properties) {
 
 324         this.properties = properties;
 
 327       public Map<String, Object> getPropertyList() {
 
 328         return jsonObject2Properties(properties);
 
 331       private Map<String, Object> jsonObject2Properties(JsonObject properties) {
 
 332         Map<String, Object> ret = new HashMap<>();
 
 333         Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
 
 334         while (iterator.hasNext()) {
 
 335           Entry<String, JsonElement> next = iterator.next();
 
 336           ret.put(next.getKey(), next.getValue().getAsString());
 
 341       public JsonObject[] getRequirements() {
 
 345       public void setRequirements(JsonObject[] requirements) {
 
 346         this.requirements = requirements;
 
 349       public JsonObject getCapabilities() {
 
 353       public void setCapabilities(JsonObject capabilities) {
 
 354         this.capabilities = capabilities;
 
 357       public List<Relationship> getRelationships() {
 
 358         return relationships;
 
 361       public void setRelationships(List<Relationship> relationships) {
 
 362         this.relationships = relationships;
 
 367        * @return NodeTemplateScalable
 
 369       public NodeTemplateScalable getScalable() {
 
 370         if (capabilities == null) {
 
 373         JsonElement scaleableJson = capabilities.get("scalable");
 
 374         if (scaleableJson == null || !scaleableJson.isJsonObject()) {
 
 377         JsonElement propertyJson = scaleableJson.getAsJsonObject().get("properties");
 
 378         if (propertyJson == null || !propertyJson.isJsonObject()) {
 
 382         NodeTemplateScalable scalable = new NodeTemplateScalable();
 
 384             .setMin_instances(propertyJson.getAsJsonObject().get("min_instances").getAsString());
 
 386             .setMax_instances(propertyJson.getAsJsonObject().get("max_instances").getAsString());
 
 387         scalable.setDefault_instances(
 
 388             propertyJson.getAsJsonObject().get("default_instances").getAsString());
 
 392       public class Relationship {
 
 393         private String targetNodeName;
 
 395         private String sourceNodeName;
 
 397         public String getTargetNodeName() {
 
 398           return targetNodeName;
 
 401         public void setTargetNodeName(String targetNodeName) {
 
 402           this.targetNodeName = targetNodeName;
 
 405         public String getType() {
 
 409         public void setType(String type) {
 
 413         public String getSourceNodeName() {
 
 414           return sourceNodeName;
 
 417         public void setSourceNodeName(String sourceNodeName) {
 
 418           this.sourceNodeName = sourceNodeName;
 
 422       public class NodeTemplateScalable {
 
 423         private String minInstances;
 
 424         private String maxInstances;
 
 425         private String defaultInstances;
 
 427         public String getMin_instances() {
 
 431         public void setMin_instances(String minInstances) {
 
 432           this.minInstances = minInstances;
 
 435         public String getMax_instances() {
 
 439         public void setMax_instances(String maxInstances) {
 
 440           this.maxInstances = maxInstances;
 
 443         public String getDefault_instances() {
 
 444           return defaultInstances;
 
 447         public void setDefault_instances(String defaultInstances) {
 
 448           this.defaultInstances = defaultInstances;
 
 453     public class SubstitutionMapping {
 
 454       private String nodeType;
 
 455       private JsonObject requirements;
 
 456       private JsonObject capabilities;
 
 457       private JsonObject properties;
 
 459       public String getNodeType() {
 
 463       public void setNodeType(String nodeType) {
 
 464         this.nodeType = nodeType;
 
 467       public JsonObject getRequirements() {
 
 471       public void setRequirements(JsonObject requirements) {
 
 472         this.requirements = requirements;
 
 475       public Map<String, String[]> getRequirementList() {
 
 476         return jsonObjects2Requirements(this.requirements);
 
 479       private Map<String, String[]> jsonObjects2Requirements(JsonObject requirements) {
 
 480         Map<String, String[]> ret = new HashMap<String, String[]>();
 
 482         Iterator<Entry<String, JsonElement>> iterator = requirements.entrySet().iterator();
 
 483         while (iterator.hasNext()) {
 
 484           Entry<String, JsonElement> next = iterator.next();
 
 485           if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
 
 486             ret.put(next.getKey(), new String[] {next.getValue().getAsString()});
 
 490           if (next.getValue().isJsonArray()) {
 
 491             String[] value = parseListValue((JsonArray) next.getValue());
 
 492             ret.put(next.getKey(), value);
 
 499       private String[] parseListValue(JsonArray jsonArray) {
 
 500         String[] value = new String[jsonArray.size()];
 
 501         for (int i = 0, size = jsonArray.size(); i < size; i++) {
 
 502           value[i] = jsonArray.get(i).getAsString();
 
 507       public JsonObject getCapabilities() {
 
 511       public void setCapabilities(JsonObject capabilities) {
 
 512         this.capabilities = capabilities;
 
 515       public Map<String, String[]> getCapabilityList() {
 
 516         return jsonObject2Capabilities(this.capabilities);
 
 519       private Map<String, String[]> jsonObject2Capabilities(JsonObject capabilities) {
 
 520         Map<String, String[]> ret = new HashMap<String, String[]>();
 
 522         Iterator<Entry<String, JsonElement>> iterator = capabilities.entrySet().iterator();
 
 523         while (iterator.hasNext()) {
 
 524           Entry<String, JsonElement> next = iterator.next();
 
 526           if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
 
 527             ret.put(next.getKey(), new String[] {next.getValue().getAsString()});
 
 531           if (next.getValue().isJsonArray()) {
 
 532             String[] value = parseListValue((JsonArray) next.getValue());
 
 533             ret.put(next.getKey(), value);
 
 540       public JsonObject getProperties() {
 
 544       public void setProperties(JsonObject properties) {
 
 545         this.properties = properties;
 
 548       public Map<String, Object> getPropertyList() {
 
 549         return jsonObject2Properties(properties);
 
 552       private Map<String, Object> jsonObject2Properties(JsonObject properties) {
 
 553         Map<String, Object> ret = new HashMap<>();
 
 554         Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
 
 555         while (iterator.hasNext()) {
 
 556           Entry<String, JsonElement> next = iterator.next();
 
 557           ret.put(next.getKey(), next.getValue().getAsString());
 
 565   public class RelationshipType {
 
 567     private RelationshipValue value;
 
 569     public String getType() {
 
 573     public void setType(String type) {
 
 577     public RelationshipValue getValue() {
 
 581     public void setValue(RelationshipValue value) {
 
 585     public class RelationshipValue {
 
 586       private String derivedFrom;
 
 587       private String[] validTargetTypes;
 
 589       public String getDerivedFrom() {
 
 593       public void setDerivedFrom(String derivedFrom) {
 
 594         this.derivedFrom = derivedFrom;
 
 597       public String[] getValid_target_types() {
 
 598         return validTargetTypes;
 
 601       public void setValid_target_types(String[] validTargetTypes) {
 
 602         this.validTargetTypes = validTargetTypes;
 
 608   public class NodeType {
 
 610     private NodeTypeValue value;
 
 612     public String getType() {
 
 616     public void setType(String type) {
 
 620     public NodeTypeValue getValue() {
 
 624     public void setValue(NodeTypeValue value) {
 
 628     public class NodeTypeValue {
 
 629       private String derivedFrom;
 
 630       private JsonObject properties;
 
 631       private JsonObject[] requirements;
 
 632       private JsonObject capabilities;
 
 634       public String getDerivedFrom() {
 
 638       public void setDerived_from(String derivedFrom) {
 
 639         this.derivedFrom = derivedFrom;
 
 642       public JsonObject getProperties() {
 
 646       public void setProperties(JsonObject properties) {
 
 647         this.properties = properties;
 
 650       public List<NodeTypeProperty> getPropertyList() {
 
 651         return jsonObject2Properties(properties);
 
 654       private List<NodeTypeProperty> jsonObject2Properties(JsonObject properties) {
 
 655         List<NodeTypeProperty> propertieList = new ArrayList<NodeTypeProperty>();
 
 656         Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
 
 657         while (iterator.hasNext()) {
 
 658           NodeTypeProperty type = new NodeTypeProperty();
 
 659           Entry<String, JsonElement> next = iterator.next();
 
 660           type.setKey(next.getKey());
 
 661           type.setValue(new Gson().fromJson(next.getValue(), JsonObject.class));
 
 662           propertieList.add(type);
 
 664         return propertieList;
 
 667       public class NodeTypeProperty {
 
 669         private JsonObject value;
 
 671         public String getKey() {
 
 675         public void setKey(String key) {
 
 681          * @return default value
 
 683         public String getDefaultValue() {
 
 684           JsonElement defaultValue = value.get("default");
 
 685           if (defaultValue == null || defaultValue.isJsonObject()) {
 
 689           return defaultValue.getAsString();
 
 692         public JsonObject getValue() {
 
 696         public void setValue(JsonObject value) {
 
 701       public JsonObject[] getRequirements() {
 
 705       public void setRequirements(JsonObject[] requirements) {
 
 706         this.requirements = requirements;
 
 709       public JsonObject getCapabilities() {
 
 713       public void setCapabilities(JsonObject capabilities) {
 
 714         this.capabilities = capabilities;
 
 721     private PlanValue value;
 
 723     public String getName() {
 
 727     public void setName(String name) {
 
 731     public String getDescription() {
 
 732       return value.getDescription();
 
 735     public String getReference() {
 
 736       return value.getReference();
 
 739     public String getPlanLanguage() {
 
 740       return value.getPlanLanguage();
 
 743     public List<PlanValue.PlanInput> getInputList() {
 
 744       return value.getInputList();
 
 747     public PlanValue getValue() {
 
 751     public void setValue(PlanValue value) {
 
 755     public class PlanValue {
 
 756       private String description;
 
 757       private String reference;
 
 758       private String planLanguage;
 
 759       private JsonObject inputs;
 
 761       public String getDescription() {
 
 765       public void setDescription(String description) {
 
 766         this.description = description;
 
 769       public String getReference() {
 
 773       public void setReference(String reference) {
 
 774         this.reference = reference;
 
 777       public String getPlanLanguage() {
 
 781       public void setPlanLanguage(String planLanguage) {
 
 782         this.planLanguage = planLanguage;
 
 785       public JsonObject getInputs() {
 
 789       public void setInputs(JsonObject inputs) {
 
 790         this.inputs = inputs;
 
 793       public List<PlanInput> getInputList() {
 
 794         return jsonObject2PlanInputList(inputs);
 
 798       private List<PlanInput> jsonObject2PlanInputList(JsonObject inputs) {
 
 799         List<PlanInput> retList = new ArrayList<PlanInput>();
 
 800         Iterator<Entry<String, JsonElement>> iterator = inputs.entrySet().iterator();
 
 801         while (iterator.hasNext()) {
 
 802           PlanInput ret = new PlanInput();
 
 803           Entry<String, JsonElement> next = iterator.next();
 
 804           ret.setName(next.getKey());
 
 805           ret.setValue(new Gson().fromJson(next.getValue(), PlanInput.PlanInputValue.class));
 
 811       public class PlanInput {
 
 813         private PlanInputValue value;
 
 815         public String getName() {
 
 819         public void setName(String name) {
 
 823         public String getType() {
 
 824           return value.getType();
 
 827         public String getDescription() {
 
 828           return value.getDescription();
 
 831         public String getDefault() {
 
 832           return value.getDefaultValue();
 
 835         public boolean isRequired() {
 
 836           return value.isRequired();
 
 839         public PlanInputValue getValue() {
 
 843         public void setValue(PlanInputValue value) {
 
 847         public class PlanInputValue {
 
 849           private String description;
 
 850           @SerializedName("default")
 
 851           private String defaultValue;
 
 852           private boolean required;
 
 854           public String getType() {
 
 858           public void setType(String type) {
 
 862           public String getDescription() {
 
 866           public void setDescription(String description) {
 
 867             this.description = description;
 
 870           public String getDefaultValue() {
 
 874           public void setDefaultValue(String defaultValue) {
 
 875             this.defaultValue = defaultValue;
 
 878           public boolean isRequired() {
 
 882           public void setRequired(boolean required) {
 
 883             this.required = required;