7f394387f0a19e0e8a29cc716adcc8db3409ae7e
[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-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
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.Collections;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Properties;
27 import java.util.Set;
28 import org.onap.aai.babel.xml.generator.data.ArtifactType;
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 import org.onap.aai.babel.xml.generator.types.ModelWidget;
33
34 public abstract class Widget extends Model {
35
36     public static final String GENERATOR_AAI_CONFIGLPROP_NOT_FOUND = "Cannot generate artifacts. Widget configuration not found for %s";
37
38     public enum Type {
39         SERVICE, VF, VFC, VSERVER, VOLUME, FLAVOR, TENANT, VOLUME_GROUP, LINT, L3_NET, VFMODULE, IMAGE, OAM_NETWORK, ALLOTTED_RESOURCE, TUNNEL_XCONNECT, CONFIGURATION;
40     }
41
42     private Set<String> keys = new HashSet<>();
43
44     /**
45      * Gets widget.
46      *
47      * @param type
48      *            the type
49      * @return the widget
50      */
51     public static Widget getWidget(Type type) {
52         switch (type) {
53             case SERVICE:
54                 return new ServiceWidget();
55             case VF:
56                 return new VfWidget();
57             case VFC:
58                 return new VfcWidget();
59             case VSERVER:
60                 return new VServerWidget();
61             case VOLUME:
62                 return new VolumeWidget();
63             case FLAVOR:
64                 return new FlavorWidget();
65             case TENANT:
66                 return new TenantWidget();
67             case VOLUME_GROUP:
68                 return new VolumeGroupWidget();
69             case LINT:
70                 return new LIntfWidget();
71             case L3_NET:
72                 return new L3NetworkWidget();
73             case VFMODULE:
74                 return new VfModuleWidget();
75             case IMAGE:
76                 return new ImageWidget();
77             case OAM_NETWORK:
78                 return new OamNetwork();
79             case ALLOTTED_RESOURCE:
80                 return new AllotedResourceWidget();
81             case TUNNEL_XCONNECT:
82                 return new TunnelXconnectWidget();
83             case CONFIGURATION:
84                 return new ConfigurationWidget();
85             default:
86                 return null;
87         }
88     }
89
90     public String getId() {
91         Properties properties = WidgetConfigurationUtil.getConfig();
92         String id = properties.getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
93         if (id == null) {
94             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
95                     ArtifactType.AAI.name() + ".model-version-id." + getName()));
96         }
97         return id;
98     }
99
100     public ModelType getType() {
101         ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
102         return widgetModel.type();
103     }
104
105     public String getName() {
106         ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
107         return widgetModel.name();
108     }
109
110     /**
111      * Get Widget Id from properties file.
112      *
113      * @return - Widget Id
114      */
115     @Override
116     public String getWidgetId() {
117         Properties properties = WidgetConfigurationUtil.getConfig();
118         String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id." + getName());
119         if (id == null) {
120             throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
121                     ArtifactType.AAI.name() + ".model-invariant-id." + getName()));
122         }
123         return id;
124     }
125
126     @Override
127     public int hashCode() {
128         return getId().hashCode();
129     }
130
131     @Override
132     public Type getWidgetType() {
133         return null;
134     }
135
136     /**
137      * Equals.
138      *
139      * @param obj
140      *            Object
141      * @return the boolean
142      */
143     @Override
144     public boolean equals(Object obj) {
145         if (obj instanceof Widget) {
146             if (getId().equals(((Widget) obj).getId())) {
147                 ((Widget) obj).keys.addAll(this.keys);
148                 return true;
149             }
150             return false;
151         } else {
152             return false;
153         }
154     }
155
156     public void addKey(String key) {
157         this.keys.add(key);
158     }
159
160     /**
161      * Member of boolean.
162      *
163      * @param keys
164      *            the keys
165      * @return the boolean
166      */
167     public boolean memberOf(List<String> keys) {
168         if (keys == null) {
169             return false;
170         }
171         return !Collections.disjoint(this.keys, keys);
172     }
173
174     /**
175      * All instances used boolean.
176      *
177      * @param collection
178      *            the collection
179      * @return the boolean
180      */
181     public boolean allInstancesUsed(Set<String> collection) {
182         Set<String> keyCopy = new HashSet<>(keys);
183         keyCopy.removeAll(collection);
184         return keyCopy.isEmpty();
185     }
186
187     @Override
188     public boolean addResource(Resource resource) {
189         throw new IllegalAccessException(Model.GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
190     }
191
192     @Override
193     public boolean addWidget(Widget widget) {
194         return true;
195     }
196
197 }