blob: cac1e0307c13cffba54c5bd7f6789de340160ba9 [file] [log] [blame]
Hyunsun Moon90163ba2016-10-12 13:35:14 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
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 Moon90163ba2016-10-12 13:35:14 -070020import org.onosproject.incubator.net.virtual.NetworkId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070021import org.onosproject.ofagent.api.OFAgent;
22import org.onosproject.ofagent.api.OFController;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070023
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090024import java.util.Objects;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070025import java.util.Set;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090026
27import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070028
29/**
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090030 * Implementation of OpenFlow agent.
Hyunsun Moon90163ba2016-10-12 13:35:14 -070031 */
32public final class DefaultOFAgent implements OFAgent {
33
34 private final NetworkId networkId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070035 private final Set<OFController> controllers;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090036 private final State state;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070037
38 private DefaultOFAgent(NetworkId networkId,
Hyunsun Moon90163ba2016-10-12 13:35:14 -070039 Set<OFController> controllers,
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090040 State state) {
Hyunsun Moon90163ba2016-10-12 13:35:14 -070041 this.networkId = networkId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070042 this.controllers = controllers;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090043 this.state = state;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070044 }
45
46 @Override
47 public NetworkId networkId() {
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090048 return networkId;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070049 }
50
51 @Override
52 public Set<OFController> controllers() {
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090053 return controllers;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070054 }
55
56 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090057 public State state() {
58 return state;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070059 }
60
61 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090062 public int hashCode() {
63 return Objects.hash(networkId);
Hyunsun Moon90163ba2016-10-12 13:35:14 -070064 }
65
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090066 @Override
67 public boolean equals(Object obj) {
68 if (this == obj) {
69 return true;
70 }
71
72 if (obj instanceof DefaultOFAgent) {
73 DefaultOFAgent that = (DefaultOFAgent) obj;
74 if (Objects.equals(networkId, that.networkId)) {
75 return true;
76 }
77 }
78 return false;
Hyunsun Moon90163ba2016-10-12 13:35:14 -070079 }
80
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090081 @Override
82 public String toString() {
83 return MoreObjects.toStringHelper(this)
84 .add("networkId", this.networkId)
85 .add("controllers", this.controllers)
86 .add("state", this.state)
87 .toString();
Hyunsun Moon90163ba2016-10-12 13:35:14 -070088 }
89
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090090 /**
91 * Returns new builder instance.
92 *
93 * @return default ofagent builder
94 */
95 public static Builder builder() {
96 return new Builder();
97 }
98
99 /**
100 * Returns new builder instance from the existing agent.
101 *
102 * @param ofAgent the existing agent
103 * @return default ofagent builder
104 */
105 public static Builder builder(OFAgent ofAgent) {
106 return new Builder()
107 .networkId(ofAgent.networkId())
108 .controllers(ofAgent.controllers())
109 .state(ofAgent.state());
110 }
111
112 public static final class Builder implements OFAgent.Builder {
113
114 private NetworkId networkId;
115 private Set<OFController> controllers;
116 private State state;
117
118 private Builder() {
119 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700120
121 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900122 public OFAgent build() {
123 checkNotNull(networkId, "Network ID cannot be null");
124 checkNotNull(state, "State cannot be null");
125 controllers = controllers == null ? ImmutableSet.of() : controllers;
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700126
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900127 return new DefaultOFAgent(networkId, controllers, state);
128 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700129
130 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900131 public Builder networkId(NetworkId networkId) {
132 this.networkId = networkId;
133 return this;
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700134 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700135
136 @Override
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900137 public Builder controllers(Set<OFController> controllers) {
138 this.controllers = controllers;
139 return this;
140 }
141
142 @Override
143 public Builder state(State state) {
144 this.state = state;
145 return this;
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700146 }
147 }
Hyunsun Moon90163ba2016-10-12 13:35:14 -0700148}