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