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