Open Kilda Java Documentation
Inet4Network.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.testing.service.traffexam.networkpool;
17 
18 import java.net.Inet4Address;
19 import java.net.UnknownHostException;
20 
21 public class Inet4Network {
22  private Inet4Address network;
23  private byte prefix;
24  private byte[] mask;
25 
26  public Inet4Network(Inet4Address network, int prefix)
27  throws Inet4ValueException {
28  this.checkPrefix(prefix, (byte) 0);
29  this.prefix = (byte) prefix;
30 
31  this.mask = this.makeMask(this.prefix);
32  this.network = this.applyMask(network, this.mask);
33  }
34 
38  public Inet4Network subnet(long index, int prefix)
39  throws Inet4ValueException {
40  this.checkPrefix(prefix, prefix);
41 
42  index <<= 32 - prefix;
43  Inet4Address target = this.merge(index);
44  this.checkOverflow(target);
45 
46  return new Inet4Network(target, prefix);
47  }
48 
52  public Inet4Address address(long index) throws Inet4ValueException {
53  Inet4Address target = this.merge(index);
54  this.checkOverflow(target);
55  return target;
56  }
57 
58  public Inet4Address applyMask(Inet4Address address)
59  throws Inet4ValueException {
60  return this.applyMask(address, this.mask);
61  }
62 
63  private Inet4Address applyMask(Inet4Address address, byte[] mask)
64  throws Inet4ValueException {
65  byte[] unpacked = address.getAddress();
66 
67  for (int idx = 0; idx < unpacked.length; idx++) {
68  unpacked[idx] &= mask[idx];
69  }
70 
71  try {
72  return (Inet4Address) Inet4Address.getByAddress(unpacked);
73  } catch (UnknownHostException e) {
74  throw new Inet4ValueException("Unable to \"pack\" raw address.", e);
75  }
76  }
77 
78  private byte[] makeMask(int prefix) {
79  byte[] mask = new byte[4];
80 
81  int shift = 32 - prefix;
82  for (int idx = 0; idx < mask.length; idx++, shift -= 8) {
83  int blank = 0xff;
84  if (0 < shift) {
85  blank <<= shift;
86  }
87  mask[mask.length - 1 - idx] = (byte) (blank & 0xff);
88  }
89 
90  return mask;
91  }
92 
93  private Inet4Address merge(long add) throws Inet4ValueException {
94  byte[] unpacked = new byte[4];
95 
96  for (int idx = 0; idx < unpacked.length; idx++) {
97  unpacked[unpacked.length - 1 - idx] = (byte) (add & 0xff);
98  add >>= 8;
99  }
100 
101  return this.merge(unpacked);
102  }
103 
104  private Inet4Address merge(byte[] add) throws Inet4ValueException {
105  byte[] unpacked = network.getAddress();
106 
107  for (int idx = 0; idx < unpacked.length; idx++) {
108  unpacked[idx] |= add[idx];
109  }
110 
111  try {
112  return (Inet4Address) Inet4Address.getByAddress(unpacked);
113  } catch (UnknownHostException e) {
114  throw new Inet4ValueException("Unable to \"pack\" raw address.", e);
115  }
116  }
117 
118  private void checkPrefix(int prefix, int lowest) throws Inet4ValueException {
119  if (prefix < lowest || 32 < prefix) {
120  throw new Inet4ValueException(String.format(
121  "Invalid network prefix. Must be in range from %d to 32",
122  lowest));
123  }
124  }
125 
126  private void checkOverflow(Inet4Address subject) throws Inet4ValueException {
127  if (!network.equals(this.applyMask(subject))) {
128  throw new Inet4ValueException("Request address beyond network");
129  }
130  }
131 
132  public Inet4Address getNetwork() {
133  return network;
134  }
135 
136  public byte getPrefix() {
137  return prefix;
138  }
139 }
target
Definition: nodes.py:50
def index()
Definition: login.py:30