ce1ef703d90955c833097f6eee8b8ceab83434df
[sdc.git] /
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       case PNF:
82         return new PnfWidget();
83       default:
84         return null;
85     }
86
87   }
88
89   /**
90    * Gets id.
91    *
92    * @return the id
93    */
94   public String getId() {
95     Properties properties = WidgetConfigurationUtil.getConfig();
96     String id = properties.getProperty(ArtifactType.AAI.name() + ".model-version-id." + getName());
97     if (id == null) {
98       throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
99           ArtifactType.AAI.name() + ".model-version-id." + getName()));
100     }
101     return id;
102   }
103
104   public ModelType getType() {
105     ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
106     return widgetModel.type();
107   }
108
109   public String getName() {
110     ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
111     return widgetModel.name();
112   }
113
114   /**
115    * Get Widget Id from properties file.
116    * @return - Widget Id
117    */
118   public String getWidgetId() {
119     Properties properties = WidgetConfigurationUtil.getConfig();
120     String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id."
121         + getName());
122     if (id == null) {
123       throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
124           ArtifactType.AAI.name() + ".model-invariant-id." + getName()));
125     }
126     return id;
127   }
128
129   public int hashCode() {
130     return getId().hashCode();
131   }
132
133   @Override
134   public Type getWidgetType() {
135     return null;
136   }
137
138   /**
139    * Equals.
140    *
141    * @param obj Object
142    * @return the boolean
143    */
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 the keys
164    * @return the boolean
165    */
166   public boolean memberOf(List<String> keys) {
167     if (keys == null) {
168       return false;
169     }
170     return !Collections.disjoint(this.keys, keys);
171   }
172
173   /**
174    * All instances used boolean.
175    *
176    * @param collection the collection
177    * @return the boolean
178    */
179   public boolean allInstancesUsed(Set<String> collection) {
180     Set<String> keyCopy = new HashSet<>(keys);
181     keyCopy.removeAll(collection);
182     return keyCopy.isEmpty();
183   }
184
185   public boolean addResource(Resource resource) {
186     throw new IllegalAccessException(GeneratorConstants
187         .GENERATOR_AAI_ERROR_UNSUPPORTED_WIDGET_OPERATION);
188   }
189
190   public boolean addWidget(Widget widget) {
191     return true;
192   }
193
194   public enum Type {
195     SERVICE, VF, VFC, VSERVER, VOLUME, FLAVOR, TENANT, VOLUME_GROUP, LINT, L3_NET, VFMODULE, IMAGE,
196     OAM_NETWORK,ALLOTTED_RESOURCE,TUNNEL_XCONNECT, PNF
197   }
198 }