49c80b44b566ca9d08c1959612c0f65afd5a4c48
[so.git] / adapters / mso-adapter-utils / src / main / java / org / onap / so / cloud / authentication / AuthenticationMethodFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.so.cloud.authentication;
22
23 import java.util.Collections;
24
25 import org.onap.so.cloud.authentication.models.RackspaceAuthentication;
26 import org.onap.so.db.catalog.beans.AuthenticationType;
27 import org.onap.so.db.catalog.beans.CloudIdentity;
28 import org.onap.so.utils.CryptoUtils;
29 import org.springframework.stereotype.Component;
30
31 import com.woorea.openstack.keystone.model.Authentication;
32 import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
33 import com.woorea.openstack.keystone.v3.model.Authentication.Identity;
34 import com.woorea.openstack.keystone.v3.model.Authentication.Identity.Password;
35 import com.woorea.openstack.keystone.v3.model.Authentication.Identity.Password.User;
36 import com.woorea.openstack.keystone.v3.model.Authentication.Identity.Password.User.Domain;
37 import com.woorea.openstack.keystone.v3.model.Authentication.Scope;
38 import com.woorea.openstack.keystone.v3.model.Authentication.Scope.Project;
39
40 /**
41  * This factory manages all the wrappers associated to authentication types.
42  *
43  */
44 @Component
45 public final class AuthenticationMethodFactory {
46
47         public final Authentication getAuthenticationFor(CloudIdentity cloudIdentity) {
48                 if (cloudIdentity == null) {
49                         throw new IllegalArgumentException("Cloud identity cannot be null");
50                 }
51                 if ((cloudIdentity.getIdentityAuthenticationType() == null)|| ("".equals(cloudIdentity.getIdentityAuthenticationType().toString()))) {
52                         throw new IllegalArgumentException("Cloud identity authentication type cannot be null or empty, provided value is " + cloudIdentity.getIdentityAuthenticationType() + ".");
53                 }
54                 AuthenticationType authenticationType = cloudIdentity.getIdentityAuthenticationType();
55                 if (AuthenticationType.RACKSPACE_APIKEY.equals(authenticationType)) {
56                         return new RackspaceAuthentication (cloudIdentity.getMsoId (), CryptoUtils.decryptCloudConfigPassword(cloudIdentity.getMsoPass ()));
57                 } else {
58                         return new UsernamePassword (cloudIdentity.getMsoId (), CryptoUtils.decryptCloudConfigPassword(cloudIdentity.getMsoPass ()));
59                 }
60         }
61         
62         
63         public final com.woorea.openstack.keystone.v3.model.Authentication getAuthenticationForV3(CloudIdentity cloudIdentity, String tenantId) {
64                 Identity identity = new Identity();
65                 Password password = new Password();
66                 User user = new User();
67                 Domain userDomain = new Domain();
68                 Scope scope = new Scope();
69                 Project project = new Project();
70                 Project.Domain projectDomain = new Project.Domain();
71                 userDomain.setName(cloudIdentity.getUserDomainName());
72                 projectDomain.setName(cloudIdentity.getProjectDomainName());
73                 user.setName(cloudIdentity.getMsoId());
74                 user.setPassword(CryptoUtils.decryptCloudConfigPassword(cloudIdentity.getMsoPass()));
75                 user.setDomain(userDomain);
76                 password.setUser(user);
77                 project.setDomain(projectDomain);
78                 project.setId(tenantId);
79                 scope.setProject(project);
80                 identity.setPassword(password);
81                 identity.setMethods(Collections.singletonList("password"));
82                 com.woorea.openstack.keystone.v3.model.Authentication v3Auth = new com.woorea.openstack.keystone.v3.model.Authentication();
83                 v3Auth.setIdentity(identity);
84                 v3Auth.setScope(scope);
85                 return v3Auth;
86         }
87 }