package com.platform.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.springframework.stereotype.Service; import com.platform.dao.VolumeDao; import com.platform.entities.Brick; import com.platform.entities.VolumeDataEntity; import com.platform.entities.VolumeInitEntity; import com.platform.http.gfs.SetVolume; import com.platform.service.IGfsService; import com.platform.service.IVolumeService; @Service(value = "volumeService") public class VolumeServiceImpl implements IVolumeService { public final static Logger log = Logger.getLogger(VolumeServiceImpl.class); /** gfs的 web 服务的 api */ SetVolume setVolume = new SetVolume(); @Resource(name = "gfsService") private IGfsService gfsService; @Resource(name = "volumeDao") private VolumeDao volumeDao; @Override public int save(VolumeDataEntity entity) throws Exception { int rest = 1; //createVolume("lili_test1", 0, "distributed", bricksToCreate, "/home/lili_test1_point") List vols = volumeDao.findAll(); Map volMap = new HashMap(); if (null!= vols) { for (VolumeInitEntity volumeInitEntity : vols) { volMap.put(volumeInitEntity.getPath(), null); } } List result = gfsService.getAllVolumes(); List addVolumes = new ArrayList(); VolumeDataEntity volumeTmp = null; if (null != result) { boolean isExits = false; //.trim() 去掉空格 for (VolumeDataEntity volumeOnServer : result) { if(entity.getName().trim().equals(volumeOnServer.getName().trim())){ isExits = true; //TODO 服务器上有该volume, volumeTmp = volumeOnServer; break; } } //如果服务器上没有该volume, if (!isExits) { addVolumes.add(entity); } } boolean isContinue = true; // 对比volume信息--原数据没有该volume,则新增volume: for (VolumeDataEntity add : addVolumes) { List bristr = new ArrayList(); if (null == add.getName() || "".equals(add.getName())) { continue; } if (null == add.getPath() || "".equals(add.getPath())) { continue; } for ( Brick bri : add.getBrick()) { if (null == bri.getIp() || "".equals(bri.getIp())) { continue; } if (null == bri.getPath() || "".equals(bri.getPath())) { continue; } bristr.add(bri.getIp()+":"+bri.getPath()); }; //创建volume if (bristr.size() > 0) { //TODO 换成 web 请求 int createreslt = setVolume.createVolume(add.getName(), 0, "Distribute", bristr, add.getPath()); if (createreslt != 1) { rest = createreslt; } } //记录volume信息, VolumeInitEntity volInSql = new VolumeInitEntity(); volInSql.setName(add.getName()); volInSql.setPath(add.getPath()); if (!volMap.keySet().contains(volInSql.getPath())) { volumeDao.save(volInSql); } isContinue = false; } // 修改 brick if (isContinue && null != volumeTmp) { List newBricks = entity.getBrick(); List serverBricks = volumeTmp.getBrick(); Map newMap = new HashMap(); Map serverMap = new HashMap(); for (Brick brick : newBricks) { //IP + path 才 确认唯一的 brick newMap.put(brick.getIp().trim() + ":" + brick.getPath().trim(), null); } for (Brick brick : serverBricks) { serverMap.put(brick.getIp().trim() + ":" + brick.getPath().trim(), null); } // ----对比volume信息--原数据有该volume,则对比brick信息,确认那几个brick是新增的,那几个brick是待删除的----start Set newBrickKeys = newMap.keySet(); Set serverBrickKeys = serverMap.keySet(); //新增的Brick的 Keys List newKeys = new ArrayList(); newBrickKeys.removeAll(serverBrickKeys); newKeys.addAll(newBrickKeys); // 新增brick s, if (newKeys.size() > 0) { //TODO 换成 web 请求 int createreslt = setVolume.addBrickVolume(entity.getName(), newKeys, 0, "distributed"); if (createreslt != 1) { rest = createreslt; } } //待删除的Brick的 Keys for (Brick brick : newBricks) { //IP + path 才 确认唯一的 brick newMap.put(brick.getIp().trim() + ":" + brick.getPath().trim(), null); } newBrickKeys = newMap.keySet(); serverBrickKeys.removeAll(newBrickKeys); List deleteKeys = new ArrayList(); deleteKeys.addAll(serverBrickKeys); // 删除brick s, if (deleteKeys.size() > 0) { //TODO 换成 web 请求 int createreslt = setVolume.deleteBrickVolume(entity.getName(), deleteKeys, 0, "distributed"); if (createreslt != 1) { rest = createreslt; } } // ---对比volume信息--原数据有该volume,则对比brick信息,确认那几个brick是新增的,那几个brick是待删除的----- end //TODO 查看状态 状态Started,Stopped,Created if (volumeTmp.isStatus() != entity.isStatus()) { if (entity.isStatus()) { this.start(entity); } else{ this.stop(entity); } } } return rest; } @Override public int delete(VolumeDataEntity entity) throws Exception { if (null == entity.getName() || "".equals(entity.getName())) { return -1; } //TODO 换成 web 请求 int rest = setVolume.deleteVolume(entity.getName()); volumeDao.remove(entity.getName()); return rest; } @Override public int start(VolumeDataEntity entity) throws Exception { if (null == entity.getName() || "".equals(entity.getName())) { return -1; } //TODO 换成 web 请求 return setVolume.startVolume(entity.getName()); } @Override public int stop(VolumeDataEntity entity) throws Exception { if (null == entity.getName() || "".equals(entity.getName())) { return -1; } //TODO 换成 web 请求 return setVolume.stopVolume(entity.getName()); } @Override public String getAllvolume() throws Exception { return setVolume.getAllvolume(); } }