blob: 50bad959760869f2af8decb4bac54d74827ce35d [file] [log] [blame]
Satish K2eb5d842017-04-04 16:28:37 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Satish K2eb5d842017-04-04 16:28:37 +05303 *
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 */
16package org.onosproject.bandwidthmgr;
17
Satish K2eb5d842017-04-04 16:28:37 +053018import org.onlab.util.KryoNamespace;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.onosproject.bandwidthmgr.api.BandwidthMgmtStore;
Satish K2eb5d842017-04-04 16:28:37 +053020import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.LinkKey;
22import org.onosproject.net.config.NetworkConfigEvent;
23import org.onosproject.net.config.NetworkConfigListener;
24import org.onosproject.net.config.NetworkConfigService;
Satish K2eb5d842017-04-04 16:28:37 +053025import org.onosproject.pcep.api.TeLinkConfig;
26import org.onosproject.store.serializers.KryoNamespaces;
27import org.onosproject.store.service.ConsistentMap;
28import org.onosproject.store.service.Serializer;
29import org.onosproject.store.service.StorageService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070030import org.osgi.service.component.annotations.Activate;
31import org.osgi.service.component.annotations.Component;
32import org.osgi.service.component.annotations.Deactivate;
33import org.osgi.service.component.annotations.Reference;
34import org.osgi.service.component.annotations.ReferenceCardinality;
Satish K2eb5d842017-04-04 16:28:37 +053035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Ray Milkeyd84f89b2018-08-17 14:54:17 -070038import java.util.LinkedHashSet;
39import java.util.Set;
40
41import static com.google.common.base.Preconditions.checkNotNull;
42import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED;
43import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED;
44import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED;
45
Satish K2eb5d842017-04-04 16:28:37 +053046/**
47 * Manages the pool of available labels to devices, links and tunnels.
48 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049@Component(immediate = true, service = BandwidthMgmtStore.class)
Satish K2eb5d842017-04-04 16:28:37 +053050public class DistributedBandwidthMgmtStore implements BandwidthMgmtStore {
51 private static final Logger log = LoggerFactory.getLogger(BandwidthManager.class);
52
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Satish K2eb5d842017-04-04 16:28:37 +053054 protected NetworkConfigService netCfgService;
55
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Satish K2eb5d842017-04-04 16:28:37 +053057 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}