blob: ce1f9b4c2e630f70b61023f1640612ae1ac3e05c [file] [log] [blame]
Ray Milkey2d572dd2017-04-14 10:01:24 -07001/*
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -07002 * Copyright 2016-present Open Networking Laboratory
Ray Milkey2d572dd2017-04-14 10:01:24 -07003 *
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -07004 * 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 Milkey2d572dd2017-04-14 10:01:24 -07007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070010 * 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 */
16package org.onosproject.kafkaintegration.api.dto;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import java.util.Objects;
22
23import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
24
25/**
26 * Representation of a subscription to an event type.
27 *
28 */
29public 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 Ambati5a11e172016-07-21 15:55:28 -070088
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070089 /**
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 Ambati5a11e172016-07-21 15:55:28 -070097
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070098 /**
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 Ambati5a11e172016-07-21 15:55:28 -0700113 public Builder setSubscriberGroupId(EventSubscriberGroupId subscriberGroupId) {
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700114 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 Ambati5a11e172016-07-21 15:55:28 -0700127 checkNotNull(subscriberGroupId,
128 "Subscriber group ID cannot " + "be " + "null");
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700129 checkNotNull(eventType, "Event type cannot be null");
130
Shravan Ambati5a11e172016-07-21 15:55:28 -0700131 return new DefaultEventSubscriber(appName, subscriberGroupId,
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -0700132 eventType);
133 }
134 }
135
136}