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