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

org.xlcloud.console.stacks.controller.LayerSecurityGroupBean 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.stacks.controller;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.xlcloud.console.extensions.scope.ViewScoped;
import org.xlcloud.console.repository.DataRepository;
import org.xlcloud.console.stacks.repository.LayersRepository;
import org.xlcloud.rest.exception.BaseException;
import org.xlcloud.rest.exception.ForbiddenException;
import org.xlcloud.rest.exception.ObjectNotFoundException;
import org.xlcloud.service.Layer;
import org.xlcloud.service.Stack;
import org.xlcloud.service.heat.template.resources.SecurityGroup;
import org.xlcloud.service.heat.template.resources.SecurityGroupRule;

/**
 * @author Andrzej Stasiak, AMG.net
 */
@Named
@ViewScoped
public class LayerSecurityGroupBean extends EditableSecurityGroupBean {
    
    private static final Logger LOG = Logger.getLogger(LayerSecurityGroupBean.class);

    @Inject
    private LayersRepository layersRepository;
    
    @Inject
    private DataRepository dataRepository;
    
    private Layer layer;
    
    @PostConstruct
    public void load() {
        layer = layersRepository.getLayer(getId());
        editedRule = new SecurityGroupRule();
        String securityGroupName = getSecurityGroupName();
        loadSecurityGroup(securityGroupName);
        
        try {
            Stack stack = dataRepository.getStack(layer.getStackId());
            subnets = dataRepository.listSubnets(stack.getProjectId());
        } catch (ForbiddenException | ObjectNotFoundException e) {
            LOG.warn("Forbidden to retrieve stack "+layer.getStackId()+" or project subnets");
        }
    }

    public void updateTemplate() {
        try {
            layersRepository.updateStackTemplate(layer.getId(), layer.toTemplate());
            addValidCallbackParameter();
        } catch (BaseException ex) {
            messages.addErrorMessage("layer.resouce.update.error", ex.getMessage());
        }
    }

    public void loadSecurityGroup(String groupName) {
        if(Layer.XIA_SECURITY_GROUP_NAME.equals(groupName)) {
            securityGroup = layer.getXiaSecurityGroup();
        } else {
            securityGroup = getOtherSecurityGroupByName(groupName);
        }
    }

    private SecurityGroup getOtherSecurityGroupByName(String name) {
        for(SecurityGroup securityGroup : layer.getOtherSecurityGroups().getSecurityGroup()) {
            if(name.equals(securityGroup.getName())) {
                return securityGroup;
            }
        }
        return null;
    }

    /** {@inheritDoc} */
    @Override
    public Long getStackId() {
        return layer.getStackId();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy