Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.store.routing.impl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 27 | import org.onlab.packet.IpPrefix; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 28 | import org.onlab.util.Tools; |
| 29 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 30 | import org.onosproject.incubator.net.routing.InternalRouteEvent; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 31 | import org.onosproject.incubator.net.routing.NextHopData; |
| 32 | import org.onosproject.incubator.net.routing.Route; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 33 | import org.onosproject.incubator.net.routing.RouteSet; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 34 | import org.onosproject.incubator.net.routing.RouteStore; |
| 35 | import org.onosproject.incubator.net.routing.RouteStoreDelegate; |
| 36 | import org.onosproject.incubator.net.routing.RouteTableId; |
| 37 | import org.onosproject.store.AbstractStore; |
| 38 | import org.onosproject.store.service.StorageService; |
| 39 | import org.osgi.service.component.ComponentContext; |
| 40 | import org.osgi.service.component.annotations.Activate; |
| 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
| 44 | import java.util.Collection; |
| 45 | import java.util.Dictionary; |
| 46 | import java.util.Map; |
| 47 | import java.util.Set; |
| 48 | |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 49 | import static com.google.common.base.Preconditions.checkState; |
| 50 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 51 | /** |
| 52 | * An implementation of RouteStore that is backed by either LocalRouteStore or |
| 53 | * DistributedRouteStore according to configuration. |
| 54 | */ |
| 55 | @Service |
| 56 | @Component |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 57 | public class RouteStoreImpl extends AbstractStore<InternalRouteEvent, RouteStoreDelegate> |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 58 | implements RouteStore { |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected ComponentConfigService componentConfigService; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | public StorageService storageService; |
| 65 | |
| 66 | @Property(name = "distributed", boolValue = false, |
| 67 | label = "Enable distributed route store") |
| 68 | private boolean distributed; |
| 69 | |
| 70 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 71 | private RouteStore currentRouteStore; |
| 72 | |
| 73 | @Activate |
| 74 | public void activate(ComponentContext context) { |
| 75 | componentConfigService.registerProperties(getClass()); |
| 76 | modified(context); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | public void deactivate() { |
| 81 | if (distributed) { |
| 82 | ((DistributedRouteStore) currentRouteStore).deactivate(); |
| 83 | } else { |
| 84 | ((LocalRouteStore) currentRouteStore).deactivate(); |
| 85 | } |
| 86 | componentConfigService.unregisterProperties(getClass(), false); |
| 87 | } |
| 88 | |
| 89 | @Modified |
| 90 | public void modified(ComponentContext context) { |
| 91 | Dictionary<?, ?> properties = context.getProperties(); |
| 92 | if (properties == null) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | String strDistributed = Tools.get(properties, "distributed"); |
| 97 | boolean expectDistributed = Boolean.parseBoolean(strDistributed); |
| 98 | |
| 99 | // Start route store during first start or config change |
| 100 | // NOTE: new route store will be empty |
| 101 | if (currentRouteStore == null || expectDistributed != distributed) { |
| 102 | if (expectDistributed) { |
| 103 | if (currentRouteStore != null) { |
| 104 | ((LocalRouteStore) currentRouteStore).deactivate(); |
| 105 | } |
| 106 | currentRouteStore = new DistributedRouteStore(storageService); |
| 107 | ((DistributedRouteStore) currentRouteStore).activate(); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 108 | ((DistributedRouteStore) currentRouteStore).setDelegate(delegate); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 109 | } else { |
| 110 | if (currentRouteStore != null) { |
| 111 | ((DistributedRouteStore) currentRouteStore).deactivate(); |
| 112 | } |
| 113 | currentRouteStore = new LocalRouteStore(); |
| 114 | ((LocalRouteStore) currentRouteStore).activate(); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 115 | ((LocalRouteStore) currentRouteStore).setDelegate(delegate); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | this.distributed = expectDistributed; |
| 119 | log.info("Switched to {} route store", distributed ? "distributed" : "local"); |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | @Override |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 125 | public void setDelegate(RouteStoreDelegate delegate) { |
| 126 | checkState(this.delegate == null || this.delegate == delegate, |
| 127 | "Store delegate already set"); |
| 128 | this.delegate = delegate; |
| 129 | |
| 130 | // Set the delegate of underlying route store implementation |
| 131 | currentRouteStore.setDelegate(delegate); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void unsetDelegate(RouteStoreDelegate delegate) { |
| 136 | if (this.delegate == delegate) { |
| 137 | this.delegate = null; |
| 138 | } |
| 139 | |
| 140 | // Unset the delegate of underlying route store implementation |
| 141 | currentRouteStore.unsetDelegate(delegate); |
| 142 | } |
| 143 | |
| 144 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 145 | public void updateRoute(Route route) { |
| 146 | currentRouteStore.updateRoute(route); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void removeRoute(Route route) { |
| 151 | currentRouteStore.removeRoute(route); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Set<RouteTableId> getRouteTables() { |
| 156 | return currentRouteStore.getRouteTables(); |
| 157 | } |
| 158 | |
| 159 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 160 | public Collection<RouteSet> getRoutes(RouteTableId table) { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 161 | return currentRouteStore.getRoutes(table); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Route longestPrefixMatch(IpAddress ip) { |
| 166 | return currentRouteStore.longestPrefixMatch(ip); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Collection<Route> getRoutesForNextHop(IpAddress ip) { |
| 171 | return currentRouteStore.getRoutesForNextHop(ip); |
| 172 | } |
| 173 | |
| 174 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame^] | 175 | public RouteSet getRoutes(IpPrefix prefix) { |
| 176 | return currentRouteStore.getRoutes(prefix); |
| 177 | } |
| 178 | |
| 179 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 180 | public void updateNextHop(IpAddress ip, NextHopData nextHopData) { |
| 181 | currentRouteStore.updateNextHop(ip, nextHopData); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void removeNextHop(IpAddress ip, NextHopData nextHopData) { |
| 186 | currentRouteStore.removeNextHop(ip, nextHopData); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public NextHopData getNextHop(IpAddress ip) { |
| 191 | return currentRouteStore.getNextHop(ip); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public Map<IpAddress, NextHopData> getNextHops() { |
| 196 | return currentRouteStore.getNextHops(); |
| 197 | } |
| 198 | } |