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