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