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