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