Add support for the CR Widget
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / model / Model.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.babel.xml.generator.model;
22
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Optional;
30 import java.util.Set;
31 import org.onap.aai.babel.logging.ApplicationMsgs;
32 import org.onap.aai.babel.logging.LogHelper;
33 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
34 import org.onap.aai.babel.xml.generator.types.Cardinality;
35 import org.onap.aai.babel.xml.generator.types.ModelType;
36 import org.onap.aai.cl.api.Logger;
37
38 public abstract class Model {
39
40     public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
41
42     private static Logger log = LogHelper.INSTANCE;
43
44     private static Map<String, Class<? extends Model>> typeToModel = new HashMap<>();
45     static {
46         typeToModel.put("org.openecomp.resource.vf.allottedResource", AllotedResource.class);
47         typeToModel.put("org.openecomp.resource.vfc.AllottedResource", ProvidingService.class);
48         typeToModel.put("org.openecomp.resource.vfc", VServerWidget.class);
49         typeToModel.put("org.openecomp.resource.cp", LIntfWidget.class);
50         typeToModel.put("org.openecomp.cp", LIntfWidget.class);
51         typeToModel.put("org.openecomp.resource.vl", L3Network.class);
52         typeToModel.put("org.openecomp.resource.vf", VirtualFunction.class);
53         typeToModel.put("org.openecomp.groups.vfmodule", VfModule.class);
54         typeToModel.put("org.openecomp.groups.VfModule", VfModule.class);
55         typeToModel.put("org.openecomp.resource.vfc.nodes.heat.cinder", VolumeWidget.class);
56         typeToModel.put("org.openecomp.nodes.PortMirroringConfiguration", Configuration.class);
57         typeToModel.put("org.openecomp.resource.cr.Kk1806Cr1", CR.class);
58     }
59
60     private enum ModelIdentification {
61         ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
62                 "providing_service_invariant_uuid") {
63             @Override
64             public void populate(Model model, String value) {
65                 model.modelId = value;
66             }
67         },
68         NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
69             @Override
70             public void populate(Model model, String value) {
71                 model.modelNameVersionId = value;
72             }
73         },
74         VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
75             @Override
76             public void populate(Model model, String value) {
77                 model.modelVersion = value;
78             }
79         },
80         NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
81             @Override
82             public void populate(Model model, String value) {
83                 model.modelName = value;
84             }
85         },
86         DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
87             @Override
88             public void populate(Model model, String value) {
89                 model.modelDescription = value;
90             }
91         },
92         NAME_AND_DESCRIPTION("providing_service_name") {
93             @Override
94             public void populate(Model model, String value) {
95                 model.modelName = model.modelDescription = value;
96             }
97         };
98
99         private static final Map<String, ModelIdentification> propertyToModelIdent;
100         private String[] keys;
101
102         ModelIdentification(String... keys) {
103             this.keys = keys;
104         }
105
106         static {
107             Map<String, ModelIdentification> mappings = new HashMap<>();
108             for (ModelIdentification ident : ModelIdentification.values()) {
109                 for (String key : ident.keys) {
110                     mappings.put(key, ident);
111                 }
112             }
113             propertyToModelIdent = Collections.unmodifiableMap(mappings);
114         }
115
116         private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
117             return Optional.ofNullable(propertyToModelIdent.get(property));
118         }
119
120         public abstract void populate(Model model, String value);
121     }
122
123     private String modelId;
124     private String modelName;
125     private String modelNameVersionId;
126     private String modelVersion;
127     private String modelDescription;
128
129     protected Set<Resource> resources = new HashSet<>();
130     protected Set<Widget> widgets = new HashSet<>();
131
132     /**
133      * Gets the object (model) corresponding to the supplied TOSCA type.
134      *
135      * @param toscaType
136      *            the tosca type
137      * @return the model for the type, or null
138      */
139     public static Model getModelFor(String toscaType) {
140         Model model = null;
141         if (toscaType != null && !toscaType.isEmpty()) {
142             model = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
143         }
144         return model;
145     }
146
147     private static Model getModelFromPrefix(String toscaType) {
148         Model model = null;
149         int lastSeparator = toscaType.lastIndexOf('.');
150         if (lastSeparator != -1) {
151             model = getModelFor(toscaType.substring(0, lastSeparator));
152         }
153         return model;
154     }
155
156     private static Optional<Model> getModelFromType(String typePrefix) {
157         Optional<Model> modelToBeReturned = Optional.empty();
158         Class<? extends Model> clazz = typeToModel.get(typePrefix);
159         if (clazz != null) {
160             try {
161                 modelToBeReturned = Optional.ofNullable(clazz.getConstructor().newInstance());
162             } catch (InstantiationException | java.lang.IllegalAccessException | IllegalArgumentException
163                     | InvocationTargetException | NoSuchMethodException | SecurityException e) {
164                 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
165             }
166         }
167         return modelToBeReturned;
168     }
169
170     public abstract boolean addResource(Resource resource);
171
172     public abstract boolean addWidget(Widget resource);
173
174     public abstract Widget.Type getWidgetType();
175
176     /**
177      * Gets cardinality.
178      *
179      * @return the cardinality
180      */
181     public Cardinality getCardinality() {
182         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
183                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
184         return model.cardinality();
185     }
186
187     /**
188      * Gets delete flag.
189      *
190      * @return the delete flag
191      */
192     public boolean getDeleteFlag() {
193         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
194                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
195         return model.dataDeleteFlag();
196     }
197
198     public String getModelDescription() {
199         return modelDescription;
200     }
201
202     public String getModelId() {
203         checkSupported();
204         return modelId;
205     }
206
207     public String getModelName() {
208         return modelName;
209     }
210
211     public String getModelVersion() {
212         return modelVersion;
213     }
214
215     public String getModelNameVersionId() {
216         checkSupported();
217         return modelNameVersionId;
218     }
219
220     /**
221      * Gets model type.
222      *
223      * @return the model type
224      */
225     public ModelType getModelType() {
226         if (this instanceof Service) {
227             return ModelType.SERVICE;
228         } else if (this instanceof Resource) {
229             return ModelType.RESOURCE;
230         } else if (this instanceof Widget) {
231             return ModelType.WIDGET;
232         } else {
233             return null;
234         }
235     }
236
237     /**
238      * Gets widget version id.
239      *
240      * @return the widget version id
241      */
242     public String getWidgetId() {
243         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
244                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
245         return Widget.getWidget(model.widget()).getId();
246     }
247
248     /**
249      * Gets invariant id.
250      *
251      * @return the invariant id
252      */
253     public String getWidgetInvariantId() {
254         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
255                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
256         return Widget.getWidget(model.widget()).getWidgetId();
257     }
258
259     /**
260      * Populate model identification information.
261      *
262      * @param modelIdentInfo
263      *            the model ident info
264      */
265     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
266         Iterator<String> iter = modelIdentInfo.keySet().iterator();
267         String property;
268         while (iter.hasNext()) {
269             property = iter.next();
270             Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
271             if (modelIdent.isPresent()) {
272                 modelIdent.get().populate(this, modelIdentInfo.get(property));
273             }
274         }
275     }
276
277     public void setModelVersion(String modelVersion) {
278         this.modelVersion = modelVersion;
279     }
280
281     public Set<Resource> getResources() {
282         return resources;
283     }
284
285     public Set<Widget> getWidgets() {
286         return widgets;
287     }
288
289     private void checkSupported() {
290         if (this instanceof Widget) {
291             throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
292         }
293     }
294 }