Open Kilda Java Documentation
Inet4NetworkPool.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.util.LinkedList;
19 import java.util.List;
20 import java.util.ListIterator;
21 
22 public class Inet4NetworkPool {
23  private Inet4Network parentNetwork;
24  private List<SupplyDescriptor> free;
25  private List<SupplyDescriptor> supplied;
26  private int prefix;
27  private long nextIndex = 0;
28 
29  public Inet4NetworkPool(Inet4Network parentNetwork, int prefix) {
30  this.parentNetwork = parentNetwork;
31  this.prefix = prefix;
32 
33  free = new LinkedList<>();
34  supplied = new LinkedList<>();
35  }
36 
41  SupplyDescriptor item;
42 
43  try {
44  item = free.remove(0);
45  } catch (IndexOutOfBoundsException e) {
46  item = new SupplyDescriptor(
47  parentNetwork.subnet(nextIndex, prefix), nextIndex);
48  nextIndex++;
49  }
50 
51  supplied.add(item);
52 
53  return item.network;
54  }
55 
59  public void free(Inet4Network network) throws Inet4ValueException {
60  ListIterator<SupplyDescriptor> iter;
61  SupplyDescriptor descriptor = null;
62 
63  for (iter = supplied.listIterator(); iter.hasNext(); ) {
64  SupplyDescriptor item = iter.next();
65  if (!network.equals(item.network)) {
66  continue;
67  }
68 
69  iter.remove();
70  descriptor = item;
71  break;
72  }
73 
74  if (descriptor == null) {
75  throw new Inet4ValueException("Network don't belong to this pool");
76  }
77 
78  free.add(descriptor);
79  }
80 
81  private final class SupplyDescriptor {
82  Inet4Network network;
83  long index;
84 
85  public SupplyDescriptor(Inet4Network network, long index) {
86  this.network = network;
87  this.index = index;
88  }
89  }
90 }
def index()
Definition: login.py:30