samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [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.net.behaviour; |
| 17 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
| 19 | import com.google.common.collect.Lists; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.SparseAnnotations; |
| 22 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 23 | import java.util.List; |
| 24 | import java.util.Optional; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * The default implementation of bridge. |
| 31 | */ |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 32 | public final class DefaultBridgeDescription implements BridgeDescription { |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 33 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 34 | private final String name; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 35 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 36 | /* Optional OpenFlow configurations */ |
| 37 | private final List<ControllerInfo> controllers; |
| 38 | private final boolean enableLocalController; |
| 39 | private final Optional<FailMode> failMode; |
| 40 | private final Optional<String> datapathId; |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 41 | private final Optional<String> datapathType; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 42 | private final Optional<Boolean> disableInBand; |
| 43 | |
| 44 | /* Adds more configurations */ |
| 45 | |
| 46 | private DefaultBridgeDescription(String name, |
| 47 | List<ControllerInfo> controllers, |
| 48 | boolean enableLocalController, |
| 49 | Optional<FailMode> failMode, |
| 50 | Optional<String> datapathId, |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 51 | Optional<String> datapathType, |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 52 | Optional<Boolean> disableInBand) { |
| 53 | this.name = checkNotNull(name); |
| 54 | this.controllers = controllers; |
| 55 | this.enableLocalController = enableLocalController; |
| 56 | this.failMode = failMode; |
| 57 | this.datapathId = datapathId; |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 58 | this.datapathType = datapathType; |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 59 | this.disableInBand = disableInBand; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 63 | public SparseAnnotations annotations() { |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String name() { |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 69 | return name; |
| 70 | } |
| 71 | |
| 72 | @Override |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 73 | public List<ControllerInfo> controllers() { |
| 74 | return controllers; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 78 | public boolean enableLocalController() { |
| 79 | return enableLocalController; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 83 | public Optional<FailMode> failMode() { |
| 84 | return failMode; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 88 | public Optional<String> datapathId() { |
| 89 | return datapathId; |
| 90 | } |
| 91 | |
| 92 | @Override |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 93 | public Optional<String> datapathType() { |
| 94 | return datapathType; |
| 95 | } |
| 96 | |
| 97 | @Override |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 98 | public Optional<DeviceId> deviceId() { |
| 99 | if (datapathId.isPresent()) { |
| 100 | return Optional.of(DeviceId.deviceId("of:" + datapathId.get())); |
| 101 | } else { |
| 102 | return Optional.empty(); |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 103 | } |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 107 | public Optional<Boolean> disableInBand() { |
| 108 | return disableInBand; |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 109 | } |
| 110 | |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Creates and returns a new builder instance. |
| 113 | * |
| 114 | * @return new builder |
| 115 | */ |
| 116 | public static BridgeDescription.Builder builder() { |
| 117 | return new Builder(); |
| 118 | } |
| 119 | |
| 120 | public static final class Builder implements BridgeDescription.Builder { |
| 121 | |
| 122 | private String name; |
| 123 | private List<ControllerInfo> controllers = Lists.newArrayList(); |
| 124 | private boolean enableLocalController = false; |
| 125 | private Optional<FailMode> failMode = Optional.empty(); |
| 126 | private Optional<String> datapathId = Optional.empty(); |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 127 | private Optional<String> datapathType = Optional.empty(); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 128 | private Optional<Boolean> disableInBand = Optional.empty(); |
| 129 | |
| 130 | private Builder() { |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public BridgeDescription build() { |
| 135 | return new DefaultBridgeDescription(name, controllers, |
| 136 | enableLocalController, |
| 137 | failMode, |
| 138 | datapathId, |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 139 | datapathType, |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 140 | disableInBand); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Builder name(String name) { |
| 145 | checkArgument(!Strings.isNullOrEmpty(name)); |
| 146 | this.name = name; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Builder controllers(List<ControllerInfo> controllers) { |
| 152 | if (controllers != null) { |
| 153 | this.controllers = Lists.newArrayList(controllers); |
| 154 | } |
| 155 | return this; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Builder enableLocalController() { |
| 160 | this.enableLocalController = true; |
| 161 | return this; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Builder failMode(FailMode failMode) { |
| 166 | this.failMode = Optional.ofNullable(failMode); |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public Builder datapathId(String datapathId) { |
| 172 | this.datapathId = Optional.ofNullable(datapathId); |
| 173 | return this; |
| 174 | } |
| 175 | |
| 176 | @Override |
jaegonkim | 80bee53 | 2017-05-15 15:16:38 +0900 | [diff] [blame] | 177 | public Builder datapathType(String datapathType) { |
| 178 | this.datapathType = Optional.ofNullable(datapathType); |
| 179 | return this; |
| 180 | } |
| 181 | |
| 182 | @Override |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 183 | public Builder disableInBand() { |
| 184 | this.disableInBand = Optional.of(Boolean.TRUE); |
| 185 | return this; |
| 186 | } |
| 187 | } |
samuel | 8d6b0a9 | 2015-07-11 13:22:57 +0800 | [diff] [blame] | 188 | } |