[MSO-8] Update the maven dependency
[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 java.io.IOException;
27 import java.net.URISyntaxException;
28 import java.security.GeneralSecurityException;
29 import java.util.Map;
30 import java.util.concurrent.ConcurrentHashMap;
31
32 import org.codehaus.jackson.annotate.JsonProperty;
33 import org.codehaus.jackson.map.JsonSerializer;
34 import org.codehaus.jackson.JsonGenerator;
35 import org.codehaus.jackson.map.SerializerProvider;
36 import org.codehaus.jackson.map.annotate.JsonDeserialize;
37 import org.codehaus.jackson.map.annotate.JsonSerialize;
38 import org.codehaus.jackson.JsonProcessingException;
39
40 import org.openecomp.mso.openstack.exceptions.MsoAdapterException;
41 import org.openecomp.mso.openstack.exceptions.MsoException;
42 import org.openecomp.mso.openstack.utils.MsoCommonUtils;
43 import org.openecomp.mso.openstack.utils.MsoKeystoneUtils;
44 import org.openecomp.mso.openstack.utils.MsoTenantUtils;
45 import org.openecomp.mso.openstack.utils.MsoTenantUtilsFactory;
46 import org.openecomp.mso.cloud.authentication.AuthenticationMethodFactory;
47 import org.openecomp.mso.cloud.authentication.AuthenticationWrapper;
48 import org.openecomp.mso.cloud.authentication.models.RackspaceAuthentication;
49 import org.openecomp.mso.cloud.authentication.wrappers.RackspaceAPIKeyWrapper;
50 import org.openecomp.mso.cloud.authentication.wrappers.UsernamePasswordWrapper;
51 import org.openecomp.mso.logger.MessageEnum;
52 import org.openecomp.mso.logger.MsoLogger;
53
54 import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
55 import org.openecomp.mso.utils.CryptoUtils;
56 import com.woorea.openstack.keystone.model.Authentication;
57
58 /**
59  * JavaBean JSON class for a CloudIdentity. This bean represents a cloud identity
60  * service instance (i.e. a DCP node) in the NVP/AIC cloud. It will be loaded via
61  * CloudConfig object, of which it is a component (a CloudConfig JSON configuration
62  * file may contain multiple CloudIdentity definitions).
63  *
64  * Note that this is only used to access Cloud Configurations loaded from a
65  * JSON config file, so there are no explicit setters.
66  *
67  */
68 public class CloudIdentity {
69
70         // This block is needed to trigger the class loader so that static initialization
71         // of both inner static classes occur. This is required when the Json Deserializer
72         // gets called and no access to any of these inner classes happened yet.
73         static {
74                 IdentityServerType.bootstrap();
75                 IdentityAuthenticationType.bootstrap();
76         }
77         
78     private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
79
80     public final static class IdentityServerType extends IdentityServerTypeAbstract {
81
82         public static final IdentityServerType KEYSTONE = new IdentityServerType("KEYSTONE", MsoKeystoneUtils.class);
83
84         public IdentityServerType(String serverType, Class<? extends MsoTenantUtils> utilsClass) {
85                 super(serverType, utilsClass);
86         }
87
88                 public static final void bootstrap() {}
89     }
90
91     public static final class IdentityAuthenticationType extends IdentityAuthenticationTypeAbstract {
92         
93         public static final IdentityAuthenticationType USERNAME_PASSWORD = new IdentityAuthenticationType("USERNAME_PASSWORD", UsernamePasswordWrapper.class);
94         
95         public static final IdentityAuthenticationType RACKSPACE_APIKEY = new IdentityAuthenticationType("RACKSPACE_APIKEY", RackspaceAPIKeyWrapper.class);
96         
97         public IdentityAuthenticationType(String identityType, Class<? extends AuthenticationWrapper> wrapperClass) {
98                 super(identityType, wrapperClass);
99         }
100
101                 public static final void bootstrap() {}
102     }
103     
104     @JsonProperty
105     private String id;
106     @JsonProperty("identity_url")
107     private String identityUrl;
108     @JsonProperty("mso_id")
109     private String msoId;
110     @JsonProperty("mso_pass")
111     private String msoPass;
112     @JsonProperty("admin_tenant")
113     private String adminTenant;
114     @JsonProperty("member_role")
115     private String memberRole;
116     @JsonProperty("tenant_metadata")
117     private Boolean tenantMetadata;
118     @JsonProperty("identity_server_type")
119     @JsonSerialize(using=IdentityServerTypeJsonSerializer.class)
120     @JsonDeserialize(using=IdentityServerTypeJsonDeserializer.class)
121     private IdentityServerType identityServerType;
122     @JsonProperty("identity_authentication_type")
123     @JsonSerialize(using=IdentityAuthenticationTypeJsonSerializer.class)
124     @JsonDeserialize(using=IdentityAuthenticationTypeJsonDeserializer.class)
125     private IdentityAuthenticationType identityAuthenticationType;
126     
127     private static String cloudKey = "aa3871669d893c7fb8abbcda31b88b4f";
128
129     public CloudIdentity () {
130     }
131
132     public String getId () {
133         return id;
134     }
135
136     public void setId (String id) {
137         this.id = id;
138     }
139
140     //DEPRECATED
141     public String getKeystoneUrl () throws MsoException {
142         if (this.identityServerType.equals(IdentityServerType.KEYSTONE))
143                 return this.identityUrl;
144         else
145                 return null;
146     }
147     
148     public String getKeystoneUrl (String regionId, String msoPropID) throws MsoException {
149         if (IdentityServerType.KEYSTONE.equals(this.identityServerType)) {
150                 return this.identityUrl;
151         } else {
152                 if (this.identityServerType == null) {
153                         return null;
154                 }
155                 MsoTenantUtils tenantUtils = new MsoTenantUtilsFactory(msoPropID).getTenantUtilsByServerType(this.identityServerType.toString());
156                 if (tenantUtils != null) {
157                         return tenantUtils.getKeystoneUrl(regionId, msoPropID, this);
158                 } else {
159                         return null;
160                 }
161         }
162     }
163     
164     public Authentication getAuthentication () throws MsoException {
165         if (this.getIdentityAuthenticationType() != null) {
166                         try {
167                         return AuthenticationMethodFactory.getAuthenticationFor(this);
168                         } catch (IllegalAccessException | InstantiationException | ClassNotFoundException | IOException | URISyntaxException e) {
169                                 throw new MsoAdapterException("Could not retrieve authentication for " + this.identityAuthenticationType, e);
170                         }
171         } else { // Fallback
172                 return new UsernamePassword(this.getMsoId(), this.getMsoPass());
173         }
174     }
175
176     public void setKeystoneUrl (String url) {
177         if (IdentityServerType.KEYSTONE.equals(this.identityServerType)) {
178                 this.identityUrl = url;
179         }
180     }
181     
182     public String getIdentityUrl() {
183         return this.identityUrl;
184     }
185     public void setIdentityUrl(String url) {
186         this.identityUrl = url;
187     }
188
189     public String getMsoId () {
190         return msoId;
191     }
192
193     public void setMsoId (String id) {
194         this.msoId = id;
195     }
196
197     public String getMsoPass () {
198         try {
199             return CryptoUtils.decrypt (msoPass, cloudKey);
200         } catch (GeneralSecurityException e) {
201             LOGGER.error (MessageEnum.RA_GENERAL_EXCEPTION, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in getMsoPass", e);
202             return null;
203         }
204     }
205
206     public void setMsoPass (String pwd) {
207         this.msoPass = pwd;
208     }
209
210     public String getAdminTenant () {
211         return adminTenant;
212     }
213
214     public void setAdminTenant (String tenant) {
215         this.adminTenant = tenant;
216     }
217
218     public String getMemberRole () {
219         return memberRole;
220     }
221
222     public void setMemberRole (String role) {
223         this.memberRole = role;
224     }
225
226     public boolean hasTenantMetadata () {
227         return tenantMetadata;
228     }
229
230     public void setTenantMetadata (boolean meta) {
231         this.tenantMetadata = meta;
232     }
233     
234     public IdentityServerType getIdentityServerType() {
235         return this.identityServerType;
236     }
237     public void setIdentityServerType(IdentityServerType ist) {
238         this.identityServerType = ist;
239     }
240     public String getIdentityServerTypeAsString() {
241         return this.identityServerType.toString();
242     }
243     /**
244          * @return the identityAuthenticationType
245          */
246         public IdentityAuthenticationType getIdentityAuthenticationType() {
247                 return identityAuthenticationType;
248         }
249
250         /**
251          * @param identityAuthenticationType the identityAuthenticationType to set
252          */
253         public void setIdentityAuthenticationType(IdentityAuthenticationType identityAuthenticationType) {
254                 this.identityAuthenticationType = identityAuthenticationType;
255         }
256         
257         @Override
258     public String toString () {
259         StringBuilder stringBuilder = new StringBuilder ();
260         stringBuilder.append ("Cloud Identity Service: id=")
261                      .append (id)
262                      .append (", identityUrl=")
263                      .append (this.identityUrl)
264                      .append (", msoId=")
265                      .append (msoId)
266                      .append (", adminTenant=")
267                      .append (adminTenant)
268                      .append (", memberRole=")
269                      .append (memberRole)
270                      .append (", tenantMetadata=")
271                      .append (tenantMetadata)
272                      .append (", identityServerType=")
273                      .append (identityServerType.toString())
274                      .append (", identityAuthenticationType=")
275                      .append (identityAuthenticationType.toString());
276         
277         return stringBuilder.toString ();
278     }
279
280     public static String encryptPassword (String msoPass) {
281         try {
282             return CryptoUtils.encrypt (msoPass, cloudKey);
283         } catch (GeneralSecurityException e) {
284             LOGGER.error (MessageEnum.RA_GENERAL_EXCEPTION, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in encryptPassword", e);
285             return null;
286         }
287     }
288
289
290         @Override
291         public CloudIdentity clone() {
292                 CloudIdentity cloudIdentityCopy = new CloudIdentity();
293
294                 cloudIdentityCopy.id = this.id;
295                 cloudIdentityCopy.identityUrl = this.identityUrl;
296                 cloudIdentityCopy.msoId = this.msoId;
297                 cloudIdentityCopy.msoPass = this.msoPass;
298                 cloudIdentityCopy.adminTenant = this.adminTenant;
299                 cloudIdentityCopy.memberRole = this.memberRole;
300                 cloudIdentityCopy.tenantMetadata = this.tenantMetadata;
301                 cloudIdentityCopy.identityServerType = this.identityServerType;
302                 cloudIdentityCopy.identityAuthenticationType = this.identityAuthenticationType;
303
304                 return cloudIdentityCopy;
305         }
306
307         @Override
308         public int hashCode() {
309                 final int prime = 31;
310                 int result = 1;
311                 result = prime * result + ((adminTenant == null) ? 0 : adminTenant.hashCode());
312                 result = prime * result + ((id == null) ? 0 : id.hashCode());
313                 result = prime * result + ((identityUrl == null) ? 0 : identityUrl.hashCode());
314                 result = prime * result + ((memberRole == null) ? 0 : memberRole.hashCode());
315                 result = prime * result + ((msoId == null) ? 0 : msoId.hashCode());
316                 result = prime * result + ((msoPass == null) ? 0 : msoPass.hashCode());
317                 result = prime * result + ((tenantMetadata == null) ? 0 : tenantMetadata.hashCode());
318                 result = prime * result + ((identityServerType == null) ? 0 : identityServerType.hashCode());
319                 result = prime * result + ((identityAuthenticationType == null) ? 0 : identityAuthenticationType.hashCode());
320                 return result;
321         }
322
323         @Override
324         public boolean equals(Object obj) {
325                 if (this == obj)
326                         return true;
327                 if (obj == null)
328                         return false;
329                 if (getClass() != obj.getClass())
330                         return false;
331                 CloudIdentity other = (CloudIdentity) obj;
332                 if (adminTenant == null) {
333                         if (other.adminTenant != null)
334                                 return false;
335                 } else if (!adminTenant.equals(other.adminTenant))
336                         return false;
337                 if (id == null) {
338                         if (other.id != null)
339                                 return false;
340                 } else if (!id.equals(other.id))
341                         return false;
342                 if (identityUrl == null) {
343                         if (other.identityUrl != null)
344                                 return false;
345                 } else if (!identityUrl.equals(other.identityUrl))
346                         return false;
347                 if (memberRole == null) {
348                         if (other.memberRole != null)
349                                 return false;
350                 } else if (!memberRole.equals(other.memberRole))
351                         return false;
352                 if (msoId == null) {
353                         if (other.msoId != null)
354                                 return false;
355                 } else if (!msoId.equals(other.msoId))
356                         return false;
357                 if (msoPass == null) {
358                         if (other.msoPass != null)
359                                 return false;
360                 } else if (!msoPass.equals(other.msoPass))
361                         return false;
362                 if (tenantMetadata == null) {
363                         if (other.tenantMetadata != null)
364                                 return false;
365                 } else if (!tenantMetadata.equals(other.tenantMetadata))
366                         return false;
367                 if (identityServerType == null) {
368                         if (other.getIdentityServerType() != null)
369                                 return false;
370                 } else if (!identityServerType.equals(other.getIdentityServerType()))
371                         return false;
372                 if (identityAuthenticationType == null) {
373                         if (other.getIdentityAuthenticationType() != null)
374                                 return false;
375                 } else if (!identityAuthenticationType.equals(other.getIdentityAuthenticationType()))
376                         return false;
377                 
378                 return true;
379         }
380 }