956db40f913fb35e5c50067d1735d74ff8ce837a
[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     }
58
59     private enum ModelIdentification {
60         ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
61                 "providing_service_invariant_uuid") {
62             @Override
63             public void populate(Model model, String value) {
64                 model.modelId = value;
65             }
66         },
67         NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
68             @Override
69             public void populate(Model model, String value) {
70                 model.modelNameVersionId = value;
71             }
72         },
73         VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
74             @Override
75             public void populate(Model model, String value) {
76                 model.modelVersion = value;
77             }
78         },
79         NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
80             @Override
81             public void populate(Model model, String value) {
82                 model.modelName = value;
83             }
84         },
85         DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
86             @Override
87             public void populate(Model model, String value) {
88                 model.modelDescription = value;
89             }
90         },
91         NAME_AND_DESCRIPTION("providing_service_name") {
92             @Override
93             public void populate(Model model, String value) {
94                 model.modelName = model.modelDescription = value;
95             }
96         };
97
98         private static final Map<String, ModelIdentification> propertyToModelIdent;
99         private String[] keys;
100
101         ModelIdentification(String... keys) {
102             this.keys = keys;
103         }
104
105         static {
106             Map<String, ModelIdentification> mappings = new HashMap<>();
107             for (ModelIdentification ident : ModelIdentification.values()) {
108                 for (String key : ident.keys) {
109                     mappings.put(key, ident);
110                 }
111             }
112             propertyToModelIdent = Collections.unmodifiableMap(mappings);
113         }
114
115         private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
116             return Optional.ofNullable(propertyToModelIdent.get(property));
117         }
118
119         public abstract void populate(Model model, String value);
120     }
121
122     private String modelId;
123     private String modelName;
124     private String modelNameVersionId;
125     private String modelVersion;
126     private String modelDescription;
127
128     protected Set<Resource> resources = new HashSet<>();
129     protected Set<Widget> widgets = new HashSet<>();
130
131     /**
132      * Gets the object (model) corresponding to the supplied TOSCA type.
133      *
134      * @param toscaType
135      *            the tosca type
136      * @return the model for the type, or null
137      */
138     public static Model getModelFor(String toscaType) {
139         Model model = null;
140         if (toscaType != null && !toscaType.isEmpty()) {
141             model = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
142         }
143         return model;
144     }
145
146     private static Model getModelFromPrefix(String toscaType) {
147         Model model = null;
148         int lastSeparator = toscaType.lastIndexOf('.');
149         if (lastSeparator != -1) {
150             model = getModelFor(toscaType.substring(0, lastSeparator));
151         }
152         return model;
153     }
154
155     private static Optional<Model> getModelFromType(String typePrefix) {
156         Optional<Model> modelToBeReturned = Optional.empty();
157         Class<? extends Model> clazz = typeToModel.get(typePrefix);
158         if (clazz != null) {
159             try {
160                 modelToBeReturned = Optional.ofNullable(clazz.getConstructor().newInstance());
161             } catch (InstantiationException | java.lang.IllegalAccessException | IllegalArgumentException
162                     | InvocationTargetException | NoSuchMethodException | SecurityException e) {
163                 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
164             }
165         }
166         return modelToBeReturned;
167     }
168
169     public abstract boolean addResource(Resource resource);
170
171     public abstract boolean addWidget(Widget resource);
172
173     public abstract Widget.Type getWidgetType();
174
175     /**
176      * Gets cardinality.
177      *
178      * @return the cardinality
179      */
180     public Cardinality getCardinality() {
181         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
182                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
183         return model.cardinality();
184     }
185
186     /**
187      * Gets delete flag.
188      *
189      * @return the delete flag
190      */
191     public boolean getDeleteFlag() {
192         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
193                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
194         return model.dataDeleteFlag();
195     }
196
197     public String getModelDescription() {
198         return modelDescription;
199     }
200
201     public String getModelId() {
202         checkSupported();
203         return modelId;
204     }
205
206     public String getModelName() {
207         return modelName;
208     }
209
210     public String getModelVersion() {
211         return modelVersion;
212     }
213
214     public String getModelNameVersionId() {
215         checkSupported();
216         return modelNameVersionId;
217     }
218
219     /**
220      * Gets model type.
221      *
222      * @return the model type
223      */
224     public ModelType getModelType() {
225         if (this instanceof Service) {
226             return ModelType.SERVICE;
227         } else if (this instanceof Resource) {
228             return ModelType.RESOURCE;
229         } else if (this instanceof Widget) {
230             return ModelType.WIDGET;
231         } else {
232             return null;
233         }
234     }
235
236     /**
237      * Gets widget version id.
238      *
239      * @return the widget version id
240      */
241     public String getWidgetId() {
242         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
243                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
244         return Widget.getWidget(model.widget()).getId();
245     }
246
247     /**
248      * Gets invariant id.
249      *
250      * @return the invariant id
251      */
252     public String getWidgetInvariantId() {
253         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
254                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
255         return Widget.getWidget(model.widget()).getWidgetId();
256     }
257
258     /**
259      * Populate model identification information.
260      *
261      * @param modelIdentInfo
262      *            the model ident info
263      */
264     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
265         Iterator<String> iter = modelIdentInfo.keySet().iterator();
266         String property;
267         while (iter.hasNext()) {
268             property = iter.next();
269             Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
270             if (modelIdent.isPresent()) {
271                 modelIdent.get().populate(this, modelIdentInfo.get(property));
272             }
273         }
274     }
275
276     public void setModelVersion(String modelVersion) {
277         this.modelVersion = modelVersion;
278     }
279
280     public Set<Resource> getResources() {
281         return resources;
282     }
283
284     public Set<Widget> getWidgets() {
285         return widgets;
286     }
287
288     private void checkSupported() {
289         if (this instanceof Widget) {
290             throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
291         }
292     }
293 }