83ded46bd11f2bdfc1e44f0cd0ade98bef3af6e7
[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.onap.sdc.generator.aai.model;
22
23 import org.onap.sdc.generator.aai.types.Cardinality;
24 import org.onap.sdc.generator.aai.types.ModelType;
25 import org.onap.sdc.generator.data.GeneratorConstants;
26 import org.onap.sdc.generator.error.IllegalAccessException;
27
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31
32 public abstract class Model {
33
34   protected Set<Resource> resources = new HashSet<>();
35   protected Set<Widget> widgets = new HashSet<>();
36   private String modelId;
37   private String modelName;
38   private String modelVersion;
39   private String modelNameVersionId;
40   private String modelDescription;
41
42   /**
43    * Gets model for.
44    *
45    * @param toscaType the tosca type
46    * @return the model for
47    */
48   public static Model getModelFor(String toscaType) {
49
50     Model modelToBeReturned = null;
51     while (isModelNotSet(toscaType, modelToBeReturned)) {
52
53       switch (toscaType) {
54
55         case "org.openecomp.resource.vf.allottedResource":
56           modelToBeReturned = new AllotedResource();
57           break;
58         case "org.openecomp.resource.vfc.AllottedResource":
59           modelToBeReturned = new ProvidingService();
60           break;
61         case "org.openecomp.resource.vfc":
62           modelToBeReturned = new VServerWidget();
63           break;
64         case "org.openecomp.resource.cp":
65         case "org.openecomp.cp":
66           modelToBeReturned = new LIntfWidget();
67           break;
68         case "org.openecomp.resource.vl":
69           modelToBeReturned = new L3Network();
70           break;
71         case "org.openecomp.resource.vf":
72           modelToBeReturned = new VirtualFunction();
73           break;
74         case "org.openecomp.groups.vfmodule":
75         case "org.openecomp.groups.VfModule":
76           modelToBeReturned = new VfModule();
77           break;
78         case "org.openecomp.resource.vfc.nodes.heat.cinder":
79           modelToBeReturned = new VolumeWidget();
80           break;
81         default:
82           modelToBeReturned = null;
83           break;
84       }
85
86       toscaType = toscaType.substring(0, toscaType.lastIndexOf("."));
87     }
88
89     return modelToBeReturned;
90   }
91
92
93   public abstract boolean addResource(Resource resource);
94
95   public abstract boolean addWidget(Widget resource);
96
97   /**
98    * Gets widget version id.
99    *
100    * @return the widget version id
101    */
102   public String getWidgetId() {
103     org.onap.sdc.generator.aai.types.Model model =
104         this.getClass().getAnnotation(org.onap.sdc.generator.aai.types.Model.class);
105     return Widget.getWidget(model.widget()).getId();
106   }
107
108   /**
109    * Gets invariant id.
110    *
111    * @return the invariant id
112    */
113   public String getWidgetInvariantId() {
114     org.onap.sdc.generator.aai.types.Model model =
115         this.getClass().getAnnotation(org.onap.sdc.generator.aai.types.Model.class);
116     return Widget.getWidget(model.widget()).getWidgetId();
117   }
118
119   /**
120    * Gets delete flag.
121    *
122    * @return the delete flag
123    */
124   public boolean getDeleteFlag() {
125     org.onap.sdc.generator.aai.types.Model model =
126         this.getClass().getAnnotation(org.onap.sdc.generator.aai.types.Model.class);
127     return model.dataDeleteFlag();
128   }
129
130   /**
131    * Gets cardinality.
132    *
133    * @return the cardinality
134    */
135   public Cardinality getCardinality() {
136     org.onap.sdc.generator.aai.types.Model model =
137         this.getClass().getAnnotation(org.onap.sdc.generator.aai.types.Model.class);
138     return model.cardinality();
139   }
140
141   public abstract Widget.Type getWidgetType();
142
143   public String getModelId() {
144     checkSupported();
145     return modelId;
146   }
147
148   /**
149    * Gets model type.
150    *
151    * @return the model type
152    */
153   public ModelType getModelType() {
154     if (this instanceof Service) {
155       return ModelType.SERVICE;
156     } else if (this instanceof Resource) {
157       return ModelType.RESOURCE;
158     } else if (this instanceof Widget) {
159       return ModelType.WIDGET;
160     } else {
161       return null;
162     }
163   }
164
165   public String getModelName() {
166     return modelName;
167   }
168
169   public String getModelVersion() {
170     return modelVersion;
171   }
172
173   public String getModelNameVersionId() {
174     checkSupported();
175     return modelNameVersionId;
176   }
177
178   public String getModelDescription() {
179     return modelDescription;
180   }
181
182   /**
183    * Populate model identification information.
184    *
185    * @param modelIdentInfo the model ident info
186    */
187   public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
188     for (Map.Entry<String,String> entry : modelIdentInfo.entrySet()) {
189       String property=entry.getKey();
190       switch (property) {
191
192         case "vfModuleModelInvariantUUID":
193         case "serviceInvariantUUID":
194         case "resourceInvariantUUID":
195         case "invariantUUID":
196         case "providing_service_invariant_uuid":
197           modelId = entry.getValue();
198           break;
199         case "vfModuleModelUUID":
200         case "resourceUUID":
201         case "serviceUUID":
202         case "UUID":
203         case "providing_service_uuid":
204           modelNameVersionId = entry.getValue();
205           break;
206         case "vfModuleModelVersion":
207         case "serviceVersion":
208         case "resourceversion":
209         case "version":
210           modelVersion = entry.getValue();
211           break;
212         case "vfModuleModelName":
213         case "serviceName":
214         case "resourceName":
215         case "name":
216           modelName = entry.getValue();
217           break;
218         case "serviceDescription":
219         case "resourceDescription":
220         case "vf_module_description":
221         case "description":
222           modelDescription = entry.getValue();
223           break;
224         case "providing_service_name":
225           modelName = entry.getValue();
226           modelDescription = entry.getValue();
227           break;
228         default:
229           break;
230       }
231     }
232
233
234
235   }
236
237   public Set<Resource> getResources() {
238     return resources;
239   }
240
241   public Set<Widget> getWidgets() {
242     return widgets;
243   }
244
245   private void checkSupported() {
246     if (this instanceof Widget) {
247       throw new IllegalAccessException(GeneratorConstants
248           .GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
249     }
250   }
251
252   private static boolean isModelNotSet(String toscaType, Model modelToBeReturned) {
253     return toscaType != null && toscaType.lastIndexOf(".") != -1 && modelToBeReturned == null;
254   }
255 }