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