5bdab1c28414e2fd74d37857e26bc56e54c6ee9b
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.commontosca.catalog.model.parser.yaml.zte.entity;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Map.Entry;
24
25 import lombok.AllArgsConstructor;
26 import lombok.Data;
27 import lombok.NoArgsConstructor;
28
29 import org.openo.commontosca.catalog.common.ToolUtil;
30
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;
36
37 @Data
38 @NoArgsConstructor
39 @AllArgsConstructor
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;
50   
51   private String rawData;
52
53   public String getToscaDefinitionsVersion() {
54     return toscaDefinitionsVersion;
55   }
56
57   public void setToscaDefinitionsVersion(String toscaDefinitionsVersion) {
58     this.toscaDefinitionsVersion = toscaDefinitionsVersion;
59   }
60
61   public String getDescription() {
62     return description;
63   }
64
65   public void setDescription(String description) {
66     this.description = description;
67   }
68
69   public JsonObject getNodeTypes() {
70     return nodeTypes;
71   }
72
73   public void setNodeTypes(JsonObject nodeTypes) {
74     this.nodeTypes = nodeTypes;
75   }
76
77   public List<NodeType> getNodeTypeList() {
78     return jsonObject2NodeTypes(nodeTypes);
79   }
80
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);
90     }
91     return nodeTypeList;
92   }
93
94   public JsonObject getCapabilityTypes() {
95     return capabilityTypes;
96   }
97
98   public void setCapabilityTypes(JsonObject capabilityTypes) {
99     this.capabilityTypes = capabilityTypes;
100   }
101
102   public JsonObject getRelationshipTypes() {
103     return relationshipTypes;
104   }
105
106   public void setRelationshipTypes(JsonObject relationshipTypes) {
107     this.relationshipTypes = relationshipTypes;
108   }
109
110   public List<RelationshipType> getRelationshipTypeList() {
111     return jsonObject2RelationshipTypes(relationshipTypes);
112   }
113
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);
123     }
124     return relationshipTypeList;
125   }
126
127   public JsonObject getPolicyTypes() {
128     return policyTypes;
129   }
130
131   public void setPolicyTypes(JsonObject policyTypes) {
132     this.policyTypes = policyTypes;
133   }
134
135   public TopologyTemplate getTopologyTemplate() {
136     return topologyTemplate;
137   }
138
139   public void setTopologyTemplate(TopologyTemplate topologyTemplate) {
140     this.topologyTemplate = topologyTemplate;
141   }
142
143   public Map<String, String> getMetadata() {
144     if (this.metadata == null) {
145       return new HashMap<>();
146     }
147     return metadata;
148   }
149
150   public void setMetadata(Map<String, String> metadata) {
151     this.metadata = metadata;
152   }
153
154   public JsonObject getPlans() {
155     return plans;
156   }
157
158   public void setPlans(JsonObject plans) {
159     this.plans = plans;
160   }
161
162   public List<Plan> getPlanList() {
163     return jsonObject2PlanList(this.plans);
164   }
165
166   private List<Plan> jsonObject2PlanList(JsonObject plans) {
167     if (plans == null) {
168       return new ArrayList<>();
169     }
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));
177       retList.add(ret);
178     }
179     return retList;
180   }
181
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;
188
189     public String getDescription() {
190       return description;
191     }
192
193     public void setDescription(String description) {
194       this.description = description;
195     }
196
197     public List<Input> getInputs() {
198       return inputs;
199     }
200
201     public void setInputs(List<Input> inputs) {
202       this.inputs = inputs;
203     }
204
205     public List<Output> getOutputs() {
206       return outputs;
207     }
208
209     public void setOutputs(List<Output> outputs) {
210       this.outputs = outputs;
211     }
212
213     public List<NodeTemplate> getNodeTemplates() {
214       return nodeTemplates;
215     }
216
217     public void setNodeTemplates(List<NodeTemplate> nodeTemplates) {
218       this.nodeTemplates = nodeTemplates;
219     }
220
221     public SubstitutionMapping getSubstitutionMappings() {
222       return substitutionMappings;
223     }
224
225     public void setSubstitutionMappings(SubstitutionMapping substitutionMappings) {
226       this.substitutionMappings = substitutionMappings;
227     }
228
229     public class Input {
230       private String name;
231       private String type;
232       private String description;
233       private String defaultValue;
234       private boolean required;
235
236       public String getName() {
237         return name;
238       }
239
240       public void setName(String name) {
241         this.name = name;
242       }
243
244       public String getType() {
245         return type;
246       }
247
248       public void setType(String type) {
249         this.type = type;
250       }
251
252       public String getDescription() {
253         return description;
254       }
255
256       public void setDescription(String description) {
257         this.description = description;
258       }
259
260       public String getDefault() {
261         return defaultValue;
262       }
263
264       public void setDefault(String defaultValue) {
265         this.defaultValue = defaultValue;
266       }
267
268       public boolean isRequired() {
269         return required;
270       }
271
272       public void setRequired(boolean required) {
273         this.required = required;
274       }
275     }
276
277     public class Output {
278       private String name;
279       private String description;
280       private Object value;
281
282       public String getName() {
283         return name;
284       }
285
286       public void setName(String name) {
287         this.name = name;
288       }
289
290       public String getDescription() {
291         return description;
292       }
293
294       public void setDescription(String description) {
295         this.description = description;
296       }
297
298       public Object getValue() {
299         return value;
300       }
301
302       public void setValue(Object value) {
303         this.value = value;
304       }
305     }
306
307     public class NodeTemplate {
308       private String name;
309       private String nodeType;
310       private JsonObject properties;
311       private JsonObject[] requirements;
312       private JsonObject capabilities;
313       private List<Relationship> relationships;
314
315       public String getName() {
316         return name;
317       }
318
319       public void setName(String name) {
320         this.name = name;
321       }
322
323       public String getNodeType() {
324         return nodeType;
325       }
326
327       public void setNodeType(String nodeType) {
328         this.nodeType = nodeType;
329       }
330
331       public JsonObject getProperties() {
332         return properties;
333       }
334
335       public void setProperties(JsonObject properties) {
336         this.properties = properties;
337       }
338
339       public Map<String, Object> getPropertyList() {
340         return jsonObject2Properties(properties);
341       }
342
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()));
349         }
350         return ret;
351       }
352
353       public JsonObject[] getRequirements() {
354         return requirements;
355       }
356
357       public void setRequirements(JsonObject[] requirements) {
358         this.requirements = requirements;
359       }
360
361       public JsonObject getCapabilities() {
362         return capabilities;
363       }
364
365       public void setCapabilities(JsonObject capabilities) {
366         this.capabilities = capabilities;
367       }
368
369       public List<Relationship> getRelationships() {
370         return relationships;
371       }
372
373       public void setRelationships(List<Relationship> relationships) {
374         this.relationships = relationships;
375       }
376
377       /**
378        * get scalable.
379        * @return NodeTemplateScalable
380        */
381       public NodeTemplateScalable getScalable() {
382         if (capabilities == null) {
383           return null;
384         }
385         JsonElement scaleableJson = capabilities.get("scalable");
386         if (scaleableJson == null || !scaleableJson.isJsonObject()) {
387           return null;
388         }
389         JsonElement propertyJson = scaleableJson.getAsJsonObject().get("properties");
390         if (propertyJson == null || !propertyJson.isJsonObject()) {
391           return null;
392         }
393
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")));
401         return scalable;
402       }
403
404       public class Relationship {
405         private String targetNodeName;
406         private String type;
407         private String sourceNodeName;
408
409         public String getTargetNodeName() {
410           return targetNodeName;
411         }
412
413         public void setTargetNodeName(String targetNodeName) {
414           this.targetNodeName = targetNodeName;
415         }
416
417         public String getType() {
418           return type;
419         }
420
421         public void setType(String type) {
422           this.type = type;
423         }
424
425         public String getSourceNodeName() {
426           return sourceNodeName;
427         }
428
429         public void setSourceNodeName(String sourceNodeName) {
430           this.sourceNodeName = sourceNodeName;
431         }
432       }
433
434       public class NodeTemplateScalable {
435         private String minInstances;
436         private String maxInstances;
437         private String defaultInstances;
438
439         public String getMin_instances() {
440           return minInstances;
441         }
442
443         public void setMin_instances(String minInstances) {
444           this.minInstances = minInstances;
445         }
446
447         public String getMax_instances() {
448           return maxInstances;
449         }
450
451         public void setMax_instances(String maxInstances) {
452           this.maxInstances = maxInstances;
453         }
454
455         public String getDefault_instances() {
456           return defaultInstances;
457         }
458
459         public void setDefault_instances(String defaultInstances) {
460           this.defaultInstances = defaultInstances;
461         }
462       }
463     }
464
465     public class SubstitutionMapping {
466       private String nodeType;
467       private JsonObject requirements;
468       private JsonObject capabilities;
469       private JsonObject properties;
470
471       public String getNodeType() {
472         return nodeType;
473       }
474
475       public void setNodeType(String nodeType) {
476         this.nodeType = nodeType;
477       }
478
479       public JsonObject getRequirements() {
480         return requirements;
481       }
482
483       public void setRequirements(JsonObject requirements) {
484         this.requirements = requirements;
485       }
486
487       public Map<String, String[]> getRequirementList() {
488         return jsonObjects2Requirements(this.requirements);
489       }
490
491       private Map<String, String[]> jsonObjects2Requirements(JsonObject requirements) {
492         Map<String, String[]> ret = new HashMap<String, String[]>();
493
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())});
499             continue;
500           }
501
502           if (next.getValue().isJsonArray()) {
503             String[] value = parseListValue((JsonArray) next.getValue());
504             ret.put(next.getKey(), value);
505           }
506         }
507
508         return ret;
509       }
510
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));
515         }
516         return value;
517       }
518
519       public JsonObject getCapabilities() {
520         return capabilities;
521       }
522
523       public void setCapabilities(JsonObject capabilities) {
524         this.capabilities = capabilities;
525       }
526
527       public Map<String, String[]> getCapabilityList() {
528         return jsonObject2Capabilities(this.capabilities);
529       }
530
531       private Map<String, String[]> jsonObject2Capabilities(JsonObject capabilities) {
532         Map<String, String[]> ret = new HashMap<String, String[]>();
533
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())});
539             continue;
540           }
541
542           if (next.getValue().isJsonArray()) {
543             String[] value = parseListValue((JsonArray) next.getValue());
544             ret.put(next.getKey(), value);
545           }
546         }
547
548         return ret;
549       }
550
551       public JsonObject getProperties() {
552         return properties;
553       }
554
555       public void setProperties(JsonObject properties) {
556         this.properties = properties;
557       }
558
559       public Map<String, Object> getPropertyList() {
560         return jsonObject2Properties(properties);
561       }
562
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()));
569         }
570         return ret;
571       }
572     }
573   }
574
575
576   public class RelationshipType {
577     private String type;
578     private RelationshipValue value;
579
580     public String getType() {
581       return type;
582     }
583
584     public void setType(String type) {
585       this.type = type;
586     }
587
588     public RelationshipValue getValue() {
589       return value;
590     }
591
592     public void setValue(RelationshipValue value) {
593       this.value = value;
594     }
595
596     public class RelationshipValue {
597       private String derivedFrom;
598       private String[] validTargetTypes;
599
600       public String getDerivedFrom() {
601         return derivedFrom;
602       }
603
604       public void setDerivedFrom(String derivedFrom) {
605         this.derivedFrom = derivedFrom;
606       }
607
608       public String[] getValid_target_types() {
609         return validTargetTypes;
610       }
611
612       public void setValid_target_types(String[] validTargetTypes) {
613         this.validTargetTypes = validTargetTypes;
614       }
615     }
616   }
617
618
619   public class NodeType {
620     private String type;
621     private NodeTypeValue value;
622
623     public String getType() {
624       return type;
625     }
626
627     public void setType(String type) {
628       this.type = type;
629     }
630
631     public NodeTypeValue getValue() {
632       return value;
633     }
634
635     public void setValue(NodeTypeValue value) {
636       this.value = value;
637     }
638
639     public class NodeTypeValue {
640       private String derivedFrom;
641       private JsonObject properties;
642       private JsonObject[] requirements;
643       private JsonObject capabilities;
644
645       public String getDerivedFrom() {
646         return derivedFrom;
647       }
648
649       public void setDerived_from(String derivedFrom) {
650         this.derivedFrom = derivedFrom;
651       }
652
653       public JsonObject getProperties() {
654         return properties;
655       }
656
657       public void setProperties(JsonObject properties) {
658         this.properties = properties;
659       }
660
661       public List<NodeTypeProperty> getPropertyList() {
662         return jsonObject2Properties(properties);
663       }
664
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);
674         }
675         return propertieList;
676       }
677
678       public class NodeTypeProperty {
679         private String key;
680         private JsonObject value;
681
682         public String getKey() {
683           return key;
684         }
685
686         public void setKey(String key) {
687           this.key = key;
688         }
689
690         /**
691          * get default value.
692          * @return default value
693          */
694         public String getDefaultValue() {
695           JsonElement defaultValue = value.get("default");
696           if (defaultValue == null || defaultValue.isJsonObject()) {
697             return "";
698           }
699
700           return ToolUtil.getAsString(defaultValue);
701         }
702
703         public JsonObject getValue() {
704           return value;
705         }
706
707         public void setValue(JsonObject value) {
708           this.value = value;
709         }
710       }
711
712       public JsonObject[] getRequirements() {
713         return requirements;
714       }
715
716       public void setRequirements(JsonObject[] requirements) {
717         this.requirements = requirements;
718       }
719
720       public JsonObject getCapabilities() {
721         return capabilities;
722       }
723
724       public void setCapabilities(JsonObject capabilities) {
725         this.capabilities = capabilities;
726       }
727     }
728   }
729
730   public class Plan {
731     private String name;
732     private PlanValue value;
733
734     public String getName() {
735       return name;
736     }
737
738     public void setName(String name) {
739       this.name = name;
740     }
741
742     public String getDescription() {
743       return value.getDescription();
744     }
745
746     public String getReference() {
747       return value.getReference();
748     }
749
750     public String getPlanLanguage() {
751       return value.getPlanLanguage();
752     }
753
754     public List<PlanValue.PlanInput> getInputList() {
755       return value.getInputList();
756     }
757
758     public PlanValue getValue() {
759       return value;
760     }
761
762     public void setValue(PlanValue value) {
763       this.value = value;
764     }
765
766     public class PlanValue {
767       private String description;
768       private String reference;
769       private String planLanguage;
770       private JsonObject inputs;
771
772       public String getDescription() {
773         return description;
774       }
775
776       public void setDescription(String description) {
777         this.description = description;
778       }
779
780       public String getReference() {
781         return reference;
782       }
783
784       public void setReference(String reference) {
785         this.reference = reference;
786       }
787
788       public String getPlanLanguage() {
789         return planLanguage;
790       }
791
792       public void setPlanLanguage(String planLanguage) {
793         this.planLanguage = planLanguage;
794       }
795
796       public JsonObject getInputs() {
797         return inputs;
798       }
799
800       public void setInputs(JsonObject inputs) {
801         this.inputs = inputs;
802       }
803
804       public List<PlanInput> getInputList() {
805         return jsonObject2PlanInputList(inputs);
806
807       }
808
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));
817           retList.add(ret);
818         }
819         return retList;
820       }
821
822       public class PlanInput {
823         private String name;
824         private PlanInputValue value;
825
826         public String getName() {
827           return name;
828         }
829
830         public void setName(String name) {
831           this.name = name;
832         }
833
834         public String getType() {
835           return value.getType();
836         }
837
838         public String getDescription() {
839           return value.getDescription();
840         }
841
842         public String getDefault() {
843           return value.getDefaultValue();
844         }
845
846         public boolean isRequired() {
847           return value.isRequired();
848         }
849
850         public PlanInputValue getValue() {
851           return value;
852         }
853
854         public void setValue(PlanInputValue value) {
855           this.value = value;
856         }
857
858         public class PlanInputValue {
859           private String type;
860           private String description;
861           @SerializedName("default")
862           private String defaultValue;
863           private boolean required;
864
865           public String getType() {
866             return type;
867           }
868
869           public void setType(String type) {
870             this.type = type;
871           }
872
873           public String getDescription() {
874             return description;
875           }
876
877           public void setDescription(String description) {
878             this.description = description;
879           }
880
881           public String getDefaultValue() {
882             return defaultValue;
883           }
884
885           public void setDefaultValue(String defaultValue) {
886             this.defaultValue = defaultValue;
887           }
888
889           public boolean isRequired() {
890             return required;
891           }
892
893           public void setRequired(boolean required) {
894             this.required = required;
895           }
896         }
897       }
898     }
899   }
900 }