Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.ofagent.impl; |
| 17 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 21 | import org.onosproject.incubator.net.virtual.NetworkId; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 22 | import org.onosproject.ofagent.api.OFAgent; |
| 23 | import org.onosproject.ofagent.api.OFController; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 24 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 25 | import java.util.Objects; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 26 | import java.util.Set; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 31 | * Implementation of OpenFlow agent. |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 32 | */ |
| 33 | public final class DefaultOFAgent implements OFAgent { |
| 34 | |
| 35 | private final NetworkId networkId; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 36 | private final Set<OFController> controllers; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 37 | private final State state; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 38 | |
| 39 | private DefaultOFAgent(NetworkId networkId, |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 40 | Set<OFController> controllers, |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 41 | State state) { |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 42 | this.networkId = networkId; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 43 | this.controllers = controllers; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 44 | this.state = state; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public NetworkId networkId() { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 49 | return networkId; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Set<OFController> controllers() { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 54 | return controllers; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 58 | public State state() { |
| 59 | return state; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 63 | public int hashCode() { |
| 64 | return Objects.hash(networkId); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 67 | @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 Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 82 | @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 Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 91 | /** |
| 92 | * Returns new builder instance. |
| 93 | * |
| 94 | * @return default ofagent builder |
| 95 | */ |
| 96 | public static Builder builder() { |
| 97 | return new Builder(); |
| 98 | } |
| 99 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 100 | public static final class Builder implements OFAgent.Builder { |
| 101 | |
| 102 | private NetworkId networkId; |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 103 | private Set<OFController> controllers = Sets.newHashSet(); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 104 | private State state; |
| 105 | |
| 106 | private Builder() { |
| 107 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 108 | |
| 109 | @Override |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 110 | 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 Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 114 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 115 | return new DefaultOFAgent(networkId, controllers, state); |
| 116 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 117 | |
| 118 | @Override |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 119 | 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 Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 127 | public Builder networkId(NetworkId networkId) { |
| 128 | this.networkId = networkId; |
| 129 | return this; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 130 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 131 | |
| 132 | @Override |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 133 | public Builder controllers(Set<OFController> controllers) { |
| 134 | this.controllers = controllers; |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | @Override |
Hyunsun Moon | 53381e8 | 2017-03-28 19:58:28 +0900 | [diff] [blame] | 139 | 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 Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 151 | public Builder state(State state) { |
| 152 | this.state = state; |
| 153 | return this; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 156 | } |