aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DefaultAlarm.java
blob: afa366ad552fcb0c1ba09b2de82f2586297b0cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright 2014-2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.incubator.net.faultmanagement.alarm;

import org.onosproject.net.DeviceId;

import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Default implementation of an alarm.
 */
public final class DefaultAlarm implements Alarm {

    private final AlarmId id;

    private final DeviceId deviceId;
    private final String description;
    private final AlarmEntityId source;
    private final long timeRaised;
    private final long timeUpdated;
    private final Long timeCleared;
    private final SeverityLevel severity;
    private final boolean isServiceAffecting;
    private final boolean isAcknowledged;
    private final boolean isManuallyClearable;
    private final String assignedUser;

    /**
     * Instantiates a new Default alarm.
     *
     * @param id the id
     * @param deviceId the device id
     * @param description the description
     * @param source the source, null indicates none.
     * @param timeRaised the time raised.
     * @param timeUpdated the time last updated.
     * @param timeCleared the time cleared, null indicates uncleared.
     * @param severity the severity
     * @param isServiceAffecting the service affecting
     * @param isAcknowledged the acknowledged
     * @param isManuallyClearable the manually clearable
     * @param assignedUser the assigned user, `null` indicates none.
     */
    private DefaultAlarm(final AlarmId id,
            final DeviceId deviceId,
            final String description,
            final AlarmEntityId source,
            final long timeRaised,
            final long timeUpdated,
            final Long timeCleared,
            final SeverityLevel severity,
            final boolean isServiceAffecting,
            final boolean isAcknowledged,
            final boolean isManuallyClearable,
            final String assignedUser) {
        this.id = id;
        this.deviceId = deviceId;
        this.description = description;
        this.source = source;
        this.timeRaised = timeRaised;
        this.timeUpdated = timeUpdated;
        this.timeCleared = timeCleared;
        this.severity = severity;
        this.isServiceAffecting = isServiceAffecting;
        this.isAcknowledged = isAcknowledged;
        this.isManuallyClearable = isManuallyClearable;
        this.assignedUser = assignedUser;
    }

    @Override
    public AlarmId id() {
        return id;
    }

    @Override
    public DeviceId deviceId() {
        return deviceId;
    }

    @Override
    public String description() {
        return description;
    }

    @Override
    public AlarmEntityId source() {
        return source;
    }

    @Override
    public long timeRaised() {
        return timeRaised;
    }

    @Override
    public long timeUpdated() {
        return timeUpdated;
    }

    @Override
    public Long timeCleared() {
        return timeCleared;
    }

    @Override
    public SeverityLevel severity() {
        return severity;
    }

    @Override
    public boolean serviceAffecting() {
        return isServiceAffecting;
    }

    @Override
    public boolean acknowledged() {
        return isAcknowledged;
    }

    @Override
    public boolean manuallyClearable() {
        return isManuallyClearable;
    }

    @Override
    public String assignedUser() {
        return assignedUser;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, deviceId, description,
                source, timeRaised, timeUpdated, timeCleared, severity,
                isServiceAffecting, isAcknowledged,
                isManuallyClearable, assignedUser);
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DefaultAlarm other = (DefaultAlarm) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        if (!Objects.equals(this.deviceId, other.deviceId)) {
            return false;
        }
        if (!Objects.equals(this.description, other.description)) {
            return false;
        }
        if (!Objects.equals(this.source, other.source)) {
            return false;
        }
        if (this.timeRaised != other.timeRaised) {
            return false;
        }
        if (this.timeUpdated != other.timeUpdated) {
            return false;
        }
        if (!Objects.equals(this.timeCleared, other.timeCleared)) {
            return false;
        }
        if (this.severity != other.severity) {
            return false;
        }
        if (this.isServiceAffecting != other.isServiceAffecting) {
            return false;
        }
        if (this.isAcknowledged != other.isAcknowledged) {
            return false;
        }
        if (this.isManuallyClearable != other.isManuallyClearable) {
            return false;
        }
        if (!Objects.equals(this.assignedUser, other.assignedUser)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return toStringHelper(this)
                .add("id", id)
                .add("deviceId", deviceId)
                .add("description", description)
                .add("source", source)
                .add("timeRaised", timeRaised)
                .add("timeUpdated", timeUpdated)
                .add("timeCleared", timeCleared)
                .add("severity", severity)
                .add("serviceAffecting", isServiceAffecting)
                .add("acknowledged", isAcknowledged)
                .add("manuallyClearable", isManuallyClearable)
                .add("assignedUser", assignedUser)
                .toString();
    }

    public static class Builder {

        // Manadatory fields ..
        private final AlarmId id;
        private final DeviceId deviceId;
        private final String description;
        private final SeverityLevel severity;
        private final long timeRaised;

        // Optional fields ..
        private AlarmEntityId source = AlarmEntityId.NONE;
        private long timeUpdated;
        private Long timeCleared = null;
        private boolean isServiceAffecting = false;
        private boolean isAcknowledged = false;
        private boolean isManuallyClearable = false;
        private String assignedUser = null;

        public Builder(final Alarm alarm) {
            this(alarm.id(), alarm.deviceId(), alarm.description(), alarm.severity(), alarm.timeRaised());
            this.source = AlarmEntityId.NONE;
            this.timeUpdated = alarm.timeUpdated();
            this.timeCleared = alarm.timeCleared();
            this.isServiceAffecting = alarm.serviceAffecting();
            this.isAcknowledged = alarm.acknowledged();
            this.isManuallyClearable = alarm.manuallyClearable();
            this.assignedUser = alarm.assignedUser();

        }

        public Builder(final AlarmId id, final DeviceId deviceId,
                final String description, final SeverityLevel severity, final long timeRaised) {
            super();
            this.id = id;
            this.deviceId = deviceId;
            this.description = description;
            this.severity = severity;
            this.timeRaised = timeRaised;
            // Unless specified time-updated is same as raised.
            this.timeUpdated = timeRaised;
        }

        public Builder forSource(final AlarmEntityId source) {
            this.source = source;
            return this;
        }

        public Builder withTimeUpdated(final long timeUpdated) {
            this.timeUpdated = timeUpdated;
            return this;
        }

        public Builder withTimeCleared(final Long timeCleared) {
            this.timeCleared = timeCleared;
            return this;
        }

        public Builder withServiceAffecting(final boolean isServiceAffecting) {
            this.isServiceAffecting = isServiceAffecting;
            return this;
        }

        public Builder withAcknowledged(final boolean isAcknowledged) {
            this.isAcknowledged = isAcknowledged;
            return this;
        }

        public Builder withManuallyClearable(final boolean isManuallyClearable) {
            this.isManuallyClearable = isManuallyClearable;
            return this;
        }

        public Builder withAssignedUser(final String assignedUser) {
            this.assignedUser = assignedUser;
            return this;
        }

        public DefaultAlarm build() {
            checkNotNull(id, "Must specify an alarm id");
            checkNotNull(deviceId, "Must specify a device");
            checkNotNull(description, "Must specify a description");
            checkNotNull(timeRaised, "Must specify a time raised");
            checkNotNull(timeUpdated, "Must specify a time updated");
            checkNotNull(severity, "Must specify a severity");

            return new DefaultAlarm(id, deviceId, description, source, timeRaised, timeUpdated, timeCleared,
                    severity, isServiceAffecting, isAcknowledged, isManuallyClearable, assignedUser);
        }
    }
}