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 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 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; |
| 33 | |
| 34 | import java.util.Set; |
| 35 | import java.util.stream.Collectors; |
| 36 | |
| 37 | /** |
| 38 | * Route source that installs static routes configured in the network configuration. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | public class ConfigurationRouteSource { |
| 42 | |
| 43 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 44 | protected NetworkConfigRegistry netcfgRegistry; |
| 45 | |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 47 | protected RouteAdminService routeService; |
| 48 | |
| 49 | private final ConfigFactory<ApplicationId, RouteConfig> routeConfigFactory = |
| 50 | new ConfigFactory<ApplicationId, RouteConfig>( |
| 51 | SubjectFactories.APP_SUBJECT_FACTORY, |
| 52 | RouteConfig.class, "routes", true) { |
| 53 | @Override |
| 54 | public RouteConfig createConfig() { |
| 55 | return new RouteConfig(); |
| 56 | } |
| 57 | }; |
| 58 | private final InternalNetworkConfigListener netcfgListener = |
| 59 | new InternalNetworkConfigListener(); |
| 60 | |
| 61 | @Activate |
| 62 | protected void activate() { |
| 63 | netcfgRegistry.addListener(netcfgListener); |
| 64 | netcfgRegistry.registerConfigFactory(routeConfigFactory); |
| 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | protected void deactivate() { |
| 69 | netcfgRegistry.removeListener(netcfgListener); |
| 70 | netcfgRegistry.unregisterConfigFactory(routeConfigFactory); |
| 71 | } |
| 72 | |
| 73 | private void processRouteConfigAdded(NetworkConfigEvent event) { |
| 74 | Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes(); |
| 75 | routeService.update(routes); |
| 76 | } |
| 77 | |
| 78 | private void processRouteConfigUpdated(NetworkConfigEvent event) { |
| 79 | Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes(); |
| 80 | Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes(); |
| 81 | Set<Route> pendingRemove = prevRoutes.stream() |
| 82 | .filter(prevRoute -> routes.stream() |
| 83 | .noneMatch(route -> route.prefix().equals(prevRoute.prefix()))) |
| 84 | .collect(Collectors.toSet()); |
| 85 | Set<Route> pendingUpdate = routes.stream() |
| 86 | .filter(route -> !pendingRemove.contains(route)).collect(Collectors.toSet()); |
| 87 | routeService.update(pendingUpdate); |
| 88 | routeService.withdraw(pendingRemove); |
| 89 | } |
| 90 | |
| 91 | private void processRouteConfigRemoved(NetworkConfigEvent event) { |
| 92 | Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes(); |
| 93 | routeService.withdraw(prevRoutes); |
| 94 | } |
| 95 | |
| 96 | private class InternalNetworkConfigListener implements |
| 97 | NetworkConfigListener { |
| 98 | @Override |
| 99 | public void event(NetworkConfigEvent event) { |
| 100 | if (event.configClass().equals(RouteConfig.class)) { |
| 101 | switch (event.type()) { |
| 102 | case CONFIG_ADDED: |
| 103 | processRouteConfigAdded(event); |
| 104 | break; |
| 105 | case CONFIG_UPDATED: |
| 106 | processRouteConfigUpdated(event); |
| 107 | break; |
| 108 | case CONFIG_REMOVED: |
| 109 | processRouteConfigRemoved(event); |
| 110 | break; |
| 111 | default: |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |