一、IP寻址
1.划分网络ID和主机ID的最初方案是使用地址分类。
2.A类:0.0.0.0-127.255.255.255
B类:128.0.0.0-191.255.255.255
C类:192.0.0.0-223.255.255.255
D类:224.0.0.0-239.255.255.255
E类:240.0.0.0-247.255.255.255
二、子网
1.子网划分可以利用IP地址系统把物理网络分解为更小的逻辑实体——子网。
2.子网的概念最早源自于地址分类系统,而且在ABC类地址中能够得到很好的展现。然而硬件厂商和internet社区建立了一种解析地址的新系统,名为无类别域间路由(CIDR),它不需要关心地址类别。
192.168.1.0/24,它指的是IP地址是192.168.1.0,子网掩码中1的个数是24个,即255.255.255.0,二进制显示为11111111 11111111 11111111 00000000,很容易看出这个就是一个C类的网络,最后的八个0可以随意组合,取值范围为0-255。
同理192.168.1.0/29,指的是IP地址是192.168.1.0,子网掩码中1的个数是24个,即255.255.255.248,二进制显示为11111111 11111111 11111111 11111000,可用IP地址个数有8个,
一般首尾IP地址为特殊地址,不在实际中使用。
三、代码实现
以下提供两个在/24子网池下划分更小的子网(24< x < 32)和在/16子网池下划分更小的子网(16< x < 24)的实现类。注意:其中去掉了主要的业务逻辑代码,仅提供了IP分配的算法实现。因为需要启用的IP地址比较多,使用了源生的JDBC事务来提高执行效率。备注:此处使用的springCloud框架,底层实现仅供参考。
3.1、基础PO
1 package com.ccb.cloud.nw.ip.data.entity; 2 // default package 3 4 import javax.persistence.Column; 5 import javax.persistence.Entity; 6 import javax.persistence.Id; 7 import javax.persistence.Table; 8 import javax.persistence.Transient; 9 10 11 12 /** 13 * NwCclassPo entity. @author MyEclipse Persistence Tools 14 */ 15 @Entity 16 @Table(name="RM_NW_CCLASS") 17 18 public class NwCclassPo implements java.io.Serializable { 19 20 21 // Fields 22 23 private Long cclassId; 24 private Long bclassId; 25 private String cclassTypeCode; 26 private String secureAreaCode; 27 private String cclassName; 28 private String subnetmask; 29 private String gateway; 30 private Long vlanId; 31 private String isActive; 32 private String secureTierCode; 33 private Integer aclassIp; 34 private Integer bclassIp; 35 private Integer cclassIp; 36 private Integer ipStart; 37 private Integer ipEnd; 38 39 private Integer ipTotalCnt; 40 private Integer ipAvailCnt; 41 42 private Long datacenterId; 43 private Long moduleId; 44 private Long vmManagerServerId; 45 private Long convergeId; 46 private String routersId; 47 48 @Transient 49 private Integer useIpNum; 50 @Transient 51 private Integer unUseIpNum; 52 @Transient 53 private Long resPoolId; 54 @Transient 55 private String resPoolType; 56 @Transient 57 private String netArea; 58 // private RmNwSubnetmaskExtPo rmNwSubnetmaskExtPo; 59 60 // Constructors 61 62 /** default constructor */ 63 public NwCclassPo() { 64 } 65 66 /** minimal constructor */ 67 public NwCclassPo(Long cclassId, Long bclassId) { 68 this.cclassId = cclassId; 69 this.bclassId = bclassId; 70 } 71 72 /** full constructor */ 73 public NwCclassPo(Long cclassId, Long bclassId, String cclassTypeCode, String secureAreaCode, String cclassName, 74 String subnetmask, String gateway, Long vlanId, String isActive, String secureTierCode, Integer aclassIp, 75 Integer bclassIp, Integer cclassIp, Integer ipStart, Integer ipEnd,Integer ipTotalCnt,Integer ipAvailCnt,Long datacenterId) { 76 this.cclassId = cclassId; 77 this.bclassId = bclassId; 78 this.cclassTypeCode = cclassTypeCode; 79 this.secureAreaCode = secureAreaCode; 80 this.cclassName = cclassName; 81 this.subnetmask = subnetmask; 82 this.gateway = gateway; 83 this.vlanId = vlanId; 84 this.isActive = isActive; 85 this.secureTierCode = secureTierCode; 86 this.aclassIp = aclassIp; 87 this.bclassIp = bclassIp; 88 this.cclassIp = cclassIp; 89 this.ipStart = ipStart; 90 this.ipEnd = ipEnd; 91 92 this.ipTotalCnt=ipTotalCnt; 93 this.ipAvailCnt=ipAvailCnt; 94 this.datacenterId=datacenterId; 95 } 96 97 98 // Property accessors 99 @Id 100 101 @Column(name="CCLASS_ID", unique=true, nullable=false, precision=18, scale=0) 102 103 public Long getCclassId() { 104 return this.cclassId; 105 } 106 107 public void setCclassId(Long cclassId) { 108 this.cclassId = cclassId; 109 } 110 111 @Column(name = "VCENTER_ID", nullable =true, precision = 18, scale = 0) 112 public Long getVmManagerServerId() { 113 return vmManagerServerId; 114 } 115 116 public void setVmManagerServerId(Long vmManagerServerId) { 117 this.vmManagerServerId = vmManagerServerId; 118 } 119 120 @Column(name = "MODULE_ID", nullable =true, precision = 18, scale = 0) 121 public Long getModuleId() { 122 return moduleId; 123 } 124 125 public void setModuleId(Long moduleId) { 126 this.moduleId = moduleId; 127 } 128 129 @Column(name="BCLASS_ID", nullable=false, precision=18, scale=0) 130 131 public Long getBclassId() { 132 return this.bclassId; 133 } 134 135 public void setBclassId(Long bclassId) { 136 this.bclassId = bclassId; 137 } 138 139 @Column(name="CCLASS_TYPE_CODE", length=32) 140 141 public String getCclassTypeCode() { 142 return this.cclassTypeCode; 143 } 144 145 public void setCclassTypeCode(String cclassTypeCode) { 146 this.cclassTypeCode = cclassTypeCode; 147 } 148 149 @Column(name="SECURE_AREA_CODE", length=32) 150 151 public String getSecureAreaCode() { 152 return this.secureAreaCode; 153 } 154 155 public void setSecureAreaCode(String secureAreaCode) { 156 this.secureAreaCode = secureAreaCode; 157 } 158 159 @Column(name="CCLASS_NAME", length=100) 160 161 public String getCclassName() { 162 return this.cclassName; 163 } 164 165 public void setCclassName(String cclassName) { 166 this.cclassName = cclassName; 167 } 168 169 @Column(name="SUBNETMASK", length=20) 170 171 public String getSubnetmask() { 172 return this.subnetmask; 173 } 174 175 public void setSubnetmask(String subnetmask) { 176 this.subnetmask = subnetmask; 177 } 178 179 @Column(name="GATEWAY", length=20) 180 181 public String getGateway() { 182 return this.gateway; 183 } 184 185 public void setGateway(String gateway) { 186 this.gateway = gateway; 187 } 188 189 @Column(name="VLAN_ID", precision=18, scale=0) 190 191 public Long getVlanId() { 192 return this.vlanId; 193 } 194 195 public void setVlanId(Long vlanId) { 196 this.vlanId = vlanId; 197 } 198 199 @Column(name="IS_ACTIVE", length=1) 200 201 public String getIsActive() { 202 return this.isActive; 203 } 204 205 public void setIsActive(String isActive) { 206 this.isActive = isActive; 207 } 208 209 @Column(name="SECURE_TIER_CODE", length=32) 210 211 public String getSecureTierCode() { 212 return this.secureTierCode; 213 } 214 215 public void setSecureTierCode(String secureTierCode) { 216 this.secureTierCode = secureTierCode; 217 } 218 219 @Column(name="ACLASS_IP", precision=3, scale=0) 220 221 public Integer getAclassIp() { 222 return this.aclassIp; 223 } 224 225 public void setAclassIp(Integer aclassIp) { 226 this.aclassIp = aclassIp; 227 } 228 229 @Column(name="BCLASS_IP", precision=3, scale=0) 230 231 public Integer getBclassIp() { 232 return this.bclassIp; 233 } 234 235 public void setBclassIp(Integer bclassIp) { 236 this.bclassIp = bclassIp; 237 } 238 239 @Column(name="CCLASS_IP", precision=3, scale=0) 240 241 public Integer getCclassIp() { 242 return this.cclassIp; 243 } 244 245 public void setCclassIp(Integer cclassIp) { 246 this.cclassIp = cclassIp; 247 } 248 249 @Column(name="IP_START", precision=3, scale=0) 250 251 public Integer getIpStart() { 252 return this.ipStart; 253 } 254 255 public void setIpStart(Integer ipStart) { 256 this.ipStart = ipStart; 257 } 258 259 @Column(name="IP_END", precision=3, scale=0) 260 261 public Integer getIpEnd() { 262 return this.ipEnd; 263 } 264 265 public void setIpEnd(Integer ipEnd) { 266 this.ipEnd = ipEnd; 267 } 268 269 // 270 @Column(name="IP_TOTAL_CNT", precision=3, scale=0) 271 272 public Integer getIpTotalCnt() { 273 return this.ipTotalCnt; 274 } 275 276 public void setIpTotalCnt(Integer ipTotalCnt) { 277 this.ipTotalCnt = ipTotalCnt; 278 } 279 280 @Column(name="IP_AVAIL_CNT", precision=3, scale=0) 281 282 public Integer getIpAvailCnt() { 283 return this.ipAvailCnt; 284 } 285 286 public void setIpAvailCnt(Integer ipAvailCnt) { 287 this.ipAvailCnt = ipAvailCnt; 288 } 289 @Column(name = "DATACENTER_ID",length=18) 290 public Long getDatacenterId() { 291 return datacenterId; 292 } 293 294 public void setDatacenterId(Long datacenterId) { 295 this.datacenterId = datacenterId; 296 } 297 @Column(name = "CONVERGE_ID", nullable =true, precision = 18, scale = 0) 298 public Long getConvergeId() { 299 return convergeId; 300 } 301 302 public void setConvergeId(Long convergeId) { 303 this.convergeId = convergeId; 304 } 305 306 @Column(name="ROUTERS_ID", length=36) 307 public String getRoutersId() { 308 return routersId; 309 } 310 311 public void setRoutersId(String routersId) { 312 this.routersId = routersId; 313 } 314 315 @Transient 316 public Integer getUseIpNum() { 317 return useIpNum; 318 } 319 320 public void setUseIpNum(Integer useIpNum) { 321 this.useIpNum = useIpNum; 322 } 323 324 @Transient 325 public Integer getUnUseIpNum() { 326 return unUseIpNum; 327 } 328 329 public void setUnUseIpNum(Integer unUseIpNum) { 330 this.unUseIpNum = unUseIpNum; 331 } 332 333 @Transient 334 public Long getResPoolId() { 335 return resPoolId; 336 } 337 338 public void setResPoolId(Long resPoolId) { 339 this.resPoolId = resPoolId; 340 } 341 342 @Transient 343 public String getResPoolType() { 344 return resPoolType; 345 } 346 347 public void setResPoolType(String resPoolType) { 348 this.resPoolType = resPoolType; 349 } 350 351 @Transient 352 public String getNetArea() { 353 return netArea; 354 } 355 356 public void setNetArea(String netArea) { 357 this.netArea = netArea; 358 } 359 360 361 }