blob: 2b312abbde5511cfa475461604fd8ae3ca8033a9 [file] [log] [blame]
Jonathan Hart60e7f512017-02-10 10:24:24 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hart60e7f512017-02-10 10:24:24 -08003 *
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
17package org.onosproject.routing.config;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Sets;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.net.config.ConfigFactory;
23import org.onosproject.net.config.NetworkConfigRegistry;
24import org.onosproject.net.config.NetworkConfigService;
25import org.onosproject.net.config.basics.SubjectFactories;
26import org.onosproject.routing.RoutingService;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.Collections;
31import java.util.Set;
32
33/**
34 * Helper class to manage routing configurations.
35 */
36public 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 Milkeyd4439e62018-01-16 09:43:00 -080080 private static final Object REGISTRATIONS_LOCK = new Object();
Jonathan Hart60e7f512017-02-10 10:24:24 -080081
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 Milkeyd4439e62018-01-16 09:43:00 -080092 synchronized (REGISTRATIONS_LOCK) {
Jonathan Hart60e7f512017-02-10 10:24:24 -080093 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 Milkeyd4439e62018-01-16 09:43:00 -0800111 synchronized (REGISTRATIONS_LOCK) {
Jonathan Hart60e7f512017-02-10 10:24:24 -0800112 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}