c55abb36c2f74b9dd04c1bc2cd9147e6618e73ef
[sdc.git] / common / openecomp-sdc-artifact-generator-lib / openecomp-sdc-artifact-generator-api / src / main / java / org / openecomp / sdc / generator / aai / model / Model.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.generator.aai.model;
22
23 import org.openecomp.sdc.generator.aai.types.Cardinality;
24 import org.openecomp.sdc.generator.aai.types.ModelType;
25 import org.openecomp.sdc.generator.data.GeneratorConstants;
26 import org.openecomp.sdc.generator.error.IllegalAccessException;
27
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.Set;
32
33 public abstract class Model {
34
35   protected Set<Resource> resources = new HashSet<>();
36   protected Set<Widget> widgets = new HashSet<>();
37   private String modelId;
38   private String modelName;
39   private ModelType modelType;
40   private String modelVersion;
41   private String modelNameVersionId;
42   private String modelDescription;
43
44   /**
45    * Gets model for.
46    *
47    * @param toscaType the tosca type
48    * @return the model for
49    */
50   public static Model getModelFor(String toscaType) {
51
52     Model modelToBeReturned = null;
53     while (toscaType != null && toscaType.lastIndexOf(".") != -1 && modelToBeReturned == null) {
54
55       switch (toscaType) {
56
57         case "org.openecomp.resource.vf.allottedResource":
58           modelToBeReturned = new AllotedResource();
59           break;
60         case "org.openecomp.resource.vfc.AllottedResource":
61           modelToBeReturned = new ProvidingService();
62           break;
63         case "org.openecomp.resource.vfc":
64           modelToBeReturned = new VServerWidget();
65           break;
66         case "org.openecomp.resource.cp":
67         case "org.openecomp.cp":
68           modelToBeReturned = new LIntfWidget();
69           break;
70         case "org.openecomp.resource.vl":
71           modelToBeReturned = new L3Network();
72           break;
73         case "org.openecomp.resource.vf":
74           modelToBeReturned = new VirtualFunction();
75           break;
76         case "org.openecomp.groups.vfmodule":
77         case "org.openecomp.groups.VfModule":
78           modelToBeReturned = new VfModule();
79           break;
80         case "org.openecomp.resource.vfc.nodes.heat.cinder":
81           modelToBeReturned = new VolumeWidget();
82           break;
83         default:
84           modelToBeReturned = null;
85           break;
86       }
87
88       toscaType = toscaType.substring(0, toscaType.lastIndexOf("."));
89     }
90
91     return modelToBeReturned;
92   }
93
94   public abstract boolean addResource(Resource resource);
95
96   public abstract boolean addWidget(Widget resource);
97
98   /**
99    * Gets widget version id.
100    *
101    * @return the widget version id
102    */
103   public String getWidgetId() {
104     org.openecomp.sdc.generator.aai.types.Model model =
105         this.getClass().getAnnotation(org.openecomp.sdc.generator.aai.types.Model.class);
106     return Widget.getWidget(model.widget()).getId();
107   }
108
109   /**
110    * Gets invariant id.
111    *
112    * @return the invariant id
113    */
114   public String getWidgetInvariantId() {
115     org.openecomp.sdc.generator.aai.types.Model model =
116         this.getClass().getAnnotation(org.openecomp.sdc.generator.aai.types.Model.class);
117     return Widget.getWidget(model.widget()).getWidgetId();
118   }
119
120   /**
121    * Gets delete flag.
122    *
123    * @return the delete flag
124    */
125   public boolean getDeleteFlag() {
126     org.openecomp.sdc.generator.aai.types.Model model =
127         this.getClass().getAnnotation(org.openecomp.sdc.generator.aai.types.Model.class);
128     return model.dataDeleteFlag();
129   }
130
131   /**
132    * Gets cardinality.
133    *
134    * @return the cardinality
135    */
136   public Cardinality getCardinality() {
137     org.openecomp.sdc.generator.aai.types.Model model =
138         this.getClass().getAnnotation(org.openecomp.sdc.generator.aai.types.Model.class);
139     return model.cardinality();
140   }
141
142   public abstract Widget.Type getWidgetType();
143
144   public String getModelId() {
145     checkSupported();
146     return modelId;
147   }
148
149   /**
150    * Gets model type.
151    *
152    * @return the model type
153    */
154   public ModelType getModelType() {
155     //checkSupported();
156     if (this instanceof Service) {
157       return ModelType.SERVICE;
158     } else if (this instanceof Resource) {
159       return ModelType.RESOURCE;
160     } else if (this instanceof Widget) {
161       return ModelType.WIDGET;
162     } else {
163       return null;
164     }
165   }
166
167   public String getModelName() {
168     //checkSupported();
169     return modelName;
170   }
171
172   public String getModelVersion() {
173     //checkSupported();
174     return modelVersion;
175   }
176
177   public String getModelNameVersionId() {
178     checkSupported();
179     return modelNameVersionId;
180   }
181
182   public String getModelDescription() {
183     //checkSupported();
184     return modelDescription;
185   }
186
187   /**
188    * Populate model identification information.
189    *
190    * @param modelIdentInfo the model ident info
191    */
192   public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
193     Iterator<String> iter = modelIdentInfo.keySet().iterator();
194     String property;
195     while (iter.hasNext()) {
196       switch (property = iter.next()) {
197
198         case "vfModuleModelInvariantUUID":
199         case "serviceInvariantUUID":
200         case "resourceInvariantUUID":
201         case "invariantUUID":
202         case "providing_service_invariant_uuid":
203           modelId = modelIdentInfo.get(property);
204           break;
205         case "vfModuleModelUUID":
206         case "resourceUUID":
207         case "serviceUUID":
208         case "UUID":
209         case "providing_service_uuid":
210           modelNameVersionId = modelIdentInfo.get(property);
211           break;
212         case "vfModuleModelVersion":
213         case "serviceVersion":
214         case "resourceversion":
215         case "version":
216           modelVersion = modelIdentInfo.get(property);
217           break;
218         case "vfModuleModelName":
219         case "serviceName":
220         case "resourceName":
221         case "name":
222           modelName = modelIdentInfo.get(property);
223           break;
224         case "serviceDescription":
225         case "resourceDescription":
226         case "vf_module_description":
227         case "description":
228           modelDescription = modelIdentInfo.get(property);
229           break;
230         case "providing_service_name":
231           modelName = modelIdentInfo.get(property);
232           modelDescription = modelIdentInfo.get(property);
233           break;
234         default:
235           break;
236       }
237       property = null;
238     }
239
240
241
242   }
243
244   public Set<Resource> getResources() {
245     return resources;
246   }
247
248   public Set<Widget> getWidgets() {
249     return widgets;
250   }
251
252   private void checkSupported() throws IllegalAccessException {
253     if (this instanceof Widget) {
254       throw new IllegalAccessException(GeneratorConstants
255           .GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
256     }
257   }
258
259 }