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