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