7b2fc42259756e0242750705e13cd82cbe8f5b68
[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.util.Collections;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.Set;
30 import org.onap.aai.babel.logging.ApplicationMsgs;
31 import org.onap.aai.babel.logging.LogHelper;
32 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
33 import org.onap.aai.babel.xml.generator.types.Cardinality;
34 import org.onap.aai.babel.xml.generator.types.ModelType;
35 import org.onap.aai.cl.api.Logger;
36
37 public abstract class Model {
38
39     public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
40
41     private static Logger log = LogHelper.INSTANCE;
42
43     private static Map<String, Class<? extends Model>> typeToModel = new HashMap<>();
44     static {
45         typeToModel.put("org.openecomp.resource.vf.allottedResource", AllotedResource.class);
46         typeToModel.put("org.openecomp.resource.vfc.AllottedResource", ProvidingService.class);
47         typeToModel.put("org.openecomp.resource.vfc", VServerWidget.class);
48         typeToModel.put("org.openecomp.resource.cp", LIntfWidget.class);
49         typeToModel.put("org.openecomp.cp", LIntfWidget.class);
50         typeToModel.put("org.openecomp.resource.vl", L3Network.class);
51         typeToModel.put("org.openecomp.resource.vf", VirtualFunction.class);
52         typeToModel.put("org.openecomp.groups.vfmodule", VfModule.class);
53         typeToModel.put("org.openecomp.groups.VfModule", VfModule.class);
54         typeToModel.put("org.openecomp.resource.vfc.nodes.heat.cinder", VolumeWidget.class);
55         typeToModel.put("org.openecomp.nodes.PortMirroringConfiguration", Configuration.class);
56         typeToModel.put("org.openecomp.resource.cr.Kk1806Cr1", CR.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 (Exception e) {
162                 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
163             }
164         }
165         return modelToBeReturned;
166     }
167
168     /**
169      * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
170      * information.
171      *
172      * @param toscaType
173      *            the TOSCA type
174      * @param metaDataType
175      *            the type from the TOSCA metadata
176      * @return the model for the type, or null
177      */
178     public static Model getModelFor(String toscaType, String metaDataType) {
179         if ("Configuration".equals(metaDataType)) {
180             return new Configuration();
181         } else if ("CR".equals(metaDataType)) {
182             return new CR();
183         } else {
184             return getModelFor(toscaType);
185         }
186     }
187
188     public abstract boolean addResource(Resource resource);
189
190     public abstract boolean addWidget(Widget resource);
191
192     public abstract Widget.Type getWidgetType();
193
194     /**
195      * Gets cardinality.
196      *
197      * @return the cardinality
198      */
199     public Cardinality getCardinality() {
200         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
201                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
202         return model.cardinality();
203     }
204
205     /**
206      * Gets delete flag.
207      *
208      * @return the delete flag
209      */
210     public boolean getDeleteFlag() {
211         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
212                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
213         return model.dataDeleteFlag();
214     }
215
216     public String getModelDescription() {
217         return modelDescription;
218     }
219
220     public String getModelId() {
221         checkSupported();
222         return modelId;
223     }
224
225     public String getModelName() {
226         return modelName;
227     }
228
229     public String getModelVersion() {
230         return modelVersion;
231     }
232
233     public String getModelNameVersionId() {
234         checkSupported();
235         return modelNameVersionId;
236     }
237
238     /**
239      * Gets model type.
240      *
241      * @return the model type
242      */
243     public ModelType getModelType() {
244         if (this instanceof Service) {
245             return ModelType.SERVICE;
246         } else if (this instanceof Resource) {
247             return ModelType.RESOURCE;
248         } else if (this instanceof Widget) {
249             return ModelType.WIDGET;
250         } else {
251             return null;
252         }
253     }
254
255     /**
256      * Gets widget version id.
257      *
258      * @return the widget version id
259      */
260     public String getWidgetId() {
261         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
262                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
263         return Widget.getWidget(model.widget()).getId();
264     }
265
266     /**
267      * Gets invariant id.
268      *
269      * @return the invariant id
270      */
271     public String getWidgetInvariantId() {
272         org.onap.aai.babel.xml.generator.types.Model model = this.getClass()
273                 .getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
274         return Widget.getWidget(model.widget()).getWidgetId();
275     }
276
277     /**
278      * Populate model identification information.
279      *
280      * @param modelIdentInfo
281      *            the model ident info
282      */
283     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
284         Iterator<String> iter = modelIdentInfo.keySet().iterator();
285         String property;
286         while (iter.hasNext()) {
287             property = iter.next();
288             Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
289             if (modelIdent.isPresent()) {
290                 modelIdent.get().populate(this, modelIdentInfo.get(property));
291             }
292         }
293     }
294
295     public void setModelVersion(String modelVersion) {
296         this.modelVersion = modelVersion;
297     }
298
299     public Set<Resource> getResources() {
300         return resources;
301     }
302
303     public Set<Widget> getWidgets() {
304         return widgets;
305     }
306
307     private void checkSupported() {
308         if (this instanceof Widget) {
309             throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
310         }
311     }
312
313 }