Move Widget attribute data to the mappings JSON
[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-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 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 package org.onap.aai.babel.xml.generator.model;
22
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Map;
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     public enum Type {
41         SERVICE, VF, VFC, VSERVER, VOLUME, FLAVOR, TENANT, VOLUME_GROUP, LINT, L3_NET, VFMODULE, IMAGE, OAM_NETWORK, ALLOTTED_RESOURCE, TUNNEL_XCONNECT, CONFIGURATION, CR, INSTANCE_GROUP;
42     }
43
44     private Set<String> keys = new HashSet<>();
45
46     protected String name;
47     protected Type type;
48     protected boolean deleteFlag = false;
49
50     public Widget(Type widgetType, String name, boolean deleteFlag) {
51         type = widgetType;
52         this.name = name;
53         this.deleteFlag = deleteFlag;
54     }
55
56     /**
57      * Copy Constructor
58      * 
59      * @param baseWidget
60      * @throws XmlArtifactGenerationException 
61      */
62     public Widget(Widget baseWidget) throws XmlArtifactGenerationException {
63         this(baseWidget.getWidgetType(), baseWidget.getName(), baseWidget.getDeleteFlag());
64         if (type == Type.VSERVER) {
65             widgets.add(getWidget(Type.FLAVOR));
66             widgets.add(getWidget(Type.IMAGE));
67             widgets.add(getWidget(Type.TENANT));
68             widgets.add(getWidget(Type.VFC));
69         }
70     }
71
72     /**
73      * Gets widget.
74      *
75      * @param type
76      *            the type
77      * @return a new widget of the specified type
78      * @throws XmlArtifactGenerationException 
79      */
80     public static Widget getWidget(Type 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     @Override
89     public boolean isResource() {
90         return false;
91     }
92
93     public String getId() {
94         String id = WidgetConfigurationUtil.getConfig()
95                 .getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
96         if (id == null) {
97             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
98                     ArtifactType.AAI.name() + ".model-version-id." + getName()));
99         }
100         return id;
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         Properties properties = WidgetConfigurationUtil.getConfig();
119         String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + getName());
120         if (id == null) {
121             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
122                     ArtifactType.AAI.name() + ".model-invariant-id." + getName()));
123         }
124         return id;
125     }
126
127     @Override
128     public int hashCode() {
129         return getId().hashCode();
130     }
131
132     @Override
133     public Type getWidgetType() {
134         return type;
135     }
136
137     /**
138      * Equals method that compares Widget IDs.
139      *
140      * @param obj
141      *            the Widget object to compare
142      * @return whether or not obj is equal to this Widget
143      */
144     @Override
145     public boolean equals(Object obj) {
146         boolean isEqual = false;
147         if (obj instanceof Widget) {
148             Widget other = (Widget) obj;
149             if (getId().equals(other.getId())) {
150                 other.keys.addAll(this.keys);
151                 isEqual = true;
152             }
153         }
154         return isEqual;
155     }
156
157     public void addKey(String key) {
158         this.keys.add(key);
159     }
160
161     /**
162      * Determine whether one or more keys belonging to this Widget appear in the specified Collection.
163      *
164      * @param keys
165      *            the keys
166      * @return the boolean
167      */
168     public boolean memberOf(Collection<String> keys) {
169         if (keys == null) {
170             return false;
171         }
172         return !Collections.disjoint(this.keys, keys);
173     }
174
175     @Override
176     public boolean addResource(Resource resource) {
177         throw new IllegalAccessException(Model.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
178     }
179
180     @Override
181     public boolean addWidget(Widget widget) {
182         if (getWidgetType() == Type.VSERVER) {
183             return widgets.add(widget);
184         }
185         return true;
186     }
187
188     @Override
189     public Map<String, Object> getProperties() {
190         return Collections.emptyMap();
191     }
192
193     @Override
194     public String toString() {
195         return getName() + " Widget keys=" + keys + ", resources=" + resources + ", widgets=" + widgets;
196     }
197
198     @Override
199     public boolean getDeleteFlag() {
200         return deleteFlag;
201     }
202 }