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