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