666883651809dd40a6101392d142c96e7b127181
[sdc.git] /
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         case "org.openecomp.resource.pnf":
84           modelToBeReturned = new PnfResource();
85           break;
86         default:
87           modelToBeReturned = null;
88           break;
89       }
90
91       toscaType = toscaType.substring(0, toscaType.lastIndexOf("."));
92     }
93
94     return modelToBeReturned;
95   }
96
97   public abstract boolean addResource(Resource resource);
98
99   public abstract boolean addWidget(Widget resource);
100
101   /**
102    * Gets widget version id.
103    *
104    * @return the widget version id
105    */
106   public String getWidgetId() {
107     org.openecomp.sdc.generator.aai.types.Model model =
108         this.getClass().getAnnotation(org.openecomp.sdc.generator.aai.types.Model.class);
109     return Widget.getWidget(model.widget()).getId();
110   }
111
112   /**
113    * Gets invariant id.
114    *
115    * @return the invariant id
116    */
117   public String getWidgetInvariantId() {
118     org.openecomp.sdc.generator.aai.types.Model model =
119         this.getClass().getAnnotation(org.openecomp.sdc.generator.aai.types.Model.class);
120     return Widget.getWidget(model.widget()).getWidgetId();
121   }
122
123   /**
124    * Gets delete flag.
125    *
126    * @return the delete flag
127    */
128   public boolean getDeleteFlag() {
129     org.openecomp.sdc.generator.aai.types.Model model =
130         this.getClass().getAnnotation(org.openecomp.sdc.generator.aai.types.Model.class);
131     return model.dataDeleteFlag();
132   }
133
134   /**
135    * Gets cardinality.
136    *
137    * @return the cardinality
138    */
139   public Cardinality getCardinality() {
140     org.openecomp.sdc.generator.aai.types.Model model =
141         this.getClass().getAnnotation(org.openecomp.sdc.generator.aai.types.Model.class);
142     return model.cardinality();
143   }
144
145   public abstract Widget.Type getWidgetType();
146
147   public String getModelId() {
148     checkSupported();
149     return modelId;
150   }
151
152   /**
153    * Gets model type.
154    *
155    * @return the model type
156    */
157   public ModelType getModelType() {
158     //checkSupported();
159     if (this instanceof Service) {
160       return ModelType.SERVICE;
161     } else if (this instanceof Resource) {
162       return ModelType.RESOURCE;
163     } else if (this instanceof Widget) {
164       return ModelType.WIDGET;
165     } else {
166       return null;
167     }
168   }
169
170   public String getModelName() {
171     //checkSupported();
172     return modelName;
173   }
174
175   public String getModelVersion() {
176     //checkSupported();
177     return modelVersion;
178   }
179
180   public String getModelNameVersionId() {
181     checkSupported();
182     return modelNameVersionId;
183   }
184
185   public String getModelDescription() {
186     //checkSupported();
187     return modelDescription;
188   }
189
190   /**
191    * Populate model identification information.
192    *
193    * @param modelIdentInfo the model ident info
194    */
195   public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
196     Iterator<String> iter = modelIdentInfo.keySet().iterator();
197     String property;
198     while (iter.hasNext()) {
199       switch (property = iter.next()) {
200
201         case "vfModuleModelInvariantUUID":
202         case "serviceInvariantUUID":
203         case "resourceInvariantUUID":
204         case "invariantUUID":
205         case "providing_service_invariant_uuid":
206           modelId = modelIdentInfo.get(property);
207           break;
208         case "vfModuleModelUUID":
209         case "resourceUUID":
210         case "serviceUUID":
211         case "UUID":
212         case "providing_service_uuid":
213           modelNameVersionId = modelIdentInfo.get(property);
214           break;
215         case "vfModuleModelVersion":
216         case "serviceVersion":
217         case "resourceversion":
218         case "version":
219           modelVersion = modelIdentInfo.get(property);
220           break;
221         case "vfModuleModelName":
222         case "serviceName":
223         case "resourceName":
224         case "name":
225           modelName = modelIdentInfo.get(property);
226           break;
227         case "serviceDescription":
228         case "resourceDescription":
229         case "vf_module_description":
230         case "description":
231           modelDescription = modelIdentInfo.get(property);
232           break;
233         case "providing_service_name":
234           modelName = modelIdentInfo.get(property);
235           modelDescription = modelIdentInfo.get(property);
236           break;
237         default:
238           break;
239       }
240       property = null;
241     }
242
243
244
245   }
246
247   public Set<Resource> getResources() {
248     return resources;
249   }
250
251   public Set<Widget> getWidgets() {
252     return widgets;
253   }
254
255   private void checkSupported() throws IllegalAccessException {
256     if (this instanceof Widget) {
257       throw new IllegalAccessException(GeneratorConstants
258           .GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
259     }
260   }
261
262 }