Merge "Add debugging of REST call"
[policy/drools-applications.git] / controlloop / common / policy-yaml / src / main / java / org / onap / policy / controlloop / policy / guard / GuardPolicy.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.LinkedList;
24 import java.util.List;
25 import java.util.UUID;
26
27 public class GuardPolicy {
28
29     private String id = UUID.randomUUID().toString();
30     private String name;
31     private String description;
32     private MatchParameters matchParameters;
33     private LinkedList<Constraint> limitConstraints;
34     
35     public GuardPolicy() {
36         //Do Nothing Empty Constructor. 
37     }
38     
39     public GuardPolicy(String id) {
40         this.id = id;
41     }
42     
43     public GuardPolicy(String name, MatchParameters matchParameters) {
44         this.name = name;
45         this.matchParameters = matchParameters;
46     }
47     
48     /**
49      * Constructor.
50      * 
51      * @param id id
52      * @param name name
53      * @param description description
54      * @param matchParameters match parameters
55      */
56     public GuardPolicy(String id, String name, String description, MatchParameters matchParameters) {
57         this(name, matchParameters);
58         this.id = id;
59         this.description = description;
60     }
61     
62     /**
63      * Constructor.
64      * 
65      * @param name name
66      * @param matchParameters match parameters
67      * @param limitConstraints limit constraints
68      */
69     public GuardPolicy(String name, MatchParameters matchParameters, List<Constraint> limitConstraints) {
70         this(name, matchParameters);
71         if (limitConstraints != null) {
72             this.limitConstraints = (LinkedList<Constraint>) limitConstraints;
73         }
74     }
75     
76     public GuardPolicy(String name, String description, MatchParameters matchParameters, 
77                     List<Constraint> limitConstraints) {
78         this(name, matchParameters, limitConstraints);
79         this.description = description;
80     }
81     
82     public GuardPolicy(String id, String name, String description, MatchParameters matchParameters, 
83                     List<Constraint> limitConstraints) {
84         this(name, description, matchParameters, limitConstraints);
85         this.id = id;
86     }
87     
88     /**
89      * Constructor.
90      * 
91      * @param policy copy object
92      */
93     public GuardPolicy(GuardPolicy policy) {
94         this.id = policy.id;
95         this.name = policy.name;
96         this.description = policy.description;
97         this.matchParameters = new MatchParameters(policy.matchParameters);
98         if (policy.limitConstraints != null) {
99             this.limitConstraints = policy.limitConstraints;
100         }
101     }
102     
103     public String getId() {
104         return id;
105     }
106
107     public void setId(String id) {
108         this.id = id;
109     }
110
111     public String getName() {
112         return name;
113     }
114
115     public void setName(String name) {
116         this.name = name;
117     }
118
119     public String getDescription() {
120         return description;
121     }
122
123     public void setDescription(String description) {
124         this.description = description;
125     }
126
127     public MatchParameters getMatch_parameters() {
128         return matchParameters;
129     }
130
131     public void setMatch_parameters(MatchParameters matchParameters) {
132         this.matchParameters = matchParameters;
133     }
134
135     public LinkedList<Constraint> getLimit_constraints() {
136         return  limitConstraints;
137     }
138
139     public void setLimit_constraints(LinkedList<Constraint> limitConstraints) {
140         this.limitConstraints = limitConstraints;
141     }
142
143     public boolean isValid() {
144         return (id == null || name == null) ? false : true;
145     }
146     
147     @Override
148     public String toString() {
149         return "Policy [id=" + id + ", name=" + name + ", description=" + description + ", match_parameters=" 
150                 + matchParameters + ", limitConstraints=" + limitConstraints + "]";
151     }
152
153     @Override
154     public int hashCode() {
155         final int prime = 31;
156         int result = 1;
157         result = prime * result + ((description == null) ? 0 : description.hashCode());
158         result = prime * result + ((id == null) ? 0 : id.hashCode());
159         result = prime * result + ((name == null) ? 0 : name.hashCode());
160         result = prime * result + ((limitConstraints == null) ? 0 : limitConstraints.hashCode());
161         result = prime * result + ((matchParameters == null) ? 0 : matchParameters.hashCode());
162         return result;
163     }
164
165     @Override
166     public boolean equals(Object obj) {
167         if (this == obj) {
168             return true;
169         }
170         if (obj == null) {
171             return false;
172         }
173         if (getClass() != obj.getClass()) {
174             return false;
175         }
176         GuardPolicy other = (GuardPolicy) obj;
177         return equalsMayBeNull(description, other.description)
178                 && equalsMayBeNull(id, other.id)
179                 && equalsMayBeNull(name, other.name)
180                 && equalsMayBeNull(limitConstraints, other.limitConstraints)
181                 && equalsMayBeNull(matchParameters, other.matchParameters);
182     }
183     
184     private boolean equalsMayBeNull(final Object obj1, final Object obj2) {
185         if ( obj1 == null ) {
186             return obj2 == null;
187         }
188         return obj1.equals(obj2);
189     }        
190 }