Re-implement model type value for Resource Mapping
[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.model.Widget.Type;
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 Resource 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 addWidget(Widget resource) throws XmlArtifactGenerationException;
160
161     public abstract Widget.Type getWidgetType();
162
163     public abstract String getModelTypeName();
164
165     public boolean addResource(Resource resource) {
166         return resources.add(resource);
167     }
168
169     /**
170      * Gets delete flag.
171      *
172      * @return the delete flag
173      */
174     public boolean getDeleteFlag() {
175         return true;
176     }
177
178     public String getModelDescription() {
179         return modelDescription;
180     }
181
182     public String getModelId() {
183         return modelId;
184     }
185
186     public String getModelName() {
187         return modelName;
188     }
189
190     public String getModelVersion() {
191         return modelVersion;
192     }
193
194     public String getModelNameVersionId() {
195         return modelNameVersionId;
196     }
197
198     /**
199      * Gets widget version id.
200      *
201      * @return the widget version id
202      * @throws XmlArtifactGenerationException
203      */
204     public String getWidgetId() throws XmlArtifactGenerationException {
205         return Widget.getWidget(getWidgetType()).getId();
206     }
207
208     /**
209      * Gets invariant id.
210      *
211      * @return the invariant id
212      * @throws XmlArtifactGenerationException
213      */
214     public String getWidgetInvariantId() throws XmlArtifactGenerationException {
215         return Widget.getWidget(getWidgetType()).getWidgetId();
216     }
217
218     /**
219      * Populate model identification information.
220      *
221      * @param modelIdentInfo
222      *            the model ident info
223      */
224     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
225         Iterator<String> iter = modelIdentInfo.keySet().iterator();
226         String property;
227         while (iter.hasNext()) {
228             property = iter.next();
229             Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
230             if (modelIdent.isPresent()) {
231                 modelIdent.get().populate(this, modelIdentInfo.get(property));
232             }
233         }
234     }
235
236     public void setModelVersion(String modelVersion) {
237         this.modelVersion = modelVersion;
238     }
239
240     public Set<Resource> getResources() {
241         return resources;
242     }
243
244     public Set<Widget> getWidgets() {
245         return widgets;
246     }
247
248     @Override
249     public String toString() {
250         return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";
251     }
252
253 }