2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.policy.guard;
24 import java.util.Collections;
25 import java.util.LinkedList;
26 import java.util.List;
29 public class Constraint {
31 private Integer freqLimitPerTarget;
32 private Map<String,String> timeWindow;
33 private Map<String, String> activeTimeRange;
34 private Integer minVnfCount;
35 private Integer maxVnfCount;
37 private List<String> blacklist;
40 // Do Nothing empty constructor.
46 * @param freqLimitPerTarget frequency limit
47 * @param timeWindow time window
49 public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow) {
50 this.freqLimitPerTarget = freqLimitPerTarget;
51 if (timeWindow != null) {
52 this.timeWindow = Collections.unmodifiableMap(timeWindow);
59 * @param minVnfCount minimum VNF count
60 * @param maxVnfCount maximum VNF count
61 * @param activeTimeRange active time range
63 public Constraint(Integer minVnfCount, Integer maxVnfCount, Map<String, String> activeTimeRange) {
64 this.minVnfCount = minVnfCount;
65 this.maxVnfCount = maxVnfCount;
67 if (activeTimeRange != null) {
68 this.activeTimeRange = Collections.unmodifiableMap(activeTimeRange);
72 public Constraint(List<String> blacklist) {
73 this.blacklist = new LinkedList<>(blacklist);
79 * @param freqLimitPerTarget frequency limit
80 * @param timeWindow time window
81 * @param blacklist blacklist
83 public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow, List<String> blacklist) {
84 this.freqLimitPerTarget = freqLimitPerTarget;
85 this.timeWindow = Collections.unmodifiableMap(timeWindow);
86 this.blacklist = new LinkedList<>(blacklist);
92 * @param freqLimitPerTarget frequency limit
93 * @param timeWindow time window
94 * @param activeTimeRange active time range
96 public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow, Map<String, String> activeTimeRange) {
97 this(freqLimitPerTarget, timeWindow);
98 if (activeTimeRange != null) {
99 this.activeTimeRange = Collections.unmodifiableMap(activeTimeRange);
106 * @param freqLimitPerTarget frequency limit
107 * @param timeWindow the time window
108 * @param activeTimeRange active time range
109 * @param blacklist incoming blacklist
111 public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow, Map<String, String> activeTimeRange,
112 List<String> blacklist) {
113 this(freqLimitPerTarget, timeWindow);
114 if (activeTimeRange != null) {
115 this.activeTimeRange = Collections.unmodifiableMap(activeTimeRange);
117 if (blacklist != null) {
118 this.blacklist = new LinkedList<>(blacklist);
125 * @param constraint objec to copy
127 public Constraint(Constraint constraint) {
128 this.freqLimitPerTarget = constraint.freqLimitPerTarget;
129 this.timeWindow = constraint.timeWindow;
130 if (constraint.activeTimeRange != null) {
131 this.activeTimeRange = Collections.unmodifiableMap(constraint.activeTimeRange);
133 this.blacklist = new LinkedList<>(constraint.blacklist);
136 public Integer getFreq_limit_per_target() {
137 return freqLimitPerTarget;
141 public void setFreq_limit_per_target(Integer freqLimitPerTarget) {
142 this.freqLimitPerTarget = freqLimitPerTarget;
146 public Map<String, String> getTime_window() {
151 public void setTime_window(Map<String, String> timeWindow) {
152 this.timeWindow = timeWindow;
156 public Map<String, String> getActive_time_range() {
157 return activeTimeRange;
161 public void setActive_time_range(Map<String, String> activeTimeRange) {
162 this.activeTimeRange = activeTimeRange;
166 public List<String> getBlacklist() {
171 public void setBlacklist(List<String> blacklist) {
172 this.blacklist = blacklist;
176 public Integer getMinVnfCount() {
181 public void setMinVnfCount(Integer minVnfCount) {
182 this.minVnfCount = minVnfCount;
186 public Integer getMaxVnfCount() {
191 public void setMaxVnfCount(Integer maxVnfCount) {
192 this.maxVnfCount = maxVnfCount;
196 * Check if these constraint values are valid.
198 * @return true if valid
200 public boolean isValid() {
202 // Sonar likes these statements combined as well as not use
205 // If the freqLimitPerTarget is null AND the timeWindow is NOT null
207 // timeWindow is null AND the freqLimitPerTarget is NOT null
209 // then we want to return false (hence the preceding !)
211 return ! ((freqLimitPerTarget == null && timeWindow != null)
212 || (timeWindow == null && freqLimitPerTarget != null));
216 public String toString() {
217 return "Constraint [freq_limit_per_target=" + freqLimitPerTarget + ", time_window="
218 + timeWindow + ", active_time_range=" + activeTimeRange + ", blacklist=" + blacklist + "]";
222 public int hashCode() {
223 final int prime = 31;
225 result = prime * result + ((freqLimitPerTarget == null) ? 0 : freqLimitPerTarget.hashCode());
226 result = prime * result + ((timeWindow == null) ? 0 : timeWindow.hashCode());
227 result = prime * result + ((activeTimeRange == null) ? 0 : activeTimeRange.hashCode());
228 result = prime * result + ((blacklist == null) ? 0 : blacklist.hashCode());
233 public boolean equals(Object obj) {
240 if (getClass() != obj.getClass()) {
243 Constraint other = (Constraint) obj;
244 return equalsMayBeNull(freqLimitPerTarget, other.freqLimitPerTarget)
245 && equalsMayBeNull(timeWindow, other.timeWindow)
246 && equalsMayBeNull(activeTimeRange, other.activeTimeRange)
247 && equalsMayBeNull(blacklist, other.blacklist);
250 private boolean equalsMayBeNull(final Object obj1, final Object obj2) {
254 return obj1.equals(obj2);