2  *     Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
\r 
   4  *     Licensed under the Apache License, Version 2.0 (the "License");
\r 
   5  *     you may not use this file except in compliance with the License.
\r 
   6  *     You may obtain a copy of the License at
\r 
   8  *             http://www.apache.org/licenses/LICENSE-2.0
\r 
  10  *     Unless required by applicable law or agreed to in writing, software
\r 
  11  *     distributed under the License is distributed on an "AS IS" BASIS,
\r 
  12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  13  *     See the License for the specific language governing permissions and
\r 
  14  *     limitations under the License.
\r 
  16 package org.openo.orchestrator.nfv.catalog.model.parser.yaml.entity;
\r 
  18 import java.util.ArrayList;
\r 
  19 import java.util.HashMap;
\r 
  20 import java.util.Iterator;
\r 
  21 import java.util.List;
\r 
  22 import java.util.Map;
\r 
  23 import java.util.Map.Entry;
\r 
  25 import com.google.gson.Gson;
\r 
  26 import com.google.gson.JsonArray;
\r 
  27 import com.google.gson.JsonElement;
\r 
  28 import com.google.gson.JsonObject;
\r 
  30 public class ParseYamlResult {
\r 
  31     private String toscaDefinitionsVersion;
\r 
  32     private String description;
\r 
  33     private JsonObject nodeTypes;
\r 
  34     private JsonObject capabilityTypes;
\r 
  35     private JsonObject relationshipTypes;
\r 
  36     private JsonObject policyTypes;
\r 
  37     private TopologyTemplate topologyTemplate;
\r 
  38     private Map<String, String> metadata;
\r 
  39     private JsonObject plans;
\r 
  42     public String getToscaDefinitionsVersion() {
\r 
  43         return toscaDefinitionsVersion;
\r 
  46     public void setToscaDefinitionsVersion(String toscaDefinitionsVersion) {
\r 
  47         this.toscaDefinitionsVersion = toscaDefinitionsVersion;
\r 
  50     public String getDescription() {
\r 
  54     public void setDescription(String description) {
\r 
  55         this.description = description;
\r 
  58     public JsonObject getNodeTypes() {
\r 
  62     public void setNodeTypes(JsonObject nodeTypes) {
\r 
  63         this.nodeTypes = nodeTypes;
\r 
  69     public List<NodeType> getNodeTypeList() {
\r 
  70         return jsonObject2NodeTypes(nodeTypes);
\r 
  73     private ArrayList<NodeType> jsonObject2NodeTypes(JsonObject nodeTypes) {
\r 
  74         ArrayList<NodeType> nodeTypeList = new ArrayList<NodeType>();
\r 
  75         Iterator<Entry<String, JsonElement>> iterator = nodeTypes.entrySet().iterator();
\r 
  76         while (iterator.hasNext()) {
\r 
  77             NodeType type = new NodeType();
\r 
  78             Entry<String, JsonElement> next = iterator.next();
\r 
  79             type.setType(next.getKey());
\r 
  80             type.setValue(new Gson().fromJson(next.getValue(),
\r 
  81                 NodeType.NodeTypeValue.class));
\r 
  82             nodeTypeList.add(type);
\r 
  84         return nodeTypeList;
\r 
  87     public JsonObject getCapabilityTypes() {
\r 
  88         return capabilityTypes;
\r 
  91     public void setCapabilityTypes(JsonObject capabilityTypes) {
\r 
  92         this.capabilityTypes = capabilityTypes;
\r 
  95     public JsonObject getRelationshipTypes() {
\r 
  96         return relationshipTypes;
\r 
  99     public void setRelationshipTypes(JsonObject relationshipTypes) {
\r 
 100         this.relationshipTypes = relationshipTypes;
\r 
 103     public List<RelationshipType> getRelationshipTypeList() {
\r 
 104         return jsonObject2RelationshipTypes(relationshipTypes);
\r 
 108      * @param relationshipTypes
\r 
 111     private ArrayList<RelationshipType> jsonObject2RelationshipTypes(JsonObject relationshipTypes) {
\r 
 112         ArrayList<RelationshipType> relationshipTypeList = new ArrayList<RelationshipType>();
\r 
 113         Iterator<Entry<String, JsonElement>> iterator = relationshipTypes.entrySet().iterator();
\r 
 114         while (iterator.hasNext()) {
\r 
 115             RelationshipType type = new RelationshipType();
\r 
 116             Entry<String, JsonElement> next = iterator.next();
\r 
 117             type.setType(next.getKey());
\r 
 118             type.setValue(new Gson().fromJson(next.getValue(),
\r 
 119                     RelationshipType.RelationshipValue.class));
\r 
 120             relationshipTypeList.add(type);
\r 
 122         return relationshipTypeList;
\r 
 125     public JsonObject getPolicyTypes() {
\r 
 126         return policyTypes;
\r 
 129     public void setPolicyTypes(JsonObject policyTypes) {
\r 
 130         this.policyTypes = policyTypes;
\r 
 133     public TopologyTemplate getTopologyTemplate() {
\r 
 134         return topologyTemplate;
\r 
 137     public void setTopologyTemplate(TopologyTemplate topologyTemplate) {
\r 
 138         this.topologyTemplate = topologyTemplate;
\r 
 141     public Map<String, String> getMetadata() {
\r 
 145     public void setMetadata(Map<String, String> metadata) {
\r 
 146         this.metadata = metadata;
\r 
 149     public JsonObject getPlans() {
\r 
 153     public void setPlans(JsonObject plans) {
\r 
 154         this.plans = plans;
\r 
 157     public List<Plan> getPlanList() {
\r 
 158         return jsonObject2PlanList(this.plans);
\r 
 165     private List<Plan> jsonObject2PlanList(JsonObject plans) {
\r 
 166         List<Plan> retList = new ArrayList<Plan>();
\r 
 167         Iterator<Entry<String, JsonElement>> iterator = plans.entrySet()
\r 
 169         while (iterator.hasNext()) {
\r 
 170             Plan ret = new Plan();
\r 
 171             Entry<String, JsonElement> next = iterator.next();
\r 
 172             ret.setName(next.getKey());
\r 
 173             ret.setValue(new Gson().fromJson(next.getValue(),
\r 
 174                     Plan.PlanValue.class));
\r 
 180     public class TopologyTemplate{
\r 
 181         private String description;
\r 
 182         private List<Input> inputs;
\r 
 183         private List<NodeTemplate> nodeTemplates;
\r 
 184         private SubstitutionMapping substitutionMappings;
\r 
 186         public String getDescription() {
\r 
 187             return description;
\r 
 189         public void setDescription(String description) {
\r 
 190             this.description = description;
\r 
 192         public List<Input> getInputs() {
\r 
 195         public void setInputs(List<Input> inputs) {
\r 
 196             this.inputs = inputs;
\r 
 198         public List<NodeTemplate> getNodeTemplates() {
\r 
 199             return nodeTemplates;
\r 
 201         public void setNodeTemplates(List<NodeTemplate> nodeTemplates) {
\r 
 202             this.nodeTemplates = nodeTemplates;
\r 
 204         public SubstitutionMapping getSubstitutionMappings() {
\r 
 205             return substitutionMappings;
\r 
 207         public void setSubstitutionMappings(SubstitutionMapping substitutionMappings) {
\r 
 208             this.substitutionMappings = substitutionMappings;
\r 
 211         public class Input{
\r 
 212             private String name;
\r 
 213             private String type;
\r 
 214             private String description;
\r 
 215             private String defaultValue;
\r 
 216             private boolean required;
\r 
 218             public String getName() {
\r 
 221             public void setName(String name) {
\r 
 224             public String getType() {
\r 
 227             public void setType(String type) {
\r 
 230             public String getDescription() {
\r 
 231                 return description;
\r 
 233             public void setDescription(String description) {
\r 
 234                 this.description = description;
\r 
 236             public String getDefault() {
\r 
 237                 return defaultValue;
\r 
 239             public void setDefault(String defaultValue) {
\r 
 240                 this.defaultValue = defaultValue;
\r 
 242             public boolean isRequired() {
\r 
 245             public void setRequired(boolean required) {
\r 
 246                 this.required = required;
\r 
 250         public class NodeTemplate{
\r 
 251             private String name;
\r 
 252             private String nodeType;
\r 
 253             private JsonObject properties;
\r 
 254             private JsonObject[] requirements;
\r 
 255             private JsonObject capabilities;
\r 
 256             private List<Relationship> relationships;
\r 
 258             public String getName() {
\r 
 262             public void setName(String name) {
\r 
 266             public String getNodeType() {
\r 
 270             public void setNodeType(String nodeType) {
\r 
 271                 this.nodeType = nodeType;
\r 
 274             public JsonObject getProperties() {
\r 
 278             public void setProperties(JsonObject properties) {
\r 
 279                 this.properties = properties;
\r 
 282             public Map<String, Object> getPropertyList() {
\r 
 283                 return jsonObject2Properties(properties);
\r 
 286             private Map<String, Object> jsonObject2Properties(
\r 
 287                     JsonObject properties) {
\r 
 288                 Map<String, Object> ret = new HashMap<>();
\r 
 289                 Iterator<Entry<String, JsonElement>> iterator = properties
\r 
 290                         .entrySet().iterator();
\r 
 291                 while (iterator.hasNext()) {
\r 
 292                     Entry<String, JsonElement> next = iterator.next();
\r 
 293                     ret.put(next.getKey(), next.getValue().getAsString());
\r 
 298             public JsonObject[] getRequirements() {
\r 
 299                 return requirements;
\r 
 302             public void setRequirements(JsonObject[] requirements) {
\r 
 303                 this.requirements = requirements;
\r 
 306             public JsonObject getCapabilities() {
\r 
 307                 return capabilities;
\r 
 310             public void setCapabilities(JsonObject capabilities) {
\r 
 311                 this.capabilities = capabilities;
\r 
 314             public List<Relationship> getRelationships() {
\r 
 315                 return relationships;
\r 
 318             public void setRelationships(List<Relationship> relationships) {
\r 
 319                 this.relationships = relationships;
\r 
 322             public NodeTemplateScalable getScalable() {
\r 
 323                 if(capabilities == null){
\r 
 326                 JsonElement scaleableJson = capabilities.get("scalable");
\r 
 327                 if (scaleableJson == null || !scaleableJson.isJsonObject()) {
\r 
 330                 JsonElement propertyJson = scaleableJson.getAsJsonObject().get("properties");
\r 
 331                 if (propertyJson == null || !propertyJson.isJsonObject()) {
\r 
 335                 NodeTemplateScalable scalable = new NodeTemplateScalable();
\r 
 336                 scalable.setMin_instances(propertyJson.getAsJsonObject().get("min_instances")
\r 
 338                 scalable.setMax_instances(propertyJson.getAsJsonObject().get("max_instances")
\r 
 340                 scalable.setDefault_instances(propertyJson.getAsJsonObject()
\r 
 341                         .get("default_instances").getAsString());
\r 
 345             public class Relationship{
\r 
 346                 private String targetNodeName;
\r 
 347                 private String type;
\r 
 348                 private String sourceNodeName;
\r 
 350                 public String getTargetNodeName() {
\r 
 351                     return targetNodeName;
\r 
 353                 public void setTargetNodeName(String targetNodeName) {
\r 
 354                     this.targetNodeName = targetNodeName;
\r 
 356                 public String getType() {
\r 
 359                 public void setType(String type) {
\r 
 362                 public String getSourceNodeName() {
\r 
 363                     return sourceNodeName;
\r 
 365                 public void setSourceNodeName(String sourceNodeName) {
\r 
 366                     this.sourceNodeName = sourceNodeName;
\r 
 370             public class NodeTemplateScalable{
\r 
 371                 private String min_instances;
\r 
 372                 private String max_instances;
\r 
 373                 private String default_instances;
\r 
 374                 public String getMin_instances() {
\r 
 375                     return min_instances;
\r 
 377                 public void setMin_instances(String min_instances) {
\r 
 378                     this.min_instances = min_instances;
\r 
 380                 public String getMax_instances() {
\r 
 381                     return max_instances;
\r 
 383                 public void setMax_instances(String max_instances) {
\r 
 384                     this.max_instances = max_instances;
\r 
 386                 public String getDefault_instances() {
\r 
 387                     return default_instances;
\r 
 389                 public void setDefault_instances(String default_instances) {
\r 
 390                     this.default_instances = default_instances;
\r 
 395         public class SubstitutionMapping{
\r 
 396             private String node_type;
\r 
 397             private JsonObject[] requirements;
\r 
 398             private JsonObject capabilities;
\r 
 399             private JsonObject properties;
\r 
 401             public String getNode_type() {
\r 
 405             public void setNode_type(String node_type) {
\r 
 406                 this.node_type = node_type;
\r 
 409             public JsonObject[] getRequirements() {
\r 
 410                 return requirements;
\r 
 413             public void setRequirements(JsonObject[] requirements) {
\r 
 414                 this.requirements = requirements;
\r 
 417             public List<Map<String, String[]>> getRequirementList() {
\r 
 418                 return jsonObjects2Requirements(this.requirements);
\r 
 421             private List<Map<String, String[]>> jsonObjects2Requirements(
\r 
 422                     JsonObject[] requirements) {
\r 
 423                 List<Map<String, String[]>> retList = new ArrayList<>();
\r 
 424                 for (JsonObject requirement : requirements) {
\r 
 425                     Iterator<Entry<String, JsonElement>> iterator = requirement
\r 
 426                             .entrySet().iterator();
\r 
 427                     while (iterator.hasNext()) {
\r 
 428                         Entry<String, JsonElement> next = iterator.next();
\r 
 429                         Map<String, String[]> ret = new HashMap<String, String[]>();
\r 
 430                         if (next.getValue().isJsonPrimitive()
\r 
 431                                 || next.getValue().isJsonObject()) {
\r 
 432                             ret.put(next.getKey(), new String[] { next
\r 
 433                                     .getValue().getAsString() });
\r 
 438                         if (next.getValue().isJsonArray()) {
\r 
 439                             String[] value = parseListValue((JsonArray) next.getValue());
\r 
 440                             ret.put(next.getKey(), value);
\r 
 449             private String[] parseListValue(JsonArray jsonArray) {
\r 
 450                 String[] value = new String[jsonArray.size()];
\r 
 451                 for (int i = 0, size = jsonArray.size(); i < size; i++) {
\r 
 452                     value[i] = jsonArray.get(i).getAsString();
\r 
 457             public JsonObject getCapabilities() {
\r 
 458                 return capabilities;
\r 
 461             public void setCapabilities(JsonObject capabilities) {
\r 
 462                 this.capabilities = capabilities;
\r 
 465             public Map<String, String[]> getCapabilityList() {
\r 
 466                 return jsonObject2Capabilities(this.capabilities);
\r 
 469             private Map<String, String[]> jsonObject2Capabilities(
\r 
 470                     JsonObject capabilities) {
\r 
 471                 Map<String, String[]> ret = new HashMap<String, String[]>();
\r 
 473                 Iterator<Entry<String, JsonElement>> iterator = capabilities
\r 
 474                         .entrySet().iterator();
\r 
 475                 while (iterator.hasNext()) {
\r 
 476                     Entry<String, JsonElement> next = iterator.next();
\r 
 478                     if (next.getValue().isJsonPrimitive()
\r 
 479                             || next.getValue().isJsonObject()) {
\r 
 480                         ret.put(next.getKey(), new String[] { next.getValue()
\r 
 485                     if (next.getValue().isJsonArray()) {
\r 
 486                         String[] value = parseListValue((JsonArray) next
\r 
 488                         ret.put(next.getKey(), value);
\r 
 495             public JsonObject getProperties() {
\r 
 499             public void setProperties(JsonObject properties) {
\r 
 500                 this.properties = properties;
\r 
 503             public Map<String, Object> getPropertyList() {
\r 
 504                 return jsonObject2Properties(properties);
\r 
 507             private Map<String, Object> jsonObject2Properties(
\r 
 508                     JsonObject properties) {
\r 
 509                 Map<String, Object> ret = new HashMap<>();
\r 
 510                 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
\r 
 511                 while (iterator.hasNext()) {
\r 
 512                     Entry<String, JsonElement> next = iterator.next();
\r 
 513                     ret.put(next.getKey(), next.getValue().getAsString());
\r 
 521     public class RelationshipType{
\r 
 522         private String type;
\r 
 523         private RelationshipValue value;
\r 
 525         public String getType() {
\r 
 529         public void setType(String type) {
\r 
 533         public RelationshipValue getValue() {
\r 
 537         public void setValue(RelationshipValue value) {
\r 
 538             this.value = value;
\r 
 541         public class RelationshipValue{
\r 
 542             private String derived_from;
\r 
 543             private String[] valid_target_types;
\r 
 545             public String getDerived_from() {
\r 
 546                 return derived_from;
\r 
 549             public void setDerived_from(String derived_from) {
\r 
 550                 this.derived_from = derived_from;
\r 
 552             public String[] getValid_target_types() {
\r 
 553                 return valid_target_types;
\r 
 555             public void setValid_target_types(String[] valid_target_types) {
\r 
 556                 this.valid_target_types = valid_target_types;
\r 
 562     public class NodeType {
\r 
 563         private String type;
\r 
 564         private NodeTypeValue value;
\r 
 566         public String getType() {
\r 
 570         public void setType(String type) {
\r 
 574         public NodeTypeValue getValue() {
\r 
 578         public void setValue(NodeTypeValue value) {
\r 
 579             this.value = value;
\r 
 582         public class NodeTypeValue{
\r 
 583             private String derived_from;
\r 
 584             private JsonObject properties;
\r 
 585             private JsonObject[] requirements;
\r 
 586             private JsonObject capabilities;
\r 
 588             public String getDerived_from() {
\r 
 589                 return derived_from;
\r 
 592             public void setDerived_from(String derived_from) {
\r 
 593                 this.derived_from = derived_from;
\r 
 596             public JsonObject getProperties() {
\r 
 600             public void setProperties(JsonObject properties) {
\r 
 601                 this.properties = properties;
\r 
 604             public List<NodeTypeProperty> getPropertyList() {
\r 
 605                 return jsonObject2Properties(properties);
\r 
 608             private List<NodeTypeProperty> jsonObject2Properties(JsonObject properties) {
\r 
 609                 List<NodeTypeProperty> propertieList = new ArrayList<NodeTypeProperty>();
\r 
 610                 Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
\r 
 611                 while (iterator.hasNext()) {
\r 
 612                     NodeTypeProperty type = new NodeTypeProperty();
\r 
 613                     Entry<String, JsonElement> next = iterator.next();
\r 
 614                     type.setKey(next.getKey());
\r 
 615                     type.setValue(new Gson().fromJson(next.getValue(),
\r 
 616                         JsonObject.class));
\r 
 617                     propertieList.add(type);
\r 
 619                 return propertieList;
\r 
 625             public class NodeTypeProperty {
\r 
 626                 private String key;
\r 
 627                 private JsonObject value;
\r 
 629                 public String getKey() {
\r 
 633                 public void setKey(String key) {
\r 
 637                 public String getDefaultValue() {
\r 
 638                     JsonElement defaultValue = value.get("default");
\r 
 639                     if (defaultValue == null || defaultValue.isJsonObject()) {
\r 
 643                     return defaultValue.getAsString();
\r 
 646                 public JsonObject getValue() {
\r 
 650                 public void setValue(JsonObject value) {
\r 
 651                     this.value = value;
\r 
 655             public JsonObject[] getRequirements() {
\r 
 656                 return requirements;
\r 
 659             public void setRequirements(JsonObject[] requirements) {
\r 
 660                 this.requirements = requirements;
\r 
 663             public JsonObject getCapabilities() {
\r 
 664                 return capabilities;
\r 
 667             public void setCapabilities(JsonObject capabilities) {
\r 
 668                 this.capabilities = capabilities;
\r 
 673     public class Plan {
\r 
 674         private String name;
\r 
 675         private PlanValue value;
\r 
 677         public String getName() {
\r 
 680         public void setName(String name) {
\r 
 684         public String getDescription() {
\r 
 685             return value.getDescription();
\r 
 687         public String getReference() {
\r 
 688             return value.getReference();
\r 
 690         public String getPlanLanguage() {
\r 
 691             return value.getPlanLanguage();
\r 
 694         public List<PlanValue.PlanInput> getInputList() {
\r 
 695             return value.getInputList();
\r 
 698         public PlanValue getValue() {
\r 
 701         public void setValue(PlanValue value) {
\r 
 702             this.value = value;
\r 
 705         public class PlanValue {
\r 
 706             private String description;
\r 
 707             private String reference;
\r 
 708             private String planLanguage;
\r 
 709             private JsonObject inputs;
\r 
 711             public String getDescription() {
\r 
 712                 return description;
\r 
 715             public void setDescription(String description) {
\r 
 716                 this.description = description;
\r 
 719             public String getReference() {
\r 
 723             public void setReference(String reference) {
\r 
 724                 this.reference = reference;
\r 
 727             public String getPlanLanguage() {
\r 
 728                 return planLanguage;
\r 
 731             public void setPlanLanguage(String planLanguage) {
\r 
 732                 this.planLanguage = planLanguage;
\r 
 735             public JsonObject getInputs() {
\r 
 739             public void setInputs(JsonObject inputs) {
\r 
 740                 this.inputs = inputs;
\r 
 743             public List<PlanInput> getInputList() {
\r 
 744                 return jsonObject2PlanInputList(inputs);
\r 
 752             private List<PlanInput> jsonObject2PlanInputList(JsonObject inputs) {
\r 
 753                 List<PlanInput> retList = new ArrayList<PlanInput>();
\r 
 754                 Iterator<Entry<String, JsonElement>> iterator = inputs
\r 
 755                         .entrySet().iterator();
\r 
 756                 while (iterator.hasNext()) {
\r 
 757                     PlanInput ret = new PlanInput();
\r 
 758                     Entry<String, JsonElement> next = iterator.next();
\r 
 759                     ret.setName(next.getKey());
\r 
 760                     ret.setValue(new Gson().fromJson(next.getValue(),
\r 
 761                             PlanInput.PlanInputValue.class));
\r 
 767             public class PlanInput {
\r 
 768                 private String name;
\r 
 769                 private PlanInputValue value;
\r 
 771                 public String getName() {
\r 
 774                 public void setName(String name) {
\r 
 778                 public String getType() {
\r 
 779                     return value.getType();
\r 
 782                 public String getDescription() {
\r 
 783                     return value.getDescription();
\r 
 786                 public String getDefault() {
\r 
 787                     return value.getDefault();
\r 
 790                 public boolean isRequired() {
\r 
 791                     return value.isRequired();
\r 
 794                 public PlanInputValue getValue() {
\r 
 798                 public void setValue(PlanInputValue value) {
\r 
 799                     this.value = value;
\r 
 802                 public class PlanInputValue {
\r 
 803                     private String type;
\r 
 804                     private String description;
\r 
 805                     private String defaultValue;
\r 
 806                     private boolean required;
\r 
 808                     public String getType() {
\r 
 812                     public void setType(String type) {
\r 
 816                     public String getDescription() {
\r 
 817                         return description;
\r 
 820                     public void setDescription(String description) {
\r 
 821                         this.description = description;
\r 
 824                     public String getDefault() {
\r 
 825                         return defaultValue;
\r 
 828                     public void setDefault(String defaultValue) {
\r 
 829                         this.defaultValue = defaultValue;
\r 
 832                     public boolean isRequired() {
\r 
 836                     public void setRequired(boolean required) {
\r 
 837                         this.required = required;
\r