update activate

This commit is contained in:
fanqq 2015-05-27 11:13:03 +08:00
parent f08bf83890
commit ecb8566fc0
6 changed files with 73 additions and 105 deletions

View file

@ -59,59 +59,25 @@ public class ActivateResource {
@GET @GET
@Path("/activate") @Path("/activate")
@Authorize(name="activate") @Authorize(name="activate")
public Response list(@Context HttpServletRequest request,@Context HttpHeaders hh,@QueryParam("slbName") List<String> slbNames, @QueryParam("appName") List<String> appNames)throws Exception{ public Response activateByName(@Context HttpServletRequest request,@Context HttpHeaders hh,@QueryParam("slbName") List<String> slbNames, @QueryParam("GroupName") List<String> groupNames)throws Exception{
return activateAll(slbNames,appNames,hh); // return activateAll(slbNames,groupNames,hh);
return null;
} }
@POST @GET
@Path("/activate") @Path("/activate")
@Authorize(name="activate") @Authorize(name="activate")
public Response activate(@Context HttpServletRequest request,@Context HttpHeaders hh, String req) throws Exception { public Response activateById(@Context HttpServletRequest request,@Context HttpHeaders hh,@QueryParam("slbId") List<Long> slbIds, @QueryParam("GroupId") List<Long> groupIds)throws Exception{
ConfReq confreq = null; return activateAll(slbIds,groupIds,hh);
if (hh.getMediaType().equals(MediaType.APPLICATION_XML_TYPE)) {
confreq = DefaultSaxParser.parseEntity(ConfReq.class, req);
}else //if (hh.getMediaType().equals(MediaType.APPLICATION_JSON_TYPE))
{
confreq = DefaultJsonParser.parse(ConfReq.class, req);
} }
AssertUtils.isNull(confreq,"the parameter is illegal!\n request parameter: "+req);
List<String> appNameList = Lists.transform(confreq.getConfAppNames(),new Function<ConfAppName, String>(){ private Response activateAll(List<Long> slbIds,List<Long> groupIds, HttpHeaders hh)throws Exception{
@Nullable
@Override
public String apply(@Nullable ConfAppName confAppName) {
try {
return confAppName.getAppname();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
});
List<String> slbNameList = Lists.transform(confreq.getConfSlbNames(),new Function<ConfSlbName, String>(){ AssertUtils.arrertNotEquels(0,slbIds.size()+groupIds.size(),"slbIds list and groupIds list are empty!");
@Nullable
@Override
public String apply(@Nullable ConfSlbName slbName) {
try {
return slbName.getSlbname();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
});
return activateAll(slbNameList,appNameList,hh);
}
private Response activateAll(List<String> slbNames,List<String> appNames, HttpHeaders hh)throws Exception{
AssertUtils.arrertNotEquels(0,slbNames.size()+appNames.size(),"slbName list and appName list are empty!");
//update active action to conf-slb-active and conf-app-active //update active action to conf-slb-active and conf-app-active
activateConfService.activate(slbNames,appNames); activateConfService.activate(slbIds,groupIds);
//find all slbs which need build config //find all slbs which need build config
Set<String> slbList = buildInfoService.getAllNeededSlb(slbNames, appNames); Set<String> slbList = buildInfoService.getAllNeededSlb(slbNames, appNames);

View file

@ -12,26 +12,26 @@ public interface ActivateService extends Repository {
/** /**
* update active data by slbname * update active data by slbname
* @param slbname the slb name * @param slbId the slb id
* @return * @return
* @throws Exception * @throws Exception
*/ */
public void activeSlb(String slbname) throws Exception; public void activeSlb(long slbId) throws Exception;
/** /**
* update active data by slbname * update active data by slbname
* @param appname the slb name * @param groupId the Group id
* @return * @return
* @throws Exception * @throws Exception
*/ */
public void activeApp(String appname) throws Exception; public void activeGroup(long groupId) throws Exception;
/** /**
* update active data by slbnames and appnames * update active data by slbnames and appnames
* @param slbNames the slb names * @param slbIds the slb ids
* @param appNames the app names * @param groupIds the group ids
* @return * @return
* @throws Exception * @throws Exception
*/ */
public void activate(List<String> slbNames, List<String> appNames)throws Exception; public void activate(List<Long> slbIds, List<Long> groupIds)throws Exception;
} }

View file

@ -4,6 +4,8 @@ import com.ctrip.zeus.dal.core.*;
import com.ctrip.zeus.model.entity.App; import com.ctrip.zeus.model.entity.App;
import com.ctrip.zeus.model.entity.AppSlb; import com.ctrip.zeus.model.entity.AppSlb;
import com.ctrip.zeus.model.entity.Archive; import com.ctrip.zeus.model.entity.Archive;
import com.ctrip.zeus.model.entity.Group;
import com.ctrip.zeus.model.entity.GroupSlb;
import com.ctrip.zeus.model.transform.DefaultSaxParser; import com.ctrip.zeus.model.transform.DefaultSaxParser;
import com.ctrip.zeus.service.Activate.ActivateService; import com.ctrip.zeus.service.Activate.ActivateService;
import com.ctrip.zeus.service.model.ArchiveService; import com.ctrip.zeus.service.model.ArchiveService;
@ -26,75 +28,75 @@ import java.util.List;
public class ActivateServiceImpl implements ActivateService { public class ActivateServiceImpl implements ActivateService {
@Resource @Resource
private ConfAppActiveDao confAppActiveDao; private ConfGroupActiveDao confGroupActiveDao;
@Resource @Resource
private ConfSlbActiveDao confSlbActiveDao; private ConfSlbActiveDao confSlbActiveDao;
@Resource @Resource
private ArchiveService archiveService; private ArchiveService archiveService;
@Resource @Resource
ConfAppSlbActiveDao confAppSlbActiveDao; ConfGroupSlbActiveDao confGroupSlbActiveDao;
private Logger logger = LoggerFactory.getLogger(ActivateServiceImpl.class); private Logger logger = LoggerFactory.getLogger(ActivateServiceImpl.class);
@Override @Override
public void activeSlb(String name) throws Exception { public void activeSlb(long slbId) throws Exception {
Archive archive = archiveService.getLatestSlbArchive(name); Archive archive = archiveService.getLatestSlbArchive(slbId);
if (archive==null) if (archive==null)
{ {
logger.info("getLatestSlbArchive return Null! SlbName: "+name); logger.info("getLatestSlbArchive return Null! SlbID: "+slbId);
AssertUtils.isNull(archive,"[Activate]getLatestSlbArchive return Null! SlbName: "+name); AssertUtils.isNull(archive,"[Activate]getLatestSlbArchive return Null! SlbID: "+slbId);
return; return;
} }
ConfSlbActiveDo c = new ConfSlbActiveDo().setCreatedTime(new Date()); ConfSlbActiveDo c = new ConfSlbActiveDo().setCreatedTime(new Date());
c.setName(archive.getName()).setContent(archive.getContent()).setVersion(archive.getVersion()); c.setSlbId(archive.getId()).setContent(archive.getContent()).setVersion(archive.getVersion());
confSlbActiveDao.insert(c); confSlbActiveDao.insert(c);
logger.debug("Conf Slb Active Inserted: [name: "+c.getName()+",Content: "+c.getContent()+",Version: "+c.getVersion()+"]"); logger.debug("Conf Slb Active Inserted: [SlbID: "+c.getSlbId()+",Content: "+c.getContent()+",Version: "+c.getVersion()+"]");
} }
@Override @Override
public void activeApp(String name) throws Exception { public void activeGroup(long groupId) throws Exception {
Archive archive = archiveService.getLatestAppArchive(name); Archive archive = archiveService.getLatestGroupArchive(groupId);
if (archive==null) if (archive==null)
{ {
logger.info("getLatestAppArchive return Null! AppName: "+name); logger.info("getLatestAppArchive return Null! GroupID: "+groupId);
AssertUtils.isNull(archive,"[Activate]getLatestAppArchive return Null! AppName: "+name); AssertUtils.isNull(archive,"[Activate]getLatestAppArchive return Null! GroupID: "+groupId);
return; return;
} }
ConfAppActiveDo c = new ConfAppActiveDo().setCreatedTime(new Date()); ConfGroupActiveDo c = new ConfGroupActiveDo().setCreatedTime(new Date());
c.setName(archive.getName()).setContent(archive.getContent()).setVersion(archive.getVersion()); c.setGroupId(archive.getId()).setContent(archive.getContent()).setVersion(archive.getVersion());
confAppActiveDao.insert(c); confGroupActiveDao.insert(c);
logger.debug("Conf App Active Inserted: [name: "+c.getName()+",Content: "+c.getContent()+",Version: "+c.getVersion()+"]"); logger.debug("Conf Group Active Inserted: [GroupId: "+c.getId()+",Content: "+c.getContent()+",Version: "+c.getVersion()+"]");
App app = DefaultSaxParser.parseEntity(App.class, c.getContent()); Group group = DefaultSaxParser.parseEntity(Group.class, c.getContent());
AssertUtils.isNull(app,"App_ctive.content XML is illegal!"); AssertUtils.isNull(group,"App_ctive.content XML is illegal!");
confAppSlbActiveDao.deleteByAppName(new ConfAppSlbActiveDo().setAppName(name)); confGroupSlbActiveDao.deleteByGroupId(new ConfGroupSlbActiveDo().setGroupId(groupId));
for (AppSlb appSlb:app.getAppSlbs()) for (GroupSlb groupSlb:group.getGroupSlbs())
{ {
confAppSlbActiveDao.insert(new ConfAppSlbActiveDo().setAppName(name) confGroupSlbActiveDao.insert(new ConfGroupSlbActiveDo().setGroupId(groupId)
.setSlbVirtualServerName(appSlb.getVirtualServer().getName()) .setPriority(groupSlb.getPriority())
.setPriority(appSlb.getPriority()) .setSlbId(groupSlb.getSlbName()).setDataChangeLastTime(new Date()))
.setSlbName(appSlb.getSlbName()).setDataChangeLastTime(new Date())); .setSlbVirtualServerId(groupSlb.getVirtualServer().getName());
} }
} }
@Override @Override
public void activate(List<String> slbNames, List<String> appNames) throws Exception { public void activate(List<Long> slbIds, List<Long> groupIds) throws Exception {
for (String slbName : slbNames) { for (Long slbId : slbIds) {
activeSlb(slbName); activeSlb(slbId);
} }
for (String appName : appNames) { for (Long groupId : groupIds) {
activeApp(appName); activeGroup(groupId);
} }
} }
} }

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<entities do-package="com.ctrip.zeus.dal.core" gen="true" do-class-suffix="Do"> <entities do-package="com.ctrip.zeus.dal.core" gen="true" do-class-suffix="Do">
<entity name="conf-app-active" table="conf_app_active" alias="caa"> <entity name="conf-group-active" table="conf_group_active" alias="cga">
<var name="names" value-type="String[]"/> <var name="ids" value-type="Long[]"/>
<query-defs> <query-defs>
<query name="insert" type="INSERT"> <query name="insert" type="INSERT">
<statement> <statement>
@ -15,20 +15,20 @@
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-all-by-names" type="SELECT" multiple="true"> <query name="find-all-by-group-ids" type="SELECT" multiple="true">
<param name="names"/> <param name="ids"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='name'/> in <IN>${names}</IN> WHERE <FIELD name='group-id'/> in <IN>${ids}</IN>
]]> ]]>
</statement> </statement>
</query> </query>
</query-defs> </query-defs>
</entity> </entity>
<entity name="conf-slb-active" table="conf_slb_active" alias="csa"> <entity name="conf-slb-active" table="conf_slb_active" alias="csa">
<var name="names" value-type="String[]"/> <var name="ids" value-type="Long[]"/>
<query-defs> <query-defs>
<query name="insert" type="INSERT"> <query name="insert" type="INSERT">
<statement> <statement>
@ -42,30 +42,30 @@
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-by-name" type="SELECT"> <query name="find-by-slb-id" type="SELECT">
<param name="name"/> <param name="id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='name'/> = ${name} WHERE <FIELD name='slb-id'/> = ${id}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-all-by-names" type="SELECT" multiple="true"> <query name="find-all-by-slb-ids" type="SELECT" multiple="true">
<param name="names"/> <param name="ids"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='name'/> in <IN>${names}</IN> WHERE <FIELD name='slb-id'/> in <IN>${ids}</IN>
]]> ]]>
</statement> </statement>
</query> </query>
</query-defs> </query-defs>
</entity> </entity>
<entity name="conf-app-slb-active" table="conf_app_slb_active" alias="casa"> <entity name="conf-group-slb-active" table="conf_group_slb_active" alias="cgsa">
<var name="names" value-type="String[]"/> <var name="ids" value-type="Long[]"/>
<query-defs> <query-defs>
<query name="insert" type="INSERT"> <query name="insert" type="INSERT">
<statement> <statement>
@ -73,29 +73,29 @@
INSERT INTO <TABLE/>(<FIELDS/>) INSERT INTO <TABLE/>(<FIELDS/>)
VALUES(<VALUES/>) VALUES(<VALUES/>)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
app_name = values(app_name), group_id = values(group_id),
slb_name = values(slb_name), slb_id = values(slb_id),
slb_virtual_server_name = values(slb_virtual_server_name), slb_virtual_server_id = values(slb_virtual_server_id),
<FIELD name="data-change-last-time"/> = NOW() <FIELD name="data-change-last-time"/> = NOW()
]]> ]]>
</statement> </statement>
</query> </query>
<query name="find-by-slb-name" type="SELECT" multiple="true"> <query name="find-by-slb-id" type="SELECT" multiple="true">
<param name="slb-name"/> <param name="slb-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
SELECT <FIELDS/> SELECT <FIELDS/>
FROM <TABLE/> FROM <TABLE/>
WHERE <FIELD name='slb-name'/> = ${slb-name} WHERE <FIELD name='slb-id'/> = ${slb-id}
]]> ]]>
</statement> </statement>
</query> </query>
<query name="delete-by-app-name" type="DELETE"> <query name="delete-by-group-id" type="DELETE">
<param name="app-name"/> <param name="group-id"/>
<statement> <statement>
<![CDATA[ <![CDATA[
DELETE FROM <TABLE/> DELETE FROM <TABLE/>
WHERE <FIELD name='app-name'/> = ${app-name} WHERE <FIELD name='group-id'/> = ${group-id}
]]> ]]>
</statement> </statement>
</query> </query>

View file

@ -92,7 +92,7 @@
<element name="ip" value-type="String" /> <element name="ip" value-type="String" />
</entity> </entity>
<entity name="archive"> <entity name="archive">
<element name="name" value-type="String" /> <element name="id" value-type="int" />
<element name="content" value-type="String" /> <element name="content" value-type="String" />
<element name="version" value-type="int" /> <element name="version" value-type="int" />
</entity> </entity>

View file

@ -84,7 +84,7 @@
</group> </group>
<archive> <archive>
<name>gateway</name> <id>1</id>
<content>groupXmlString</content> <content>groupXmlString</content>
<version>1</version> <version>1</version>
</archive> </archive>