diff --git a/src/main/java/org/cloud/sonic/agent/bridge/ios/CompareVersionUtil.java b/src/main/java/org/cloud/sonic/agent/bridge/ios/CompareVersionUtil.java new file mode 100644 index 00000000..15929ed2 --- /dev/null +++ b/src/main/java/org/cloud/sonic/agent/bridge/ios/CompareVersionUtil.java @@ -0,0 +1,37 @@ +package org.cloud.sonic.agent.bridge.ios; + +/** + * Created by fengbincao on 2024/08/19 + */ +public class CompareVersionUtil { + + /** + * 比较版本号的大小,前者大则返回一个正数,后者大返回一个负数,相等则返回0 + * + * @param version1 第一个版本号 + * @param version2 第二个版本号 + * @return 前者大则返回一个正数, 后者大返回一个负数, 相等则返回0 + */ + public static int compareVersion(String version1, String version2) { + String[] v1 = version1.split("\\."), v2 = version2.split("\\."); + int index = 0; + for (; index < v1.length && index < v2.length; index++) { + int v1n = Integer.parseInt(v1[index]), v2n = Integer.parseInt(v2[index]); + if (v1n > v2n) return 1; + if (v1n < v2n) return -1; + } + if (index < v1.length) { + for (; index < v1.length; index++) { + int v1n = Integer.parseInt(v1[index]); + if (v1n > 0) return 1; + } + } + if (index < v2.length) { + for (; index < v2.length; index++) { + int v2n = Integer.parseInt(v2[index]); + if (v2n > 0) return -1; + } + } + return 0; + } +} diff --git a/src/main/java/org/cloud/sonic/agent/bridge/ios/SibTool.java b/src/main/java/org/cloud/sonic/agent/bridge/ios/SibTool.java index 4bb4785b..7cdeca8d 100755 --- a/src/main/java/org/cloud/sonic/agent/bridge/ios/SibTool.java +++ b/src/main/java/org/cloud/sonic/agent/bridge/ios/SibTool.java @@ -1005,4 +1005,14 @@ public static void startShare(String udId, int port) { e.printStackTrace(); } } + + public static boolean isUpperThanIos17(String udId) { + JSONObject deviceInfoObj = IOSInfoMap.getDetailMap().get(udId); + if (deviceInfoObj != null && deviceInfoObj.containsKey("productVersion")) { + String productVersion = deviceInfoObj.getString("productVersion"); + return CompareVersionUtil.compareVersion(productVersion, "17.0") >= 0; + } + return false; + } + } diff --git a/src/test/java/org/cloud/sonic/agent/bridge/ios/CompareVersionUtilTest.java b/src/test/java/org/cloud/sonic/agent/bridge/ios/CompareVersionUtilTest.java new file mode 100644 index 00000000..3835c639 --- /dev/null +++ b/src/test/java/org/cloud/sonic/agent/bridge/ios/CompareVersionUtilTest.java @@ -0,0 +1,60 @@ +package org.cloud.sonic.agent.bridge.ios; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by fengbincao on 2024/08/19 + */ +public class CompareVersionUtilTest { + @Test + public void compareVersion1() { + String version1 = "17.1.0"; + String version2 = "17.0.0"; + Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) > 0); + } + + @Test + public void compareVersion2() { + String version1 = "17.1.0"; + String version2 = "17"; + Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) > 0); + } + + @Test + public void compareVersion3() { + String version1 = "17.1"; + String version2 = "17.0"; + Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) > 0); + } + + @Test + public void compareVersion4() { + String version1 = "17.0.0"; + String version2 = "17"; + assertEquals(0, CompareVersionUtil.compareVersion(version1, version2)); + } + + @Test + public void compareVersion5() { + String version1 = "16.8"; + String version2 = "17.0"; + Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) < 0); + } + + @Test + public void compareVersion6() { + String version1 = "16"; + String version2 = "17"; + Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) < 0); + } + + @Test + public void compareVersion7() { + String version1 = "18.0.1"; + String version2 = "17.0"; + Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) > 0); + } +} \ No newline at end of file