re base code
[sdc.git] / common / onap-sdc-artifact-generator-lib / onap-sdc-artifact-generator-api / src / main / java / org / onap / 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.onap.sdc.generator.aai.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
29 import org.onap.sdc.generator.aai.types.ModelType;
30 import org.onap.sdc.generator.aai.types.ModelWidget;
31 import org.onap.sdc.generator.data.ArtifactType;
32 import org.onap.sdc.generator.data.GeneratorConstants;
33 import org.onap.sdc.generator.data.WidgetConfigurationUtil;
34 import org.onap.sdc.generator.error.IllegalAccessException;
35
36 public abstract class Widget extends Model {
37
38   private Set<String> keys = new HashSet<>();
39
40   /**
41    * Gets widget.
42    *
43    * @param type the type
44    * @return the widget
45    */
46   public static Widget getWidget(Type type) {
47
48     switch (type) {
49       case SERVICE:
50         return new ServiceWidget();
51       case VF:
52         return new VfWidget();
53       case VFC:
54         return new VfcWidget();
55       case VSERVER:
56         return new VServerWidget();
57       case VOLUME:
58         return new VolumeWidget();
59       case FLAVOR:
60         return new FlavorWidget();
61       case TENANT:
62         return new TenantWidget();
63       case VOLUME_GROUP:
64         return new VolumeGroupWidget();
65       case LINT:
66         return new LIntfWidget();
67       case L3_NET:
68         return new L3NetworkWidget();
69       case VFMODULE:
70         return new VfModuleWidget();
71       case IMAGE:
72         return new ImageWidget();
73       case OAM_NETWORK:
74         return new OamNetwork();
75       case ALLOTTED_RESOURCE:
76         return new AllotedResourceWidget();
77       case TUNNEL_XCONNECT:
78         return new TunnelXconnectWidget();
79       default:
80         return null;
81     }
82
83   }
84
85   /**
86    * Gets id.
87    *
88    * @return the id
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(
95           GeneratorConstants.GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,
96           ArtifactType.AAI.name() + ".model-version-id." + getName()));
97     }
98     return id;
99   }
100
101   public ModelType getType() {
102     ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
103     return widgetModel.type();
104   }
105
106   public String getName() {
107     ModelWidget widgetModel = this.getClass().getAnnotation(ModelWidget.class);
108     return widgetModel.name();
109   }
110
111   /**
112    * Get Widget Id from properties file.
113    * @return - Widget Id
114    */
115   public String getWidgetId() {
116     Properties properties = WidgetConfigurationUtil.getConfig();
117     String id = properties.getProperty(ArtifactType.AAI.name() + ".model-invariant-id."
118         + getName());
119     if (id == null) {
120       throw new IllegalArgumentException(String.format(
121           GeneratorConstants.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 }