6220519a3f315efeb30c28f670a4b7a5a9ffbe3b
[sdc.git] / common / openecomp-sdc-artifact-generator-lib / openecomp-sdc-artifact-generator-api / src / main / java / org / openecomp / sdc / generator / aai / model / Widget.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.generator.aai.model;
22
23 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGLPROP_NOT_FOUND;
24
25 import org.openecomp.sdc.generator.aai.types.ModelType;
26 import org.openecomp.sdc.generator.aai.types.ModelWidget;
27 import org.openecomp.sdc.generator.data.ArtifactType;
28 import org.openecomp.sdc.generator.data.GeneratorConstants;
29 import org.openecomp.sdc.generator.data.WidgetConfigurationUtil;
30 import org.openecomp.sdc.generator.error.IllegalAccessException;
31
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Properties;
36 import java.util.Set;
37
38 public abstract class Widget extends Model {
39
40   private Set<String> keys = new HashSet<>();
41
42   /**
43    * Gets widget.
44    *
45    * @param type the type
46    * @return the widget
47    */
48   public static Widget getWidget(Type type) {
49
50     switch (type) {
51       case SERVICE:
52         return new ServiceWidget();
53       case VF:
54         return new VfWidget();
55       case VFC:
56         return new VfcWidget();
57       case VSERVER:
58         return new VServerWidget();
59       case VOLUME:
60         return new VolumeWidget();
61       case FLAVOR:
62         return new FlavorWidget();
63       case TENANT:
64         return new TenantWidget();
65       case VOLUME_GROUP:
66         return new VolumeGroupWidget();
67       case LINT:
68         return new LIntfWidget();
69       case L3_NET:
70         return new L3NetworkWidget();
71       case VFMODULE:
72         return new VfModuleWidget();
73       case IMAGE:
74         return new ImageWidget();
75       case OAM_NETWORK:
76         return new OamNetwork();
77       case ALLOTTED_RESOURCE:
78         return new AllotedResourceWidget();
79       case TUNNEL_XCONNECT:
80         return new TunnelXconnectWidget();
81       default:
82         return null;
83     }
84
85   }
86
87   /**
88    * Gets id.
89    *
90    * @return the id
91    */
92   public String getId() {
93     Properties properties = WidgetConfigurationUtil.getConfig();
94     String id = properties.getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
95     if (id == null) {
96       throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
97           ArtifactType.AAI.name() + ".model-version-id." + getName()));
98     }
99     return id;
100   }
101
102   public ModelType getType() {
103     ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
104     return widgetModel.type();
105   }
106
107   public String getName() {
108     ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
109     return widgetModel.name();
110   }
111
112   /**
113    * Get Widget Id from properties file.
114    * @return - Widget Id
115    */
116   public String getWidgetId() {
117     Properties properties = WidgetConfigurationUtil.getConfig();
118     String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id."
119         + 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   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 Object
140    * @return the boolean
141    */
142   public boolean equals(Object obj) {
143     if (obj instanceof Widget) {
144       if (getId().equals(((Widget) obj).getId())) {
145         ((Widget) obj).keys.addAll(this.keys);
146         return true;
147       }
148       return false;
149     } else {
150       return false;
151     }
152   }
153
154   public void addKey(String key) {
155     this.keys.add(key);
156   }
157
158   /**
159    * Member of boolean.
160    *
161    * @param keys the keys
162    * @return the boolean
163    */
164   public boolean memberOf(List<String> keys) {
165     if (keys == null) {
166       return false;
167     }
168     return !Collections.disjoint(this.keys, keys);
169   }
170
171   /**
172    * All instances used boolean.
173    *
174    * @param collection the collection
175    * @return the boolean
176    */
177   public boolean allInstancesUsed(Set<String> collection) {
178     Set<String> keyCopy = new HashSet<>(keys);
179     keyCopy.removeAll(collection);
180     return keyCopy.isEmpty();
181   }
182
183   public boolean addResource(Resource resource) {
184     throw new IllegalAccessException(GeneratorConstants
185         .GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
186   }
187
188   public boolean addWidget(Widget widget) {
189     return true;
190   }
191
192   public enum Type {
193     SERVICE, VF, VFC, VSERVER, VOLUME, FLAVOR, TENANT, VOLUME_GROUP, LINT, L3_NET, VFMODULE, IMAGE,
194     OAM_NETWORK,ALLOTTED_RESOURCE,TUNNEL_XCONNECT
195   }
196 }