55b96e14e6a0c671fee94a0dd8502e4b60b71784
[policy/models.git] / models-interactions / model-yaml / src / main / java / org / onap / policy / controlloop / policy / guard / GuardPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml
4  * ================================================================================
5  * Copyright (C) 2017-2019 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.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 List<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         this.limitConstraints = limitConstraints;
72     }
73
74     public GuardPolicy(String name, String description, MatchParameters matchParameters,
75                     List<Constraint> limitConstraints) {
76         this(name, matchParameters, limitConstraints);
77         this.description = description;
78     }
79
80     public GuardPolicy(String id, String name, String description, MatchParameters matchParameters,
81                     List<Constraint> limitConstraints) {
82         this(name, description, matchParameters, limitConstraints);
83         this.id = id;
84     }
85
86     /**
87      * Constructor.
88      *
89      * @param policy copy object
90      */
91     public GuardPolicy(GuardPolicy policy) {
92         this.id = policy.id;
93         this.name = policy.name;
94         this.description = policy.description;
95         this.matchParameters = new MatchParameters(policy.matchParameters);
96         this.limitConstraints = policy.limitConstraints;
97     }
98
99     public String getId() {
100         return id;
101     }
102
103     public void setId(String id) {
104         this.id = id;
105     }
106
107     public String getName() {
108         return name;
109     }
110
111     public void setName(String name) {
112         this.name = name;
113     }
114
115     public String getDescription() {
116         return description;
117     }
118
119     public void setDescription(String description) {
120         this.description = description;
121     }
122
123     public MatchParameters getMatch_parameters() {
124         return matchParameters;
125     }
126
127     public void setMatch_parameters(MatchParameters matchParameters) {
128         this.matchParameters = matchParameters;
129     }
130
131     public List<Constraint> getLimit_constraints() {
132         return  limitConstraints;
133     }
134
135     public void setLimit_constraints(List<Constraint> limitConstraints) {
136         this.limitConstraints = limitConstraints;
137     }
138
139     public boolean isValid() {
140         return (id != null && name != null);
141     }
142
143     @Override
144     public String toString() {
145         return "Policy [id=" + id + ", name=" + name + ", description=" + description + ", match_parameters="
146                 + matchParameters + ", limitConstraints=" + limitConstraints + "]";
147     }
148
149     @Override
150     public int hashCode() {
151         final int prime = 31;
152         int result = 1;
153         result = prime * result + ((description == null) ? 0 : description.hashCode());
154         result = prime * result + ((id == null) ? 0 : id.hashCode());
155         result = prime * result + ((name == null) ? 0 : name.hashCode());
156         result = prime * result + ((limitConstraints == null) ? 0 : limitConstraints.hashCode());
157         result = prime * result + ((matchParameters == null) ? 0 : matchParameters.hashCode());
158         return result;
159     }
160
161     @Override
162     public boolean equals(Object obj) {
163         if (this == obj) {
164             return true;
165         }
166         if (obj == null) {
167             return false;
168         }
169         if (getClass() != obj.getClass()) {
170             return false;
171         }
172         GuardPolicy other = (GuardPolicy) obj;
173         boolean isEq = equalsMayBeNull(description, other.description)
174                 && equalsMayBeNull(id, other.id)
175                 && equalsMayBeNull(name, other.name);
176         return isEq
177                 && equalsMayBeNull(limitConstraints, other.limitConstraints)
178                 && equalsMayBeNull(matchParameters, other.matchParameters);
179     }
180
181     private boolean equalsMayBeNull(final Object obj1, final Object obj2) {
182         if ( obj1 == null ) {
183             return obj2 == null;
184         }
185         return obj1.equals(obj2);
186     }
187 }