3dd4cbfc9cb12af9157755dacc95c6ff97ec08fa
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / cloud / CloudIdentity.java
1 /*
2  * ============LICENSE_START==========================================
3  * ===================================================================
4  * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5  * ===================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END============================================
18  *
19  * ECOMP and OpenECOMP are trademarks
20  * and service marks of AT&T Intellectual Property.
21  *
22  */
23
24 package org.openecomp.mso.cloud;
25
26 import com.woorea.openstack.keystone.model.Authentication;
27 import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
28 import java.security.GeneralSecurityException;
29 import org.codehaus.jackson.annotate.JsonProperty;
30 import org.codehaus.jackson.map.annotate.JsonDeserialize;
31 import org.codehaus.jackson.map.annotate.JsonSerialize;
32 import org.openecomp.mso.cloud.authentication.AuthenticationMethodFactory;
33 import org.openecomp.mso.cloud.authentication.AuthenticationWrapper;
34 import org.openecomp.mso.cloud.authentication.wrappers.RackspaceAPIKeyWrapper;
35 import org.openecomp.mso.cloud.authentication.wrappers.UsernamePasswordWrapper;
36 import org.openecomp.mso.logger.MessageEnum;
37 import org.openecomp.mso.logger.MsoLogger;
38 import org.openecomp.mso.openstack.exceptions.MsoException;
39 import org.openecomp.mso.openstack.utils.MsoKeystoneUtils;
40 import org.openecomp.mso.openstack.utils.MsoTenantUtils;
41 import org.openecomp.mso.openstack.utils.MsoTenantUtilsFactory;
42 import org.openecomp.mso.utils.CryptoUtils;
43
44 /**
45  * JavaBean JSON class for a CloudIdentity. This bean represents a cloud identity
46  * service instance (i.e. a DCP node) in the NVP/AIC cloud. It will be loaded via
47  * CloudConfig object, of which it is a component (a CloudConfig JSON configuration
48  * file may contain multiple CloudIdentity definitions).
49  *
50  * Note that this is only used to access Cloud Configurations loaded from a
51  * JSON config file, so there are no explicit setters.
52  *
53  */
54 public class CloudIdentity {
55
56         // This block is needed to trigger the class loader so that static initialization
57         // of both inner static classes occur. This is required when the Json Deserializer
58         // gets called and no access to any of these inner classes happened yet.
59         static {
60                 IdentityServerType.bootstrap();
61                 IdentityAuthenticationType.bootstrap();
62         }
63         
64     private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
65
66     public final static class IdentityServerType extends IdentityServerTypeAbstract {
67
68         public static final IdentityServerType KEYSTONE = new IdentityServerType("KEYSTONE", MsoKeystoneUtils.class);
69
70         public IdentityServerType(String serverType, Class<? extends MsoTenantUtils> utilsClass) {
71                 super(serverType, utilsClass);
72         }
73
74                 public static final void bootstrap() {}
75     }
76
77     public static final class IdentityAuthenticationType extends IdentityAuthenticationTypeAbstract {
78         
79         public static final IdentityAuthenticationType USERNAME_PASSWORD = new IdentityAuthenticationType("USERNAME_PASSWORD", UsernamePasswordWrapper.class);
80         
81         public static final IdentityAuthenticationType RACKSPACE_APIKEY = new IdentityAuthenticationType("RACKSPACE_APIKEY", RackspaceAPIKeyWrapper.class);
82         
83         public IdentityAuthenticationType(String identityType, Class<? extends AuthenticationWrapper> wrapperClass) {
84                 super(identityType, wrapperClass);
85         }
86
87                 public static final void bootstrap() {}
88     }
89     
90     @JsonProperty
91     private String id;
92     @JsonProperty("identity_url")
93     private String identityUrl;
94     @JsonProperty("mso_id")
95     private String msoId;
96     @JsonProperty("mso_pass")
97     private String msoPass;
98     @JsonProperty("admin_tenant")
99     private String adminTenant;
100     @JsonProperty("member_role")
101     private String memberRole;
102     @JsonProperty("tenant_metadata")
103     private Boolean tenantMetadata;
104     @JsonProperty("identity_server_type")
105     @JsonSerialize(using=IdentityServerTypeJsonSerializer.class)
106     @JsonDeserialize(using=IdentityServerTypeJsonDeserializer.class)
107     private IdentityServerType identityServerType;
108     @JsonProperty("identity_authentication_type")
109     @JsonSerialize(using=IdentityAuthenticationTypeJsonSerializer.class)
110     @JsonDeserialize(using=IdentityAuthenticationTypeJsonDeserializer.class)
111     private IdentityAuthenticationType identityAuthenticationType;
112     
113     private static String cloudKey = "aa3871669d893c7fb8abbcda31b88b4f";
114
115     public CloudIdentity () {
116     }
117
118     public String getId () {
119         return id;
120     }
121
122     public void setId (String id) {
123         this.id = id;
124     }
125
126     public String getKeystoneUrl (String regionId, String msoPropID) throws MsoException {
127         if (IdentityServerType.KEYSTONE.equals(this.identityServerType)) {
128                 return this.identityUrl;
129         } else {
130                 if (this.identityServerType == null) {
131                         return null;
132                 }
133                 MsoTenantUtils tenantUtils = new MsoTenantUtilsFactory(msoPropID).getTenantUtilsByServerType(this.identityServerType.toString());
134                 if (tenantUtils != null) {
135                         return tenantUtils.getKeystoneUrl(regionId, msoPropID, this);
136                 } else {
137                         return null;
138                 }
139         }
140     }
141
142         public Authentication getAuthentication() {
143         if (this.getIdentityAuthenticationType() != null) {
144                         return AuthenticationMethodFactory.getAuthenticationFor(this);
145         } else {
146                 return new UsernamePassword(this.getMsoId(), this.getMsoPass());
147         }
148     }
149
150     public void setKeystoneUrl (String url) {
151         if (IdentityServerType.KEYSTONE.equals(this.identityServerType)) {
152                 this.identityUrl = url;
153         }
154     }
155     
156     public String getIdentityUrl() {
157         return this.identityUrl;
158     }
159     public void setIdentityUrl(String url) {
160         this.identityUrl = url;
161     }
162
163     public String getMsoId () {
164         return msoId;
165     }
166
167     public void setMsoId (String id) {
168         this.msoId = id;
169     }
170
171     public String getMsoPass () {
172         try {
173             return CryptoUtils.decrypt (msoPass, cloudKey);
174         } catch (GeneralSecurityException e) {
175             LOGGER.error (MessageEnum.RA_GENERAL_EXCEPTION, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in getMsoPass", e);
176             return null;
177         }
178     }
179
180     public void setMsoPass (String pwd) {
181         this.msoPass = pwd;
182     }
183
184     public String getAdminTenant () {
185         return adminTenant;
186     }
187
188     public void setAdminTenant (String tenant) {
189         this.adminTenant = tenant;
190     }
191
192     public String getMemberRole () {
193         return memberRole;
194     }
195
196     public void setMemberRole (String role) {
197         this.memberRole = role;
198     }
199
200     public boolean hasTenantMetadata () {
201         return tenantMetadata;
202     }
203
204     public void setTenantMetadata (boolean meta) {
205         this.tenantMetadata = meta;
206     }
207     
208     public IdentityServerType getIdentityServerType() {
209         return this.identityServerType;
210     }
211     public void setIdentityServerType(IdentityServerType ist) {
212         this.identityServerType = ist;
213     }
214     public String getIdentityServerTypeAsString() {
215         return this.identityServerType.toString();
216     }
217     /**
218          * @return the identityAuthenticationType
219          */
220         public IdentityAuthenticationType getIdentityAuthenticationType() {
221                 return identityAuthenticationType;
222         }
223
224         /**
225          * @param identityAuthenticationType the identityAuthenticationType to set
226          */
227         public void setIdentityAuthenticationType(IdentityAuthenticationType identityAuthenticationType) {
228                 this.identityAuthenticationType = identityAuthenticationType;
229         }
230
231         @Override
232     public String toString () {
233                 return "Cloud Identity Service: id=" + id +
234                         ", identityUrl=" + this.identityUrl +
235                         ", msoId=" + msoId +
236                         ", adminTenant=" + adminTenant +
237                         ", memberRole=" + memberRole +
238                         ", tenantMetadata=" + tenantMetadata +
239                         ", identityServerType=" + (identityServerType == null ? "null" : identityServerType.toString()) +
240                         ", identityAuthenticationType=" + (identityAuthenticationType == null ? "null" : identityAuthenticationType.toString());
241         }
242
243     public static String encryptPassword (String msoPass) {
244         try {
245             return CryptoUtils.encrypt (msoPass, cloudKey);
246         } catch (GeneralSecurityException e) {
247             LOGGER.error (MessageEnum.RA_GENERAL_EXCEPTION, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in encryptPassword", e);
248             return null;
249         }
250     }
251
252
253         @Override
254         public CloudIdentity clone() {
255                 CloudIdentity cloudIdentityCopy = new CloudIdentity();
256
257                 cloudIdentityCopy.id = this.id;
258                 cloudIdentityCopy.identityUrl = this.identityUrl;
259                 cloudIdentityCopy.msoId = this.msoId;
260                 cloudIdentityCopy.msoPass = this.msoPass;
261                 cloudIdentityCopy.adminTenant = this.adminTenant;
262                 cloudIdentityCopy.memberRole = this.memberRole;
263                 cloudIdentityCopy.tenantMetadata = this.tenantMetadata;
264                 cloudIdentityCopy.identityServerType = this.identityServerType;
265                 cloudIdentityCopy.identityAuthenticationType = this.identityAuthenticationType;
266
267                 return cloudIdentityCopy;
268         }
269
270         @Override
271         public int hashCode() {
272                 final int prime = 31;
273                 int result = 1;
274                 result = prime * result + ((adminTenant == null) ? 0 : adminTenant.hashCode());
275                 result = prime * result + ((id == null) ? 0 : id.hashCode());
276                 result = prime * result + ((identityUrl == null) ? 0 : identityUrl.hashCode());
277                 result = prime * result + ((memberRole == null) ? 0 : memberRole.hashCode());
278                 result = prime * result + ((msoId == null) ? 0 : msoId.hashCode());
279                 result = prime * result + ((msoPass == null) ? 0 : msoPass.hashCode());
280                 result = prime * result + ((tenantMetadata == null) ? 0 : tenantMetadata.hashCode());
281                 result = prime * result + ((identityServerType == null) ? 0 : identityServerType.hashCode());
282                 result = prime * result + ((identityAuthenticationType == null) ? 0 : identityAuthenticationType.hashCode());
283                 return result;
284         }
285
286         @Override
287         public boolean equals(Object obj) {
288                 if (this == obj)
289                         return true;
290                 if (obj == null)
291                         return false;
292                 if (getClass() != obj.getClass())
293                         return false;
294                 CloudIdentity other = (CloudIdentity) obj;
295                 if (adminTenant == null) {
296                         if (other.adminTenant != null)
297                                 return false;
298                 } else if (!adminTenant.equals(other.adminTenant))
299                         return false;
300                 if (id == null) {
301                         if (other.id != null)
302                                 return false;
303                 } else if (!id.equals(other.id))
304                         return false;
305                 if (identityUrl == null) {
306                         if (other.identityUrl != null)
307                                 return false;
308                 } else if (!identityUrl.equals(other.identityUrl))
309                         return false;
310                 if (memberRole == null) {
311                         if (other.memberRole != null)
312                                 return false;
313                 } else if (!memberRole.equals(other.memberRole))
314                         return false;
315                 if (msoId == null) {
316                         if (other.msoId != null)
317                                 return false;
318                 } else if (!msoId.equals(other.msoId))
319                         return false;
320                 if (msoPass == null) {
321                         if (other.msoPass != null)
322                                 return false;
323                 } else if (!msoPass.equals(other.msoPass))
324                         return false;
325                 if (tenantMetadata == null) {
326                         if (other.tenantMetadata != null)
327                                 return false;
328                 } else if (!tenantMetadata.equals(other.tenantMetadata))
329                         return false;
330                 if (identityServerType == null) {
331                         if (other.getIdentityServerType() != null)
332                                 return false;
333                 } else if (!identityServerType.equals(other.getIdentityServerType()))
334                         return false;
335                 if (identityAuthenticationType == null) {
336                         if (other.getIdentityAuthenticationType() != null)
337                                 return false;
338                 } else if (!identityAuthenticationType.equals(other.getIdentityAuthenticationType()))
339                         return false;
340                 
341                 return true;
342         }
343 }