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