Create 1.13.0 maven release
[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 (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 2017-2019 European Software Marketing Ltd.
7  * Copyright (C) 2019-2020 Wipro Limited.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *       http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.babel.xml.generator.model;
24
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Optional;
31 import java.util.Set;
32 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
33 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
34
35 public abstract class Model {
36
37     public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
38
39     private enum ModelIdentification {
40         ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
41                 "providing_service_invariant_uuid") {
42             @Override
43             public void populate(Model model, String value) {
44                 model.modelId = value;
45             }
46         },
47         NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
48             @Override
49             public void populate(Model model, String value) {
50                 model.modelNameVersionId = value;
51             }
52         },
53         VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
54             @Override
55             public void populate(Model model, String value) {
56                 model.modelVersion = value;
57             }
58         },
59         NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
60             @Override
61             public void populate(Model model, String value) {
62                 model.modelName = value;
63             }
64         },
65         CATEGORY("category") {
66                 @Override
67             public void populate(Model model, String value) {
68                 model.category = value;
69             }
70         },
71         DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
72             @Override
73             public void populate(Model model, String value) {
74                 model.modelDescription = value;
75             }
76         },
77         ORCHESTRATION_TYPE("instantiationType"){
78             @Override
79             public void populate(Model model, String value) {
80                 model.instantiationType = value;
81             }
82         },
83         NAME_AND_DESCRIPTION("providing_service_name") {
84             @Override
85             public void populate(Model model, String value) {
86                 model.modelName = model.modelDescription = value;
87             }
88         },
89         SDNC_MODEL_NAME("sdnc_model_name") {
90             @Override
91             public void populate(Model model, String value) {
92                 model.sdncModelName = value;
93             }
94         },
95         SDNC_MODEL_VERSION("sdnc_model_version") {
96             @Override
97             public void populate(Model model, String value) {
98                 model.sdncModelVersion = value;
99             }
100         };
101
102         private static final Map<String, ModelIdentification> propertyToModelIdent;
103         private String[] keys;
104
105         ModelIdentification(String... keys) {
106             this.keys = keys;
107         }
108
109         static {
110             Map<String, ModelIdentification> mappings = new HashMap<>();
111             for (ModelIdentification ident : ModelIdentification.values()) {
112                 for (String key : ident.keys) {
113                     mappings.put(key, ident);
114                 }
115             }
116             propertyToModelIdent = Collections.unmodifiableMap(mappings);
117         }
118
119         private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
120             return Optional.ofNullable(propertyToModelIdent.get(property));
121         }
122
123         public abstract void populate(Model model, String value);
124     }
125
126     private String modelId; // model-invariant-id
127     private String modelName;
128     private String modelNameVersionId; // model-version-id
129     private String modelVersion;
130     private String modelDescription;
131     private String instantiationType;
132     private String category;
133     private String sdncModelVersion;
134     private String sdncModelName;
135     protected Set<Resource> resources = new HashSet<>();
136     protected Set<Widget> widgets = new HashSet<>();
137
138     /**
139      * Gets the Resource Model corresponding to the supplied TOSCA type.
140      *
141      * @param toscaType
142      *            the tosca type
143      * @return the model for the type, or null
144      */
145     public static Resource getModelFor(String toscaType) {
146         Resource resource = null;
147         if (toscaType != null && !toscaType.isEmpty()) {
148             resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
149         }
150         return resource;
151     }
152
153     private static Resource getModelFromPrefix(String toscaType) {
154         Resource resource = null;
155         int lastSeparator = toscaType.lastIndexOf('.');
156         if (lastSeparator != -1) {
157             resource = getModelFor(toscaType.substring(0, lastSeparator));
158         }
159         return resource;
160     }
161
162     private static Optional<Resource> getModelFromType(String typePrefix) {
163         return WidgetConfigurationUtil.createModelFromType(typePrefix);
164     }
165
166     /**
167      * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
168      * information.
169      *
170      * @param toscaType
171      *            the TOSCA type
172      * @param metaDataType
173      *            the type from the TOSCA metadata
174      * @return the model for the type, or null
175      */
176     public static Resource getModelFor(String toscaType, String metaDataType) {
177         if ("Configuration".equals(metaDataType)) {
178             return new Resource(WidgetType.valueOf("CONFIGURATION"), true);
179         } else if ("CR".equals(metaDataType)) {
180             return new Resource(WidgetType.valueOf("CR"), true);
181         } else {
182             return getModelFor(toscaType);
183         }
184     }
185
186     public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
187
188     /**
189      * @return the Widget Type of this model.
190      */
191     public abstract WidgetType getWidgetType();
192
193     public abstract String getModelTypeName();
194
195     /**
196      * Check whether the model's Widget Type matches the supplied type.
197      *
198      * @param type
199      *            the Widget Type to compare
200      * @return true if the Widget Type of this model matches the supplied type
201      */
202     public boolean hasWidgetType(String type) {
203         return getWidgetType() == WidgetType.valueOf(type);
204     }
205
206     public boolean addResource(Resource resource) {
207         return resources.add(resource);
208     }
209
210     /**
211      * Gets delete flag.
212      *
213      * @return the delete flag
214      */
215     public boolean getDeleteFlag() {
216         return true;
217     }
218
219     public String getModelDescription() {
220         return modelDescription;
221     }
222
223     public String getModelId() {
224         return modelId;
225     }
226
227     public String getModelName() {
228         return modelName;
229     }
230
231     public String getModelVersion() {
232         return modelVersion;
233     }
234
235     public String getModelNameVersionId() {
236         return modelNameVersionId;
237     }
238
239     public String getInstantiationType() {
240         return instantiationType;
241     }
242
243     public String getCategory() {
244         return category;
245     }
246
247     public String getSdncModelName() {
248         return sdncModelName;
249     }
250
251     public String getSdncModelVersion() {
252         return sdncModelVersion;
253     }
254
255     /**
256      * Gets widget version id.
257      *
258      * @return the widget version id
259      * @throws XmlArtifactGenerationException
260      */
261     public String getWidgetId() throws XmlArtifactGenerationException {
262         return Widget.createWidget(getWidgetType()).getId();
263     }
264
265     /**
266      * Gets invariant id.
267      *
268      * @return the invariant id
269      * @throws XmlArtifactGenerationException
270      */
271     public String getWidgetInvariantId() throws XmlArtifactGenerationException {
272         return Widget.createWidget(getWidgetType()).getWidgetId();
273     }
274
275     /**
276      * Populate model identification information.
277      *
278      * @param modelIdentInfo
279      *            the model ident info
280      */
281     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
282         if (modelIdentInfo == null) {
283             return;
284         }
285         Iterator<String> iter = modelIdentInfo.keySet().iterator();
286         String property;
287         while (iter.hasNext()) {
288             property = iter.next();
289             Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
290             if (modelIdent.isPresent()) {
291                 modelIdent.get().populate(this, modelIdentInfo.get(property));
292             }
293         }
294     }
295
296     public void setModelVersion(String modelVersion) {
297         this.modelVersion = modelVersion;
298     }
299
300     public Set<Resource> getResources() {
301         return resources;
302     }
303
304     public Set<Widget> getWidgets() {
305         return widgets;
306     }
307
308     @Override
309     public String toString() {
310         return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";
311     }
312
313 }