a777e4133c43534e3d87d2436c539eed6a54becd
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / cloud / CloudIdentity.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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.mso.cloud;
22
23 import java.security.GeneralSecurityException;
24
25 import org.codehaus.jackson.annotate.JsonProperty;
26 import org.openecomp.mso.openstack.exceptions.MsoException;
27 import org.openecomp.mso.logger.MessageEnum;
28 import org.openecomp.mso.logger.MsoLogger;
29 import com.woorea.openstack.keystone.model.authentication.RackspaceAuthentication;
30 import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
31 import org.openecomp.mso.utils.CryptoUtils;
32 import com.woorea.openstack.keystone.model.Authentication;
33
34 /**
35  * JavaBean JSON class for a CloudIdentity. This bean represents a cloud identity
36  * service instance (i.e. a DCP node) in the NVP/AIC cloud. It will be loaded via
37  * CloudConfig object, of which it is a component (a CloudConfig JSON configuration
38  * file may contain multiple CloudIdentity definitions).
39  *
40  * Note that this is only used to access Cloud Configurations loaded from a
41  * JSON config file, so there are no explicit setters.
42  *
43  */
44 public class CloudIdentity {
45
46     private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
47     
48     public enum IdentityServerType {KEYSTONE};
49
50     public enum IdentityAuthenticationType { USERNAME_PASSWORD, RACKSPACE_APIKEY };
51     
52     @JsonProperty
53     private String id;
54     @JsonProperty("identity_url")
55     private String identityUrl;
56     @JsonProperty("mso_id")
57     private String msoId;
58     @JsonProperty("mso_pass")
59     private String msoPass;
60     @JsonProperty("admin_tenant")
61     private String adminTenant;
62     @JsonProperty("member_role")
63     private String memberRole;
64     @JsonProperty("tenant_metadata")
65     private Boolean tenantMetadata;
66     @JsonProperty("identity_server_type")
67     private IdentityServerType identityServerType;
68     @JsonProperty("identity_authentication_type")
69     private IdentityAuthenticationType identityAuthenticationType;
70     
71     private static String cloudKey = "aa3871669d893c7fb8abbcda31b88b4f";
72
73     public CloudIdentity () {
74     }
75
76     public String getId () {
77         return id;
78     }
79
80     public void setId (String id) {
81         this.id = id;
82     }
83
84     //DEPRECATED
85     public String getKeystoneUrl () throws MsoException {
86         if (this.identityServerType.equals(IdentityServerType.KEYSTONE))
87                 return this.identityUrl;
88         else
89                 return null;
90     }
91     
92     public String getKeystoneUrl (String regionId, String msoPropID) throws MsoException {
93         if (IdentityServerType.KEYSTONE.equals(this.identityServerType)) {
94                 return this.identityUrl;
95         }
96         else {
97                 return null;
98         }
99     }
100     
101     public Authentication getAuthentication () throws MsoException {
102         if (IdentityAuthenticationType.RACKSPACE_APIKEY.equals(this.identityAuthenticationType)) {
103                 return new RackspaceAuthentication (this.getMsoId (),this.getMsoPass ());
104         }
105         else {
106                 // Use default case
107                 return new UsernamePassword (this.getMsoId (),this.getMsoPass ());
108         }
109                 
110     }
111
112     public void setKeystoneUrl (String url) {
113         if (IdentityServerType.KEYSTONE.equals(this.identityServerType)) {
114                 this.identityUrl = url; 
115         }
116     }
117     
118     public String getIdentityUrl() {
119         return this.identityUrl;
120     }
121     public void setIdentityUrl(String url) {
122         this.identityUrl = url;
123     }
124
125     public String getMsoId () {
126         return msoId;
127     }
128
129     public void setMsoId (String id) {
130         this.msoId = id;
131     }
132
133     public String getMsoPass () {
134         try {
135             return CryptoUtils.decrypt (msoPass, cloudKey);
136         } catch (GeneralSecurityException e) {
137             LOGGER.error (MessageEnum.RA_GENERAL_EXCEPTION, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in getMsoPass", e);
138             return null;
139         }
140     }
141
142     public void setMsoPass (String pwd) {
143         this.msoPass = pwd;
144     }
145
146     public String getAdminTenant () {
147         return adminTenant;
148     }
149
150     public void setAdminTenant (String tenant) {
151         this.adminTenant = tenant;
152     }
153
154     public String getMemberRole () {
155         return memberRole;
156     }
157
158     public void setMemberRole (String role) {
159         this.memberRole = role;
160     }
161
162     public boolean hasTenantMetadata () {
163         return tenantMetadata;
164     }
165
166     public void setTenantMetadata (boolean meta) {
167         this.tenantMetadata = meta;
168     }
169     
170     public IdentityServerType getIdentityServerType() {
171         return this.identityServerType;
172     }
173     public void setIdentityServerType(IdentityServerType ist) {
174         this.identityServerType = ist;
175     }
176     public String getIdentityServerTypeAsString() {
177         return this.identityServerType.toString();
178     }
179     /**
180          * @return the identityAuthenticationType
181          */
182         public IdentityAuthenticationType getIdentityAuthenticationType() {
183                 return identityAuthenticationType;
184         }
185
186         /**
187          * @param identityAuthenticationType the identityAuthenticationType to set
188          */
189         public void setIdentityAuthenticationType(IdentityAuthenticationType identityAuthenticationType) {
190                 this.identityAuthenticationType = identityAuthenticationType;
191         }
192         
193         @Override
194     public String toString () {
195         StringBuilder stringBuilder = new StringBuilder ();
196         stringBuilder.append ("Cloud Identity Service: id=")
197                      .append (id)
198                      .append (", identityUrl=")
199                      .append (this.identityUrl)
200                      .append (", msoId=")
201                      .append (msoId)
202                      .append (", adminTenant=")
203                      .append (adminTenant)
204                      .append (", memberRole=")
205                      .append (memberRole)
206                      .append (", tenantMetadata=")
207                      .append (tenantMetadata)
208                      .append (", identityServerType=")
209                      .append (identityServerType.toString())
210                      .append (", identityAuthenticationType=")
211                      .append (identityAuthenticationType.toString());
212         
213         return stringBuilder.toString ();
214     }
215
216     public static String encryptPassword (String msoPass) {
217         try {
218             return CryptoUtils.encrypt (msoPass, cloudKey);
219         } catch (GeneralSecurityException e) {
220             LOGGER.error (MessageEnum.RA_GENERAL_EXCEPTION, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in encryptPassword", e);
221             return null;
222         }
223     }
224
225
226         @Override
227         public CloudIdentity clone() {
228                 CloudIdentity cloudIdentityCopy = new CloudIdentity();
229
230                 cloudIdentityCopy.id = this.id;
231                 cloudIdentityCopy.identityUrl = this.identityUrl;
232                 cloudIdentityCopy.msoId = this.msoId;
233                 cloudIdentityCopy.msoPass = this.msoPass;
234                 cloudIdentityCopy.adminTenant = this.adminTenant;
235                 cloudIdentityCopy.memberRole = this.memberRole;
236                 cloudIdentityCopy.tenantMetadata = this.tenantMetadata;
237                 cloudIdentityCopy.identityServerType = this.identityServerType;
238                 cloudIdentityCopy.identityAuthenticationType = this.identityAuthenticationType;
239
240                 return cloudIdentityCopy;
241         }
242
243         @Override
244         public int hashCode() {
245                 final int prime = 31;
246                 int result = 1;
247                 result = prime * result + ((adminTenant == null) ? 0 : adminTenant.hashCode());
248                 result = prime * result + ((id == null) ? 0 : id.hashCode());
249                 result = prime * result + ((identityUrl == null) ? 0 : identityUrl.hashCode());
250                 result = prime * result + ((memberRole == null) ? 0 : memberRole.hashCode());
251                 result = prime * result + ((msoId == null) ? 0 : msoId.hashCode());
252                 result = prime * result + ((msoPass == null) ? 0 : msoPass.hashCode());
253                 result = prime * result + ((tenantMetadata == null) ? 0 : tenantMetadata.hashCode());
254                 result = prime * result + ((identityServerType == null) ? 0 : identityServerType.hashCode());
255                 result = prime * result + ((identityAuthenticationType == null) ? 0 : identityAuthenticationType.hashCode());
256                 return result;
257         }
258
259         @Override
260         public boolean equals(Object obj) {
261                 if (this == obj)
262                         return true;
263                 if (obj == null)
264                         return false;
265                 if (getClass() != obj.getClass())
266                         return false;
267                 CloudIdentity other = (CloudIdentity) obj;
268                 if (adminTenant == null) {
269                         if (other.adminTenant != null)
270                                 return false;
271                 } else if (!adminTenant.equals(other.adminTenant))
272                         return false;
273                 if (id == null) {
274                         if (other.id != null)
275                                 return false;
276                 } else if (!id.equals(other.id))
277                         return false;
278                 if (identityUrl == null) {
279                         if (other.identityUrl != null)
280                                 return false;
281                 } else if (!identityUrl.equals(other.identityUrl))
282                         return false;
283                 if (memberRole == null) {
284                         if (other.memberRole != null)
285                                 return false;
286                 } else if (!memberRole.equals(other.memberRole))
287                         return false;
288                 if (msoId == null) {
289                         if (other.msoId != null)
290                                 return false;
291                 } else if (!msoId.equals(other.msoId))
292                         return false;
293                 if (msoPass == null) {
294                         if (other.msoPass != null)
295                                 return false;
296                 } else if (!msoPass.equals(other.msoPass))
297                         return false;
298                 if (tenantMetadata == null) {
299                         if (other.tenantMetadata != null)
300                                 return false;
301                 } else if (!tenantMetadata.equals(other.tenantMetadata))
302                         return false;
303                 if (identityServerType == null) {
304                         if (other.getIdentityServerType() != null)
305                                 return false;
306                 } else if (!identityServerType.equals(other.getIdentityServerType()))
307                         return false;
308                 if (identityAuthenticationType == null) {
309                         if (other.getIdentityAuthenticationType() != null)
310                                 return false;
311                 } else if (!identityAuthenticationType.equals(other.getIdentityAuthenticationType()))
312                         return false;
313                 
314                 return true;
315         }
316 }