jp.co.ctc_g.rack.core.process.ProcessServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.ctc_g.rack.core.process.ProcessServiceImpl.java

Source

/*
 *  Copyright (c) 2013 ITOCHU Techno-Solutions Corporation.
 *
 *  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 jp.co.ctc_g.rack.core.process;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import jp.co.ctc_g.jfw.core.internal.InternalException;
import jp.co.ctc_g.jfw.core.util.Maps;
import jp.co.ctc_g.rack.core.model.process.Metadata;
import jp.co.ctc_g.rack.core.model.process.ProcessCreate;
import jp.co.ctc_g.rack.core.model.process.ProcessDelete;
import jp.co.ctc_g.rack.core.model.process.ProcessEntity;
import jp.co.ctc_g.rack.core.model.process.Processes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Strings;

/**
 * <p>
 * ?????
 * </p>
 * @author ITOCHU Techno-Solutions Corporation.
 */
@Service
public class ProcessServiceImpl implements ProcessService {

    @Autowired
    private ProcessRepository<ProcessEntity> repository;

    @Autowired
    private ProcessConnector<ProcessEntity> connector;

    /**
     * {@inheritDoc}
     */
    @Override
    public void create(ProcessCreate create) {

        if (!Strings.isNullOrEmpty(create.getPpid())) {
            ProcessEntity server = show(create.getGroupId(), create.getPpid());
            if (Strings.isNullOrEmpty(create.getImageId())) {
                create.setImageId(server.getImageId());
            }
            if (Strings.isNullOrEmpty(create.getFlavorId())) {
                create.setFlavorId(server.getFlavorId());
            }
            if (Strings.isNullOrEmpty(create.getSecurityGroup())) {
                create.setSecurityGroup(server.getSecurityGroup());
            }
        }
        ProcessEntity server = connector.create(create);
        repository.save(server);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Processes list(String groupId) {

        List<ProcessEntity> serverList = repository.findBy(groupId);
        Processes servers = new Processes();
        servers.setServers(connector.show(serverList));
        return servers;

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ProcessEntity show(String groupId, String pid) {

        ProcessEntity server = repository.findOneBy(groupId, pid);
        if (server == null) {
            throw new InternalException(ProcessServiceImpl.class, "E-PROCESS#0001",
                    Maps.hash("groupId", groupId).map("pid", pid));
        }
        ProcessEntity entity = connector.show(server);
        return entity;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void delete(ProcessDelete delete) {

        List<ProcessEntity> servers = Collections.synchronizedList(new ArrayList<ProcessEntity>());
        if (Strings.isNullOrEmpty(delete.getPid())) {
            servers = repository.findBy(delete.getGroupId());
            if (servers.isEmpty()) {
                throw new InternalException(ProcessServiceImpl.class, "E-PROCESS#0004",
                        Maps.hash("groupId", delete.getGroupId()));
            }
        } else {
            servers.add(show(delete.getGroupId(), delete.getPid()));
            for (int i = 0; i < servers.size(); i++) {
                List<ProcessEntity> childservers = repository.findByPpid(servers.get(i).getGroupId(),
                        servers.get(i).getPid());
                if (childservers != null && !childservers.isEmpty()) {
                    servers.addAll(childservers);
                }
            }
        }
        for (ProcessEntity sv : servers) {
            connector.delete(sv);
            repository.delete(sv.getGroupId(), sv.getPid());
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setMetadata(String groupId, String pid, Metadata metadata) {

        ProcessEntity server = show(groupId, pid);
        Map<String, String> map = metadata.getMetadata();
        for (Map.Entry<String, String> e : map.entrySet()) {
            if (server.getMetadata().containsKey(e.getKey())) {
                throw new InternalException(ProcessServiceImpl.class, "E-PROCESS#0002",
                        Maps.hash("key", e.getKey()).map("value", e.getValue()));
            }
        }
        repository.updateMetadata(groupId, pid, metadata.getMetadata());
        createOrUpdateMetadata(groupId, pid);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void updateMetadata(String groupId, String pid, Metadata metadata) {

        ProcessEntity server = show(groupId, pid);
        Map<String, String> map = metadata.getMetadata();
        for (Map.Entry<String, String> e : map.entrySet()) {
            if (!server.getMetadata().containsKey(e.getKey())) {
                throw new InternalException(ProcessServiceImpl.class, "E-PROCESS#0003",
                        Maps.hash("key", e.getKey()).map("value", e.getValue()));
            }
        }
        repository.updateMetadata(groupId, pid, metadata.getMetadata());
        createOrUpdateMetadata(groupId, pid);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void deleteMetadata(String groupId, String pid, String keys) {

        ProcessEntity server = show(groupId, pid);
        List<String> keyList = Arrays.asList(keys.split(","));
        repository.deleteMetadata(groupId, pid, keyList);
        connector.deleteMetadata(server, keyList);
    }

    private void createOrUpdateMetadata(String groupId, String pid) {

        ProcessEntity server = show(groupId, pid);
        Metadata metadata = new Metadata();
        metadata.setMetadata(server.getMetadata());
        connector.setMetadata(server, metadata);
    }
}