Replace Resource sub-classes with configuration
[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-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2019 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.lang.reflect.InvocationTargetException;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.EnumMap;
27 import java.util.HashSet;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.Set;
31 import org.onap.aai.babel.logging.ApplicationMsgs;
32 import org.onap.aai.babel.logging.LogHelper;
33 import org.onap.aai.babel.xml.generator.data.ArtifactType;
34 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
35 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
36 import org.onap.aai.babel.xml.generator.types.ModelType;
37 import org.onap.aai.babel.xml.generator.types.ModelWidget;
38 import org.onap.aai.cl.api.Logger;
39
40 public abstract class Widget extends Model {
41
42     public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND =
43             "Cannot generate artifacts. Widget configuration not found for %s";
44
45     public enum Type {
46         SERVICE, VF, VFC, VSERVER, VOLUME, FLAVOR, TENANT, VOLUME_GROUP, LINT, L3_NET, VFMODULE, IMAGE, OAM_NETWORK, ALLOTTED_RESOURCE, TUNNEL_XCONNECT, CONFIGURATION, CR, INSTANCE_GROUP;
47     }
48
49     private static Logger log = LogHelper.INSTANCE;
50
51     private Set<String> keys = new HashSet<>();
52
53     private static EnumMap<Widget.Type, Class<? extends Widget>> typeToWidget = new EnumMap<>(Widget.Type.class);
54     static {
55         typeToWidget.put(Type.SERVICE, ServiceWidget.class);
56         typeToWidget.put(Type.VF, VfWidget.class);
57         typeToWidget.put(Type.VFC, VfcWidget.class);
58         typeToWidget.put(Type.VSERVER, VServerWidget.class);
59         typeToWidget.put(Type.VOLUME, VolumeWidget.class);
60         typeToWidget.put(Type.FLAVOR, FlavorWidget.class);
61         typeToWidget.put(Type.TENANT, TenantWidget.class);
62         typeToWidget.put(Type.VOLUME_GROUP, VolumeGroupWidget.class);
63         typeToWidget.put(Type.LINT, LIntfWidget.class);
64         typeToWidget.put(Type.L3_NET, L3NetworkWidget.class);
65         typeToWidget.put(Type.VFMODULE, VfModuleWidget.class);
66         typeToWidget.put(Type.IMAGE, ImageWidget.class);
67         typeToWidget.put(Type.OAM_NETWORK, OamNetwork.class);
68         typeToWidget.put(Type.ALLOTTED_RESOURCE, AllotedResourceWidget.class);
69         typeToWidget.put(Type.TUNNEL_XCONNECT, TunnelXconnectWidget.class);
70         typeToWidget.put(Type.CONFIGURATION, ConfigurationWidget.class);
71         typeToWidget.put(Type.CR, CRWidget.class);
72         typeToWidget.put(Type.INSTANCE_GROUP, InstanceGroupWidget.class);
73     }
74
75     /**
76      * Gets widget.
77      *
78      * @param type
79      *            the type
80      * @return the widget
81      */
82     public static Widget getWidget(Type type) {
83         Widget widget = null;
84         Class<? extends Widget> clazz = typeToWidget.get(type);
85         if (clazz != null) {
86             try {
87                 widget = clazz.getConstructor().newInstance();
88             } catch (InstantiationException | java.lang.IllegalAccessException | IllegalArgumentException
89                     | InvocationTargetException | NoSuchMethodException | SecurityException e) {
90                 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
91             }
92         }
93         return widget;
94     }
95
96     @Override
97     public boolean isResource() {
98         return false;
99     }
100
101     public String getId() {
102         String id = WidgetConfigurationUtil.getConfig()
103                 .getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
104         if (id == null) {
105             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
106                     ArtifactType.AAI.name() + ".model-version-id." + getName()));
107         }
108         return id;
109     }
110
111     public ModelType getType() {
112         ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
113         return widgetModel.type();
114     }
115
116     public String getName() {
117         ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
118         return widgetModel.name();
119     }
120
121     /**
122      * Get Widget Id from properties file.
123      *
124      * @return - Widget Id
125      */
126     @Override
127     public String getWidgetId() {
128         Properties properties = WidgetConfigurationUtil.getConfig();
129         String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + getName());
130         if (id == null) {
131             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
132                     ArtifactType.AAI.name() + ".model-invariant-id." + getName()));
133         }
134         return id;
135     }
136
137     @Override
138     public int hashCode() {
139         return getId().hashCode();
140     }
141
142     @Override
143     public Type getWidgetType() {
144         return null;
145     }
146
147     /**
148      * Equals method that compares Widget IDs.
149      *
150      * @param obj
151      *            the Widget object to compare
152      * @return whether or not obj is equal to this Widget
153      */
154     @Override
155     public boolean equals(Object obj) {
156         boolean isEqual = false;
157         if (obj instanceof Widget) {
158             Widget other = (Widget) obj;
159             if (getId().equals(other.getId())) {
160                 other.keys.addAll(this.keys);
161                 isEqual = true;
162             }
163         }
164         return isEqual;
165     }
166
167     public void addKey(String key) {
168         this.keys.add(key);
169     }
170
171     /**
172      * Determine whether one or more keys belonging to this Widget appear in the specified Collection.
173      *
174      * @param keys
175      *            the keys
176      * @return the boolean
177      */
178     public boolean memberOf(Collection<String> keys) {
179         if (keys == null) {
180             return false;
181         }
182         return !Collections.disjoint(this.keys, keys);
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     @Override
196     public Map<String, Object> getProperties() {
197         return Collections.emptyMap();
198     }
199 }