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