Adding examples module to apex-pdp
[policy/apex-pdp.git] / examples / adaptive / src / main / java / org / onap / policy / apex / examples / adaptive / concepts / AnomalyDetection.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.examples.adaptive.concepts;
22
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.LinkedList;
26 import java.util.List;
27
28 /**
29  * The Class AnomalyDetection is used as a Java context for Adaptive anomaly detection in the adaptive domain.
30  */
31 public class AnomalyDetection implements Serializable {
32     private static final long serialVersionUID = -823013127095523727L;
33
34     private static final int HASH_PRIME_1 = 31;
35     private static final int HASH_PRIME_2 = 1231;
36     private static final int HASH_PRIME_3 = 1237;
37
38     private boolean firstRound = true;
39     private int frequency = 0;
40
41     /**
42      * The Constructor creates an AnomalyDetection instance.
43      */
44     public AnomalyDetection() {
45         firstRound = true;
46         frequency = 0;
47     }
48
49     private List<Double> anomalyScores = new LinkedList<>();
50     private List<Double> frequencyForecasted;
51
52     /**
53      * Checks if the AnomalyDetection instance is initialized.
54      *
55      * @return true, if the AnomalyDetection instance is initialized
56      */
57     public boolean isInitialized() {
58         return (frequencyForecasted != null);
59     }
60
61     /**
62      * Initializes the AnomalyDetection instance.
63      *
64      * @param incomingFrequency the frequency
65      */
66     public void init(final int incomingFrequency) {
67         frequencyForecasted = new ArrayList<>(incomingFrequency);
68         for (int i = 0; i < incomingFrequency; i++) {
69             frequencyForecasted.add(null);
70         }
71     }
72
73     /**
74      * Indicates if this is the first round of the algorithm.
75      *
76      * @return true if this is the first round of the algorithm
77      */
78     public boolean getFirstRound() {
79         return firstRound;
80     }
81
82     /**
83      * Sets the first round indicator of the algorithm.
84      *
85      * @param firstRound the first round indicator of the algorithm
86      */
87     public void setFirstRound(final boolean firstRound) {
88         this.firstRound = firstRound;
89     }
90
91     /**
92      * Gets the frequency value of the algorithm.
93      *
94      * @return the frequency value of the algorithm
95      */
96     public int getFrequency() {
97         return frequency;
98     }
99
100     /**
101      * Sets the frequency value of the algorithm.
102      *
103      * @param frequency the frequency value of the algorithm
104      */
105     public void setFrequency(final int frequency) {
106         this.frequency = frequency;
107     }
108
109     /**
110      * Gets the anomaly score values of the algorithm.
111      *
112      * @return the anomaly score values of the algorithm
113      */
114     public List<Double> getAnomalyScores() {
115         return anomalyScores;
116     }
117
118     /**
119      * Sets the anomaly score values of the algorithm.
120      *
121      * @param anomalyScores the anomaly score values of the algorithm
122      */
123     public void setAnomalyScores(final LinkedList<Double> anomalyScores) {
124         this.anomalyScores = anomalyScores;
125     }
126
127     /**
128      * Check if the anomaly score values of the algorithm are set.
129      *
130      * @return true, if the anomaly score values of the algorithm are set
131      */
132     public boolean checkSetAnomalyScores() {
133         return ((anomalyScores != null) && (!anomalyScores.isEmpty()));
134     }
135
136     /**
137      * Unset the anomaly score values of the algorithm.
138      */
139     public void unsetAnomalyScores() {
140         anomalyScores = null;
141     }
142
143     /**
144      * Gets the frequency forecasted by the algorithm.
145      *
146      * @return the frequency forecasted by the algorithm
147      */
148     public List<Double> getFrequencyForecasted() {
149         return frequencyForecasted;
150     }
151
152     /**
153      * Sets the frequency forecasted by the algorithm.
154      *
155      * @param frequencyForecasted the frequency forecasted by the algorithm
156      */
157     public void setFrequencyForecasted(final List<Double> frequencyForecasted) {
158         this.frequencyForecasted = frequencyForecasted;
159     }
160
161     /**
162      * Check if the frequency forecasted by the algorithm is set.
163      *
164      * @return true, if the frequency forecasted by the algorithm is set
165      */
166     public boolean checkSetFrequencyForecasted() {
167         return ((frequencyForecasted != null) && (!frequencyForecasted.isEmpty()));
168     }
169
170     /**
171      * Unset the frequency forecasted by the algorithm.
172      */
173     public void unsetFrequencyForecasted() {
174         frequencyForecasted = null;
175     }
176
177     /*
178      * (non-Javadoc)
179      *
180      * @see java.lang.Object#toString()
181      */
182     @Override
183     public String toString() {
184         return "AnomalyDetection [firstRound=" + firstRound + ", frequency=" + frequency + ", anomalyScores="
185                 + anomalyScores + ", frequencyForecasted=" + frequencyForecasted + "]";
186     }
187
188     /*
189      * (non-Javadoc)
190      *
191      * @see java.lang.Object#hashCode()
192      */
193     @Override
194     public int hashCode() {
195         final int prime = HASH_PRIME_1;
196         int result = 1;
197         result = prime * result + ((anomalyScores == null) ? 0 : anomalyScores.hashCode());
198         result = prime * result + (firstRound ? HASH_PRIME_2 : HASH_PRIME_3);
199         result = prime * result + frequency;
200         result = prime * result + ((frequencyForecasted == null) ? 0 : frequencyForecasted.hashCode());
201         return result;
202     }
203
204     /*
205      * (non-Javadoc)
206      *
207      * @see java.lang.Object#equals(java.lang.Object)
208      */
209     @Override
210     public boolean equals(final Object obj) {
211         if (this == obj) {
212             return true;
213         }
214         if (obj == null) {
215             return false;
216         }
217         if (getClass() != obj.getClass()) {
218             return false;
219         }
220         final AnomalyDetection other = (AnomalyDetection) obj;
221         if (anomalyScores == null) {
222             if (other.anomalyScores != null) {
223                 return false;
224             }
225         } else if (!anomalyScores.equals(other.anomalyScores)) {
226             return false;
227         }
228         if (firstRound != other.firstRound) {
229             return false;
230         }
231         if (frequency != other.frequency) {
232             return false;
233         }
234         if (frequencyForecasted == null) {
235             if (other.frequencyForecasted != null) {
236                 return false;
237             }
238         } else if (!frequencyForecasted.equals(other.frequencyForecasted)) {
239             return false;
240         }
241         return true;
242     }
243 }