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