Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | */ |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 16 | package org.onosproject.vpls; |
| 17 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Lists; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.net.ConnectPoint; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 24 | import org.onosproject.net.EncapsulationType; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 25 | import org.onosproject.net.FilteredConnectPoint; |
| 26 | import org.onosproject.net.Host; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 28 | import org.onosproject.net.flow.TrafficSelector; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 29 | import org.onosproject.net.intent.ConnectivityIntent; |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.Constraint; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 31 | import org.onosproject.net.intent.Intent; |
| 32 | import org.onosproject.net.intent.IntentService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 33 | import org.onosproject.net.intent.IntentState; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.Key; |
| 35 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 36 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 37 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.constraint.PartialFailureConstraint; |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 39 | import org.onosproject.intentsync.IntentSynchronizationService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 43 | import java.util.ArrayList; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 44 | import java.util.Collection; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 45 | import java.util.List; |
| 46 | import java.util.Set; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 47 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 48 | import static org.onosproject.net.EncapsulationType.*; |
| 49 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Synchronizes intents between the in-memory intent store and the |
| 52 | * IntentService. |
| 53 | */ |
| 54 | public class IntentInstaller { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 55 | private static final String SUBMIT = |
| 56 | "Submitting intents to the Intent Synchronizer"; |
| 57 | private static final String WITHDRAW = |
| 58 | "Withdrawing intents to the Intent Synchronizer"; |
| 59 | private static final String SP2MP = |
| 60 | "Building sp2mp intent from {}"; |
| 61 | private static final String MP2SP = |
| 62 | "Building mp2sp intent to {}"; |
| 63 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 64 | private static final Logger log = LoggerFactory.getLogger( |
| 65 | IntentInstaller.class); |
| 66 | |
| 67 | private static final int PRIORITY_OFFSET = 1000; |
| 68 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 69 | private static final Set<IntentState> WITHDRAWN_INTENT_STATES = |
| 70 | ImmutableSet.of(IntentState.WITHDRAWN, |
| 71 | IntentState.WITHDRAW_REQ, |
| 72 | IntentState.WITHDRAWING); |
| 73 | |
| 74 | static final String PREFIX_BROADCAST = "brc"; |
| 75 | static final String PREFIX_UNICAST = "uni"; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 76 | static final String SEPARATOR = "-"; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 77 | |
| 78 | private final ApplicationId appId; |
| 79 | private final IntentSynchronizationService intentSynchronizer; |
| 80 | private final IntentService intentService; |
| 81 | |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 82 | public static final ImmutableList<Constraint> PARTIAL_FAILURE_CONSTRAINT = |
| 83 | ImmutableList.of(new PartialFailureConstraint()); |
| 84 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Class constructor. |
| 87 | * |
| 88 | * @param appId the Application ID |
| 89 | * @param intentService the intent service |
| 90 | * @param intentSynchronizer the intent synchronizer service |
| 91 | */ |
| 92 | public IntentInstaller(ApplicationId appId, IntentService intentService, |
| 93 | IntentSynchronizationService intentSynchronizer) { |
| 94 | this.appId = appId; |
| 95 | this.intentService = intentService; |
| 96 | this.intentSynchronizer = intentSynchronizer; |
| 97 | } |
| 98 | |
| 99 | /** |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 100 | * Requests to install the intents passed as argument to the Intent Service. |
| 101 | * |
| 102 | * @param intents intents to be submitted |
| 103 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 104 | protected void submitIntents(Collection<Intent> intents) { |
| 105 | log.debug(SUBMIT); |
| 106 | intents.forEach(intentSynchronizer::submit); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 110 | * Requests to withdraw the intents passed as argument to the Intent Service. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 111 | * |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 112 | * @param intents intents to be withdraw |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 113 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 114 | protected void withdrawIntents(Collection<Intent> intents) { |
| 115 | log.debug(WITHDRAW); |
| 116 | intents.forEach(intentSynchronizer::withdraw); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns list of intents belongs to a VPLS. |
| 121 | * |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 122 | * @param name the name of the VPLS |
| 123 | * @return the list of intents belonging to a VPLS |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 124 | */ |
| 125 | protected List<Intent> getIntentsFromVpls(String name) { |
| 126 | List<Intent> intents = Lists.newArrayList(); |
| 127 | |
| 128 | intentService.getIntents().forEach(intent -> { |
| 129 | if (intent.key().toString().startsWith(name)) { |
| 130 | intents.add(intent); |
| 131 | } |
| 132 | }); |
| 133 | |
| 134 | return intents; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Builds a broadcast intent. |
| 139 | * |
| 140 | * @param key key to identify the intent |
| 141 | * @param src the source connect point |
| 142 | * @param dsts the destination connect points |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 143 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 144 | * @return the generated single-point to multi-point intent |
| 145 | */ |
| 146 | protected SinglePointToMultiPointIntent buildBrcIntent(Key key, |
| 147 | FilteredConnectPoint src, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 148 | Set<FilteredConnectPoint> dsts, |
| 149 | EncapsulationType encap) { |
| 150 | log.debug("Building broadcast intent {} for source {}", SP2MP, src); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 151 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 152 | SinglePointToMultiPointIntent.Builder intentBuilder; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 153 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 154 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 155 | .matchEthDst(MacAddress.BROADCAST) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 156 | .build(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 157 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 158 | intentBuilder = SinglePointToMultiPointIntent.builder() |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 159 | .appId(appId) |
| 160 | .key(key) |
| 161 | .selector(selector) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 162 | .filteredIngressPoint(src) |
| 163 | .filteredEgressPoints(dsts) |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 164 | .constraints(PARTIAL_FAILURE_CONSTRAINT) |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 165 | .priority(PRIORITY_OFFSET); |
| 166 | |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 167 | setEncap(intentBuilder, PARTIAL_FAILURE_CONSTRAINT, encap); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 168 | |
| 169 | return intentBuilder.build(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 173 | * Builds a unicast intent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 174 | * |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 175 | * @param key key to identify the intent |
| 176 | * @param srcs the source Connect Points |
| 177 | * @param dst the destination Connect Point |
| 178 | * @param host destination Host |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 179 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 180 | * @return the generated multi-point to single-point intent |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 181 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 182 | protected MultiPointToSinglePointIntent buildUniIntent(Key key, |
| 183 | Set<FilteredConnectPoint> srcs, |
| 184 | FilteredConnectPoint dst, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 185 | Host host, |
| 186 | EncapsulationType encap) { |
| 187 | log.debug("Building unicast intent {} for destination {}", MP2SP, dst); |
| 188 | |
| 189 | MultiPointToSinglePointIntent.Builder intentBuilder; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 190 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 191 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 192 | .matchEthDst(host.mac()) |
| 193 | .build(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 194 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 195 | intentBuilder = MultiPointToSinglePointIntent.builder() |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 196 | .appId(appId) |
| 197 | .key(key) |
| 198 | .selector(selector) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 199 | .filteredIngressPoints(srcs) |
| 200 | .filteredEgressPoint(dst) |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 201 | .constraints(PARTIAL_FAILURE_CONSTRAINT) |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 202 | .priority(PRIORITY_OFFSET); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 203 | |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 204 | setEncap(intentBuilder, PARTIAL_FAILURE_CONSTRAINT, encap); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 205 | |
| 206 | return intentBuilder.build(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 210 | * Builds an intent key either for single-point to multi-point or |
| 211 | * multi-point to single-point intents, based on a prefix that defines |
| 212 | * the type of intent, the single connect point representing the single |
| 213 | * source or destination for that intent, the name of the VPLS the intent |
| 214 | * belongs to, and the destination host MAC address the intent reaches. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 215 | * |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 216 | * @param prefix the key prefix |
| 217 | * @param cPoint the connect point identifying the source/destination |
| 218 | * @param vplsName the name of the VPLS |
| 219 | * @param hostMac the source/destination MAC address |
| 220 | * @return the key to identify the intent |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 221 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 222 | protected Key buildKey(String prefix, |
| 223 | ConnectPoint cPoint, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 224 | String vplsName, |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 225 | MacAddress hostMac) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 226 | String keyString = vplsName + |
| 227 | SEPARATOR + |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 228 | prefix + |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 229 | SEPARATOR + |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 230 | cPoint.deviceId() + |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 231 | SEPARATOR + |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 232 | cPoint.port() + |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 233 | SEPARATOR + |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 234 | hostMac; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 235 | |
| 236 | return Key.of(keyString, appId); |
| 237 | } |
| 238 | |
| 239 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 240 | * Returns true if the specified intent exists; false otherwise. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 241 | * |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 242 | * @param intentKey the intent key |
| 243 | * @return true if the intent exists; false otherwise |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 244 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 245 | protected boolean intentExists(Key intentKey) { |
| 246 | if (intentService.getIntent(intentKey) == null) { |
| 247 | return false; |
| 248 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 249 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 250 | // Intent does not exist if intent withdrawn |
| 251 | IntentState currentIntentState = intentService.getIntentState(intentKey); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 252 | return !WITHDRAWN_INTENT_STATES.contains(currentIntentState); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 253 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 257 | * Sets one or more encapsulation constraints on the intent builder given. |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 258 | * |
| 259 | * @param builder the intent builder |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 260 | * @param constraints the existing intent constraints |
| 261 | * @param encap the encapsulation type to be set |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 262 | */ |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 263 | public static void setEncap(ConnectivityIntent.Builder builder, |
| 264 | List<Constraint> constraints, |
| 265 | EncapsulationType encap) { |
| 266 | // Constraints might be an immutable list, so a new modifiable list |
| 267 | // is created |
| 268 | List<Constraint> newConstraints = new ArrayList<>(constraints); |
| 269 | |
| 270 | // Remove any encapsulation constraint if already in the list |
| 271 | constraints.stream() |
| 272 | .filter(c -> c instanceof EncapsulationConstraint) |
| 273 | .forEach(newConstraints::remove); |
| 274 | |
| 275 | // if the new encapsulation is different from NONE, a new encapsulation |
| 276 | // constraint should be added to the list |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 277 | if (!encap.equals(NONE)) { |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 278 | newConstraints.add(new EncapsulationConstraint(encap)); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 279 | } |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 280 | |
| 281 | // Submit new constraint list as immutable list |
| 282 | builder.constraints(ImmutableList.copyOf(newConstraints)); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 283 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 284 | } |