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