All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.huawei.openstack4j.openstack.identity.v2.domain.KeystoneAccess Maven / Gradle / Ivy

There is a newer version: 1.0.26
Show newest version
/*******************************************************************************
 * 	Copyright 2016 ContainX and OpenStack4j                                          
 * 	                                                                                 
 * 	Licensed under the Apache License, Version 2.0 (the "License"); you may not      
 * 	use this file except in compliance with the License. You may obtain a copy of    
 * 	the License at                                                                   
 * 	                                                                                 
 * 	    http://www.apache.org/licenses/LICENSE-2.0                                   
 * 	                                                                                 
 * 	Unless required by applicable law or agreed to in writing, software              
 * 	distributed under the License is distributed on an "AS IS" BASIS, WITHOUT        
 * 	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the         
 * 	License for the specific language governing permissions and limitations under    
 * 	the License.                                                                     
 *******************************************************************************/
package com.huawei.openstack4j.openstack.identity.v2.domain;

import static com.huawei.openstack4j.openstack.identity.functions.ServiceFunctions.*;

import java.io.Serializable;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.huawei.openstack4j.api.types.ServiceType;
import com.huawei.openstack4j.model.common.Link;
import com.huawei.openstack4j.model.identity.AuthStore;
import com.huawei.openstack4j.model.identity.AuthVersion;
import com.huawei.openstack4j.model.identity.v2.Access;
import com.huawei.openstack4j.model.identity.v2.Endpoint;
import com.huawei.openstack4j.model.identity.v2.Role;
import com.huawei.openstack4j.model.identity.v2.TokenV2;
import com.huawei.openstack4j.openstack.common.GenericLink;
import com.huawei.openstack4j.openstack.common.ListResult;

import com.google.common.base.MoreObjects;
import com.google.common.collect.SortedSetMultimap;
import com.google.common.collect.TreeMultimap;

@JsonRootName("access")
public class KeystoneAccess implements Access {

	private static final long serialVersionUID = 1L;
	private static final String CACHE_FMT = "%s:%s";
	private KeystoneToken token;
	private List serviceCatalog;
	private AccessUser user;
	private String endpoint;
	private AuthStore credentials;
	private TokenAuth tokenAuth;
	@JsonIgnore
	private volatile SortedSetMultimap aggregatedCatalog;

	/**
	 * @return the token
	 */
	public TokenV2 getToken() {
		return token;
	}

	/**
	 * @return the serviceCatalog
	 */
	public List getServiceCatalog() {
		return serviceCatalog;
	}

	/**
	 * A Lazy loading Aggregated Service Catalog Mapping.  The key is a stripped version service type or name with a collection
	 * of Services sorted by version
	 *
	 * @return sorted aggregate service catalog
	 */
	@Override
	@JsonIgnore
	public SortedSetMultimap getAggregatedCatalog() {
	    if (aggregatedCatalog == null) {
	        synchronized(this) {
	            if (aggregatedCatalog == null) {
	                aggregatedCatalog = TreeMultimap.create();
	                for (AccessService sc : serviceCatalog) {
	                    String nameKey = TYPE_WITHOUT_VERSION.apply(sc.getName());
	                    String typeKey = TYPE_WITHOUT_VERSION.apply(sc.getType());
	                    aggregatedCatalog.put(nameKey, sc);
                        aggregatedCatalog.put(typeKey, sc);
	                }
	            }
	        }
	    }
	    return aggregatedCatalog;
	}

	/**
	 * @return the user
	 */
	public UserDetails getUser() {
		return user;
	}

	public String getEndpoint() {
		return endpoint;
	}

	/**
	 * ONLY used for unit tests
	 * @param endpoint
	 */
	public void setEndpoint(String endpoint) {
		this.endpoint = endpoint;
	}

	public AuthStore getCredentials() {
		return credentials;
	}

	public TokenAuth getTokenAuth() {
	    return tokenAuth;
	}

	public boolean isCredentialType() {
	    return credentials != null;
	}

	public KeystoneAccess applyContext(String endpoint, AuthStore credentials) {
		this.credentials = credentials;
		this.endpoint = endpoint;
		return this;
	}

	public KeystoneAccess applyContext(String endpoint, TokenAuth token) {
        this.endpoint = endpoint;
        return this;
    }

	/**
	 * {@inheritDoc}
	 */
	public String toString() {
		return MoreObjects.toStringHelper(this).omitNullValues()
				.add("token", token).add("serviceCatalog", serviceCatalog).add("user", user)
				.toString();
	}

	@JsonIgnoreProperties(ignoreUnknown=true)
	public static final class AccessUser implements UserDetails, Serializable {

		private static final long serialVersionUID = 1L;

		private String id;
		private String name;
		private String username;
		private List roles;
		private Boolean enabled;

		@JsonProperty("roles_links")
		private List rolesLinks;

		public String getId() {
			return id;
		}

		public String getName() {
			return name;
		}

		public String getUsername() {
			return username;
		}

		public List getRoles() {
			return roles;
		}

		public List getRolesLinks() {
			return rolesLinks;
		}

		public boolean isEnabled() {
			return (enabled != null && enabled);
		}

		/**
		 * {@inheritDoc}
		 */
		public String toString() {
			return MoreObjects.toStringHelper(this).omitNullValues()
					.add("id", id).add("name", name).add("username", username).add("enabled", enabled)
					.add("roles", roles).add("rolesLinks", rolesLinks)
					.toString();
		}

		@JsonIgnoreProperties(ignoreUnknown=true)
		public static final class AccessRole extends KeystoneRole
		{
			private static final long serialVersionUID = 1L;

			/**
			 * {@inheritDoc}
			 */
			public String toString() {
				return MoreObjects.toStringHelper(this).omitNullValues()
						.add("id", getId()).add("name", getName()).add("tenantId", getTenantId())
						.toString();
			}
		}
	}

	public static class AccessUsers extends ListResult {

		private static final long serialVersionUID = 1L;
		@JsonProperty("users")
		private List list;

		public List value() {
			return list;
		}
	}


	public static final class AccessService implements Service, Comparable, Serializable
	{
		private static final long serialVersionUID = 1L;

		private String type;
		private String name;
		private List endpoints;
		private ServiceType serviceType;
        @JsonIgnore
        private Integer version;

		@JsonProperty("endpoints_links")
		private List endpointsLinks;

		/**
		 * @return the type
		 */
		public String getType() {
			return type;
		}

		/**
		 * @return the name
		 */
		public String getName() {
			return name;
		}

		@JsonIgnore
		public ServiceType getServiceType() {
			if (serviceType == null)
				serviceType = ServiceType.forName(name);
			if (serviceType == ServiceType.UNKNOWN)
			    serviceType = ServiceType.forName(type);
			return serviceType;
		}

		/**
		 * @return the endpoints
		 */
		public List getEndpoints() {
			return endpoints;
		}

		/**
		 * @return the endpointsLinks
		 */
		public List getEndpointsLinks() {
			return endpointsLinks;
		}

		@JsonIgnore
		public Integer getVersion() {
		    if (version == null) {
                version = VERSION_FROM_TYPE.apply(type);
            }
		    return version;
		}

		/**
		 * {@inheritDoc}
		 */
		public String toString() {
			return MoreObjects.toStringHelper(this).omitNullValues()
					.add("name", name).add("type", type).add("version", getVersion()).add("endpoints", endpoints).addValue("\n")
					.toString();
		}

        @Override
        public int compareTo(AccessService o) {
            return getVersion().compareTo(o.getVersion());
        }
	}

	@JsonIgnore
	@Override
	public AuthVersion getVersion() {
		return AuthVersion.V2;
	}

	@SuppressWarnings("unchecked")
	@JsonIgnore
	@Override
	public  T unwrap() {
		return (T) this;
	}

	@JsonIgnore
    @Override
    public String getCacheIdentifier() {
	    String uniq = null;
	    if (token.getTenant() != null)
	        uniq = token.getTenant().getId();
	    else if (user != null)
	        uniq = user.getId();
	    else
	        uniq = "";
	    return String.format(CACHE_FMT, endpoint, uniq);

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy