Merge "Add debugging of REST call"
[policy/drools-applications.git] / controlloop / common / policy-yaml / src / main / java / org / onap / policy / controlloop / policy / guard / Constraint.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.policy.guard;
22
23 import java.util.Collections;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27
28 public class Constraint {
29
30     private Integer freqLimitPerTarget;
31     private Map<String,String> timeWindow;
32     private Map<String, String> activeTimeRange;
33     private Integer minVnfCount;
34     private Integer maxVnfCount;
35     
36     private List<String> blacklist;
37     
38     public Constraint() {
39         // Do Nothing empty constructor. 
40     }
41     
42     /**
43      * Constructor.
44      * 
45      * @param freqLimitPerTarget frequency limit
46      * @param timeWindow time window
47      */
48     public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow) {
49         this.freqLimitPerTarget = freqLimitPerTarget;
50         if (timeWindow != null) {
51             this.timeWindow = Collections.unmodifiableMap(timeWindow);
52         }
53     }
54
55     /**
56      * Constructor.
57      * 
58      * @param minVnfCount minimum VNF count
59      * @param maxVnfCount maximum VNF count
60      * @param activeTimeRange active time range
61      */
62     public Constraint(Integer minVnfCount, Integer maxVnfCount, Map<String, String> activeTimeRange) {
63         this.minVnfCount = minVnfCount;
64         this.maxVnfCount = maxVnfCount;
65
66         if (activeTimeRange != null) {
67             this.activeTimeRange = Collections.unmodifiableMap(activeTimeRange);
68         }
69     }
70     
71     public Constraint(List<String> blacklist) {
72         this.blacklist = new LinkedList<>(blacklist);
73     }
74     
75     /**
76      * Constructor.
77      * 
78      * @param freqLimitPerTarget frequency limit
79      * @param timeWindow time window
80      * @param blacklist blacklist
81      */
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);
86     }
87     
88     /**
89      * Constructor.
90      * 
91      * @param freqLimitPerTarget frequency limit
92      * @param timeWindow time window
93      * @param activeTimeRange active time range
94      */
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);
99         }
100     }
101     
102     /**
103      * Constructor.
104      * 
105      * @param freqLimitPerTarget frequency limit
106      * @param timeWindow the time window
107      * @param activeTimeRange active time range
108      * @param blacklist incoming blacklist
109      */
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);
115         }
116         if (blacklist != null) {
117             this.blacklist = new LinkedList<>(blacklist);
118         }
119     }
120     
121     /**
122      * Constructor.
123      * 
124      * @param constraint objec to copy
125      */
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);
131         }
132         this.blacklist = new LinkedList<>(constraint.blacklist);
133     }
134
135     public Integer getFreq_limit_per_target() {
136         return freqLimitPerTarget;
137     }
138
139
140     public void setFreq_limit_per_target(Integer freqLimitPerTarget) {
141         this.freqLimitPerTarget = freqLimitPerTarget;
142     }
143
144
145     public Map<String, String> getTime_window() {
146         return timeWindow;
147     }
148
149
150     public void setTime_window(Map<String, String> timeWindow) {
151         this.timeWindow = timeWindow;
152     }
153
154
155     public Map<String, String> getActive_time_range() {
156         return activeTimeRange;
157     }
158
159
160     public void setActive_time_range(Map<String, String> activeTimeRange) {
161         this.activeTimeRange = activeTimeRange;
162     }
163
164     
165     public List<String> getBlacklist() {
166         return blacklist;
167     }
168
169     
170     public void setBlacklist(List<String> blacklist) {
171         this.blacklist = blacklist;
172     }
173     
174     
175     public Integer getMinVnfCount() {
176         return minVnfCount;
177     }
178
179     
180     public void setMinVnfCount(Integer minVnfCount) {
181         this.minVnfCount = minVnfCount;
182     }
183
184     
185     public Integer getMaxVnfCount() {
186         return maxVnfCount;
187     }
188
189     
190     public void setMaxVnfCount(Integer maxVnfCount) {
191         this.maxVnfCount = maxVnfCount;
192     }
193
194     /**
195      * Check if these constraint values are valid.
196      * 
197      * @return true if valid
198      */
199     public boolean isValid() {
200         //
201         // Sonar likes these statements combined as well as not use
202         // boolean literals.
203         //
204         // If the freqLimitPerTarget is null AND the timeWindow is NOT null
205         // OR
206         // timeWindow is null AND the freqLimitPerTarget is NOT null
207         //
208         // then we want to return false (hence the preceding !)
209         //
210         return ! ((freqLimitPerTarget == null && timeWindow != null)
211                         || (timeWindow == null && freqLimitPerTarget != null));
212     }
213     
214     @Override
215     public String toString() {
216         return "Constraint [freq_limit_per_target=" + freqLimitPerTarget + ", time_window=" 
217                + timeWindow + ", active_time_range=" + activeTimeRange + ", blacklist=" + blacklist + "]";
218     }
219
220     @Override
221     public int hashCode() {
222         final int prime = 31;
223         int result = 1;
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());
228         return result;
229     }
230
231     @Override
232     public boolean equals(Object obj) {
233         if (this == obj) {
234             return true;
235         }
236         if (obj == null) {
237             return false;
238         }
239         if (getClass() != obj.getClass()) {
240             return false;
241         }
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);
247     }
248     
249     private boolean equalsMayBeNull(final Object obj1, final Object obj2) {
250         if (obj1 == null) {
251             return obj2 == null;
252         } 
253         return obj1.equals(obj2);
254     }
255 }