2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.policy.guard;
23 import java.util.Collections;
24 import java.util.LinkedList;
25 import java.util.List;
28 public class Constraint {
30 private Integer freqLimitPerTarget;
31 private Map<String,String> timeWindow;
32 private Map<String, String> activeTimeRange;
33 private Integer minVnfCount;
34 private Integer maxVnfCount;
36 private List<String> blacklist;
39 // Do Nothing empty constructor.
45 * @param freqLimitPerTarget frequency limit
46 * @param timeWindow time window
48 public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow) {
49 this.freqLimitPerTarget = freqLimitPerTarget;
50 if (timeWindow != null) {
51 this.timeWindow = Collections.unmodifiableMap(timeWindow);
58 * @param minVnfCount minimum VNF count
59 * @param maxVnfCount maximum VNF count
60 * @param activeTimeRange active time range
62 public Constraint(Integer minVnfCount, Integer maxVnfCount, Map<String, String> activeTimeRange) {
63 this.minVnfCount = minVnfCount;
64 this.maxVnfCount = maxVnfCount;
66 if (activeTimeRange != null) {
67 this.activeTimeRange = Collections.unmodifiableMap(activeTimeRange);
71 public Constraint(List<String> blacklist) {
72 this.blacklist = new LinkedList<>(blacklist);
78 * @param freqLimitPerTarget frequency limit
79 * @param timeWindow time window
80 * @param blacklist blacklist
82 public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow, List<String> blacklist) {
83 this.freqLimitPerTarget = freqLimitPerTarget;
84 this.timeWindow = Collections.unmodifiableMap(timeWindow);
85 this.blacklist = new LinkedList<>(blacklist);
91 * @param freqLimitPerTarget frequency limit
92 * @param timeWindow time window
93 * @param activeTimeRange active time range
95 public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow, Map<String, String> activeTimeRange) {
96 this(freqLimitPerTarget, timeWindow);
97 if (activeTimeRange != null) {
98 this.activeTimeRange = Collections.unmodifiableMap(activeTimeRange);
105 * @param freqLimitPerTarget frequency limit
106 * @param timeWindow the time window
107 * @param activeTimeRange active time range
108 * @param blacklist incoming blacklist
110 public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow, Map<String, String> activeTimeRange,
111 List<String> blacklist) {
112 this(freqLimitPerTarget, timeWindow);
113 if (activeTimeRange != null) {
114 this.activeTimeRange = Collections.unmodifiableMap(activeTimeRange);
116 if (blacklist != null) {
117 this.blacklist = new LinkedList<>(blacklist);
124 * @param constraint objec to copy
126 public Constraint(Constraint constraint) {
127 this.freqLimitPerTarget = constraint.freqLimitPerTarget;
128 this.timeWindow = constraint.timeWindow;
129 if (constraint.activeTimeRange != null) {
130 this.activeTimeRange = Collections.unmodifiableMap(constraint.activeTimeRange);
132 this.blacklist = new LinkedList<>(constraint.blacklist);
135 public Integer getFreq_limit_per_target() {
136 return freqLimitPerTarget;
140 public void setFreq_limit_per_target(Integer freqLimitPerTarget) {
141 this.freqLimitPerTarget = freqLimitPerTarget;
145 public Map<String, String> getTime_window() {
150 public void setTime_window(Map<String, String> timeWindow) {
151 this.timeWindow = timeWindow;
155 public Map<String, String> getActive_time_range() {
156 return activeTimeRange;
160 public void setActive_time_range(Map<String, String> activeTimeRange) {
161 this.activeTimeRange = activeTimeRange;
165 public List<String> getBlacklist() {
170 public void setBlacklist(List<String> blacklist) {
171 this.blacklist = blacklist;
175 public Integer getMinVnfCount() {
180 public void setMinVnfCount(Integer minVnfCount) {
181 this.minVnfCount = minVnfCount;
185 public Integer getMaxVnfCount() {
190 public void setMaxVnfCount(Integer maxVnfCount) {
191 this.maxVnfCount = maxVnfCount;
195 * Check if these constraint values are valid.
197 * @return true if valid
199 public boolean isValid() {
201 // Sonar likes these statements combined as well as not use
204 // If the freqLimitPerTarget is null AND the timeWindow is NOT null
206 // timeWindow is null AND the freqLimitPerTarget is NOT null
208 // then we want to return false (hence the preceding !)
210 return ! ((freqLimitPerTarget == null && timeWindow != null)
211 || (timeWindow == null && freqLimitPerTarget != null));
215 public String toString() {
216 return "Constraint [freq_limit_per_target=" + freqLimitPerTarget + ", time_window="
217 + timeWindow + ", active_time_range=" + activeTimeRange + ", blacklist=" + blacklist + "]";
221 public int hashCode() {
222 final int prime = 31;
224 result = prime * result + ((freqLimitPerTarget == null) ? 0 : freqLimitPerTarget.hashCode());
225 result = prime * result + ((timeWindow == null) ? 0 : timeWindow.hashCode());
226 result = prime * result + ((activeTimeRange == null) ? 0 : activeTimeRange.hashCode());
227 result = prime * result + ((blacklist == null) ? 0 : blacklist.hashCode());
232 public boolean equals(Object obj) {
239 if (getClass() != obj.getClass()) {
242 Constraint other = (Constraint) obj;
243 return equalsMayBeNull(freqLimitPerTarget, other.freqLimitPerTarget)
244 && equalsMayBeNull(timeWindow, other.timeWindow)
245 && equalsMayBeNull(activeTimeRange, other.activeTimeRange)
246 && equalsMayBeNull(blacklist, other.blacklist);
249 private boolean equalsMayBeNull(final Object obj1, final Object obj2) {
253 return obj1.equals(obj2);