Merge remote-tracking branch 'origin/v2.0.5_dev' into v2.0.5_dev

This commit is contained in:
HuangXin 2021-01-28 18:33:06 +08:00
commit 9f147ed229
10 changed files with 94 additions and 31 deletions

View File

@ -6,6 +6,7 @@ import inet.ipaddr.AddressStringException;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressSeqRange;
import inet.ipaddr.IPAddressString;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
@ -227,4 +228,38 @@ public class Helper {
return ret.orElse("");
}
/**
* Is ip port match boolean.
*
* @param originPort the origin port
* @param targetPort the target port
* @param portType the port type
* @return the boolean
*/
public static boolean isIpPortMatch(String originPort, String targetPort, HttpType portType) {
String oPort, tPort;
if (portType == null) {
return false;
}
if (originPort != null && originPort.length() > 0) {
oPort = originPort;
} else {
oPort = portType.equals(HttpType.HTTP) ?
HttpType.getDefaultPort(HttpType.HTTP) :
HttpType.getDefaultPort(HttpType.HTTPS);
}
if (targetPort != null && targetPort.length() > 0) {
tPort = targetPort;
} else {
tPort = portType.equals(HttpType.HTTP) ?
HttpType.getDefaultPort(HttpType.HTTP) :
HttpType.getDefaultPort(HttpType.HTTPS);
}
return oPort.equals(tPort);
}
}

View File

@ -1,6 +1,7 @@
package com.dispose.manager.impl;
import com.dispose.common.ErrorCode;
import com.dispose.common.Helper;
import com.dispose.common.HttpType;
import com.dispose.common.IpAddrType;
import com.dispose.common.ObjectStatus;
@ -56,7 +57,7 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager {
@Override
public MulReturnType<ErrorCode, Long> addDisposeDevice(DisposeDevice dev) {
// 看看系统中有没有存在相同IP+端口地址的设备有的话返回失败
DisposeDevice tDev = disposeDeviceMapper.getDeviceByAddress(dev.getIpAddr(), dev.getIpPort(),
DisposeDevice tDev = disposeDeviceMapper.getDeviceByAddrAndType(dev.getIpAddr(), dev.getIpPort(),
dev.getDeviceType().getValue());
if (tDev != null) {
@ -136,22 +137,11 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager {
*/
@Override
public MulReturnType<ErrorCode, Long> upgradeDisposeDevice(DisposeDevice dev) {
DisposeDevice tDev = disposeDeviceMapper.getDeviceByAddress(dev.getIpAddr(), dev.getIpPort(),
dev.getDeviceType().getValue());
// 根据Ip和设备类型获取设备
DisposeDevice tDev = disposeDeviceMapper.getDeviceByIpAndType(dev.getIpAddr(), dev.getDeviceType().getValue());
// 处理默认端口情况
if (tDev == null) {
if (dev.getIpPort() == null || dev.getIpPort().length() == 0) {
tDev = disposeDeviceMapper.getDeviceByAddress(dev.getIpAddr(),
dev.getUrlType() == HttpType.HTTP ?
HttpType.getDefaultPort(HttpType.HTTP) :
HttpType.getDefaultPort(HttpType.HTTPS),
dev.getDeviceType().getValue());
}
}
// 实在找不到设备返回错误
if (tDev == null) {
// 找不到设备
if (tDev == null || !Helper.isIpPortMatch(tDev.getIpPort(), dev.getIpPort(), dev.getUrlType())) {
return new MulReturnType<>(ErrorCode.ERR_NOSUCHDEVICE, -1L);
}
@ -160,6 +150,9 @@ public class DisposeDeviceManagerImpl implements DisposeDeviceManager {
dev.setUserName(null);
dev.setUrlType(null);
dev.setPassword(null);
dev.setIpPort(null);
dev.setIpAddr(null);
dev.setDeviceType(null);
// 更新值
upgradeDisposeDeviceProperties(tDev, dev);

View File

@ -83,10 +83,20 @@ public interface DisposeDeviceMapper {
* @param devType the device type
* @return the device by address
*/
DisposeDevice getDeviceByAddress(@Param("ipAddr") String ipAddr,
DisposeDevice getDeviceByAddrAndType(@Param("ipAddr") String ipAddr,
@Param("ipPort") String ipPort,
@Param("devType") Integer devType);
/**
* Gets device by ip and type.
*
* @param ipAddr the ip addr
* @param devType the dev type
* @return the device by ip and type
*/
DisposeDevice getDeviceByIpAndType(@Param("ipAddr") String ipAddr,
@Param("devType") Integer devType);
/**
* Gets device by id.

View File

@ -63,7 +63,7 @@
LEFT JOIN dispose_capacity c ON d.id = c.deviceId
</select>
<select id="getDeviceByAddress" resultMap="dispose_device">
<select id="getDeviceByAddrAndType" resultMap="dispose_device">
SELECT d.*,
c.*
FROM dispose_device d
@ -73,6 +73,15 @@
AND d.deviceType = #{devType}
</select>
<select id="getDeviceByIpAndType" resultMap="dispose_device">
SELECT d.*,
c.*
FROM dispose_device d
LEFT JOIN dispose_capacity c ON d.id = c.deviceId
WHERE d.ipAddr = #{ipAddr}
AND d.deviceType = #{devType}
</select>
<select id="getDeviceById" resultMap="dispose_device">
SELECT d.*,
c.*

View File

@ -666,4 +666,20 @@ public class demo {
log.info("demoList2 difference demoList1: {}", demoList.equals(demoList2));
log.info("tmpTable difference tmpTable: {}", tmpTable.equals(tmpTable2));
}
/**
* Ip port match test.
*/
@Test
public void ipPortMatchTest() {
String[] srcPort = new String[] {"", "80", "443", "81", null};
String[] dstPort = new String[] {"", "80", "443", "81", null};
for(String s : srcPort) {
for(String d : dstPort) {
log.info("HTTP: [{}] match [{}] is {}", s, d, Helper.isIpPortMatch(s, d, HttpType.HTTP));
log.info("HTTPS: [{}] match [{}] is {}", s, d, Helper.isIpPortMatch(s, d, HttpType.HTTPS));
}
}
}
}

View File

@ -200,7 +200,7 @@ public class DisposeDeviceManagerTest extends InitTestEnvironment {
) {
MulReturnType<ErrorCode, ObjectStatus> ret = disposeDeviceManager.changeDisposeDeviceStatus(v.getId(), obj);
DisposeDevice dev = disposeDeviceMapper.getDeviceByAddress(v.getIpAddr(), v.getIpPort(),
DisposeDevice dev = disposeDeviceMapper.getDeviceByAddrAndType(v.getIpAddr(), v.getIpPort(),
v.getDeviceType().getValue());
if (ret.getFirstParam() == ErrorCode.ERR_OK) {

View File

@ -114,7 +114,7 @@ public class DisposeDeviceMapperTest extends InitTestEnvironment {
devList.forEach(v -> {
if(v.getIpPort().length() != 0){
DisposeDevice dev = disposeDeviceMapper.getDeviceByAddress(v.getIpAddr(), v.getIpPort(),
DisposeDevice dev = disposeDeviceMapper.getDeviceByAddrAndType(v.getIpAddr(), v.getIpPort(),
v.getDeviceType().getValue());
String beforeVer = dev.getVersion();
@ -165,12 +165,12 @@ public class DisposeDeviceMapperTest extends InitTestEnvironment {
List<DisposeDevice> devList = disposeDeviceMapper.selectAll();
devList.forEach(v -> {
DisposeDevice dev = disposeDeviceMapper.getDeviceByAddress(v.getIpAddr(), v.getIpPort(),
DisposeDevice dev = disposeDeviceMapper.getDeviceByAddrAndType(v.getIpAddr(), v.getIpPort(),
v.getDeviceType().getValue());
Assert.assertNotNull(dev);
if(v.getIpPort().length() != 0){
dev = disposeDeviceMapper.getDeviceByAddress(v.getIpAddr(),
dev = disposeDeviceMapper.getDeviceByAddrAndType(v.getIpAddr(),
String.valueOf(Long.parseLong(Optional.ofNullable(v.getIpPort()).orElse("123")) + 1233),
v.getDeviceType().getValue());

View File

@ -196,7 +196,7 @@ public class P1All {
Assert.assertNotNull(k.getDevStatus());
});
DisposeDevice dev = c.getDisposeDeviceMapper().getDeviceByAddress("127.0.0.1", "",
DisposeDevice dev = c.getDisposeDeviceMapper().getDeviceByAddrAndType("127.0.0.1", "",
DisposeDeviceType.VIRTUAL_DISPOSE.getValue());
Assert.assertNotNull(dev);

View File

@ -797,7 +797,7 @@ public class P2DeviceAdd {
Assert.assertNotNull(k.getDevId());
});
DisposeDevice dev = c.getDisposeDeviceMapper().getDeviceByAddress("10.88.77.15", "", DisposeDeviceType.DPTECH_UMC
DisposeDevice dev = c.getDisposeDeviceMapper().getDeviceByAddrAndType("10.88.77.15", "", DisposeDeviceType.DPTECH_UMC
.getValue());
Assert.assertNotNull(dev);

View File

@ -985,7 +985,7 @@ public class P2DeviceUpgrade {
Assert.assertNotNull(k.getDevStatus());
});
DisposeDevice dev = c.getDisposeDeviceMapper().getDeviceByAddress("127.0.0.1", "", DisposeDeviceType.VIRTUAL_DISPOSE
DisposeDevice dev = c.getDisposeDeviceMapper().getDeviceByAddrAndType("127.0.0.1", "", DisposeDeviceType.VIRTUAL_DISPOSE
.getValue());
Assert.assertNotNull(dev);