Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [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.bandwidthmgr; |
| 17 | |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 18 | import org.onlab.util.KryoNamespace; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.onosproject.bandwidthmgr.api.BandwidthMgmtStore; |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.LinkKey; |
| 22 | import org.onosproject.net.config.NetworkConfigEvent; |
| 23 | import org.onosproject.net.config.NetworkConfigListener; |
| 24 | import org.onosproject.net.config.NetworkConfigService; |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 25 | import org.onosproject.pcep.api.TeLinkConfig; |
| 26 | import org.onosproject.store.serializers.KryoNamespaces; |
| 27 | import org.onosproject.store.service.ConsistentMap; |
| 28 | import org.onosproject.store.service.Serializer; |
| 29 | import org.onosproject.store.service.StorageService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 38 | import java.util.LinkedHashSet; |
| 39 | import java.util.Set; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
| 42 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; |
| 43 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED; |
| 44 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
| 45 | |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 46 | /** |
| 47 | * Manages the pool of available labels to devices, links and tunnels. |
| 48 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 49 | @Component(immediate = true, service = BandwidthMgmtStore.class) |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 50 | public class DistributedBandwidthMgmtStore implements BandwidthMgmtStore { |
| 51 | private static final Logger log = LoggerFactory.getLogger(BandwidthManager.class); |
| 52 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 54 | protected NetworkConfigService netCfgService; |
| 55 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 57 | protected StorageService storageService; |
| 58 | |
| 59 | private InternalConfigListener cfgListener = new InternalConfigListener(); |
| 60 | |
| 61 | private ConsistentMap<LinkKey, Double> teCost; |
| 62 | // Locally maintain unreserved bandwidth of each link. |
| 63 | private ConsistentMap<LinkKey, Set<Double>> unResvBw; |
| 64 | |
| 65 | // Mapping tunnel with link key with local reserved bandwidth |
| 66 | private ConsistentMap<LinkKey, Double> localReservedBw; |
| 67 | |
| 68 | private static final Serializer SERIALIZER = Serializer |
| 69 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 70 | .register(KryoNamespaces.API) |
| 71 | .register(LinkKey.class) |
| 72 | .register(ConnectPoint.class) |
| 73 | .build()); |
| 74 | |
| 75 | @Activate |
| 76 | protected void activate() { |
| 77 | netCfgService.addListener(cfgListener); |
| 78 | |
| 79 | localReservedBw = storageService.<LinkKey, Double>consistentMapBuilder() |
| 80 | .withName("local-reserved-bandwith") |
| 81 | .withSerializer(SERIALIZER) |
| 82 | .build(); |
| 83 | |
| 84 | unResvBw = storageService.<LinkKey, Set<Double>>consistentMapBuilder() |
| 85 | .withName("onos-unreserved-bandwidth") |
| 86 | .withSerializer(SERIALIZER) |
| 87 | .build(); |
| 88 | |
| 89 | teCost = storageService.<LinkKey, Double>consistentMapBuilder() |
| 90 | .withName("onos-tecost") |
| 91 | .withSerializer(SERIALIZER) |
| 92 | .build(); |
| 93 | log.info("Started"); |
| 94 | } |
| 95 | |
| 96 | @Deactivate |
| 97 | protected void deactivate() { |
| 98 | netCfgService.removeListener(cfgListener); |
| 99 | log.info("Stopped"); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Double getTeCost(LinkKey linkKey) { |
| 104 | if (teCost.get(linkKey) != null) { |
| 105 | return teCost.get(linkKey).value(); |
| 106 | } |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean allocLocalReservedBw(LinkKey linkkey, Double bandwidth) { |
| 112 | Double allocatedBw = null; |
| 113 | |
| 114 | if (localReservedBw.get(linkkey) != null) { |
| 115 | allocatedBw = localReservedBw.get(linkkey).value(); |
| 116 | } |
| 117 | if (allocatedBw != null) { |
| 118 | localReservedBw.put(linkkey, (allocatedBw + bandwidth)); |
| 119 | } else { |
| 120 | localReservedBw.put(linkkey, bandwidth); |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public boolean releaseLocalReservedBw(LinkKey linkkey, Double bandwidth) { |
| 128 | |
| 129 | Double allocatedBw = null; |
| 130 | if (localReservedBw.get(linkkey) != null) { |
| 131 | allocatedBw = localReservedBw.get(linkkey).value(); |
| 132 | } |
| 133 | |
| 134 | if (allocatedBw == null || allocatedBw < bandwidth) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | Double releasedBw = allocatedBw - bandwidth; |
| 139 | if (releasedBw == 0.0) { |
| 140 | localReservedBw.remove(linkkey); |
| 141 | } else { |
| 142 | localReservedBw.put(linkkey, releasedBw); |
| 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Double getAllocatedLocalReservedBw(LinkKey linkkey) { |
| 149 | return localReservedBw.get(linkkey) != null ? localReservedBw.get(linkkey).value() : null; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public boolean addUnreservedBw(LinkKey linkkey, Set<Double> bandwidth) { |
| 154 | unResvBw.put(linkkey, bandwidth); |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public boolean removeUnreservedBw(LinkKey linkkey) { |
| 160 | unResvBw.remove(linkkey); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Set<Double> getUnreservedBw(LinkKey linkkey) { |
| 166 | checkNotNull(linkkey); |
| 167 | return unResvBw.get(linkkey) != null ? unResvBw.get(linkkey).value() : null; |
| 168 | } |
| 169 | |
| 170 | private class InternalConfigListener implements NetworkConfigListener { |
| 171 | |
| 172 | @Override |
| 173 | public void event(NetworkConfigEvent event) { |
| 174 | |
| 175 | if (event.configClass().equals(TeLinkConfig.class)) { |
| 176 | if ((event.type() != CONFIG_ADDED) && (event.type() != CONFIG_UPDATED) |
| 177 | && (event.type() != CONFIG_REMOVED)) { |
| 178 | return; |
| 179 | } |
| 180 | LinkKey linkKey = (LinkKey) event.subject(); |
| 181 | switch (event.type()) { |
| 182 | case CONFIG_ADDED: |
| 183 | case CONFIG_UPDATED: |
| 184 | |
| 185 | TeLinkConfig cfg = netCfgService.getConfig(linkKey, TeLinkConfig.class); |
| 186 | if (cfg == null) { |
| 187 | log.error("Unable to get the configuration of the link."); |
| 188 | return; |
| 189 | } |
| 190 | Set<Double> unresvBw = new LinkedHashSet<>(); |
| 191 | unresvBw.add(cfg.unResvBandwidth()); |
| 192 | addUnreservedBw(linkKey, unresvBw); |
| 193 | |
| 194 | if (cfg.teCost() != 0) { |
| 195 | teCost.put(linkKey, (double) cfg.teCost()); |
| 196 | } |
| 197 | |
| 198 | break; |
| 199 | case CONFIG_REMOVED: |
| 200 | removeUnreservedBw(linkKey); |
| 201 | localReservedBw.remove(linkKey); |
| 202 | teCost.remove(linkKey); |
| 203 | |
| 204 | break; |
| 205 | default: |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | } |