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