dcd5d15cb07f61c68d32463f6666ff76461c486e
[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.error.IllegalAccessException;
28 import org.onap.aai.babel.xml.generator.types.Cardinality;
29 import org.onap.aai.babel.xml.generator.types.ModelType;
30
31 public abstract class Model {
32     public static final String GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION = "Operation Not Supported for Widgets";
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                 case "org.openecomp.nodes.PortMirroringConfiguration":
80                     modelToBeReturned = new Configuration();
81                     break;
82                 default:
83                     modelToBeReturned = null;
84                     break;
85             }
86             typePrefix = typePrefix.substring(0, typePrefix.lastIndexOf('.'));
87         }
88
89         return modelToBeReturned;
90     }
91
92     public abstract boolean addResource(Resource resource);
93
94     public abstract boolean addWidget(Widget resource);
95
96     /**
97      * Gets widget version id.
98      *
99      * @return the widget version id
100      */
101     public String getWidgetId() {
102         org.onap.aai.babel.xml.generator.types.Model model =
103                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
104         return Widget.getWidget(model.widget()).getId();
105     }
106
107     /**
108      * Gets invariant id.
109      *
110      * @return the invariant id
111      */
112     public String getWidgetInvariantId() {
113         org.onap.aai.babel.xml.generator.types.Model model =
114                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
115         return Widget.getWidget(model.widget()).getWidgetId();
116     }
117
118     /**
119      * Gets delete flag.
120      *
121      * @return the delete flag
122      */
123     public boolean getDeleteFlag() {
124         org.onap.aai.babel.xml.generator.types.Model model =
125                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
126         return model.dataDeleteFlag();
127     }
128
129     /**
130      * Gets cardinality.
131      *
132      * @return the cardinality
133      */
134     public Cardinality getCardinality() {
135         org.onap.aai.babel.xml.generator.types.Model model =
136                 this.getClass().getAnnotation(org.onap.aai.babel.xml.generator.types.Model.class);
137         return model.cardinality();
138     }
139
140     public abstract Widget.Type getWidgetType();
141
142     public String getModelId() {
143         checkSupported();
144         return modelId;
145     }
146
147     /**
148      * Gets model type.
149      *
150      * @return the model type
151      */
152     public ModelType getModelType() {
153         if (this instanceof Service) {
154             return ModelType.SERVICE;
155         } else if (this instanceof Resource) {
156             return ModelType.RESOURCE;
157         } else if (this instanceof Widget) {
158             return ModelType.WIDGET;
159         } else {
160             return null;
161         }
162     }
163
164     public String getModelName() {
165         return modelName;
166     }
167
168     public String getModelVersion() {
169         return modelVersion;
170     }
171
172     public String getModelNameVersionId() {
173         checkSupported();
174         return modelNameVersionId;
175     }
176
177     public String getModelDescription() {
178         return modelDescription;
179     }
180
181     /**
182      * Populate model identification information.
183      *
184      * @param modelIdentInfo the model ident info
185      */
186     public void populateModelIdentificationInformation(Map<String, String> modelIdentInfo) {
187         Iterator<String> iter = modelIdentInfo.keySet().iterator();
188         String property;
189         while (iter.hasNext()) {
190             property = iter.next();
191             switch (property) {
192                 case "vfModuleModelInvariantUUID":
193                 case "serviceInvariantUUID":
194                 case "resourceInvariantUUID":
195                 case "invariantUUID":
196                 case "providing_service_invariant_uuid":
197                     modelId = modelIdentInfo.get(property);
198                     break;
199                 case "vfModuleModelUUID":
200                 case "resourceUUID":
201                 case "serviceUUID":
202                 case "UUID":
203                 case "providing_service_uuid":
204                     modelNameVersionId = modelIdentInfo.get(property);
205                     break;
206                 case "vfModuleModelVersion":
207                 case "serviceVersion":
208                 case "resourceversion":
209                 case "version":
210                     modelVersion = modelIdentInfo.get(property);
211                     break;
212                 case "vfModuleModelName":
213                 case "serviceName":
214                 case "resourceName":
215                 case "name":
216                     modelName = modelIdentInfo.get(property);
217                     break;
218                 case "serviceDescription":
219                 case "resourceDescription":
220                 case "vf_module_description":
221                 case "description":
222                     modelDescription = modelIdentInfo.get(property);
223                     break;
224                 case "providing_service_name":
225                     modelName = modelIdentInfo.get(property);
226                     modelDescription = modelIdentInfo.get(property);
227                     break;
228                 default:
229                     break;
230             }
231         }
232     }
233
234     public void setModelVersion(String modelVersion) {
235         this.modelVersion = modelVersion;
236     }
237
238     public Set<Resource> getResources() {
239         return resources;
240     }
241
242     public Set<Widget> getWidgets() {
243         return widgets;
244     }
245
246     private void checkSupported() {
247         if (this instanceof Widget) {
248             throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
249         }
250     }
251 }