blob: 04f4e0b7d34aeb8e20967b0082da2ca522d1fa3d [file] [log] [blame]
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -07001/**
2 * Copyright 2016-present Open Networking Laboratory
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6
7 * http://www.apache.org/licenses/LICENSE-2.0
8
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package org.onosproject.kafkaintegration.api.dto;
16
17import static com.google.common.base.MoreObjects.toStringHelper;
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Objects;
21
22import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
23
24/**
25 * Representation of a subscription to an event type.
26 *
27 */
28public final class DefaultEventSubscriber implements EventSubscriber {
29 private final String appName;
30 private final EventSubscriberGroupId subscriberGroupId;
31 private final Type eventType;
32
33 /**
34 * Creates a new Event Subscriber.
35 *
36 * @param name Application Name
37 * @param groupId Subscriber group id of the application
38 * @param eventType ONOS event type
39 */
40 public DefaultEventSubscriber(String name, EventSubscriberGroupId groupId,
41 Type eventType) {
42 this.appName = checkNotNull(name);
43 this.subscriberGroupId = checkNotNull(groupId);
44 this.eventType = checkNotNull(eventType);
45 }
46
47 @Override
48 public String appName() {
49 return appName;
50 }
51
52 @Override
53 public EventSubscriberGroupId subscriberGroupId() {
54 return subscriberGroupId;
55 }
56
57 @Override
58 public Type eventType() {
59 return eventType;
60 }
61
62 @Override
63 public int hashCode() {
64 return Objects.hash(appName, subscriberGroupId, eventType);
65 }
66
67 @Override
68 public boolean equals(Object o) {
69 if (o instanceof DefaultEventSubscriber) {
70 DefaultEventSubscriber sub = (DefaultEventSubscriber) o;
71 if (sub.appName.equals(appName)
72 && sub.subscriberGroupId.equals(subscriberGroupId)
73 && sub.eventType.equals(eventType)) {
74 return true;
75 }
76 }
77
78 return false;
79 }
80
81 @Override
82 public String toString() {
83 return toStringHelper(this).add("appName", appName)
84 .addValue(subscriberGroupId.toString())
85 .add("eventType", eventType).toString();
86 }
Shravan Ambati5a11e172016-07-21 15:55:28 -070087
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070088 /**
89 * To create an instance of the builder.
90 *
91 * @return instance of builder
92 */
93 public static Builder builder() {
94 return new Builder();
95 }
Shravan Ambati5a11e172016-07-21 15:55:28 -070096
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070097 /**
98 * Builder class for Event subscriber.
99 */
100 public static final class Builder implements EventSubscriber.Builder {
101 private String appName;
102 private EventSubscriberGroupId subscriberGroupId;
103 private Type eventType;
104
105 @Override
106 public Builder setAppName(String appName) {
107 this.appName = appName;
108 return this;
109 }
110
111 @Override
Shravan Ambati5a11e172016-07-21 15:55:28 -0700112 public Builder setSubscriberGroupId(EventSubscriberGroupId subscriberGroupId) {
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700113 this.subscriberGroupId = subscriberGroupId;
114 return this;
115 }
116
117 @Override
118 public Builder setEventType(Type eventType) {
119 this.eventType = eventType;
120 return this;
121 }
122
123 @Override
124 public EventSubscriber build() {
125 checkNotNull(appName, "App name cannot be null");
Shravan Ambati5a11e172016-07-21 15:55:28 -0700126 checkNotNull(subscriberGroupId,
127 "Subscriber group ID cannot " + "be " + "null");
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700128 checkNotNull(eventType, "Event type cannot be null");
129
Shravan Ambati5a11e172016-07-21 15:55:28 -0700130 return new DefaultEventSubscriber(appName, subscriberGroupId,
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700131 eventType);
132 }
133 }
134
135}