Incorporate the ECOMP SDC Artefact Generator code
[aai/babel.git] / src / main / java / org / onap / aai / babel / xml / generator / model / Widget.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 static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGLPROP_NOT_FOUND;
24
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Properties;
29 import java.util.Set;
30 import org.onap.aai.babel.xml.generator.data.ArtifactType;
31 import org.onap.aai.babel.xml.generator.data.GeneratorConstants;
32 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
33 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
34 import org.onap.aai.babel.xml.generator.types.ModelType;
35 import org.onap.aai.babel.xml.generator.types.ModelWidget;
36
37 public abstract class Widget extends Model {
38
39     private Set<String> keys = new HashSet<>();
40
41     /**
42      * Gets widget.
43      *
44      * @param type the type
45      * @return the widget
46      */
47     public static Widget getWidget(Type type) {
48
49         switch (type) {
50             case SERVICE:
51                 return new ServiceWidget();
52             case VF:
53                 return new VfWidget();
54             case VFC:
55                 return new VfcWidget();
56             case VSERVER:
57                 return new VServerWidget();
58             case VOLUME:
59                 return new VolumeWidget();
60             case FLAVOR:
61                 return new FlavorWidget();
62             case TENANT:
63                 return new TenantWidget();
64             case VOLUME_GROUP:
65                 return new VolumeGroupWidget();
66             case LINT:
67                 return new LIntfWidget();
68             case L3_NET:
69                 return new L3NetworkWidget();
70             case VFMODULE:
71                 return new VfModuleWidget();
72             case IMAGE:
73                 return new ImageWidget();
74             case OAM_NETWORK:
75                 return new OamNetwork();
76             case ALLOTTED_RESOURCE:
77                 return new AllotedResourceWidget();
78             case TUNNEL_XCONNECT:
79                 return new TunnelXconnectWidget();
80             default:
81                 return null;
82         }
83     }
84
85     /**
86      * Gets id.
87      *
88      * @return the id
89      */
90     public String getId() {
91         Properties properties = WidgetConfigurationUtil.getConfig();
92         String id = properties.getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
93         if (id == null) {
94             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
95                     ArtifactType.AAI.name() + ".model-version-id." + getName()));
96         }
97         return id;
98     }
99
100     public ModelType getType() {
101         ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
102         return widgetModel.type();
103     }
104
105     public String getName() {
106         ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
107         return widgetModel.name();
108     }
109
110     /**
111      * Get Widget Id from properties file.
112      *
113      * @return - Widget Id
114      */
115     @Override
116     public String getWidgetId() {
117         Properties properties = WidgetConfigurationUtil.getConfig();
118         String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + getName());
119         if (id == null) {
120             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
121                     ArtifactType.AAI.name() + ".model-invariant-id." + getName()));
122         }
123         return id;
124     }
125
126     @Override
127     public int hashCode() {
128         return getId().hashCode();
129     }
130
131     @Override
132     public Type getWidgetType() {
133         return null;
134     }
135
136     /**
137      * Equals.
138      *
139      * @param obj Object
140      * @return the boolean
141      */
142     @Override
143     public boolean equals(Object obj) {
144         if (obj instanceof Widget) {
145             if (getId().equals(((Widget) obj).getId())) {
146                 ((Widget) obj).keys.addAll(this.keys);
147                 return true;
148             }
149             return false;
150         } else {
151             return false;
152         }
153     }
154
155     public void addKey(String key) {
156         this.keys.add(key);
157     }
158
159     /**
160      * Member of boolean.
161      *
162      * @param keys the keys
163      * @return the boolean
164      */
165     public boolean memberOf(List<String> keys) {
166         if (keys == null) {
167             return false;
168         }
169         return !Collections.disjoint(this.keys, keys);
170     }
171
172     /**
173      * All instances used boolean.
174      *
175      * @param collection the collection
176      * @return the boolean
177      */
178     public boolean allInstancesUsed(Set<String> collection) {
179         Set<String> keyCopy = new HashSet<>(keys);
180         keyCopy.removeAll(collection);
181         return keyCopy.isEmpty();
182     }
183
184     @Override
185     public boolean addResource(Resource resource) {
186         throw new IllegalAccessException(GeneratorConstants.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
187     }
188
189     @Override
190     public boolean addWidget(Widget widget) {
191         return true;
192     }
193
194     public enum Type {
195         SERVICE,
196         VF,
197         VFC,
198         VSERVER,
199         VOLUME,
200         FLAVOR,
201         TENANT,
202         VOLUME_GROUP,
203         LINT,
204         L3_NET,
205         VFMODULE,
206         IMAGE,
207         OAM_NETWORK,
208         ALLOTTED_RESOURCE,
209         TUNNEL_XCONNECT
210     }
211 }