Jonathan Hart | 60e7f51 | 2017-02-10 10:24:24 -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 | 60e7f51 | 2017-02-10 10:24:24 -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 | |
| 17 | package org.onosproject.routing.config; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.net.config.ConfigFactory; |
| 23 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 24 | import org.onosproject.net.config.NetworkConfigService; |
| 25 | import org.onosproject.net.config.basics.SubjectFactories; |
| 26 | import org.onosproject.routing.RoutingService; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.util.Collections; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | /** |
| 34 | * Helper class to manage routing configurations. |
| 35 | */ |
| 36 | public final class RoutingConfiguration { |
| 37 | |
| 38 | private static final String WARNING = |
| 39 | "Config apps/org.onosproject.routing/router is deprecated " |
| 40 | + "and will be removed in a future release."; |
| 41 | private static final String WARNING2 = |
| 42 | "Use apps/org.onosproject.routing/routers instead"; |
| 43 | |
| 44 | private static final Logger log = LoggerFactory.getLogger(RoutingConfiguration.class); |
| 45 | |
| 46 | private static ConfigFactory<ApplicationId, BgpConfig> bgpConfigFactory = |
| 47 | new ConfigFactory<ApplicationId, BgpConfig>( |
| 48 | SubjectFactories.APP_SUBJECT_FACTORY, BgpConfig.class, "bgp") { |
| 49 | @Override |
| 50 | public BgpConfig createConfig() { |
| 51 | return new BgpConfig(); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | private static ConfigFactory<ApplicationId, RouterConfig> routerConfigFactory = |
| 56 | new ConfigFactory<ApplicationId, RouterConfig>( |
| 57 | SubjectFactories.APP_SUBJECT_FACTORY, RouterConfig.class, "router") { |
| 58 | @Override |
| 59 | public RouterConfig createConfig() { |
| 60 | return new RouterConfig(); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | private static ConfigFactory<ApplicationId, RoutersConfig> routersConfigFactory = |
| 65 | new ConfigFactory<ApplicationId, RoutersConfig>( |
| 66 | SubjectFactories.APP_SUBJECT_FACTORY, RoutersConfig.class, "routers", true) { |
| 67 | @Override |
| 68 | public RoutersConfig createConfig() { |
| 69 | return new RoutersConfig(); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | private static ImmutableList<ConfigFactory<?, ?>> factories = ImmutableList.<ConfigFactory<?, ?>>builder() |
| 74 | .add(bgpConfigFactory) |
| 75 | .add(routerConfigFactory) |
| 76 | .add(routersConfigFactory) |
| 77 | .build(); |
| 78 | |
| 79 | private static Integer registrations = 0; |
Ray Milkey | d4439e6 | 2018-01-16 09:43:00 -0800 | [diff] [blame] | 80 | private static final Object REGISTRATIONS_LOCK = new Object(); |
Jonathan Hart | 60e7f51 | 2017-02-10 10:24:24 -0800 | [diff] [blame] | 81 | |
| 82 | private RoutingConfiguration() { |
| 83 | // make checkstyle happy |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Registers the routing configuration factories. |
| 88 | * |
| 89 | * @param registry network config registry service |
| 90 | */ |
| 91 | public static void register(NetworkConfigRegistry registry) { |
Ray Milkey | d4439e6 | 2018-01-16 09:43:00 -0800 | [diff] [blame] | 92 | synchronized (REGISTRATIONS_LOCK) { |
Jonathan Hart | 60e7f51 | 2017-02-10 10:24:24 -0800 | [diff] [blame] | 93 | if (registrations == 0) { |
| 94 | factories.forEach(registry::registerConfigFactory); |
| 95 | } |
| 96 | registrations++; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Unregisters the routing configuration factories. |
| 102 | * <p>The factories will only be unregistered from the network config |
| 103 | * registry if no other routing applications are using them. Any components |
| 104 | * that call NetworkConfigHelper#registerConfigFactories need to also call |
| 105 | * this method when they are no longer using the config |
| 106 | * </p> |
| 107 | * |
| 108 | * @param registry network config registry service |
| 109 | */ |
| 110 | public static void unregister(NetworkConfigRegistry registry) { |
Ray Milkey | d4439e6 | 2018-01-16 09:43:00 -0800 | [diff] [blame] | 111 | synchronized (REGISTRATIONS_LOCK) { |
Jonathan Hart | 60e7f51 | 2017-02-10 10:24:24 -0800 | [diff] [blame] | 112 | registrations--; |
| 113 | if (registrations == 0) { |
| 114 | factories.forEach(registry::unregisterConfigFactory); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Retrieves the router configurations. |
| 121 | * |
| 122 | * @param configService network config service |
| 123 | * @param routingAppId routing app ID |
| 124 | * @return set of router configurations |
| 125 | */ |
| 126 | public static Set<RoutersConfig.Router> getRouterConfigurations( |
| 127 | NetworkConfigService configService, ApplicationId routingAppId) { |
| 128 | |
| 129 | RouterConfig config = configService.getConfig( |
| 130 | routingAppId, RoutingService.ROUTER_CONFIG_CLASS); |
| 131 | RoutersConfig multiConfig = configService.getConfig(routingAppId, RoutersConfig.class); |
| 132 | |
| 133 | if (config != null) { |
| 134 | log.warn(WARNING); |
| 135 | log.warn(WARNING2); |
| 136 | |
| 137 | return Collections.singleton( |
| 138 | new RoutersConfig.Router(config.getControlPlaneConnectPoint(), |
| 139 | config.getOspfEnabled(), |
| 140 | Sets.newHashSet(config.getInterfaces()))); |
| 141 | } else if (multiConfig != null) { |
| 142 | return multiConfig.getRouters(); |
| 143 | } else { |
| 144 | return Collections.emptySet(); |
| 145 | } |
| 146 | } |
| 147 | } |