org.xlcloud.service.impl.AccountsImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.xlcloud.service.impl.AccountsImpl.java

Source

/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * 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 org.xlcloud.service.impl;

import static org.xlcloud.validation.Validations.validateThat;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.log4j.Logger;

import org.xlcloud.service.Account;
import org.xlcloud.service.Accounts;
import org.xlcloud.service.Application;
import org.xlcloud.service.Applications;
import org.xlcloud.service.CatalogScopeType;
import org.xlcloud.service.Group;
import org.xlcloud.service.Groups;
import org.xlcloud.service.Image;
import org.xlcloud.service.Images;
import org.xlcloud.service.Project;
import org.xlcloud.service.Projects;
import org.xlcloud.service.Repositories;
import org.xlcloud.service.Repository;
import org.xlcloud.service.User;
import org.xlcloud.service.Users;
import org.xlcloud.service.VirtualClusterDefinition;
import org.xlcloud.service.VirtualClusterDefinitions;
import org.xlcloud.service.VirtualClusters;
import org.xlcloud.service.api.AccountsApi;
import org.xlcloud.service.exception.VcmsDuplicatedEntityException;
import org.xlcloud.service.exception.VcmsException;
import org.xlcloud.service.exception.VcmsObjectNotFoundException;
import org.xlcloud.service.exception.VcmsValidationException;
import org.xlcloud.service.iam.Utils;
import org.xlcloud.service.manager.AccountsManager;
import org.xlcloud.service.manager.ApplicationsManager;
import org.xlcloud.service.manager.GroupsManager;
import org.xlcloud.service.manager.ImagesManager;
import org.xlcloud.service.manager.ProjectsManager;
import org.xlcloud.service.manager.UsersManager;
import org.xlcloud.service.manager.VcbsManager;
import org.xlcloud.service.manager.VirtualClusterDefinitionsManager;
import org.xlcloud.service.manager.VirtualClustersManager;

/**
 * TODO [tadamcze] documentation
 * 
 * @author Maciej Osyda, AMG.net
 * @author Tomek Adamczewski, AMG.net
 * @author Ksiczyk Grzegorz, AMG.net
 * @author Andrzej Stasiak, AMG.net
 */
public class AccountsImpl implements AccountsApi {

    private static final Logger LOG = Logger.getLogger(AccountsImpl.class);

    @Inject
    private ApplicationsManager applicationsManager;

    @Inject
    private AccountsManager accountsManager;

    @Inject
    private GroupsManager groupsManager;

    @Inject
    private UsersManager usersManager;

    @Inject
    private VirtualClustersManager virtualClustersManager;

    @Inject
    private ApplicationsManager applicationManager;

    @Inject
    private VirtualClusterDefinitionsManager vcDefsManager;

    @Inject
    private ImagesManager imagesManager;

    @Inject
    private VcbsManager vcbsManager;

    @Inject
    private ProjectsManager projectsManager;

    /** {@inheritDoc} */
    @Override
    public Accounts getAccounts() {
        return accountsManager.getAccounts();
    }

    /** {@inheritDoc} */
    @Override
    public Account getAccount(Long id) {
        return accountsManager.getAccount(id);
    }

    /** {@inheritDoc} */
    @Override
    public Account createAccount(Account account) {
        LOG.debug("AdminService.createAccount(" + account + ") called.");
        ModelValidator.validateAccount(account);
        return accountsManager.createAccount(account);
    }

    /** {@inheritDoc} */
    @Override
    public void removeAccount(Long id) {

        Utils.verifyId(id);

        Account account = accountsManager.getAccount(id);
        if (account.getUsers() != null && account.getUsers().getUser().size() > 0) {
            String message = "Cannot remove account - it has not empty users list";
            LOG.info(message);
            throw new VcmsValidationException(message);
        }

        VirtualClusters virtualClusters = virtualClustersManager.getProjectVirtualClusters(id);
        if (virtualClusters != null && virtualClusters.getVirtualCluster().size() > 0) {
            String message = "Cannot remove account - it has not empty virtual clusteers list";
            LOG.info(message);
        }

        accountsManager.removeAccount(id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Projects listProjects(Long accountId) throws VcmsObjectNotFoundException {
        return projectsManager.listForAccount(accountId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Project createProject(Long accountId, Project project)
            throws VcmsObjectNotFoundException, VcmsValidationException, VcmsDuplicatedEntityException {
        // assert that account exists
        accountsManager.getAccount(accountId);
        LOG.debug("account id validated: account with id=" + accountId + " exists");

        project.setAccountId(accountId);
        ModelValidator.validateProject(project);

        return projectsManager.create(project);
    }

    /** {@inheritDoc} */
    @Override
    public VirtualClusters listVirtualClusters(Long id) {
        return virtualClustersManager.listForAccount(id);
    }

    /** {@inheritDoc} */
    @Override
    public Users getUsersForAccount(Long id) {
        return usersManager.getUsersByAccount(id);
    }

    /** {@inheritDoc} */
    @Override
    public User createAccountUser(Long id, User user) {
        ModelValidator.validateUser(user);
        user.setAccountId(id);
        user = usersManager.addUser(user);
        return user;
    }

    /** {@inheritDoc} */
    @Override
    public Group createAccountGroup(Long accountId, Group group) {
        ModelValidator.validateGroup(group);
        return groupsManager.createGroup(accountId, group);
    }

    /** {@inheritDoc} */
    @Override
    public Groups getAccountGroups(Long accountId) {
        return groupsManager.getAccountGroups(accountId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public VirtualClusterDefinition createVirtualClusterDefinition(Long accountId,
            VirtualClusterDefinition definition)
            throws VcmsObjectNotFoundException, VcmsValidationException, VcmsException {
        validateThat(accountId).as("accountId").isNotNull();

        if (definition.getCatalogScope() == null) {
            definition.setCatalogScope(CatalogScopeType.PRIVATE);
        } else if (!CatalogScopeType.PRIVATE.equals(definition.getCatalogScope())) {
            throw new VcmsValidationException("Catalog Scope exception",
                    "During register of private virtual cluster definition catalog scope cannot be set to: "
                            + definition.getCatalogScope().value());
        }

        ModelValidator.validateVirtualClusterDefinition(definition);
        ModelValidator.validateParameters(definition.getTemplate().getParameters());
        vcbsManager.validateTemplate(definition.getTemplate());

        definition.setAccountId(accountId);
        definition.setCatalogScope(CatalogScopeType.PRIVATE);

        return vcDefsManager.create(definition);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public VirtualClusterDefinitions listVirtualClusterDefinitions(Long accountId, List<Long> appIds, String type,
            List<String> tags) throws VcmsObjectNotFoundException {
        validateThat(accountId).as("accountId").isNotNull();

        if (!appIds.isEmpty()) {
            return getVirtualClusterDefinitionsForApps(accountId, appIds);
        }
        if (StringUtils.isNotBlank(type)) {
            return getVirtualClusterDefinitionsForTypeAndTags(accountId, type, tags);
        }

        return vcDefsManager.list(accountId);
    }

    /** {@inheritDoc} */
    @Override
    public Application registerApplication(Long accountId, Application application) {
        validateThat(accountId).as("accountId").isNotNull();

        if (application.getId() != null) {
            LOG.warn("Tried to register new application: " + application + "with id");
            throw new VcmsValidationException("Application Id exception",
                    "During register of applcation Id cannot be set: to update application use: /applications/id service");
        }
        if (application.getCatalogScope() == null) {
            application.setCatalogScope(CatalogScopeType.PRIVATE);
        } else if (!CatalogScopeType.PRIVATE.equals(application.getCatalogScope())) {
            LOG.warn("Tried to register application: " + application + "with invalid scope");
            throw new VcmsValidationException("Catalog Scope exception",
                    "During register of public application catalog scope cannot be set to: "
                            + application.getCatalogScope().value());
        }

        if (application.getAccountId() == null) {
            application.setAccountId(accountId);
        } else if (accountId != application.getAccountId()) {
            LOG.warn("Tried to register application: " + application
                    + "for different account on resource: /accounts/" + accountId + "/applications");
            throw new VcmsValidationException("Account id on application does not match resource");
        }

        ModelValidator.validateApplication(application);
        return applicationManager.registerApplication(accountId, application);
    }

    /** {@inheritDoc} */
    @Override
    public Applications getApplicationsForAccount(Long accountId) {
        validateThat(accountId).as("accountId").isNotNull();
        Collection<Application> applicationCollection = applicationManager
                .getPrivateApplicationsForAccount(accountId);
        Applications applications = new Applications();
        applications.getApplication().addAll(applicationCollection);
        return applications;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Image createImage(Long accountId, Image image) {
        image.setAccountId(accountId);
        if (CatalogScopeType.PUBLIC.equals(image.getCatalogScope())) {
            throw new VcmsValidationException("Catalog scope cannot be public when posting account-scoped image.");
        }
        image.setCatalogScope(CatalogScopeType.PRIVATE);
        return imagesManager.create(image);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Images listImages(Long accountId) {
        return imagesManager.list(accountId);
    }

    private VirtualClusterDefinitions getVirtualClusterDefinitionsForApps(Long accountId, List<Long> appIds) {
        LOG.info("Getting VirtualClusterDefinition for applications: [ "
                + ToStringBuilder.reflectionToString(appIds) + " ]");
        Set<String> tags = applicationsManager.getTagsForApplications(new HashSet<Long>(appIds));
        String type = applicationsManager.getCommonTypeForApplications(new HashSet<Long>(appIds));
        return vcDefsManager.listByTypeAndTags(accountId, type, tags);
    }

    private VirtualClusterDefinitions getVirtualClusterDefinitionsForTypeAndTags(Long accountId, String type,
            List<String> tags) {
        LOG.info("Getting VirtualClusteDefinition by type: [ " + type + " ], and tags: [ "
                + ToStringBuilder.reflectionToString(tags) + " ].");
        return vcDefsManager.listByTypeAndTags(accountId, type, new HashSet<String>(tags));
    }

    protected void setAccountsManager(AccountsManager accountsManager) {
        this.accountsManager = accountsManager;
    }

    @Override
    public Repositories listRepositories(Long accountId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Repository createRepository(Long accountId, Repository repo) {
        // TODO Auto-generated method stub
        return null;
    }
}