Incorporate the ECOMP SDC Artefact Generator code
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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.HashSet;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Set;
27 import org.onap.aai.babel.xml.generator.data.GeneratorConstants;
28 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
29 import org.onap.aai.babel.xml.generator.types.Cardinality;
30 import org.onap.aai.babel.xml.generator.types.ModelType;
31
32 public abstract class Model {
33
34     protected Set<Resource> resources = new HashSet<>();
35     protected Set<Widget> widgets = new HashSet<>();
36     private String modelId;
37     private String modelName;
38     private String modelVersion;
39     private String modelNameVersionId;
40     private String modelDescription;
41
42     /**
43      * Gets the object (model) corresponding to the supplied TOSCA type.
44      *
45      * @param toscaType the tosca type
46      * @return the model for the type, or null
47      */
48     public static Model getModelFor(String toscaType) {
49         Model modelToBeReturned = null;
50         String typePrefix = toscaType;
51         while (modelToBeReturned == null && typePrefix != null && typePrefix.lastIndexOf('.') != -1) {
52             switch (typePrefix) {
53                 case "org.openecomp.resource.vf.allottedResource":
54                     modelToBeReturned = new AllotedResource();
55                     break;
56                 case "org.openecomp.resource.vfc.AllottedResource":
57                     modelToBeReturned = new ProvidingService();
58                     break;
59                 case "org.openecomp.resource.vfc":
60                     modelToBeReturned = new VServerWidget();
61                     break;
62                 case "org.openecomp.resource.cp":
63                 case "org.openecomp.cp":
64                     modelToBeReturned = new LIntfWidget();
65                     break;
66                 case "org.openecomp.resource.vl":
67                     modelToBeReturned = new L3Network();
68                     break;
69                 case "org.openecomp.resource.vf":
70                     modelToBeReturned = new VirtualFunction();
71                     break;
72                 case "org.openecomp.groups.vfmodule":
73                 case "org.openecomp.groups.VfModule":
74                     modelToBeReturned = new VfModule();
75                     break;
76                 case "org.openecomp.resource.vfc.nodes.heat.cinder":
77                     modelToBeReturned = new VolumeWidget();
78                     break;
79                 default:
80                     modelToBeReturned = null;
81                     break;
82             }
83             typePrefix = typePrefix.substring(0, typePrefix.lastIndexOf('.'));
84         }
85
86         return modelToBeReturned;
87     }
88
89     public abstract boolean addResource(Resource resource);
90
91     public abstract boolean addWidget(Widget resource);
92
93     /**
94      * Gets widget version id.
95      *
96      * @return the widget version id
97      */
98     public String getWidgetId() {
99         org.onap.aai.babel.xml.generator.types.Model model =
100                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
101         return Widget.getWidget(model.widget()).getId();
102     }
103
104     /**
105      * Gets invariant id.
106      *
107      * @return the invariant id
108      */
109     public String getWidgetInvariantId() {
110         org.onap.aai.babel.xml.generator.types.Model model =
111                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
112         return Widget.getWidget(model.widget()).getWidgetId();
113     }
114
115     /**
116      * Gets delete flag.
117      *
118      * @return the delete flag
119      */
120     public boolean getDeleteFlag() {
121         org.onap.aai.babel.xml.generator.types.Model model =
122                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
123         return model.dataDeleteFlag();
124     }
125
126     /**
127      * Gets cardinality.
128      *
129      * @return the cardinality
130      */
131     public Cardinality getCardinality() {
132         org.onap.aai.babel.xml.generator.types.Model model =
133                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
134         return model.cardinality();
135     }
136
137     public abstract Widget.Type getWidgetType();
138
139     public String getModelId() {
140         checkSupported();
141         return modelId;
142     }
143
144     /**
145      * Gets model type.
146      *
147      * @return the model type
148      */
149     public ModelType getModelType() {
150         if (this instanceof Service) {
151             return ModelType.SERVICE;
152         } else if (this instanceof Resource) {
153             return ModelType.RESOURCE;
154         } else if (this instanceof Widget) {
155             return ModelType.WIDGET;
156         } else {
157             return null;
158         }
159     }
160
161     public String getModelName() {
162         return modelName;
163     }
164
165     public String getModelVersion() {
166         return modelVersion;
167     }
168
169     public String getModelNameVersionId() {
170         checkSupported();
171         return modelNameVersionId;
172     }
173
174     public String getModelDescription() {
175         return modelDescription;
176     }
177
178     /**
179      * Populate model identification information.
180      *
181      * @param modelIdentInfo the model ident info
182      */
183     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
184         Iterator<String> iter = modelIdentInfo.keySet().iterator();
185         String property;
186         while (iter.hasNext()) {
187             property = iter.next();
188             switch (property) {
189                 case "vfModuleModelInvariantUUID":
190                 case "serviceInvariantUUID":
191                 case "resourceInvariantUUID":
192                 case "invariantUUID":
193                 case "providing_service_invariant_uuid":
194                     modelId = modelIdentInfo.get(property);
195                     break;
196                 case "vfModuleModelUUID":
197                 case "resourceUUID":
198                 case "serviceUUID":
199                 case "UUID":
200                 case "providing_service_uuid":
201                     modelNameVersionId = modelIdentInfo.get(property);
202                     break;
203                 case "vfModuleModelVersion":
204                 case "serviceVersion":
205                 case "resourceversion":
206                 case "version":
207                     modelVersion = modelIdentInfo.get(property);
208                     break;
209                 case "vfModuleModelName":
210                 case "serviceName":
211                 case "resourceName":
212                 case "name":
213                     modelName = modelIdentInfo.get(property);
214                     break;
215                 case "serviceDescription":
216                 case "resourceDescription":
217                 case "vf_module_description":
218                 case "description":
219                     modelDescription = modelIdentInfo.get(property);
220                     break;
221                 case "providing_service_name":
222                     modelName = modelIdentInfo.get(property);
223                     modelDescription = modelIdentInfo.get(property);
224                     break;
225                 default:
226                     break;
227             }
228         }
229     }
230
231     public void setModelVersion(String modelVersion) {
232         this.modelVersion = modelVersion;
233     }
234
235     public Set<Resource> getResources() {
236         return resources;
237     }
238
239     public Set<Widget> getWidgets() {
240         return widgets;
241     }
242
243     private void checkSupported() {
244         if (this instanceof Widget) {
245             throw new IllegalAccessException(GeneratorConstants.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
246         }
247     }
248 }