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