Jonathan Hart | 2b5ceec | 2017-12-04 13:57:19 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.opencord.cordconfig; |
| 18 | |
| 19 | import org.onosproject.event.AbstractEvent; |
| 20 | import org.joda.time.LocalDateTime; |
| 21 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 22 | |
| 23 | /** |
| 24 | * Describes an CORD config event. |
| 25 | */ |
| 26 | public class CordConfigEvent extends AbstractEvent<CordConfigEvent.Type, Object> { |
| 27 | private final Object prevSubject; |
| 28 | |
| 29 | public enum Type { |
| 30 | /** |
| 31 | * Indicates a new access agent has been added. |
| 32 | * Event subject should be AccessAgentData. |
| 33 | */ |
| 34 | ACCESS_AGENT_ADDED, |
| 35 | |
| 36 | /** |
| 37 | * Indicates an access agent has been updated. |
| 38 | * Event subject and prevSubject should be AccessAgentData. |
| 39 | */ |
| 40 | ACCESS_AGENT_UPDATED, |
| 41 | |
| 42 | /** |
| 43 | * Indicates an access agent has been removed. |
| 44 | * Event prevSubject should be AccessAgentData. |
| 45 | */ |
| 46 | ACCESS_AGENT_REMOVED, |
| 47 | |
| 48 | /** |
| 49 | * Indicates a new access device has been added. |
| 50 | * Event subject should be AccessDeviceData. |
| 51 | */ |
| 52 | ACCESS_DEVICE_ADDED, |
| 53 | |
| 54 | /** |
| 55 | * Indicates an access device has been updated. |
| 56 | * Event subject and prevSubject should be AccessDeviceData. |
| 57 | */ |
| 58 | ACCESS_DEVICE_UPDATED, |
| 59 | |
| 60 | /** |
| 61 | * Indicates an access device has been removed. |
| 62 | * Event prevSubject should be AccessDeviceData. |
| 63 | */ |
| 64 | ACCESS_DEVICE_REMOVED, |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates an CORD config event with type and subject. |
| 69 | * |
| 70 | * @param type event type |
| 71 | * @param subject subject CORD config |
| 72 | */ |
| 73 | public CordConfigEvent(Type type, Object subject) { |
| 74 | this(type, subject, null); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates an CORD config event with type, subject and time of event. |
| 79 | * |
| 80 | * @param type event type |
| 81 | * @param subject subject CORD config |
| 82 | * @param time time of event |
| 83 | */ |
| 84 | public CordConfigEvent(Type type, Object subject, long time) { |
| 85 | this(type, subject, null, time); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Creates an CORD config event with type, subject and previous subject. |
| 90 | * |
| 91 | * @param type event type |
| 92 | * @param subject subject CORD config |
| 93 | * @param prevSubject previous CORD config subject |
| 94 | */ |
| 95 | public CordConfigEvent(Type type, Object subject, Object prevSubject) { |
| 96 | super(type, subject); |
| 97 | this.prevSubject = prevSubject; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Creates an CORD config event with type, subject, previous subject and time. |
| 102 | * |
| 103 | * @param type event type |
| 104 | * @param subject subject CORD config |
| 105 | * @param prevSubject previous CORD config subject |
| 106 | * @param time time of event |
| 107 | */ |
| 108 | public CordConfigEvent(Type type, Object subject, Object prevSubject, long time) { |
| 109 | super(type, subject, time); |
| 110 | this.prevSubject = prevSubject; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the previous CORD config subject. |
| 115 | * |
| 116 | * @return previous subject of CORD config or null if previous subject does not exist. |
| 117 | */ |
| 118 | public Object prevSubject() { |
| 119 | return prevSubject; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public String toString() { |
| 124 | if (prevSubject == null) { |
| 125 | return super.toString(); |
| 126 | } |
| 127 | return toStringHelper(this) |
| 128 | .add("time", new LocalDateTime(time())) |
| 129 | .add("type", type()) |
| 130 | .add("subject", subject()) |
| 131 | .add("prevSubject", prevSubject) |
| 132 | .toString(); |
| 133 | } |
| 134 | } |