Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [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 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice.store; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 18 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 20 | import org.onlab.packet.IpPrefix; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 21 | import org.onlab.util.Tools; |
| 22 | import org.onosproject.cfg.ComponentConfigService; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 23 | import org.onosproject.routeservice.InternalRouteEvent; |
| 24 | import org.onosproject.routeservice.Route; |
| 25 | import org.onosproject.routeservice.RouteSet; |
| 26 | import org.onosproject.routeservice.RouteStore; |
| 27 | import org.onosproject.routeservice.RouteStoreDelegate; |
| 28 | import org.onosproject.routeservice.RouteTableId; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 29 | import org.onosproject.store.AbstractStore; |
| 30 | import org.onosproject.store.service.StorageService; |
| 31 | import org.osgi.service.component.ComponentContext; |
| 32 | import org.osgi.service.component.annotations.Activate; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Modified; |
| 36 | import org.osgi.service.component.annotations.Reference; |
| 37 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
| 41 | import java.util.Collection; |
| 42 | import java.util.Dictionary; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 43 | import java.util.Set; |
| 44 | |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 45 | import static org.onosproject.routeservice.store.OsgiPropertyConstants.DISTRIBUTED; |
| 46 | import static org.onosproject.routeservice.store.OsgiPropertyConstants.DISTRIBUTED_DEFAULT; |
| 47 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 48 | /** |
| 49 | * An implementation of RouteStore that is backed by either LocalRouteStore or |
| 50 | * DistributedRouteStore according to configuration. |
| 51 | */ |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 52 | @Component( |
| 53 | service = RouteStore.class, |
| 54 | property = { |
| 55 | DISTRIBUTED + ":Boolean=" + DISTRIBUTED_DEFAULT |
| 56 | } |
| 57 | ) |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 58 | public class RouteStoreImpl extends AbstractStore<InternalRouteEvent, RouteStoreDelegate> |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 59 | implements RouteStore { |
| 60 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 62 | protected ComponentConfigService componentConfigService; |
| 63 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 65 | public StorageService storageService; |
| 66 | |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 67 | /** Enable distributed route store. */ |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 68 | private boolean distributed; |
| 69 | |
| 70 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 71 | private RouteStore currentRouteStore; |
| 72 | |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 73 | private DistributedRouteStore distributedRouteStore; |
| 74 | private LocalRouteStore localRouteStore; |
| 75 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 76 | @Activate |
| 77 | public void activate(ComponentContext context) { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 78 | distributedRouteStore = new DistributedRouteStore(storageService); |
| 79 | distributedRouteStore.activate(); |
| 80 | localRouteStore = new LocalRouteStore(); |
| 81 | localRouteStore.activate(); |
| 82 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 83 | componentConfigService.registerProperties(getClass()); |
| 84 | modified(context); |
| 85 | } |
| 86 | |
| 87 | @Deactivate |
| 88 | public void deactivate() { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 89 | localRouteStore.deactivate(); |
| 90 | distributedRouteStore.deactivate(); |
| 91 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 92 | componentConfigService.unregisterProperties(getClass(), false); |
| 93 | } |
| 94 | |
| 95 | @Modified |
| 96 | public void modified(ComponentContext context) { |
| 97 | Dictionary<?, ?> properties = context.getProperties(); |
| 98 | if (properties == null) { |
| 99 | return; |
| 100 | } |
| 101 | |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 102 | String strDistributed = Tools.get(properties, DISTRIBUTED); |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 103 | boolean expectDistributed = Boolean.parseBoolean(strDistributed); |
| 104 | |
| 105 | // Start route store during first start or config change |
| 106 | // NOTE: new route store will be empty |
| 107 | if (currentRouteStore == null || expectDistributed != distributed) { |
| 108 | if (expectDistributed) { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 109 | currentRouteStore = distributedRouteStore; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 110 | } else { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 111 | currentRouteStore = localRouteStore; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | this.distributed = expectDistributed; |
| 115 | log.info("Switched to {} route store", distributed ? "distributed" : "local"); |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | @Override |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 121 | public void setDelegate(RouteStoreDelegate delegate) { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 122 | super.setDelegate(delegate); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 123 | |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 124 | // Set the delegate of underlying route store implementations |
| 125 | localRouteStore.setDelegate(delegate); |
| 126 | distributedRouteStore.setDelegate(delegate); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void unsetDelegate(RouteStoreDelegate delegate) { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 131 | super.unsetDelegate(delegate); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 132 | |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 133 | // Unset the delegate of underlying route store implementations |
| 134 | localRouteStore.unsetDelegate(delegate); |
| 135 | distributedRouteStore.unsetDelegate(delegate); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 139 | public void updateRoute(Route route) { |
| 140 | currentRouteStore.updateRoute(route); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void removeRoute(Route route) { |
| 145 | currentRouteStore.removeRoute(route); |
| 146 | } |
| 147 | |
| 148 | @Override |
Daniel Ginsburg | 83b7645 | 2018-06-09 01:43:59 +0300 | [diff] [blame] | 149 | public void replaceRoute(Route route) { |
| 150 | currentRouteStore.replaceRoute(route); |
| 151 | } |
| 152 | |
| 153 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 154 | public Set<RouteTableId> getRouteTables() { |
| 155 | return currentRouteStore.getRouteTables(); |
| 156 | } |
| 157 | |
| 158 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 159 | public Collection<RouteSet> getRoutes(RouteTableId table) { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 160 | return currentRouteStore.getRoutes(table); |
| 161 | } |
| 162 | |
| 163 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 164 | public Collection<Route> getRoutesForNextHop(IpAddress ip) { |
| 165 | return currentRouteStore.getRoutesForNextHop(ip); |
| 166 | } |
| 167 | |
| 168 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 169 | public RouteSet getRoutes(IpPrefix prefix) { |
| 170 | return currentRouteStore.getRoutes(prefix); |
| 171 | } |
Charles Chan | 4f36573 | 2017-07-20 17:11:57 -0700 | [diff] [blame] | 172 | |
| 173 | @Override |
| 174 | public String name() { |
| 175 | return currentRouteStore.name(); |
| 176 | } |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 177 | } |