2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel.xml.generator.model;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.Properties;
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;
35 public class Widget extends Model {
37 public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND =
38 "Cannot generate artifacts. Widget configuration not found for %s";
40 private Set<String> keys = new HashSet<>();
42 protected String name;
43 protected WidgetType type;
44 protected boolean deleteFlag = false;
46 public Widget(WidgetType widgetType, String name, boolean deleteFlag) {
49 this.deleteFlag = deleteFlag;
56 * @throws XmlArtifactGenerationException
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")));
73 * @return a new widget of the specified type
74 * @throws XmlArtifactGenerationException
75 * if there is no configuration defined for the specified type
77 public static Widget getWidget(WidgetType type) throws XmlArtifactGenerationException {
78 Widget widget = WidgetConfigurationUtil.createWidgetFromType(type.toString());
80 throw new XmlArtifactGenerationException("No widget type is defined for " + type);
85 public String getId() {
86 String id = WidgetConfigurationUtil.getConfig()
87 .getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
89 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
90 ArtifactType.AAI.name() + ".model-version-id." + getName()));
95 public ModelType getType() {
96 return ModelType.WIDGET;
99 public String getName() {
104 * Get Widget Id from properties file.
106 * @return - Widget Id
109 public String getWidgetId() {
110 Properties properties = WidgetConfigurationUtil.getConfig();
111 String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + getName());
113 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
114 ArtifactType.AAI.name() + ".model-invariant-id." + getName()));
120 public int hashCode() {
121 return getId().hashCode();
125 public WidgetType getWidgetType() {
130 * Equals method that compares Widget IDs.
133 * the Widget object to compare
134 * @return whether or not obj is equal to this Widget
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);
149 public void addKey(String key) {
154 * Determine whether one or more keys belonging to this Widget appear in the specified Collection.
158 * @return the boolean
160 public boolean memberOf(Collection<String> keys) {
164 return !Collections.disjoint(this.keys, keys);
168 public boolean addResource(Resource resource) {
169 throw new IllegalAccessException(Model.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
173 public boolean addWidget(Widget widget) {
174 if (getWidgetType() == WidgetType.valueOf("VSERVER")) {
175 return widgets.add(widget);
181 public String toString() {
182 return getName() + " Widget keys=" + keys + ", resources=" + resources + ", widgets=" + widgets;
186 public boolean getDeleteFlag() {
191 public String getModelTypeName() {
192 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
196 public String getModelId() {
197 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
201 public String getModelNameVersionId() {
202 throw new IllegalAccessException(GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);