d78e2e67c95657c46b52d3dc54c85f867fa7fd87
[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 =
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         Properties properties = WidgetConfigurationUtil.getConfig();
113         String id = properties.getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
114         if (id == null) {
115             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
116                     ArtifactType.AAI.name() + ".model-version-id." + getName()));
117         }
118         return id;
119     }
120
121     public ModelType getType() {
122         ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
123         return widgetModel.type();
124     }
125
126     public String getName() {
127         ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
128         return widgetModel.name();
129     }
130
131     /**
132      * Get Widget Id from properties file.
133      *
134      * @return - Widget Id
135      */
136     @Override
137     public String getWidgetId() {
138         Properties properties = WidgetConfigurationUtil.getConfig();
139         String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + getName());
140         if (id == null) {
141             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
142                     ArtifactType.AAI.name() + ".model-invariant-id." + getName()));
143         }
144         return id;
145     }
146
147     @Override
148     public int hashCode() {
149         return getId().hashCode();
150     }
151
152     @Override
153     public Type getWidgetType() {
154         return null;
155     }
156
157     /**
158      * Equals method that compares Widget IDs.
159      *
160      * @param obj the Widget object to compare
161      * @return whether or not obj is equal to this Widget
162      */
163     @Override
164     public boolean equals(Object obj) {
165         boolean isEqual = false;
166         if (obj instanceof Widget) {
167             Widget other = (Widget) obj;
168             if (getId().equals(other.getId())) {
169                 other.keys.addAll(this.keys);
170                 isEqual = true;
171             }
172         }
173         return isEqual;
174     }
175
176     public void addKey(String key) {
177         this.keys.add(key);
178     }
179
180     /**
181      * Determine whether one or more keys belonging to this Widget appear in the specified Collection.
182      *
183      * @param keys the keys
184      * @return the boolean
185      */
186     public boolean memberOf(Collection<String> keys) {
187         if (keys == null) {
188             return false;
189         }
190         return !Collections.disjoint(this.keys, keys);
191     }
192
193     @Override
194     public boolean addResource(Resource resource) {
195         throw new IllegalAccessException(Model.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
196     }
197
198     @Override
199     public boolean addWidget(Widget widget) {
200         return true;
201     }
202 }