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

org.xlcloud.console.controllers.utils.StackDisplayUtils Maven / Gradle / Ivy

The newest version!
/*
 * 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.console.controllers.utils;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.xlcloud.console.entitlements.engine.wrappers.StackEntitlementsEngineWrapper;
import org.xlcloud.console.wrappers.StateWrapper;
import org.xlcloud.service.Project;
import org.xlcloud.service.Stack;
import org.xlcloud.service.State;

/**
 * Util for {@link Stack} management user interface.
 * 
 * @author Andrzej Stasiak, AMG.net
 */
@Named
@ApplicationScoped
public class StackDisplayUtils {
    
    @Inject
    private StackEntitlementsEngineWrapper stackEntitlements;

    private static final Set START_CAPABLE_STATES;
    static {
        Set startCapableStates = new HashSet<>();
        Collections.addAll(startCapableStates, State.STOPPED);
        START_CAPABLE_STATES = Collections.unmodifiableSet(startCapableStates);
    }
    
    private static final Set STOP_CAPABLE_STATES;
    static {
        Set stopCapableStates = new HashSet<>();
        Collections.addAll(stopCapableStates, State.RUNNING);
        STOP_CAPABLE_STATES = Collections.unmodifiableSet(stopCapableStates);
    }
    
    private static final Set TERMINATE_CAPABLE_STATES;
    static {
        Set terminateCapableStates = new HashSet<>();
        Collections.addAll(terminateCapableStates,
                State.CONFIGURING,
                State.FAILED_ON_STARTING,
                State.FAILED_ON_STOPPING,
                State.FAILED_ON_UPDATE,
                State.FAILED_ON_PAUSE,
                State.FAILED_ON_RESUME);
        TERMINATE_CAPABLE_STATES = Collections.unmodifiableSet(terminateCapableStates);
    }
    
    private static final Set RESUME_CAPABLE_STATES;
    static {
        Set resumeCapableStates = new HashSet<>();
        Collections.addAll(resumeCapableStates, State.PAUSED);
        RESUME_CAPABLE_STATES = Collections.unmodifiableSet(resumeCapableStates);
    }
    
    /**
     * Checks if stack permanent features allow it to be started.
     * Permanent features are all but the state of stack which may be filtered on client-side
     * basing on the changing stack status: stack origin, project status, entitlements.
     * @param stack
     * @param project
     * @return
     */
    public boolean renderStart(Stack stack, Project project) {
        return isProjectActive(project) && stackEntitlements.canStart(stack);
    }
    
    /**
     * Checks if stack permanent features allow it to be stopped.
     * Permanent features are all but the state of stack which may be filtered on client-side
     * basing on the changing stack status: stack origin, project status, entitlements.
     * @param stack
     * @param project
     * @return
     */
    public boolean renderStop(Stack stack, Project project) {
        return isProjectActive(project) && stackEntitlements.canStop(stack);
    }
    
    /**
     * Checks if stack permanent features allow it to be teminated.
     * Permanent features are all but the state of stack which may be filtered on client-side
     * basing on the changing stack status: stack origin, project status, entitlements.
     * @param stack
     * @param project
     * @return
     */
    public boolean renderTerminate(Stack stack, Project project) {
        return isProjectActive(project) && stackEntitlements.canStop(stack);
    }
    
    /**
     * Checks if stack permanent features allow it to be suspended.
     * Permanent features are all but the state of stack which may be filtered on client-side
     * basing on the changing stack status: stack origin, project status, entitlements.
     * @param stack
     * @param project
     * @return
     */
    public boolean renderSuspend(Stack stack, Project project) {
        return isProjectActive(project) && stackEntitlements.canUpdateSession(stack);
    }
    
    /**
     * Checks if stack permanent features allow it to be resumed.
     * Permanent features are all but the state of stack which may be filtered on client-side
     * basing on the changing stack status: stack origin, project status, entitlements.
     * @param stack
     * @param project
     * @return
     */
    public boolean renderResume(Stack stack, Project project) {
        return isProjectActive(project) && stackEntitlements.canUpdateSession(stack);
    }
    
    /**
     * Checks if stack permanent features allow it to be deleted.
     * Permanent features are all but the state of stack which may be filtered on client-side
     * basing on the changing stack status: stack origin, project status, entitlements.
     * @param stack
     * @param project
     * @return
     */
    public boolean renderDelete(Stack stack, Project project) {
        return stackEntitlements.canDelete(stack);
    }
    
    /**
     * Checks if stack features allow it to be started.
     * @param stack
     * @param project
     * @return
     */
    public boolean renderPromote(Stack stack, Project project) {
        return stackEntitlements.canPromoteToPrivate(stack) || stackEntitlements.canPromoteToPublic(stack);
    }
    
    /**
     * Checks if stack features allow it to have an application deployed.
     * @param stack
     * @param project
     * @return
     */
    public boolean renderDeploy(Stack stack, Project project) {
        return isProjectActive(project) && stackEntitlements.canDeployApplication(stack);
    }
    
    /**
     * Checks if the stack current stack allows it to be started.
     * It verifies only status of the stack and may be used if no client-side
     * filtering is done.
     * @param state
     * @return
     */
    public boolean isAbleToStart(StateWrapper state) {
        return START_CAPABLE_STATES.contains(state.getCompleteState());
    }
    
    /**
     * Checks if the stack current stack allows it to be stopped.
     * It verifies only status of the stack and may be used if no client-side
     * filtering is done.
     * @param state
     * @return
     */
    public boolean isAbleToStop(StateWrapper state) {
        return STOP_CAPABLE_STATES.contains(state.getCompleteState());
    }
    
    /**
     * Checks if the stack current stack allows it to be terminated.
     * It verifies only status of the stack and may be used if no client-side
     * filtering is done.
     * @param state
     * @return
     */
    public boolean isAbleToTerminate(StateWrapper state) {
        return TERMINATE_CAPABLE_STATES.contains(state.getCompleteState());
    }
    
    /**
     * Checks if the stack current stack allows it to be deleted.
     * It verifies only status of the stack and may be used if no client-side
     * filtering is done.
     * @param state
     * @return
     */
    public boolean isAbleToDelete(StateWrapper state) {
        return state.isStopped();
    }
    
    /**
     * Checks if the stack current stack allows it to be suspended.
     * It verifies only status of the stack and may be used if no client-side
     * filtering is done.
     * @param state
     * @return
     */
    public boolean isAbleToSuspend(StateWrapper state) {
        return isAbleToStop(state);
    }
    
    /**
     * Checks if the stack current stack allows it to be resumed.
     * It verifies only status of the stack and may be used if no client-side
     * filtering is done.
     * @param state
     * @return
     */
    public boolean isAbleToResume(StateWrapper state) {
        return RESUME_CAPABLE_STATES.contains(state.getCompleteState());
    }
    
    private boolean isProjectActive(Project project) {
        return project!=null
                && project.getStatus() != null
                && project.getStatus().isActive();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy