Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -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.impl; |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.osgi.service.component.annotations.Activate; |
| 20 | import org.osgi.service.component.annotations.Component; |
| 21 | import org.osgi.service.component.annotations.Deactivate; |
| 22 | import org.osgi.service.component.annotations.Reference; |
| 23 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 25 | import org.onosproject.routeservice.Route; |
| 26 | import org.onosproject.routeservice.RouteAdminService; |
| 27 | import org.onosproject.routeservice.RouteConfig; |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 28 | import org.onosproject.net.config.ConfigFactory; |
| 29 | import org.onosproject.net.config.NetworkConfigEvent; |
| 30 | import org.onosproject.net.config.NetworkConfigListener; |
| 31 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 32 | import org.onosproject.net.config.basics.SubjectFactories; |
Charles Chan | bfc5e55 | 2020-07-17 19:12:43 -0700 | [diff] [blame^] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 35 | |
Charles Chan | bfc5e55 | 2020-07-17 19:12:43 -0700 | [diff] [blame^] | 36 | import java.util.Optional; |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 37 | import java.util.Set; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
| 40 | /** |
| 41 | * Route source that installs static routes configured in the network configuration. |
| 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | public class ConfigurationRouteSource { |
| 45 | |
Charles Chan | bfc5e55 | 2020-07-17 19:12:43 -0700 | [diff] [blame^] | 46 | private static final Logger log = LoggerFactory.getLogger(ConfigurationRouteSource.class); |
| 47 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 49 | protected NetworkConfigRegistry netcfgRegistry; |
| 50 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 52 | protected RouteAdminService routeService; |
| 53 | |
| 54 | private final ConfigFactory<ApplicationId, RouteConfig> routeConfigFactory = |
| 55 | new ConfigFactory<ApplicationId, RouteConfig>( |
| 56 | SubjectFactories.APP_SUBJECT_FACTORY, |
| 57 | RouteConfig.class, "routes", true) { |
| 58 | @Override |
| 59 | public RouteConfig createConfig() { |
| 60 | return new RouteConfig(); |
| 61 | } |
| 62 | }; |
| 63 | private final InternalNetworkConfigListener netcfgListener = |
| 64 | new InternalNetworkConfigListener(); |
| 65 | |
| 66 | @Activate |
| 67 | protected void activate() { |
| 68 | netcfgRegistry.addListener(netcfgListener); |
| 69 | netcfgRegistry.registerConfigFactory(routeConfigFactory); |
Charles Chan | bfc5e55 | 2020-07-17 19:12:43 -0700 | [diff] [blame^] | 70 | |
| 71 | // Read initial routes in netcfg |
| 72 | netcfgRegistry.getSubjects(ApplicationId.class, RouteConfig.class).forEach(subject -> { |
| 73 | Optional.ofNullable(netcfgRegistry.getConfig(subject, RouteConfig.class)) |
| 74 | .map(RouteConfig::getRoutes) |
| 75 | .ifPresent(routes -> { |
| 76 | log.info("Load initial routes from netcfg: {}", routes); |
| 77 | routeService.update(routes); |
| 78 | }); |
| 79 | }); |
| 80 | |
Jonathan Hart | 79a0abd | 2017-02-23 19:13:27 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | protected void deactivate() { |
| 85 | netcfgRegistry.removeListener(netcfgListener); |
| 86 | netcfgRegistry.unregisterConfigFactory(routeConfigFactory); |
| 87 | } |
| 88 | |
| 89 | private void processRouteConfigAdded(NetworkConfigEvent event) { |
| 90 | Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes(); |
| 91 | routeService.update(routes); |
| 92 | } |
| 93 | |
| 94 | private void processRouteConfigUpdated(NetworkConfigEvent event) { |
| 95 | Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes(); |
| 96 | Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes(); |
| 97 | Set<Route> pendingRemove = prevRoutes.stream() |
| 98 | .filter(prevRoute -> routes.stream() |
| 99 | .noneMatch(route -> route.prefix().equals(prevRoute.prefix()))) |
| 100 | .collect(Collectors.toSet()); |
| 101 | Set<Route> pendingUpdate = routes.stream() |
| 102 | .filter(route -> !pendingRemove.contains(route)).collect(Collectors.toSet()); |
| 103 | routeService.update(pendingUpdate); |
| 104 | routeService.withdraw(pendingRemove); |
| 105 | } |
| 106 | |
| 107 | private void processRouteConfigRemoved(NetworkConfigEvent event) { |
| 108 | Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes(); |
| 109 | routeService.withdraw(prevRoutes); |
| 110 | } |
| 111 | |
| 112 | private class InternalNetworkConfigListener implements |
| 113 | NetworkConfigListener { |
| 114 | @Override |
| 115 | public void event(NetworkConfigEvent event) { |
| 116 | if (event.configClass().equals(RouteConfig.class)) { |
| 117 | switch (event.type()) { |
| 118 | case CONFIG_ADDED: |
| 119 | processRouteConfigAdded(event); |
| 120 | break; |
| 121 | case CONFIG_UPDATED: |
| 122 | processRouteConfigUpdated(event); |
| 123 | break; |
| 124 | case CONFIG_REMOVED: |
| 125 | processRouteConfigRemoved(event); |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |