Add 'category' of service model to Babel artifact
[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
90         private static final Map<String, ModelIdentification> propertyToModelIdent;
91         private String[] keys;
92
93         ModelIdentification(String... keys) {
94             this.keys = keys;
95         }
96
97         static {
98             Map<String, ModelIdentification> mappings = new HashMap<>();
99             for (ModelIdentification ident : ModelIdentification.values()) {
100                 for (String key : ident.keys) {
101                     mappings.put(key, ident);
102                 }
103             }
104             propertyToModelIdent = Collections.unmodifiableMap(mappings);
105         }
106
107         private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
108             return Optional.ofNullable(propertyToModelIdent.get(property));
109         }
110
111         public abstract void populate(Model model, String value);
112     }
113
114     private String modelId; // model-invariant-id
115     private String modelName;
116     private String modelNameVersionId; // model-version-id
117     private String modelVersion;
118     private String modelDescription;
119     private String instantiationType;
120     private String category;
121     protected Set<Resource> resources = new HashSet<>();
122     protected Set<Widget> widgets = new HashSet<>();
123
124     /**
125      * Gets the Resource Model corresponding to the supplied TOSCA type.
126      *
127      * @param toscaType
128      *            the tosca type
129      * @return the model for the type, or null
130      */
131     public static Resource getModelFor(String toscaType) {
132         Resource resource = null;
133         if (toscaType != null && !toscaType.isEmpty()) {
134             resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
135         }
136         return resource;
137     }
138
139     private static Resource getModelFromPrefix(String toscaType) {
140         Resource resource = null;
141         int lastSeparator = toscaType.lastIndexOf('.');
142         if (lastSeparator != -1) {
143             resource = getModelFor(toscaType.substring(0, lastSeparator));
144         }
145         return resource;
146     }
147
148     private static Optional<Resource> getModelFromType(String typePrefix) {
149         return WidgetConfigurationUtil.createModelFromType(typePrefix);
150     }
151
152     /**
153      * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
154      * information.
155      *
156      * @param toscaType
157      *            the TOSCA type
158      * @param metaDataType
159      *            the type from the TOSCA metadata
160      * @return the model for the type, or null
161      */
162     public static Resource getModelFor(String toscaType, String metaDataType) {
163         if ("Configuration".equals(metaDataType)) {
164             return new Resource(WidgetType.valueOf("CONFIGURATION"), true);
165         } else if ("CR".equals(metaDataType)) {
166             return new Resource(WidgetType.valueOf("CR"), true);
167         } else {
168             return getModelFor(toscaType);
169         }
170     }
171
172     public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
173
174     /**
175      * @return the Widget Type of this model.
176      */
177     public abstract WidgetType getWidgetType();
178
179     public abstract String getModelTypeName();
180
181     /**
182      * Check whether the model's Widget Type matches the supplied type.
183      *
184      * @param type
185      *            the Widget Type to compare
186      * @return true if the Widget Type of this model matches the supplied type
187      */
188     public boolean hasWidgetType(String type) {
189         return getWidgetType() == WidgetType.valueOf(type);
190     }
191
192     public boolean addResource(Resource resource) {
193         return resources.add(resource);
194     }
195
196     /**
197      * Gets delete flag.
198      *
199      * @return the delete flag
200      */
201     public boolean getDeleteFlag() {
202         return true;
203     }
204
205     public String getModelDescription() {
206         return modelDescription;
207     }
208
209     public String getModelId() {
210         return modelId;
211     }
212
213     public String getModelName() {
214         return modelName;
215     }
216
217     public String getModelVersion() {
218         return modelVersion;
219     }
220
221     public String getModelNameVersionId() {
222         return modelNameVersionId;
223     }
224
225     public String getInstantiationType() {
226         return instantiationType;
227     }
228
229     public String getCategory() {
230         return category;
231     }
232     /**
233      * Gets widget version id.
234      *
235      * @return the widget version id
236      * @throws XmlArtifactGenerationException
237      */
238     public String getWidgetId() throws XmlArtifactGenerationException {
239         return Widget.createWidget(getWidgetType()).getId();
240     }
241
242     /**
243      * Gets invariant id.
244      *
245      * @return the invariant id
246      * @throws XmlArtifactGenerationException
247      */
248     public String getWidgetInvariantId() throws XmlArtifactGenerationException {
249         return Widget.createWidget(getWidgetType()).getWidgetId();
250     }
251
252     /**
253      * Populate model identification information.
254      *
255      * @param modelIdentInfo
256      *            the model ident info
257      */
258     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
259         if (modelIdentInfo == null) {
260             return;
261         }
262         Iterator<String> iter = modelIdentInfo.keySet().iterator();
263         String property;
264         while (iter.hasNext()) {
265             property = iter.next();
266             Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
267             if (modelIdent.isPresent()) {
268                 modelIdent.get().populate(this, modelIdentInfo.get(property));
269             }
270         }
271     }
272
273     public void setModelVersion(String modelVersion) {
274         this.modelVersion = modelVersion;
275     }
276
277     public Set<Resource> getResources() {
278         return resources;
279     }
280
281     public Set<Widget> getWidgets() {
282         return widgets;
283     }
284
285     @Override
286     public String toString() {
287         return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";
288     }
289
290 }