blob: eaac3cdb065996a126f59c1d7bfed30872f6e18d [file] [log] [blame]
Hyunsun Moon90163ba2016-10-12 13:35:14 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon90163ba2016-10-12 13:35:14 -07003 *
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 */
16package org.onosproject.ofagent.impl;
17
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090018import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableSet;
Hyunsun Moon53381e82017-03-28 19:58:28 +090020import com.google.common.collect.Sets;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070021import org.onosproject.incubator.net.virtual.NetworkId;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080022import org.onosproject.net.TenantId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070023import org.onosproject.ofagent.api.OFAgent;
24import org.onosproject.ofagent.api.OFController;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070025
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090026import java.util.Objects;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070027import java.util.Set;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090028
29import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070030
31/**
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090032 * Implementation of OpenFlow agent.
Hyunsun Moon90163ba2016-10-12 13:35:14 -070033 */
34public final class DefaultOFAgent implements OFAgent {
35
36 private final NetworkId networkId;
Jovana Vuletac884b692017-11-28 16:52:35 +010037 private final TenantId tenantId;
38
Hyunsun Moon90163ba2016-10-12 13:35:14 -070039 private final Set<OFController> controllers;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090040 private final State state;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070041
Jovana Vuletac884b692017-11-28 16:52:35 +010042 private DefaultOFAgent(NetworkId networkId, TenantId tenantId,
Hyunsun Moon90163ba2016-10-12 13:35:14 -070043 Set<OFController> controllers,
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090044 State state) {
Hyunsun Moon90163ba2016-10-12 13:35:14 -070045 this.networkId = networkId;
Jovana Vuletac884b692017-11-28 16:52:35 +010046 this.tenantId = tenantId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070047 this.controllers = controllers;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090048 this.state = state;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070049 }
50
51 @Override
52 public NetworkId networkId() {
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090053 return networkId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070054 }
55
56 @Override
Jovana Vuletac884b692017-11-28 16:52:35 +010057 public TenantId tenantId() {
58 return tenantId;
59 }
60
61 @Override
Hyunsun Moon90163ba2016-10-12 13:35:14 -070062 public Set<OFController> controllers() {
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090063 return controllers;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070064 }
65
66 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090067 public State state() {
68 return state;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070069 }
70
71 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090072 public int hashCode() {
73 return Objects.hash(networkId);
Hyunsun Moon90163ba2016-10-12 13:35:14 -070074 }
75
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090076 @Override
77 public boolean equals(Object obj) {
78 if (this == obj) {
79 return true;
80 }
81
82 if (obj instanceof DefaultOFAgent) {
83 DefaultOFAgent that = (DefaultOFAgent) obj;
84 if (Objects.equals(networkId, that.networkId)) {
85 return true;
86 }
87 }
88 return false;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070089 }
90
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090091 @Override
92 public String toString() {
93 return MoreObjects.toStringHelper(this)
94 .add("networkId", this.networkId)
Jovana Vuletac884b692017-11-28 16:52:35 +010095 .add("tenantId", this.tenantId)
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090096 .add("controllers", this.controllers)
97 .add("state", this.state)
98 .toString();
Hyunsun Moon90163ba2016-10-12 13:35:14 -070099 }
100
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900101 /**
102 * Returns new builder instance.
103 *
104 * @return default ofagent builder
105 */
106 public static Builder builder() {
107 return new Builder();
108 }
109
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900110 public static final class Builder implements OFAgent.Builder {
111
112 private NetworkId networkId;
Jovana Vuletac884b692017-11-28 16:52:35 +0100113 private TenantId tenantId;
Hyunsun Moon53381e82017-03-28 19:58:28 +0900114 private Set<OFController> controllers = Sets.newHashSet();
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900115 private State state;
116
117 private Builder() {
118 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700119
120 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900121 public OFAgent build() {
122 checkNotNull(networkId, "Network ID cannot be null");
Jovana Vuletac884b692017-11-28 16:52:35 +0100123 checkNotNull(tenantId, "Tenant ID cannot be null");
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900124 checkNotNull(state, "State cannot be null");
125 controllers = controllers == null ? ImmutableSet.of() : controllers;
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700126
Jovana Vuletac884b692017-11-28 16:52:35 +0100127 return new DefaultOFAgent(networkId, tenantId, controllers, state);
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900128 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700129
130 @Override
Hyunsun Moon53381e82017-03-28 19:58:28 +0900131 public Builder from(OFAgent ofAgent) {
132 this.networkId = ofAgent.networkId();
Jovana Vuletac884b692017-11-28 16:52:35 +0100133 this.tenantId = ofAgent.tenantId();
Hyunsun Moon53381e82017-03-28 19:58:28 +0900134 this.controllers = Sets.newHashSet(ofAgent.controllers());
135 this.state = ofAgent.state();
136 return this;
137 }
138
139 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900140 public Builder networkId(NetworkId networkId) {
141 this.networkId = networkId;
142 return this;
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700143 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700144
145 @Override
Jovana Vuletac884b692017-11-28 16:52:35 +0100146 public Builder tenantId(TenantId tenantId) {
147 this.tenantId = tenantId;
148 return this;
149 }
150
151 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900152 public Builder controllers(Set<OFController> controllers) {
153 this.controllers = controllers;
154 return this;
155 }
156
157 @Override
Hyunsun Moon53381e82017-03-28 19:58:28 +0900158 public OFAgent.Builder addController(OFController controller) {
159 this.controllers.add(controller);
160 return this;
161 }
162
163 @Override
164 public OFAgent.Builder deleteController(OFController controller) {
165 this.controllers.remove(controller);
166 return this;
167 }
168
169 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900170 public Builder state(State state) {
171 this.state = state;
172 return this;
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700173 }
174 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700175}