Reimplement Widget.Type enum 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 (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 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
22 package org.onap.aai.babel.xml.generator.model;
23
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.Properties;
28 import java.util.Set;
29 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
30 import org.onap.aai.babel.xml.generator.data.ArtifactType;
31 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
32 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
33 import org.onap.aai.babel.xml.generator.types.ModelType;
34
35 public class Widget extends Model {
36
37     public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND =
38             "Cannot generate artifacts. Widget configuration not found for %s";
39
40     private Set<String> keys = new HashSet<>();
41
42     protected String name;
43     protected WidgetType type;
44     protected boolean deleteFlag = false;
45
46     public Widget(WidgetType widgetType, String name, boolean deleteFlag) {
47         type = widgetType;
48         this.name = name;
49         this.deleteFlag = deleteFlag;
50     }
51
52     /**
53      * Copy Constructor.
54      * 
55      * @param baseWidget
56      * @throws XmlArtifactGenerationException
57      */
58     public Widget(Widget baseWidget) throws XmlArtifactGenerationException {
59         this(baseWidget.getWidgetType(), baseWidget.getName(), baseWidget.getDeleteFlag());
60         if (type == WidgetType.valueOf("VSERVER")) {
61             widgets.add(getWidget(WidgetType.valueOf("FLAVOR")));
62             widgets.add(getWidget(WidgetType.valueOf("IMAGE")));
63             widgets.add(getWidget(WidgetType.valueOf("TENANT")));
64             widgets.add(getWidget(WidgetType.valueOf("VFC")));
65         }
66     }
67
68     /**
69      * Gets widget.
70      *
71      * @param typeString
72      * 
73      * @return a new widget of the specified type
74      * @throws XmlArtifactGenerationException
75      *             if there is no configuration defined for the specified type
76      */
77     public static Widget getWidget(WidgetType type) throws XmlArtifactGenerationException {
78         Widget widget = WidgetConfigurationUtil.createWidgetFromType(type.toString());
79         if (widget == null) {
80             throw new XmlArtifactGenerationException("No widget type is defined for " + type);
81         }
82         return widget;
83     }
84
85     public String getId() {
86         String id = WidgetConfigurationUtil.getConfig()
87                 .getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
88         if (id == null) {
89             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
90                     ArtifactType.AAI.name() + ".model-version-id." + getName()));
91         }
92         return id;
93     }
94
95     public ModelType getType() {
96         return ModelType.WIDGET;
97     }
98
99     public String getName() {
100         return name;
101     }
102
103     /**
104      * Get Widget Id from properties file.
105      *
106      * @return - Widget Id
107      */
108     @Override
109     public String getWidgetId() {
110         Properties properties = WidgetConfigurationUtil.getConfig();
111         String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + getName());
112         if (id == null) {
113             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
114                     ArtifactType.AAI.name() + ".model-invariant-id." + getName()));
115         }
116         return id;
117     }
118
119     @Override
120     public int hashCode() {
121         return getId().hashCode();
122     }
123
124     @Override
125     public WidgetType getWidgetType() {
126         return type;
127     }
128
129     /**
130      * Equals method that compares Widget IDs.
131      *
132      * @param obj
133      *            the Widget object to compare
134      * @return whether or not obj is equal to this Widget
135      */
136     @Override
137     public boolean equals(Object obj) {
138         boolean isEqual = false;
139         if (obj instanceof Widget) {
140             Widget other = (Widget) obj;
141             if (getId().equals(other.getId())) {
142                 other.keys.addAll(this.keys);
143                 isEqual = true;
144             }
145         }
146         return isEqual;
147     }
148
149     public void addKey(String key) {
150         this.keys.add(key);
151     }
152
153     /**
154      * Determine whether one or more keys belonging to this Widget appear in the specified Collection.
155      *
156      * @param keys
157      *            the keys
158      * @return the boolean
159      */
160     public boolean memberOf(Collection<String> keys) {
161         if (keys == null) {
162             return false;
163         }
164         return !Collections.disjoint(this.keys, keys);
165     }
166
167     @Override
168     public boolean addResource(Resource resource) {
169         throw new IllegalAccessException(Model.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
170     }
171
172     @Override
173     public boolean addWidget(Widget widget) {
174         if (getWidgetType() == WidgetType.valueOf("VSERVER")) {
175             return widgets.add(widget);
176         }
177         return true;
178     }
179
180     @Override
181     public String toString() {
182         return getName() + " Widget keys=" + keys + ", resources=" + resources + ", widgets=" + widgets;
183     }
184
185     @Override
186     public boolean getDeleteFlag() {
187         return deleteFlag;
188     }
189
190     @Override
191     public String getModelTypeName() {
192         throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
193     }
194
195     @Override
196     public String getModelId() {
197         throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
198     }
199
200     @Override
201     public String getModelNameVersionId() {
202         throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
203     }
204
205 }