2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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=========================================================
21 package org.onap.aai.babel.xml.generator.model;
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.EnumMap;
27 import java.util.HashSet;
28 import java.util.Properties;
30 import org.onap.aai.babel.logging.ApplicationMsgs;
31 import org.onap.aai.babel.logging.LogHelper;
32 import org.onap.aai.babel.xml.generator.data.ArtifactType;
33 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
34 import org.onap.aai.babel.xml.generator.error.IllegalAccessException;
35 import org.onap.aai.babel.xml.generator.types.ModelType;
36 import org.onap.aai.babel.xml.generator.types.ModelWidget;
37 import org.onap.aai.cl.api.Logger;
39 public abstract class Widget extends Model {
41 public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND =
42 "Cannot generate artifacts. Widget configuration not found for %s";
65 private static Logger log = LogHelper.INSTANCE;
67 private Set<String> keys = new HashSet<>();
69 private static EnumMap<Widget.Type, Class<? extends Widget>> typeToWidget = new EnumMap<>(Widget.Type.class);
71 typeToWidget.put(Type.SERVICE, ServiceWidget.class);
72 typeToWidget.put(Type.VF, VfWidget.class);
73 typeToWidget.put(Type.VFC, VfcWidget.class);
74 typeToWidget.put(Type.VSERVER, VServerWidget.class);
75 typeToWidget.put(Type.VOLUME, VolumeWidget.class);
76 typeToWidget.put(Type.FLAVOR, FlavorWidget.class);
77 typeToWidget.put(Type.TENANT, TenantWidget.class);
78 typeToWidget.put(Type.VOLUME_GROUP, VolumeGroupWidget.class);
79 typeToWidget.put(Type.LINT, LIntfWidget.class);
80 typeToWidget.put(Type.L3_NET, L3NetworkWidget.class);
81 typeToWidget.put(Type.VFMODULE, VfModuleWidget.class);
82 typeToWidget.put(Type.IMAGE, ImageWidget.class);
83 typeToWidget.put(Type.OAM_NETWORK, OamNetwork.class);
84 typeToWidget.put(Type.ALLOTTED_RESOURCE, AllotedResourceWidget.class);
85 typeToWidget.put(Type.TUNNEL_XCONNECT, TunnelXconnectWidget.class);
86 typeToWidget.put(Type.CONFIGURATION, ConfigurationWidget.class);
87 typeToWidget.put(Type.CR, CRWidget.class);
88 typeToWidget.put(Type.INSTANCE_GROUP, InstanceGroupWidget.class);
94 * @param type the type
97 public static Widget getWidget(Type type) {
99 Class<? extends Widget> clazz = typeToWidget.get(type);
102 widget = clazz.getConstructor().newInstance();
103 } catch (InstantiationException | java.lang.IllegalAccessException | IllegalArgumentException
104 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
105 log.error(ApplicationMsgs.INVALID_CSAR_FILE, e);
111 public String getId() {
112 Properties properties = WidgetConfigurationUtil.getConfig();
113 String id = properties.getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
115 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
116 ArtifactType.AAI.name() + ".model-version-id." + getName()));
121 public ModelType getType() {
122 ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
123 return widgetModel.type();
126 public String getName() {
127 ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
128 return widgetModel.name();
132 * Get Widget Id from properties file.
134 * @return - Widget Id
137 public String getWidgetId() {
138 Properties properties = WidgetConfigurationUtil.getConfig();
139 String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + getName());
141 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
142 ArtifactType.AAI.name() + ".model-invariant-id." + getName()));
148 public int hashCode() {
149 return getId().hashCode();
153 public Type getWidgetType() {
158 * Equals method that compares Widget IDs.
160 * @param obj the Widget object to compare
161 * @return whether or not obj is equal to this Widget
164 public boolean equals(Object obj) {
165 boolean isEqual = false;
166 if (obj instanceof Widget) {
167 Widget other = (Widget) obj;
168 if (getId().equals(other.getId())) {
169 other.keys.addAll(this.keys);
176 public void addKey(String key) {
181 * Determine whether one or more keys belonging to this Widget appear in the specified Collection.
183 * @param keys the keys
184 * @return the boolean
186 public boolean memberOf(Collection<String> keys) {
190 return !Collections.disjoint(this.keys, keys);
194 public boolean addResource(Resource resource) {
195 throw new IllegalAccessException(Model.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
199 public boolean addWidget(Widget widget) {