Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 20 | import java.util.Collection; |
Jonathan Hart | 9fd6bfc | 2014-12-03 15:30:33 -0800 | [diff] [blame] | 21 | import java.util.Dictionary; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 22 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | 9fd6bfc | 2014-12-03 15:30:33 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Modified; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.cluster.ClusterService; |
| 31 | import org.onosproject.cluster.ControllerNode; |
| 32 | import org.onosproject.cluster.LeadershipEvent; |
| 33 | import org.onosproject.cluster.LeadershipEventListener; |
| 34 | import org.onosproject.cluster.LeadershipService; |
Pavlin Radoslavov | 492cc3a | 2015-01-22 18:46:26 -0800 | [diff] [blame^] | 35 | import org.onosproject.config.NetworkConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.core.ApplicationId; |
| 37 | import org.onosproject.core.CoreService; |
| 38 | import org.onosproject.net.host.HostService; |
| 39 | import org.onosproject.net.intent.IntentService; |
| 40 | import org.onosproject.sdnip.bgp.BgpRouteEntry; |
| 41 | import org.onosproject.sdnip.bgp.BgpSession; |
| 42 | import org.onosproject.sdnip.bgp.BgpSessionManager; |
| 43 | import org.onosproject.sdnip.config.SdnIpConfigurationReader; |
Jonathan Hart | 9fd6bfc | 2014-12-03 15:30:33 -0800 | [diff] [blame] | 44 | import org.osgi.service.component.ComponentContext; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 45 | import org.slf4j.Logger; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 46 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 47 | /** |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 48 | * Component for the SDN-IP peering application. |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 49 | */ |
| 50 | @Component(immediate = true) |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 51 | @Service |
| 52 | public class SdnIp implements SdnIpService { |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 53 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | private static final String SDN_IP_APP = "org.onosproject.sdnip"; |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 55 | private final Logger log = getLogger(getClass()); |
| 56 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 58 | protected CoreService coreService; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 61 | protected IntentService intentService; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected HostService hostService; |
| 65 | |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 67 | protected ClusterService clusterService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 70 | protected LeadershipService leadershipService; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 71 | |
Pavlin Radoslavov | 492cc3a | 2015-01-22 18:46:26 -0800 | [diff] [blame^] | 72 | // |
| 73 | // NOTE: Unused reference - needed to guarentee that the |
| 74 | // NetworkConfigReader component is activated and the network configuration |
| 75 | // is read. |
| 76 | // |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected NetworkConfigService networkConfigService; |
| 79 | |
Jonathan Hart | 9fd6bfc | 2014-12-03 15:30:33 -0800 | [diff] [blame] | 80 | private static final int DEFAULT_BGP_PORT = 2000; |
| 81 | private int bgpPort; |
| 82 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 83 | private IntentSynchronizer intentSynchronizer; |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 84 | private SdnIpConfigurationReader config; |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 85 | private PeerConnectivityManager peerConnectivity; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 86 | private Router router; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 87 | private BgpSessionManager bgpSessionManager; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 88 | private LeadershipEventListener leadershipEventListener = |
| 89 | new InnerLeadershipEventListener(); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 90 | private ApplicationId appId; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 91 | private ControllerNode localControllerNode; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 92 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 93 | @Activate |
Jonathan Hart | 9fd6bfc | 2014-12-03 15:30:33 -0800 | [diff] [blame] | 94 | protected void activate(ComponentContext context) { |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 95 | log.info("SDN-IP started"); |
Jonathan Hart | 9fd6bfc | 2014-12-03 15:30:33 -0800 | [diff] [blame] | 96 | readComponentConfiguration(context); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 97 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 98 | appId = coreService.registerApplication(SDN_IP_APP); |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 99 | config = new SdnIpConfigurationReader(); |
| 100 | config.readConfiguration(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 101 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 102 | localControllerNode = clusterService.getLocalNode(); |
| 103 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 104 | InterfaceService interfaceService = |
| 105 | new HostToInterfaceAdaptor(hostService); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 106 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 107 | intentSynchronizer = new IntentSynchronizer(appId, intentService); |
| 108 | intentSynchronizer.start(); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 109 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 110 | peerConnectivity = new PeerConnectivityManager(appId, |
| 111 | intentSynchronizer, |
| 112 | config, |
| 113 | interfaceService); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 114 | peerConnectivity.start(); |
| 115 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 116 | router = new Router(appId, intentSynchronizer, config, |
| 117 | interfaceService, hostService); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 118 | router.start(); |
| 119 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 120 | leadershipService.addListener(leadershipEventListener); |
| 121 | leadershipService.runForLeadership(appId.name()); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 122 | |
Jonathan Hart | 9fd6bfc | 2014-12-03 15:30:33 -0800 | [diff] [blame] | 123 | log.info("Starting BGP with port {}", bgpPort); |
| 124 | |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 125 | bgpSessionManager = new BgpSessionManager(router); |
Jonathan Hart | 9fd6bfc | 2014-12-03 15:30:33 -0800 | [diff] [blame] | 126 | bgpSessionManager.start(bgpPort); |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Deactivate |
| 130 | protected void deactivate() { |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 131 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 132 | bgpSessionManager.stop(); |
| 133 | router.stop(); |
| 134 | peerConnectivity.stop(); |
| 135 | intentSynchronizer.stop(); |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 136 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 137 | leadershipService.withdraw(appId.name()); |
| 138 | leadershipService.removeListener(leadershipEventListener); |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 139 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 140 | log.info("SDN-IP Stopped"); |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 141 | } |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 142 | |
Jonathan Hart | 9fd6bfc | 2014-12-03 15:30:33 -0800 | [diff] [blame] | 143 | /** |
| 144 | * Extracts properties from the component configuration context. |
| 145 | * |
| 146 | * @param context the component context |
| 147 | */ |
| 148 | private void readComponentConfiguration(ComponentContext context) { |
| 149 | Dictionary<?, ?> properties = context.getProperties(); |
| 150 | try { |
| 151 | String strPort = (String) properties.get("bgpPort"); |
| 152 | if (strPort != null) { |
| 153 | bgpPort = Integer.parseInt(strPort); |
| 154 | } else { |
| 155 | bgpPort = DEFAULT_BGP_PORT; |
| 156 | } |
| 157 | } catch (Exception e) { |
| 158 | bgpPort = DEFAULT_BGP_PORT; |
| 159 | } |
| 160 | log.debug("BGP port is set to {}", bgpPort); |
| 161 | } |
| 162 | |
| 163 | @Modified |
| 164 | public void modified(ComponentContext context) { |
| 165 | // Blank @Modified method to catch modifications to the context. |
| 166 | // If no @Modified method exists, it seems @Activate is called again |
| 167 | // when the context is modified. |
| 168 | } |
| 169 | |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 170 | @Override |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 171 | public Collection<BgpSession> getBgpSessions() { |
| 172 | return bgpSessionManager.getBgpSessions(); |
| 173 | } |
| 174 | |
| 175 | @Override |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 176 | public Collection<BgpRouteEntry> getBgpRoutes4() { |
| 177 | return bgpSessionManager.getBgpRoutes4(); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 181 | public Collection<BgpRouteEntry> getBgpRoutes6() { |
| 182 | return bgpSessionManager.getBgpRoutes6(); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public Collection<RouteEntry> getRoutes4() { |
| 187 | return router.getRoutes4(); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public Collection<RouteEntry> getRoutes6() { |
| 192 | return router.getRoutes6(); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 193 | } |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 194 | |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 195 | @Override |
| 196 | public void modifyPrimary(boolean isPrimary) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 197 | intentSynchronizer.leaderChanged(isPrimary); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 200 | /** |
| 201 | * Converts DPIDs of the form xx:xx:xx:xx:xx:xx:xx to OpenFlow provider |
| 202 | * device URIs. |
| 203 | * |
| 204 | * @param dpid the DPID string to convert |
| 205 | * @return the URI string for this device |
| 206 | */ |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 207 | static String dpidToUri(String dpid) { |
| 208 | return "of:" + dpid.replace(":", ""); |
| 209 | } |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 210 | |
| 211 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 212 | * A listener for Leadership Events. |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 213 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 214 | private class InnerLeadershipEventListener |
| 215 | implements LeadershipEventListener { |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 216 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 217 | @Override |
| 218 | public void event(LeadershipEvent event) { |
| 219 | log.debug("Leadership Event: time = {} type = {} event = {}", |
| 220 | event.time(), event.type(), event); |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 221 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 222 | if (!event.subject().topic().equals(appId.name())) { |
| 223 | return; // Not our topic: ignore |
| 224 | } |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 225 | if (!event.subject().leader().equals( |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 226 | localControllerNode.id())) { |
| 227 | return; // The event is not about this instance: ignore |
| 228 | } |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 229 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 230 | switch (event.type()) { |
| 231 | case LEADER_ELECTED: |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame] | 232 | log.info("SDN-IP Leader Elected"); |
| 233 | intentSynchronizer.leaderChanged(true); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 234 | break; |
| 235 | case LEADER_BOOTED: |
| 236 | log.info("SDN-IP Leader Lost Election"); |
| 237 | intentSynchronizer.leaderChanged(false); |
| 238 | break; |
| 239 | case LEADER_REELECTED: |
| 240 | break; |
| 241 | default: |
| 242 | break; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 245 | } |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 246 | } |