HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -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 | */ |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource.impl; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.util.Set; |
| 23 | import java.util.concurrent.ExecutorService; |
| 24 | |
| 25 | import org.onlab.util.Bandwidth; |
Sho SHIMIZU | 82b9d17 | 2016-02-24 16:53:59 -0800 | [diff] [blame] | 26 | import org.onosproject.mastership.MastershipService; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.config.NetworkConfigEvent; |
| 29 | import org.onosproject.net.config.NetworkConfigListener; |
| 30 | import org.onosproject.net.config.NetworkConfigService; |
Sho SHIMIZU | b6c63a3 | 2016-05-26 12:07:19 -0700 | [diff] [blame] | 31 | import org.onosproject.net.config.basics.BandwidthCapacity; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 32 | import org.onosproject.net.resource.ResourceAdminService; |
| 33 | import org.onosproject.net.resource.Resources; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import com.google.common.annotations.Beta; |
| 37 | import com.google.common.collect.ImmutableSet; |
| 38 | |
| 39 | // TODO Consider merging this with ResourceDeviceListener. |
| 40 | /** |
| 41 | * Handler for NetworkConfiguration changes. |
| 42 | */ |
| 43 | @Beta |
| 44 | final class ResourceNetworkConfigListener implements NetworkConfigListener { |
| 45 | |
| 46 | /** |
| 47 | * Config classes relevant to this listener. |
| 48 | */ |
| 49 | private static final Set<Class<?>> CONFIG_CLASSES = ImmutableSet.of(BandwidthCapacity.class); |
| 50 | |
| 51 | private final Logger log = getLogger(getClass()); |
| 52 | |
| 53 | private final ResourceAdminService adminService; |
| 54 | private final NetworkConfigService cfgService; |
Sho SHIMIZU | 82b9d17 | 2016-02-24 16:53:59 -0800 | [diff] [blame] | 55 | private final MastershipService mastershipService; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 56 | private final ExecutorService executor; |
| 57 | |
| 58 | /** |
| 59 | * Creates an instance of listener. |
| 60 | * |
| 61 | * @param adminService {@link ResourceAdminService} |
| 62 | * @param cfgService {@link NetworkConfigService} |
Ray Milkey | d7909ca | 2016-03-04 08:46:13 -0800 | [diff] [blame] | 63 | * @param mastershipService {@link MastershipService} |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 64 | * @param executor Executor to use. |
| 65 | */ |
| 66 | ResourceNetworkConfigListener(ResourceAdminService adminService, NetworkConfigService cfgService, |
Sho SHIMIZU | 82b9d17 | 2016-02-24 16:53:59 -0800 | [diff] [blame] | 67 | MastershipService mastershipService, ExecutorService executor) { |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 68 | this.adminService = checkNotNull(adminService); |
| 69 | this.cfgService = checkNotNull(cfgService); |
Sho SHIMIZU | 82b9d17 | 2016-02-24 16:53:59 -0800 | [diff] [blame] | 70 | this.mastershipService = checkNotNull(mastershipService); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 71 | this.executor = checkNotNull(executor); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean isRelevant(NetworkConfigEvent event) { |
HIGUCHI Yuta | 8652389 | 2016-02-18 16:33:11 -0800 | [diff] [blame] | 76 | switch (event.type()) { |
| 77 | case CONFIG_ADDED: |
| 78 | case CONFIG_REMOVED: |
| 79 | case CONFIG_UPDATED: |
| 80 | return CONFIG_CLASSES.contains(event.configClass()); |
| 81 | |
| 82 | case CONFIG_REGISTERED: |
| 83 | case CONFIG_UNREGISTERED: |
| 84 | default: |
| 85 | return false; |
| 86 | } |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void event(NetworkConfigEvent event) { |
| 91 | if (event.configClass() == BandwidthCapacity.class) { |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 92 | executor.execute(() -> { |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 93 | try { |
| 94 | handleBandwidthCapacity(event); |
| 95 | } catch (Exception e) { |
| 96 | log.error("Exception handling BandwidthCapacity", e); |
| 97 | } |
| 98 | }); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private void handleBandwidthCapacity(NetworkConfigEvent event) { |
| 103 | checkArgument(event.configClass() == BandwidthCapacity.class); |
| 104 | |
| 105 | ConnectPoint cp = (ConnectPoint) event.subject(); |
Sho SHIMIZU | 82b9d17 | 2016-02-24 16:53:59 -0800 | [diff] [blame] | 106 | if (!mastershipService.isLocalMaster(cp.deviceId())) { |
| 107 | return; |
| 108 | } |
| 109 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 110 | BandwidthCapacity bwCapacity = cfgService.getConfig(cp, BandwidthCapacity.class); |
| 111 | |
| 112 | switch (event.type()) { |
| 113 | case CONFIG_ADDED: |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 114 | if (!adminService.register(Resources.continuous(cp.deviceId(), |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 115 | cp.port(), Bandwidth.class) |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame] | 116 | .resource(bwCapacity.capacity().bps()))) { |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 117 | log.info("Failed to register Bandwidth for {}, attempting update", cp); |
| 118 | |
| 119 | // Bandwidth based on port speed, was probably already registered. |
| 120 | // need to update to the valued based on configuration |
| 121 | |
| 122 | if (!updateRegistration(cp, bwCapacity)) { |
| 123 | log.warn("Failed to update Bandwidth for {}", cp); |
| 124 | } |
| 125 | } |
| 126 | break; |
| 127 | |
| 128 | case CONFIG_UPDATED: |
| 129 | if (!updateRegistration(cp, bwCapacity)) { |
| 130 | log.warn("Failed to update Bandwidth for {}", cp); |
| 131 | } |
| 132 | break; |
| 133 | |
| 134 | case CONFIG_REMOVED: |
| 135 | // FIXME Following should be an update to the value based on port speed |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 136 | if (!adminService.unregister(Resources.continuous(cp.deviceId(), |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 137 | cp.port(), |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 138 | Bandwidth.class).id())) { |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 139 | log.warn("Failed to unregister Bandwidth for {}", cp); |
| 140 | } |
| 141 | break; |
| 142 | |
| 143 | case CONFIG_REGISTERED: |
| 144 | case CONFIG_UNREGISTERED: |
| 145 | // no-op |
| 146 | break; |
| 147 | |
| 148 | default: |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private boolean updateRegistration(ConnectPoint cp, BandwidthCapacity bwCapacity) { |
| 154 | // FIXME workaround until replace/update semantics become available |
| 155 | // this potentially blows up existing registration |
| 156 | // or end up as no-op |
| 157 | // |
| 158 | // Current code end up in situation like below: |
| 159 | // PortNumber: 2 |
| 160 | // MplsLabel: [[16‥240)] |
| 161 | // VlanId: [[0‥4095)] |
| 162 | // Bandwidth: 2000000.000000 |
| 163 | // Bandwidth: 20000000.000000 |
| 164 | // |
| 165 | // but both unregisterResources(..) and registerResources(..) |
| 166 | // returns true (success) |
| 167 | |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 168 | if (!adminService.unregister( |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 169 | Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class).id())) { |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 170 | log.warn("unregisterResources for {} failed", cp); |
| 171 | } |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 172 | return adminService.register(Resources.continuous(cp.deviceId(), |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 173 | cp.port(), |
| 174 | Bandwidth.class).resource(bwCapacity.capacity().bps())); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | } |