Add hasWidgetType() helper method
[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  * ================================================================================
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
22 package org.onap.aai.babel.xml.generator.model;
23
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.xml.generator.XmlArtifactGenerationException;
32 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
33
34 public abstract class Model {
35
36     public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
37
38     private enum ModelIdentification {
39         ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
40                 "providing_service_invariant_uuid") {
41             @Override
42             public void populate(Model model, String value) {
43                 model.modelId = value;
44             }
45         },
46         NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
47             @Override
48             public void populate(Model model, String value) {
49                 model.modelNameVersionId = value;
50             }
51         },
52         VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
53             @Override
54             public void populate(Model model, String value) {
55                 model.modelVersion = value;
56             }
57         },
58         NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
59             @Override
60             public void populate(Model model, String value) {
61                 model.modelName = value;
62             }
63         },
64         DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
65             @Override
66             public void populate(Model model, String value) {
67                 model.modelDescription = value;
68             }
69         },
70         NAME_AND_DESCRIPTION("providing_service_name") {
71             @Override
72             public void populate(Model model, String value) {
73                 model.modelName = model.modelDescription = value;
74             }
75         };
76
77         private static final Map<String, ModelIdentification> propertyToModelIdent;
78         private String[] keys;
79
80         ModelIdentification(String... keys) {
81             this.keys = keys;
82         }
83
84         static {
85             Map<String, ModelIdentification> mappings = new HashMap<>();
86             for (ModelIdentification ident : ModelIdentification.values()) {
87                 for (String key : ident.keys) {
88                     mappings.put(key, ident);
89                 }
90             }
91             propertyToModelIdent = Collections.unmodifiableMap(mappings);
92         }
93
94         private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
95             return Optional.ofNullable(propertyToModelIdent.get(property));
96         }
97
98         public abstract void populate(Model model, String value);
99     }
100
101     private String modelId; // model-invariant-id
102     private String modelName;
103     private String modelNameVersionId; // model-version-id
104     private String modelVersion;
105     private String modelDescription;
106
107     protected Set<Resource> resources = new HashSet<>();
108     protected Set<Widget> widgets = new HashSet<>();
109
110     /**
111      * Gets the Resource Model corresponding to the supplied TOSCA type.
112      *
113      * @param toscaType
114      *            the tosca type
115      * @return the model for the type, or null
116      */
117     public static Resource getModelFor(String toscaType) {
118         Resource resource = null;
119         if (toscaType != null && !toscaType.isEmpty()) {
120             resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
121         }
122         return resource;
123     }
124
125     private static Resource getModelFromPrefix(String toscaType) {
126         Resource resource = null;
127         int lastSeparator = toscaType.lastIndexOf('.');
128         if (lastSeparator != -1) {
129             resource = getModelFor(toscaType.substring(0, lastSeparator));
130         }
131         return resource;
132     }
133
134     private static Optional<Resource> getModelFromType(String typePrefix) {
135         return WidgetConfigurationUtil.createModelFromType(typePrefix);
136     }
137
138     /**
139      * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
140      * information.
141      *
142      * @param toscaType
143      *            the TOSCA type
144      * @param metaDataType
145      *            the type from the TOSCA metadata
146      * @return the model for the type, or null
147      */
148     public static Resource getModelFor(String toscaType, String metaDataType) {
149         if ("Configuration".equals(metaDataType)) {
150             return new Resource(WidgetType.valueOf("CONFIGURATION"), true);
151         } else if ("CR".equals(metaDataType)) {
152             return new Resource(WidgetType.valueOf("CR"), true);
153         } else {
154             return getModelFor(toscaType);
155         }
156     }
157
158     public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
159
160     /**
161      * @return the Widget Type of this model.
162      */
163     public abstract WidgetType getWidgetType();
164
165     public abstract String getModelTypeName();
166
167     /**
168      * Check whether the model's Widget Type matches the supplied type.
169      *
170      * @param type
171      *            the Widget Type to compare
172      * @return true if the Widget Type of this model matches the supplied type
173      */
174     public boolean hasWidgetType(String type) {
175         return getWidgetType() == WidgetType.valueOf(type);
176     }
177
178     public boolean addResource(Resource resource) {
179         return resources.add(resource);
180     }
181
182     /**
183      * Gets delete flag.
184      *
185      * @return the delete flag
186      */
187     public boolean getDeleteFlag() {
188         return true;
189     }
190
191     public String getModelDescription() {
192         return modelDescription;
193     }
194
195     public String getModelId() {
196         return modelId;
197     }
198
199     public String getModelName() {
200         return modelName;
201     }
202
203     public String getModelVersion() {
204         return modelVersion;
205     }
206
207     public String getModelNameVersionId() {
208         return modelNameVersionId;
209     }
210
211     /**
212      * Gets widget version id.
213      *
214      * @return the widget version id
215      * @throws XmlArtifactGenerationException
216      */
217     public String getWidgetId() throws XmlArtifactGenerationException {
218         return Widget.getWidget(getWidgetType()).getId();
219     }
220
221     /**
222      * Gets invariant id.
223      *
224      * @return the invariant id
225      * @throws XmlArtifactGenerationException
226      */
227     public String getWidgetInvariantId() throws XmlArtifactGenerationException {
228         return Widget.getWidget(getWidgetType()).getWidgetId();
229     }
230
231     /**
232      * Populate model identification information.
233      *
234      * @param modelIdentInfo
235      *            the model ident info
236      */
237     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
238         if (modelIdentInfo == null) {
239             return;
240         }
241         Iterator<String> iter = modelIdentInfo.keySet().iterator();
242         String property;
243         while (iter.hasNext()) {
244             property = iter.next();
245             Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
246             if (modelIdent.isPresent()) {
247                 modelIdent.get().populate(this, modelIdentInfo.get(property));
248             }
249         }
250     }
251
252     public void setModelVersion(String modelVersion) {
253         this.modelVersion = modelVersion;
254     }
255
256     public Set<Resource> getResources() {
257         return resources;
258     }
259
260     public Set<Widget> getWidgets() {
261         return widgets;
262     }
263
264     @Override
265     public String toString() {
266         return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";
267     }
268
269 }