Replace Resource sub-classes with configuration
[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.data.WidgetConfigurationUtil;
31 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
32 import org.onap.aai.babel.xml.generator.model.Widget.Type;
33 import org.onap.aai.babel.xml.generator.types.ModelType;
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         DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
66             @Override
67             public void populate(Model model, String value) {
68                 model.modelDescription = value;
69             }
70         },
71         NAME_AND_DESCRIPTION("providing_service_name") {
72             @Override
73             public void populate(Model model, String value) {
74                 model.modelName = model.modelDescription = value;
75             }
76         };
77
78         private static final Map<String, ModelIdentification> propertyToModelIdent;
79         private String[] keys;
80
81         ModelIdentification(String... keys) {
82             this.keys = keys;
83         }
84
85         static {
86             Map<String, ModelIdentification> mappings = new HashMap<>();
87             for (ModelIdentification ident : ModelIdentification.values()) {
88                 for (String key : ident.keys) {
89                     mappings.put(key, ident);
90                 }
91             }
92             propertyToModelIdent = Collections.unmodifiableMap(mappings);
93         }
94
95         private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
96             return Optional.ofNullable(propertyToModelIdent.get(property));
97         }
98
99         public abstract void populate(Model model, String value);
100     }
101
102     private String modelId;
103     private String modelName;
104     private String modelNameVersionId;
105     private String modelVersion;
106     private String modelDescription;
107
108     protected Set<Resource> resources = new HashSet<>();
109     protected Set<Widget> widgets = new HashSet<>();
110
111     /**
112      * Gets the object (model) corresponding to the supplied TOSCA type.
113      *
114      * @param toscaType
115      *     the tosca type
116      * @return the model for the type, or null
117      */
118     public static Resource getModelFor(String toscaType) {
119         Resource resource = null;
120         if (toscaType != null && !toscaType.isEmpty()) {
121             resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
122         }
123         return resource;
124     }
125
126     private static Resource getModelFromPrefix(String toscaType) {
127         Resource resource = null;
128         int lastSeparator = toscaType.lastIndexOf('.');
129         if (lastSeparator != -1) {
130             resource = getModelFor(toscaType.substring(0, lastSeparator));
131         }
132         return resource;
133     }
134
135     private static Optional<Resource> getModelFromType(String typePrefix) {
136         return WidgetConfigurationUtil.createModelFromType(typePrefix);
137     }
138
139     /**
140      * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
141      * information.
142      *
143      * @param toscaType
144      *     the TOSCA type
145      * @param metaDataType
146      *     the type from the TOSCA metadata
147      * @return the model for the type, or null
148      */
149     public static Resource getModelFor(String toscaType, String metaDataType) {
150         if ("Configuration".equals(metaDataType)) {
151             return new Resource(Type.CONFIGURATION, true);
152         } else if ("CR".equals(metaDataType)) {
153             return new Resource(Type.CR, true);
154         } else {
155             return getModelFor(toscaType);
156         }
157     }
158
159     public abstract boolean addResource(Resource resource);
160
161     public abstract boolean addWidget(Widget resource);
162
163     public abstract Widget.Type getWidgetType();
164
165     public abstract Map<String, Object> getProperties();
166
167     public abstract boolean isResource();
168
169     /**
170      * Gets delete flag.
171      *
172      * @return the delete flag
173      */
174     public boolean getDeleteFlag() {
175         org.onap.aai.babel.xml.generator.types.Model model =
176                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
177         return model.dataDeleteFlag();
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      */
224     public String getWidgetId() {
225         org.onap.aai.babel.xml.generator.types.Model model =
226                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
227         return Widget.getWidget(model.widget()).getId();
228     }
229
230     /**
231      * Gets invariant id.
232      *
233      * @return the invariant id
234      */
235     public String getWidgetInvariantId() {
236         org.onap.aai.babel.xml.generator.types.Model model =
237                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
238         return Widget.getWidget(model.widget()).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         Iterator<String> iter = modelIdentInfo.keySet().iterator();
249         String property;
250         while (iter.hasNext()) {
251             property = iter.next();
252             Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
253             if (modelIdent.isPresent()) {
254                 modelIdent.get().populate(this, modelIdentInfo.get(property));
255             }
256         }
257     }
258
259     public void setModelVersion(String modelVersion) {
260         this.modelVersion = modelVersion;
261     }
262
263     public Set<Resource> getResources() {
264         return resources;
265     }
266
267     public Set<Widget> getWidgets() {
268         return widgets;
269     }
270
271     private void checkSupported() {
272         if (this instanceof Widget) {
273             throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
274         }
275     }
276
277 }