Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 1 | /* |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 3 | * |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 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 |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 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 | package org.onosproject.kafkaintegration.api.dto; |
| 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type; |
| 24 | |
| 25 | /** |
| 26 | * Representation of a subscription to an event type. |
| 27 | * |
| 28 | */ |
| 29 | public final class DefaultEventSubscriber implements EventSubscriber { |
| 30 | private final String appName; |
| 31 | private final EventSubscriberGroupId subscriberGroupId; |
| 32 | private final Type eventType; |
| 33 | |
| 34 | /** |
| 35 | * Creates a new Event Subscriber. |
| 36 | * |
| 37 | * @param name Application Name |
| 38 | * @param groupId Subscriber group id of the application |
| 39 | * @param eventType ONOS event type |
| 40 | */ |
| 41 | public DefaultEventSubscriber(String name, EventSubscriberGroupId groupId, |
| 42 | Type eventType) { |
| 43 | this.appName = checkNotNull(name); |
| 44 | this.subscriberGroupId = checkNotNull(groupId); |
| 45 | this.eventType = checkNotNull(eventType); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public String appName() { |
| 50 | return appName; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public EventSubscriberGroupId subscriberGroupId() { |
| 55 | return subscriberGroupId; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Type eventType() { |
| 60 | return eventType; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int hashCode() { |
| 65 | return Objects.hash(appName, subscriberGroupId, eventType); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean equals(Object o) { |
| 70 | if (o instanceof DefaultEventSubscriber) { |
| 71 | DefaultEventSubscriber sub = (DefaultEventSubscriber) o; |
| 72 | if (sub.appName.equals(appName) |
| 73 | && sub.subscriberGroupId.equals(subscriberGroupId) |
| 74 | && sub.eventType.equals(eventType)) { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public String toString() { |
| 84 | return toStringHelper(this).add("appName", appName) |
| 85 | .addValue(subscriberGroupId.toString()) |
| 86 | .add("eventType", eventType).toString(); |
| 87 | } |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 88 | |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 89 | /** |
| 90 | * To create an instance of the builder. |
| 91 | * |
| 92 | * @return instance of builder |
| 93 | */ |
| 94 | public static Builder builder() { |
| 95 | return new Builder(); |
| 96 | } |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 97 | |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Builder class for Event subscriber. |
| 100 | */ |
| 101 | public static final class Builder implements EventSubscriber.Builder { |
| 102 | private String appName; |
| 103 | private EventSubscriberGroupId subscriberGroupId; |
| 104 | private Type eventType; |
| 105 | |
| 106 | @Override |
| 107 | public Builder setAppName(String appName) { |
| 108 | this.appName = appName; |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | @Override |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 113 | public Builder setSubscriberGroupId(EventSubscriberGroupId subscriberGroupId) { |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 114 | this.subscriberGroupId = subscriberGroupId; |
| 115 | return this; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Builder setEventType(Type eventType) { |
| 120 | this.eventType = eventType; |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public EventSubscriber build() { |
| 126 | checkNotNull(appName, "App name cannot be null"); |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 127 | checkNotNull(subscriberGroupId, |
| 128 | "Subscriber group ID cannot " + "be " + "null"); |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 129 | checkNotNull(eventType, "Event type cannot be null"); |
| 130 | |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 131 | return new DefaultEventSubscriber(appName, subscriberGroupId, |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 132 | eventType); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } |