summaryrefslogtreecommitdiffstats
path: root/adaptors/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/rm/util/RangeUtil.java
blob: 2e378f1aa2971d7d31fb5b96e69128ed8cb203f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 *                         reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.adaptors.rm.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem;
import org.onap.ccsdk.sli.adaptors.rm.data.Range;
import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationItem;
import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationRequest;
import org.onap.ccsdk.sli.adaptors.rm.data.RangeResource;
import org.onap.ccsdk.sli.adaptors.rm.data.ResourceKey;
import org.onap.ccsdk.sli.adaptors.rm.data.ResourceType;

public class RangeUtil {

    public static void recalculate(RangeResource r) {
        r.used = new TreeSet<>();
        if (r.allocationItems != null) {
            for (AllocationItem ai : r.allocationItems) {
                RangeAllocationItem rai = (RangeAllocationItem) ai;
                if (rai.used != null) {
                    r.used.addAll(rai.used);
                }
            }
        }
    }

    public static boolean checkRange(RangeResource r, RangeAllocationRequest req, int num) {
        if (req.excludeNumbers != null && req.excludeNumbers.contains(num)) {
            return false;
        }

        if (req.rangeList != null && !req.rangeList.isEmpty()) {
            boolean good = false;
            for (Range range : req.rangeList) {
                if (num < range.min || num > range.max) {
                    continue;
                }

                boolean found = false;
                if (r.allocationItems != null) {
                    for (AllocationItem ai : r.allocationItems) {
                        RangeAllocationItem rai = (RangeAllocationItem) ai;
                        if (!eq(req.resourceUnionId, rai.resourceUnionId) && rai.used != null
                                && rai.used.contains(num)) {
                            if (!overlap(rai.resourceShareGroupList, req.resourceShareGroupList)) {
                                found = true;
                                break;
                            }
                        }
                        if (!req.replace && eq(req.resourceSetId, rai.resourceSetId) && rai.used != null
                                && rai.used.contains(num)) {
                            found = true;
                            break;
                        }
                        if (req.forceNewNumbers && rai.used.contains(num)) {
                            found = true;
                            break;
                        }
                    }
                }

                if (!found) {
                    good = true;
                    break;
                }
            }

            return good;
        }

        return true;
    }

    public static SortedSet<Integer> getUsed(RangeResource r, String resourceUnionId) {
        SortedSet<Integer> used = new TreeSet<>();
        if (r.allocationItems != null) {
            for (AllocationItem ai : r.allocationItems) {
                RangeAllocationItem rai = (RangeAllocationItem) ai;
                if (eq(resourceUnionId, rai.resourceUnionId) && rai.used != null) {
                    used.addAll(rai.used);
                }
            }
        }
        return used;
    }

    public static void allocateRange(RangeResource rr, SortedSet<Integer> requestedNumbers,
            RangeAllocationRequest req) {
        if (!req.allocate) {
            return;
        }

        RangeAllocationItem rai = (RangeAllocationItem) ResourceUtil.getAllocationItem(rr, req.resourceSetId);
        if (rai == null) {
            rai = new RangeAllocationItem();
            rai.resourceType = ResourceType.Range;
            rai.resourceKey = new ResourceKey();
            rai.resourceKey.assetId = req.assetId;
            rai.resourceKey.resourceName = req.resourceName;
            rai.applicationId = req.applicationId;
            rai.resourceSetId = req.resourceSetId;
            rai.resourceUnionId = req.resourceUnionId;
            rai.resourceShareGroupList = req.resourceShareGroupList;
            rai.used = requestedNumbers;

            if (rr.allocationItems == null) {
                rr.allocationItems = new ArrayList<>();
            }
            rr.allocationItems.add(rai);
        } else if (req.replace) {
            rai.used = requestedNumbers;
        } else {
            rai.used.addAll(requestedNumbers);
        }

        rai.allocationTime = new Date();

        recalculate(rr);
    }

    private static boolean eq(Object o1, Object o2) {
        return o1 == null ? o2 == null : o1.equals(o2);
    }

    private static boolean overlap(Set<String> s1, Set<String> s2) {
        if (s1 == null || s1.isEmpty() || s2 == null || s2.isEmpty()) {
            return false;
        }
        for (String ss1 : s1) {
            if (s2.contains(ss1)) {
                return true;
            }
        }
        return false;
    }
}