blob: c82dc947104bf3def345298364c263f19758132b [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;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070022import org.onosproject.ofagent.api.OFAgent;
23import org.onosproject.ofagent.api.OFController;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070024
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090025import java.util.Objects;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070026import java.util.Set;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090027
28import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070029
30/**
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090031 * Implementation of OpenFlow agent.
Hyunsun Moon90163ba2016-10-12 13:35:14 -070032 */
33public final class DefaultOFAgent implements OFAgent {
34
35 private final NetworkId networkId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070036 private final Set<OFController> controllers;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090037 private final State state;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070038
39 private DefaultOFAgent(NetworkId networkId,
Hyunsun Moon90163ba2016-10-12 13:35:14 -070040 Set<OFController> controllers,
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090041 State state) {
Hyunsun Moon90163ba2016-10-12 13:35:14 -070042 this.networkId = networkId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070043 this.controllers = controllers;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090044 this.state = state;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070045 }
46
47 @Override
48 public NetworkId networkId() {
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090049 return networkId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070050 }
51
52 @Override
53 public Set<OFController> controllers() {
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090054 return controllers;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070055 }
56
57 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090058 public State state() {
59 return state;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070060 }
61
62 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090063 public int hashCode() {
64 return Objects.hash(networkId);
Hyunsun Moon90163ba2016-10-12 13:35:14 -070065 }
66
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090067 @Override
68 public boolean equals(Object obj) {
69 if (this == obj) {
70 return true;
71 }
72
73 if (obj instanceof DefaultOFAgent) {
74 DefaultOFAgent that = (DefaultOFAgent) obj;
75 if (Objects.equals(networkId, that.networkId)) {
76 return true;
77 }
78 }
79 return false;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070080 }
81
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090082 @Override
83 public String toString() {
84 return MoreObjects.toStringHelper(this)
85 .add("networkId", this.networkId)
86 .add("controllers", this.controllers)
87 .add("state", this.state)
88 .toString();
Hyunsun Moon90163ba2016-10-12 13:35:14 -070089 }
90
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090091 /**
92 * Returns new builder instance.
93 *
94 * @return default ofagent builder
95 */
96 public static Builder builder() {
97 return new Builder();
98 }
99
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900100 public static final class Builder implements OFAgent.Builder {
101
102 private NetworkId networkId;
Hyunsun Moon53381e82017-03-28 19:58:28 +0900103 private Set<OFController> controllers = Sets.newHashSet();
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900104 private State state;
105
106 private Builder() {
107 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700108
109 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900110 public OFAgent build() {
111 checkNotNull(networkId, "Network ID cannot be null");
112 checkNotNull(state, "State cannot be null");
113 controllers = controllers == null ? ImmutableSet.of() : controllers;
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700114
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900115 return new DefaultOFAgent(networkId, controllers, state);
116 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700117
118 @Override
Hyunsun Moon53381e82017-03-28 19:58:28 +0900119 public Builder from(OFAgent ofAgent) {
120 this.networkId = ofAgent.networkId();
121 this.controllers = Sets.newHashSet(ofAgent.controllers());
122 this.state = ofAgent.state();
123 return this;
124 }
125
126 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900127 public Builder networkId(NetworkId networkId) {
128 this.networkId = networkId;
129 return this;
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700130 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700131
132 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900133 public Builder controllers(Set<OFController> controllers) {
134 this.controllers = controllers;
135 return this;
136 }
137
138 @Override
Hyunsun Moon53381e82017-03-28 19:58:28 +0900139 public OFAgent.Builder addController(OFController controller) {
140 this.controllers.add(controller);
141 return this;
142 }
143
144 @Override
145 public OFAgent.Builder deleteController(OFController controller) {
146 this.controllers.remove(controller);
147 return this;
148 }
149
150 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900151 public Builder state(State state) {
152 this.state = state;
153 return this;
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700154 }
155 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700156}