aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/babel/xml/generator/model/Model.java
blob: 3f081df2340d92b1bcdde830f41a9da7745bd410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2019 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.babel.xml.generator.model;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
import org.onap.aai.babel.xml.generator.model.Widget.Type;
import org.onap.aai.babel.xml.generator.types.ModelType;

public abstract class Model {

    public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";

    private enum ModelIdentification {
        ID("vfModuleModelInvariantUUID", "serviceInvariantUUID", "resourceInvariantUUID", "invariantUUID",
                "providing_service_invariant_uuid") {
            @Override
            public void populate(Model model, String value) {
                model.modelId = value;
            }
        },
        NAME_VERSION_ID("vfModuleModelUUID", "resourceUUID", "serviceUUID", "UUID", "providing_service_uuid") {
            @Override
            public void populate(Model model, String value) {
                model.modelNameVersionId = value;
            }
        },
        VERSION("vfModuleModelVersion", "serviceVersion", "resourceversion", "version") {
            @Override
            public void populate(Model model, String value) {
                model.modelVersion = value;
            }
        },
        NAME("vfModuleModelName", "serviceName", "resourceName", "name") {
            @Override
            public void populate(Model model, String value) {
                model.modelName = value;
            }
        },
        DESCRIPTION("serviceDescription", "resourceDescription", "vf_module_description", "description") {
            @Override
            public void populate(Model model, String value) {
                model.modelDescription = value;
            }
        },
        NAME_AND_DESCRIPTION("providing_service_name") {
            @Override
            public void populate(Model model, String value) {
                model.modelName = model.modelDescription = value;
            }
        };

        private static final Map<String, ModelIdentification> propertyToModelIdent;
        private String[] keys;

        ModelIdentification(String... keys) {
            this.keys = keys;
        }

        static {
            Map<String, ModelIdentification> mappings = new HashMap<>();
            for (ModelIdentification ident : ModelIdentification.values()) {
                for (String key : ident.keys) {
                    mappings.put(key, ident);
                }
            }
            propertyToModelIdent = Collections.unmodifiableMap(mappings);
        }

        private static Optional<ModelIdentification> getModelIdentFromProperty(String property) {
            return Optional.ofNullable(propertyToModelIdent.get(property));
        }

        public abstract void populate(Model model, String value);
    }

    private String modelId;
    private String modelName;
    private String modelNameVersionId;
    private String modelVersion;
    private String modelDescription;

    protected Set<Resource> resources = new HashSet<>();
    protected Set<Widget> widgets = new HashSet<>();

    /**
     * Gets the Resource Model corresponding to the supplied TOSCA type.
     *
     * @param toscaType
     *            the tosca type
     * @return the model for the type, or null
     */
    public static Resource getModelFor(String toscaType) {
        Resource resource = null;
        if (toscaType != null && !toscaType.isEmpty()) {
            resource = getModelFromType(toscaType).orElseGet(() -> Model.getModelFromPrefix(toscaType));
        }
        return resource;
    }

    private static Resource getModelFromPrefix(String toscaType) {
        Resource resource = null;
        int lastSeparator = toscaType.lastIndexOf('.');
        if (lastSeparator != -1) {
            resource = getModelFor(toscaType.substring(0, lastSeparator));
        }
        return resource;
    }

    private static Optional<Resource> getModelFromType(String typePrefix) {
        return WidgetConfigurationUtil.createModelFromType(typePrefix);
    }

    /**
     * Gets the object (model) corresponding to the supplied TOSCA type information, prioritising the metadata
     * information.
     *
     * @param toscaType
     *            the TOSCA type
     * @param metaDataType
     *            the type from the TOSCA metadata
     * @return the model for the type, or null
     */
    public static Resource getModelFor(String toscaType, String metaDataType) {
        if ("Configuration".equals(metaDataType)) {
            return new Resource(Type.CONFIGURATION, true);
        } else if ("CR".equals(metaDataType)) {
            return new Resource(Type.CR, true);
        } else {
            return getModelFor(toscaType);
        }
    }

    public abstract boolean addResource(Resource resource);

    public abstract boolean addWidget(Widget resource) throws XmlArtifactGenerationException;

    public abstract Widget.Type getWidgetType();

    public abstract Map<String, Object> getProperties();

    public abstract boolean isResource();

    /**
     * Gets delete flag.
     *
     * @return the delete flag
     */
    public boolean getDeleteFlag() {
        return true;
    }

    public String getModelDescription() {
        return modelDescription;
    }

    public String getModelId() {
        checkSupported();
        return modelId;
    }

    public String getModelName() {
        return modelName;
    }

    public String getModelVersion() {
        return modelVersion;
    }

    public String getModelNameVersionId() {
        checkSupported();
        return modelNameVersionId;
    }

    /**
     * Gets model type.
     *
     * @return the model type
     */
    public ModelType getModelType() {
        if (this instanceof Service) {
            return ModelType.SERVICE;
        } else if (this instanceof Resource) {
            return ModelType.RESOURCE;
        } else if (this instanceof Widget) {
            return ModelType.WIDGET;
        } else {
            return null;
        }
    }

    /**
     * Gets widget version id.
     *
     * @return the widget version id
     * @throws XmlArtifactGenerationException
     */
    public String getWidgetId() throws XmlArtifactGenerationException {
        return Widget.getWidget(getWidgetType()).getId();
    }

    /**
     * Gets invariant id.
     *
     * @return the invariant id
     * @throws XmlArtifactGenerationException
     */
    public String getWidgetInvariantId() throws XmlArtifactGenerationException {
        return Widget.getWidget(getWidgetType()).getWidgetId();
    }

    /**
     * Populate model identification information.
     *
     * @param modelIdentInfo
     *            the model ident info
     */
    public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
        Iterator<String> iter = modelIdentInfo.keySet().iterator();
        String property;
        while (iter.hasNext()) {
            property = iter.next();
            Optional<ModelIdentification> modelIdent = ModelIdentification.getModelIdentFromProperty(property);
            if (modelIdent.isPresent()) {
                modelIdent.get().populate(this, modelIdentInfo.get(property));
            }
        }
    }

    public void setModelVersion(String modelVersion) {
        this.modelVersion = modelVersion;
    }

    public Set<Resource> getResources() {
        return resources;
    }

    public Set<Widget> getWidgets() {
        return widgets;
    }

    private void checkSupported() {
        if (this instanceof Widget) {
            throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
        }
    }

}