ea1d4785129cde97a3e673b35751541d4f938602
[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         NAME_AND_DESCRIPTION("providing_service_name") {
71             @Override
72             public void populate(Model model, String value) {
73                 model.modelName = model.modelDescription = value;
74             }
75         };
76
77         private static final Map<String, ModelIdentification> propertyToModelIdent;
78         private String[] keys;
79
80         ModelIdentification(String... keys) {
81             this.keys = keys;
82         }
83
84         static {
85             Map<String, ModelIdentification> mappings = new HashMap<>();
86             for (ModelIdentification ident : ModelIdentification.values()) {
87                 for (String key : ident.keys) {
88                     mappings.put(key, ident);
89                 }
90             }
91             propertyToModelIdent = Collections.unmodifiableMap(mappings);
92         }
93
94         private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
95             return Optional.ofNullable(propertyToModelIdent.get(property));
96         }
97
98         public abstract void populate(Model model, String value);
99     }
100
101     private String modelId; // model-invariant-id
102     private String modelName;
103     private String modelNameVersionId; // model-version-id
104     private String modelVersion;
105     private String modelDescription;
106
107     protected Set<Resource> resources = new HashSet<>();
108     protected Set<Widget> widgets = new HashSet<>();
109
110     /**
111      * Gets the Resource Model corresponding to the supplied TOSCA type.
112      *
113      * @param toscaType
114      *            the tosca type
115      * @return the model for the type, or null
116      */
117     public static Resource getModelFor(String toscaType) {
118         Resource resource = null;
119         if (toscaType != null && !toscaType.isEmpty()) {
120             resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
121         }
122         return resource;
123     }
124
125     private static Resource getModelFromPrefix(String toscaType) {
126         Resource resource = null;
127         int lastSeparator = toscaType.lastIndexOf('.');
128         if (lastSeparator != -1) {
129             resource = getModelFor(toscaType.substring(0, lastSeparator));
130         }
131         return resource;
132     }
133
134     private static Optional<Resource> getModelFromType(String typePrefix) {
135         return WidgetConfigurationUtil.createModelFromType(typePrefix);
136     }
137
138     /**
139      * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
140      * information.
141      *
142      * @param toscaType
143      *            the TOSCA type
144      * @param metaDataType
145      *            the type from the TOSCA metadata
146      * @return the model for the type, or null
147      */
148     public static Resource getModelFor(String toscaType, String metaDataType) {
149         if ("Configuration".equals(metaDataType)) {
150             return new Resource(WidgetType.valueOf("CONFIGURATION"), true);
151         } else if ("CR".equals(metaDataType)) {
152             return new Resource(WidgetType.valueOf("CR"), true);
153         } else {
154             return getModelFor(toscaType);
155         }
156     }
157
158     public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;
159
160     public abstract WidgetType getWidgetType();
161
162     public abstract String getModelTypeName();
163
164     public boolean addResource(Resource resource) {
165         return resources.add(resource);
166     }
167
168     /**
169      * Gets delete flag.
170      *
171      * @return the delete flag
172      */
173     public boolean getDeleteFlag() {
174         return true;
175     }
176
177     public String getModelDescription() {
178         return modelDescription;
179     }
180
181     public String getModelId() {
182         return modelId;
183     }
184
185     public String getModelName() {
186         return modelName;
187     }
188
189     public String getModelVersion() {
190         return modelVersion;
191     }
192
193     public String getModelNameVersionId() {
194         return modelNameVersionId;
195     }
196
197     /**
198      * Gets widget version id.
199      *
200      * @return the widget version id
201      * @throws XmlArtifactGenerationException
202      */
203     public String getWidgetId() throws XmlArtifactGenerationException {
204         return Widget.getWidget(getWidgetType()).getId();
205     }
206
207     /**
208      * Gets invariant id.
209      *
210      * @return the invariant id
211      * @throws XmlArtifactGenerationException
212      */
213     public String getWidgetInvariantId() throws XmlArtifactGenerationException {
214         return Widget.getWidget(getWidgetType()).getWidgetId();
215     }
216
217     /**
218      * Populate model identification information.
219      *
220      * @param modelIdentInfo
221      *            the model ident info
222      */
223     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
224         Iterator<String> iter = modelIdentInfo.keySet().iterator();
225         String property;
226         while (iter.hasNext()) {
227             property = iter.next();
228             Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
229             if (modelIdent.isPresent()) {
230                 modelIdent.get().populate(this, modelIdentInfo.get(property));
231             }
232         }
233     }
234
235     public void setModelVersion(String modelVersion) {
236         this.modelVersion = modelVersion;
237     }
238
239     public Set<Resource> getResources() {
240         return resources;
241     }
242
243     public Set<Widget> getWidgets() {
244         return widgets;
245     }
246
247     @Override
248     public String toString() {
249         return "Model [type=" + getModelTypeName() + ", name=" + getModelName() + "]";
250     }
251
252 }