4fc2d825df437f122c2a64174b9a326ca5a22a5b
[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 node_type;
467       private JsonObject requirements;
468       private JsonObject capabilities;
469       
470       public String getNode_type() {
471         return node_type;
472       }
473
474       public void setNode_type(String node_type) {
475         this.node_type = node_type;
476       }
477
478       public JsonObject getRequirements() {
479         return requirements;
480       }
481
482       public void setRequirements(JsonObject requirements) {
483         this.requirements = requirements;
484       }
485
486       public Map<String, String[]> getRequirementList() {
487         return jsonObjects2Requirements(this.requirements);
488       }
489
490       private Map<String, String[]> jsonObjects2Requirements(JsonObject requirements) {
491         if (requirements == null) {
492           return new HashMap<String, String[]>();
493         }
494         
495         Map<String, String[]> ret = new HashMap<String, String[]>();
496         Iterator<Entry<String, JsonElement>> iterator = requirements.entrySet().iterator();
497         while (iterator.hasNext()) {
498           Entry<String, JsonElement> next = iterator.next();
499           if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
500             ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())});
501             continue;
502           }
503
504           if (next.getValue().isJsonArray()) {
505             String[] value = parseListValue((JsonArray) next.getValue());
506             ret.put(next.getKey(), value);
507           }
508         }
509
510         return ret;
511       }
512
513       private String[] parseListValue(JsonArray jsonArray) {
514         String[] value = new String[jsonArray.size()];
515         for (int i = 0, size = jsonArray.size(); i < size; i++) {
516           value[i] = ToolUtil.getAsString(jsonArray.get(i));
517         }
518         return value;
519       }
520
521       public JsonObject getCapabilities() {
522         return capabilities;
523       }
524
525       public void setCapabilities(JsonObject capabilities) {
526         this.capabilities = capabilities;
527       }
528
529       public Map<String, String[]> getCapabilityList() {
530         return jsonObject2Capabilities(this.capabilities);
531       }
532
533       private Map<String, String[]> jsonObject2Capabilities(JsonObject capabilities) {
534         if (capabilities == null) {
535           return new HashMap<String, String[]>();
536         }
537         
538         Map<String, String[]> ret = new HashMap<String, String[]>();
539         Iterator<Entry<String, JsonElement>> iterator = capabilities.entrySet().iterator();
540         while (iterator.hasNext()) {
541           Entry<String, JsonElement> next = iterator.next();
542           if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) {
543             ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())});
544             continue;
545           }
546
547           if (next.getValue().isJsonArray()) {
548             String[] value = parseListValue((JsonArray) next.getValue());
549             ret.put(next.getKey(), value);
550           }
551         }
552
553         return ret;
554       }
555     }
556   }
557
558
559   public class RelationshipType {
560     private String type;
561     private RelationshipValue value;
562
563     public String getType() {
564       return type;
565     }
566
567     public void setType(String type) {
568       this.type = type;
569     }
570
571     public RelationshipValue getValue() {
572       return value;
573     }
574
575     public void setValue(RelationshipValue value) {
576       this.value = value;
577     }
578
579     public class RelationshipValue {
580       private String derivedFrom;
581       private String[] validTargetTypes;
582
583       public String getDerivedFrom() {
584         return derivedFrom;
585       }
586
587       public void setDerivedFrom(String derivedFrom) {
588         this.derivedFrom = derivedFrom;
589       }
590
591       public String[] getValid_target_types() {
592         return validTargetTypes;
593       }
594
595       public void setValid_target_types(String[] validTargetTypes) {
596         this.validTargetTypes = validTargetTypes;
597       }
598     }
599   }
600
601
602   public class NodeType {
603     private String type;
604     private NodeTypeValue value;
605
606     public String getType() {
607       return type;
608     }
609
610     public void setType(String type) {
611       this.type = type;
612     }
613
614     public NodeTypeValue getValue() {
615       return value;
616     }
617
618     public void setValue(NodeTypeValue value) {
619       this.value = value;
620     }
621
622     public class NodeTypeValue {
623       private String derivedFrom;
624       private JsonObject properties;
625       private JsonObject[] requirements;
626       private JsonObject capabilities;
627
628       public String getDerivedFrom() {
629         return derivedFrom;
630       }
631
632       public void setDerived_from(String derivedFrom) {
633         this.derivedFrom = derivedFrom;
634       }
635
636       public JsonObject getProperties() {
637         return properties;
638       }
639
640       public void setProperties(JsonObject properties) {
641         this.properties = properties;
642       }
643
644       public List<NodeTypeProperty> getPropertyList() {
645         return jsonObject2Properties(properties);
646       }
647
648       private List<NodeTypeProperty> jsonObject2Properties(JsonObject properties) {
649         List<NodeTypeProperty> propertieList = new ArrayList<NodeTypeProperty>();
650         Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator();
651         while (iterator.hasNext()) {
652           NodeTypeProperty type = new NodeTypeProperty();
653           Entry<String, JsonElement> next = iterator.next();
654           type.setKey(next.getKey());
655           type.setValue(new Gson().fromJson(next.getValue(), JsonObject.class));
656           propertieList.add(type);
657         }
658         return propertieList;
659       }
660
661       public class NodeTypeProperty {
662         private String key;
663         private JsonObject value;
664
665         public String getKey() {
666           return key;
667         }
668
669         public void setKey(String key) {
670           this.key = key;
671         }
672
673         /**
674          * get default value.
675          * @return default value
676          */
677         public String getDefaultValue() {
678           JsonElement defaultValue = value.get("default");
679           if (defaultValue == null || defaultValue.isJsonObject()) {
680             return "";
681           }
682
683           return ToolUtil.getAsString(defaultValue);
684         }
685
686         public JsonObject getValue() {
687           return value;
688         }
689
690         public void setValue(JsonObject value) {
691           this.value = value;
692         }
693       }
694
695       public JsonObject[] getRequirements() {
696         return requirements;
697       }
698
699       public void setRequirements(JsonObject[] requirements) {
700         this.requirements = requirements;
701       }
702
703       public JsonObject getCapabilities() {
704         return capabilities;
705       }
706
707       public void setCapabilities(JsonObject capabilities) {
708         this.capabilities = capabilities;
709       }
710     }
711   }
712
713   public class Plan {
714     private String name;
715     private PlanValue value;
716
717     public String getName() {
718       return name;
719     }
720
721     public void setName(String name) {
722       this.name = name;
723     }
724
725     public String getDescription() {
726       return value.getDescription();
727     }
728
729     public String getReference() {
730       return value.getReference();
731     }
732
733     public String getPlanLanguage() {
734       return value.getPlanLanguage();
735     }
736
737     public List<PlanValue.PlanInput> getInputList() {
738       return value.getInputList();
739     }
740
741     public PlanValue getValue() {
742       return value;
743     }
744
745     public void setValue(PlanValue value) {
746       this.value = value;
747     }
748
749     public class PlanValue {
750       private String description;
751       private String reference;
752       private String planLanguage;
753       private JsonObject inputs;
754
755       public String getDescription() {
756         return description;
757       }
758
759       public void setDescription(String description) {
760         this.description = description;
761       }
762
763       public String getReference() {
764         return reference;
765       }
766
767       public void setReference(String reference) {
768         this.reference = reference;
769       }
770
771       public String getPlanLanguage() {
772         return planLanguage;
773       }
774
775       public void setPlanLanguage(String planLanguage) {
776         this.planLanguage = planLanguage;
777       }
778
779       public JsonObject getInputs() {
780         return inputs;
781       }
782
783       public void setInputs(JsonObject inputs) {
784         this.inputs = inputs;
785       }
786
787       public List<PlanInput> getInputList() {
788         return jsonObject2PlanInputList(inputs);
789
790       }
791
792       private List<PlanInput> jsonObject2PlanInputList(JsonObject inputs) {
793         List<PlanInput> retList = new ArrayList<PlanInput>();
794         Iterator<Entry<String, JsonElement>> iterator = inputs.entrySet().iterator();
795         while (iterator.hasNext()) {
796           PlanInput ret = new PlanInput();
797           Entry<String, JsonElement> next = iterator.next();
798           ret.setName(next.getKey());
799           ret.setValue(new Gson().fromJson(next.getValue(), PlanInput.PlanInputValue.class));
800           retList.add(ret);
801         }
802         return retList;
803       }
804
805       public class PlanInput {
806         private String name;
807         private PlanInputValue value;
808
809         public String getName() {
810           return name;
811         }
812
813         public void setName(String name) {
814           this.name = name;
815         }
816
817         public String getType() {
818           return value.getType();
819         }
820
821         public String getDescription() {
822           return value.getDescription();
823         }
824
825         public String getDefault() {
826           return value.getDefaultValue();
827         }
828
829         public boolean isRequired() {
830           return value.isRequired();
831         }
832
833         public PlanInputValue getValue() {
834           return value;
835         }
836
837         public void setValue(PlanInputValue value) {
838           this.value = value;
839         }
840
841         public class PlanInputValue {
842           private String type;
843           private String description;
844           @SerializedName("default")
845           private String defaultValue;
846           private boolean required;
847
848           public String getType() {
849             return type;
850           }
851
852           public void setType(String type) {
853             this.type = type;
854           }
855
856           public String getDescription() {
857             return description;
858           }
859
860           public void setDescription(String description) {
861             this.description = description;
862           }
863
864           public String getDefaultValue() {
865             return defaultValue;
866           }
867
868           public void setDefaultValue(String defaultValue) {
869             this.defaultValue = defaultValue;
870           }
871
872           public boolean isRequired() {
873             return required;
874           }
875
876           public void setRequired(boolean required) {
877             this.required = required;
878           }
879         }
880       }
881     }
882   }
883 }