Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.incubator.net.virtual.impl; |
| 18 | |
| 19 | import com.google.common.collect.Iterators; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 20 | import org.onosproject.incubator.net.virtual.NetworkId; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 21 | import org.onosproject.incubator.net.virtual.VirtualNetworkIntent; |
| 22 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
| 24 | import org.onosproject.incubator.net.virtual.VirtualPort; |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 25 | import org.onosproject.incubator.net.virtual.VnetService; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 26 | import org.onosproject.incubator.net.virtual.event.AbstractVirtualListenerManager; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Port; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.intent.Intent; |
| 32 | import org.onosproject.net.intent.IntentData; |
| 33 | import org.onosproject.net.intent.IntentEvent; |
| 34 | import org.onosproject.net.intent.IntentListener; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 35 | import org.onosproject.net.intent.WorkPartitionService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 36 | import org.onosproject.net.intent.IntentService; |
| 37 | import org.onosproject.net.intent.IntentState; |
| 38 | import org.onosproject.net.intent.Key; |
| 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
| 41 | |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.List; |
| 44 | import java.util.Optional; |
| 45 | |
| 46 | import static com.google.common.base.Preconditions.*; |
| 47 | |
| 48 | /** |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 49 | * Intent service implementation built on the virtual network service. |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 50 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 51 | public class VirtualNetworkIntentManager |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 52 | extends AbstractVirtualListenerManager<IntentEvent, IntentListener> |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 53 | implements IntentService, VnetService { |
| 54 | |
| 55 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 56 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 57 | private static final String NETWORK_ID_NULL = "Network ID cannot be null"; |
| 58 | private static final String DEVICE_NULL = "Device cannot be null"; |
| 59 | private static final String INTENT_NULL = "Intent cannot be null"; |
| 60 | private static final String KEY_NULL = "Key cannot be null"; |
| 61 | private static final String APP_ID_NULL = "Intent app identifier cannot be null"; |
| 62 | private static final String INTENT_KEY_NULL = "Intent key cannot be null"; |
| 63 | private static final String CP_NULL = "Connect Point cannot be null"; |
| 64 | |
| 65 | protected IntentService intentService; |
| 66 | protected VirtualNetworkStore store; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 67 | protected WorkPartitionService partitionService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 68 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 69 | /** |
| 70 | * Creates a new VirtualNetworkIntentService object. |
| 71 | * |
| 72 | * @param virtualNetworkManager virtual network manager service |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 73 | * @param networkId a virtual network identifier |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 74 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 75 | public VirtualNetworkIntentManager(VirtualNetworkService virtualNetworkManager, |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 76 | NetworkId networkId) { |
| 77 | |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 78 | super(virtualNetworkManager, networkId, IntentEvent.class); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 79 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 80 | this.store = serviceDirectory.get(VirtualNetworkStore.class); |
| 81 | this.intentService = serviceDirectory.get(IntentService.class); |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 82 | this.partitionService = serviceDirectory.get(WorkPartitionService.class); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void submit(Intent intent) { |
| 87 | checkNotNull(intent, INTENT_NULL); |
| 88 | checkState(intent instanceof VirtualNetworkIntent, "Only VirtualNetworkIntent is supported."); |
| 89 | checkArgument(validateIntent((VirtualNetworkIntent) intent), "Invalid Intent"); |
| 90 | |
| 91 | intentService.submit(intent); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns true if the virtual network intent is valid. |
| 96 | * |
| 97 | * @param intent virtual network intent |
| 98 | * @return true if intent is valid |
| 99 | */ |
| 100 | private boolean validateIntent(VirtualNetworkIntent intent) { |
| 101 | checkNotNull(intent, INTENT_NULL); |
| 102 | checkNotNull(intent.networkId(), NETWORK_ID_NULL); |
| 103 | checkNotNull(intent.appId(), APP_ID_NULL); |
| 104 | checkNotNull(intent.key(), INTENT_KEY_NULL); |
| 105 | ConnectPoint ingressPoint = intent.ingressPoint(); |
| 106 | ConnectPoint egressPoint = intent.egressPoint(); |
| 107 | |
| 108 | return (validateConnectPoint(ingressPoint) && validateConnectPoint(egressPoint)); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns true if the connect point is valid. |
| 113 | * |
| 114 | * @param connectPoint connect point |
| 115 | * @return true if connect point is valid |
| 116 | */ |
| 117 | private boolean validateConnectPoint(ConnectPoint connectPoint) { |
| 118 | checkNotNull(connectPoint, CP_NULL); |
| 119 | Port port = getPort(connectPoint.deviceId(), connectPoint.port()); |
| 120 | return port == null ? false : true; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns the virtual port for the given device identifier and port number. |
| 125 | * |
| 126 | * @param deviceId virtual device identifier |
| 127 | * @param portNumber virtual port number |
| 128 | * @return virtual port |
| 129 | */ |
| 130 | private Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 131 | checkNotNull(deviceId, DEVICE_NULL); |
| 132 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 133 | Optional<VirtualPort> foundPort = manager.getVirtualPorts(this.networkId(), deviceId) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 134 | .stream() |
| 135 | .filter(port -> port.number().equals(portNumber)) |
| 136 | .findFirst(); |
| 137 | if (foundPort.isPresent()) { |
| 138 | return foundPort.get(); |
| 139 | } |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void withdraw(Intent intent) { |
| 145 | checkNotNull(intent, INTENT_NULL); |
| 146 | // Withdraws the physical intents created due to the virtual intents. |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 147 | store.getTunnelIds(intent).forEach(tunnelId -> { |
| 148 | Key intentKey = Key.of(tunnelId.id(), intent.appId()); |
| 149 | Intent physicalIntent = intentService.getIntent(intentKey); |
| 150 | checkNotNull(physicalIntent, INTENT_NULL); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 151 | |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 152 | // Withdraw the physical intent(s) |
| 153 | log.debug("Withdrawing pt-pt intent: " + physicalIntent); |
| 154 | intentService.withdraw(physicalIntent); |
| 155 | }); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 156 | // Now withdraw the virtual intent |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 157 | log.debug("Withdrawing virtual intent: " + intent); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 158 | intentService.withdraw(intent); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void purge(Intent intent) { |
| 163 | checkNotNull(intent, INTENT_NULL); |
| 164 | // Purges the physical intents created for each tunnelId. |
| 165 | store.getTunnelIds(intent) |
| 166 | .forEach(tunnelId -> { |
| 167 | Key intentKey = Key.of(tunnelId.id(), intent.appId()); |
| 168 | Intent physicalIntent = intentService.getIntent(intentKey); |
| 169 | checkNotNull(physicalIntent, INTENT_NULL); |
| 170 | |
| 171 | // Purge the physical intent(s) |
| 172 | intentService.purge(physicalIntent); |
| 173 | store.removeTunnelId(intent, tunnelId); |
| 174 | }); |
| 175 | // Now purge the virtual intent |
| 176 | intentService.purge(intent); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public Intent getIntent(Key key) { |
| 181 | checkNotNull(key, KEY_NULL); |
| 182 | return store.getIntent(key); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public Iterable<Intent> getIntents() { |
| 187 | return store.getIntents(); |
| 188 | } |
| 189 | |
| 190 | @Override |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 191 | public void addPending(IntentData intentData) { |
| 192 | intentService.addPending(intentData); |
| 193 | } |
| 194 | |
| 195 | @Override |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 196 | public Iterable<IntentData> getIntentData() { |
| 197 | return store.getIntentData(); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public long getIntentCount() { |
| 202 | return Iterators.size(getIntents().iterator()); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public IntentState getIntentState(Key intentKey) { |
| 207 | checkNotNull(intentKey, KEY_NULL); |
Yuta HIGUCHI | e2eeae8 | 2016-07-28 13:56:31 -0700 | [diff] [blame] | 208 | return Optional.ofNullable(store.getIntentData(intentKey)) |
| 209 | .map(IntentData::state) |
| 210 | .orElse(null); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public List<Intent> getInstallableIntents(Key intentKey) { |
| 215 | List<Intent> intents = new ArrayList<>(); |
| 216 | getIntentData().forEach(intentData -> { |
| 217 | if (intentData.intent().key().equals(intentKey)) { |
| 218 | intents.addAll(intentData.installables()); |
| 219 | } |
| 220 | }); |
| 221 | return intents; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public boolean isLocal(Key intentKey) { |
| 226 | checkNotNull(intentKey, INTENT_KEY_NULL); |
| 227 | Intent intent = getIntent(intentKey); |
| 228 | checkNotNull(intent, INTENT_NULL); |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 229 | return partitionService.isMine(intentKey, Key::hash); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public Iterable<Intent> getPending() { |
| 234 | return null; |
| 235 | } |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 236 | } |