diff --git a/src/main/java/com/ctrip/zeus/restful/resource/TrafficPolicyResource.java b/src/main/java/com/ctrip/zeus/restful/resource/TrafficPolicyResource.java index 67d22fd8..1b55ca72 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/TrafficPolicyResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/TrafficPolicyResource.java @@ -70,6 +70,9 @@ public class TrafficPolicyResource { queryRender.readToCommand(cmd); IdVersion[] searchKeys = trafficPolicyQuery.queryByCommand(cmd, SelectionMode.getMode(mode)); + if (searchKeys == null) { + searchKeys = trafficPolicyQuery.queryAll(SelectionMode.getMode(mode)).toArray(new IdVersion[]{}); + } List result = trafficPolicyRepository.list(searchKeys); List viewArray = new ArrayList<>(result.size()); @@ -132,6 +135,13 @@ public class TrafficPolicyResource { if (extendedView.getTags() != null) { addTag(p.getId(), extendedView.getTags()); } + try { + if (trafficPolicyQuery.queryByIdAndMode(p.getId(), SelectionMode.ONLINE_EXCLUSIVE).length == 1) { + propertyBox.set("status", "toBeActivated", "policy", p.getId()); + } + } catch (Exception ex) { + } + return responseHandler.handle(p, hh.getMediaType()); } diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/ModelValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/ModelValidator.java index 78e4ea5e..8c940c59 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/ModelValidator.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/ModelValidator.java @@ -11,6 +11,8 @@ public interface ModelValidator { void validateForActivate(T[] toBeActivatedItems, boolean escapedPathValidation) throws Exception; + void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception; + void checkVersionForUpdate(T target) throws Exception; void removable(Long targetId) throws Exception; diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java index b44ad61e..f469928f 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java @@ -121,11 +121,18 @@ public class DefaultGroupValidator implements GroupValidator { } } + @Override + public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception { + for (RTrafficPolicyGroupDo e : rTrafficPolicyGroupDao.findByGroupsAndPolicyActiveVersion(toBeDeactivatedItems, RTrafficPolicyGroupEntity.READSET_FULL)) { + throw new ValidationException("Group that you try to deactivate is hold by an online traffic policy " + e.getPolicyId() + "."); + } + } + @Override public void checkVersionForUpdate(Group target) throws Exception { RelGroupStatusDo check = rGroupStatusDao.findByGroup(target.getId(), RGroupStatusEntity.READSET_FULL); if (check == null) { - throw new ValidationException("Group that you tries to update does not exist."); + throw new ValidationException("Group that you try to update does not exist."); } if (check.getOfflineVersion() > target.getVersion()) { throw new ValidationException("Newer version is detected."); @@ -306,7 +313,7 @@ public class DefaultGroupValidator implements GroupValidator { } else { // check priority and reformat if (!e.getPriority().equals(insertEntry.getPriority())) { - throw new ValidationException("Group that you tries to create/modify may cause path prefix-overlap problem with other entries on virtual-server " + vsId + ". Recommend priority will be " + insertEntry.getPriority() + "."); + throw new ValidationException("Group that you try to create/modify may cause path prefix-overlap problem with other entries on virtual-server " + vsId + ". Recommend priority will be " + insertEntry.getPriority() + "."); } e.setPath(insertEntry.getPath()); } diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultSlbValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultSlbValidator.java index 781c38bf..cc35a5bd 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultSlbValidator.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultSlbValidator.java @@ -85,6 +85,11 @@ public class DefaultSlbValidator implements SlbValidator { } + @Override + public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception { + + } + @Override public void checkVersionForUpdate(Slb target) throws Exception { SlbDo check = slbDao.findById(target.getId(), SlbEntity.READSET_FULL); diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultTrafficPolicyValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultTrafficPolicyValidator.java index 0f3b642a..4ca02d7e 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultTrafficPolicyValidator.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultTrafficPolicyValidator.java @@ -29,6 +29,8 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator { private RGroupVsDao rGroupVsDao; @Resource private PathValidator pathValidator; + @Resource + private RGroupStatusDao rGroupStatusDao; @Override public boolean exists(Long targetId) throws Exception { @@ -73,6 +75,7 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator { for (RelGroupVsDo e : rGroupVsDao.findByVsesAndGroupOfflineVersion(vsIds, RGroupVsEntity.READSET_FULL)) { putArrayEntryValue(gvsListByVsId, e.getVsId(), e); } + validatePolicyControls(target, groupIds, vsIds, gvsListByVsId); Map> pvsListByVsId = new HashMap<>(); for (RTrafficPolicyVsDo e : rTrafficPolicyVsDao.findByVsesAndPolicyVersion(vsIds, RTrafficPolicyVsEntity.READSET_FULL)) { @@ -84,8 +87,6 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator { putArrayEntryValue(policyListByGroupId, e.getGroupId(), e); } - validatePolicyControls(target, groupIds, vsIds, gvsListByVsId); - Map> currentLocationEntriesByVs = new HashMap<>(); compareAndBuildCurrentLocationEntries(target, groupIds, vsIds, policyListByGroupId, pvsListByVsId, gvsListByVsId, escapePathValidation ? null : currentLocationEntriesByVs); @@ -199,6 +200,13 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator { policies[i] = e.getId(); hashes[i] = VersionUtils.getHash(e.getId(), e.getVersion()); } + Long[] groupIds = groupLookup.toArray(new Long[groupLookup.size()]); + for (RelGroupStatusDo e : rGroupStatusDao.findByGroups(groupIds, RGroupStatusEntity.READSET_FULL)) { + if (e.getOnlineVersion() < 0) { + throw new ValidationException("Group " + e.getGroupId() + " has not been activated."); + } + } + Long[] vsIds = vsLookup.toArray(new Long[vsLookup.size()]); Map> gvsListByVsId = new HashMap<>(); @@ -219,7 +227,7 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator { } Map> policyListByGroupId = new HashMap<>(); - for (RTrafficPolicyGroupDo e : rTrafficPolicyGroupDao.findByGroupsAndPolicyActiveVersion(groupLookup.toArray(new Long[groupLookup.size()]), RTrafficPolicyGroupEntity.READSET_FULL)) { + for (RTrafficPolicyGroupDo e : rTrafficPolicyGroupDao.findByGroupsAndPolicyActiveVersion(groupIds, RTrafficPolicyGroupEntity.READSET_FULL)) { if (Arrays.binarySearch(policies, e.getPolicyId()) >= 0) continue; putArrayEntryValue(policyListByGroupId, e.getGroupId(), e); } @@ -252,6 +260,11 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator { } } + @Override + public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception { + + } + private void putArrayEntryValue(Map> map, Long key, T e) { List v = map.get(key); if (v == null) { @@ -265,12 +278,15 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator { Long prev = groupIds[0]; for (int i = 1; i < groupIds.length; i++) { if (prev.equals(groupIds[i])) { - throw new ValidationException("Traffic policy that you tries to create/modify declares the same group " + prev + " more than once."); + throw new ValidationException("Traffic policy that you try to create/modify declares the same group " + prev + " more than once."); } prev = groupIds[i]; } if (groupIds.length <= 1) { - throw new ValidationException("Traffic policy that you tries to create/modify does not have enough traffic-controls."); + throw new ValidationException("Traffic policy that you try to create/modify does not have enough traffic-controls."); + } + if (rGroupStatusDao.findByGroups(groupIds, RGroupStatusEntity.READSET_FULL).size() != groupIds.length) { + throw new ValidationException("Traffic policy that you try to create/modify contains group that does not exist."); } int[] versions = new int[groupIds.length]; @@ -355,7 +371,7 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator { public void checkVersionForUpdate(TrafficPolicy target) throws Exception { TrafficPolicyDo d = trafficPolicyDao.findById(target.getId(), TrafficPolicyEntity.READSET_FULL); if (d == null) { - throw new ValidationException("Traffic policy that you tries to update does not exist."); + throw new ValidationException("Traffic policy that you try to update does not exist."); } if (d.getVersion() > target.getVersion()) { throw new ValidationException("Newer version is detected."); @@ -369,7 +385,7 @@ public class DefaultTrafficPolicyValidator implements TrafficPolicyValidator { public void removable(Long targetId) throws Exception { TrafficPolicyDo d = trafficPolicyDao.findById(targetId, TrafficPolicyEntity.READSET_FULL); if (d != null && d.getActiveVersion() > 0) { - throw new ValidationException("Traffic policy that you tried to delete is still active."); + throw new ValidationException("Traffic policy that you try to delete is still active."); } } diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java index da7dafb7..114db6f2 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java @@ -43,6 +43,11 @@ public class DefaultVGroupValidator implements VGroupValidator { } + @Override + public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception { + + } + @Override public void checkVersionForUpdate(Group target) throws Exception { groupModelValidator.checkVersionForUpdate(target); diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVirtualServerValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVirtualServerValidator.java index 674d714a..61e25aa9 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVirtualServerValidator.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVirtualServerValidator.java @@ -112,6 +112,11 @@ public class DefaultVirtualServerValidator implements VirtualServerValidator { } + @Override + public void validateForDeactivate(Long[] toBeDeactivatedItems) throws Exception { + + } + @Override public void checkVersionForUpdate(VirtualServer target) throws Exception { diff --git a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultTrafficPolicyCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultTrafficPolicyCriteriaQuery.java index 83715598..914d8458 100644 --- a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultTrafficPolicyCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultTrafficPolicyCriteriaQuery.java @@ -1,6 +1,7 @@ package com.ctrip.zeus.service.query.impl; import com.ctrip.zeus.dal.core.*; +import com.ctrip.zeus.executor.impl.ResultHandler; import com.ctrip.zeus.model.entity.TrafficPolicy; import com.ctrip.zeus.service.model.IdVersion; import com.ctrip.zeus.service.model.SelectionMode; @@ -130,7 +131,19 @@ public class DefaultTrafficPolicyCriteriaQuery implements TrafficPolicyQuery { } return result; } - }).build(IdVersion.class).run(); + }).build(IdVersion.class).run(new ResultHandler() { + @Override + public IdVersion[] handle(Set result) throws Exception { + if (result == null) return null; + if (result.size() == 0) return new IdVersion[0]; + Set ids = new HashSet<>(); + for (IdVersion e : result) { + ids.add(e.getId()); + } + result.retainAll(queryByIdsAndMode(ids.toArray(new Long[ids.size()]), mode)); + return result.toArray(new IdVersion[result.size()]); + } + }); return result; }