Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame^] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Service; |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame^] | 28 | import org.onosproject.app.ApplicationService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
| 30 | import org.onosproject.cluster.ControllerNode; |
| 31 | import org.onosproject.cluster.LeadershipEvent; |
| 32 | import org.onosproject.cluster.LeadershipEventListener; |
| 33 | import org.onosproject.cluster.LeadershipService; |
| 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame^] | 37 | import org.onosproject.net.config.NetworkConfigService; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 38 | import org.onosproject.net.host.HostService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.IntentService; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 40 | import org.onosproject.routing.IntentSynchronizationService; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 41 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 42 | import org.onosproject.routing.SdnIpService; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 43 | import org.onosproject.routing.config.RoutingConfigurationService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 44 | import org.slf4j.Logger; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 45 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 46 | /** |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 47 | * Component for the SDN-IP peering application. |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 48 | */ |
| 49 | @Component(immediate = true) |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 50 | @Service |
| 51 | public class SdnIp implements SdnIpService { |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 52 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | private static final String SDN_IP_APP = "org.onosproject.sdnip"; |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 57 | protected CoreService coreService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 60 | protected IntentService intentService; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame^] | 63 | protected ApplicationService applicationService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 66 | protected HostService hostService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 69 | protected ClusterService clusterService; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 72 | protected LeadershipService leadershipService; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 73 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected RoutingService routingService; |
| 76 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected RoutingConfigurationService config; |
| 79 | |
Pavlin Radoslavov | 492cc3a | 2015-01-22 18:46:26 -0800 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected NetworkConfigService networkConfigService; |
| 82 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected InterfaceService interfaceService; |
| 85 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 86 | private IntentSynchronizer intentSynchronizer; |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 87 | private PeerConnectivityManager peerConnectivity; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 88 | private SdnIpFib fib; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 89 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 90 | private LeadershipEventListener leadershipEventListener = |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame^] | 91 | new InnerLeadershipEventListener(); |
Pavlin Radoslavov | c91eebe | 2014-11-25 18:45:46 -0800 | [diff] [blame] | 92 | private ApplicationId appId; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 93 | private ControllerNode localControllerNode; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 94 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 95 | @Activate |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 96 | protected void activate() { |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 97 | log.info("SDN-IP started"); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 98 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 99 | appId = coreService.registerApplication(SDN_IP_APP); |
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 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 103 | intentSynchronizer = new IntentSynchronizer(appId, intentService); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 104 | intentSynchronizer.start(); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 105 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 106 | peerConnectivity = new PeerConnectivityManager(appId, |
| 107 | intentSynchronizer, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 108 | networkConfigService, |
| 109 | coreService.getAppId(RoutingService.ROUTER_APP_ID), |
| 110 | interfaceService); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 111 | peerConnectivity.start(); |
| 112 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 113 | fib = new SdnIpFib(appId, interfaceService, intentSynchronizer); |
| 114 | |
| 115 | routingService.addFibListener(fib); |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 116 | routingService.start(); |
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()); |
Pingping Lin | c07781f | 2015-10-30 00:44:41 -0700 | [diff] [blame^] | 120 | |
| 121 | applicationService.registerDeactivateHook(appId, |
| 122 | intentSynchronizer::removeIntents); |
| 123 | |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Deactivate |
| 127 | protected void deactivate() { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 128 | routingService.stop(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 129 | peerConnectivity.stop(); |
| 130 | intentSynchronizer.stop(); |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 131 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 132 | leadershipService.withdraw(appId.name()); |
| 133 | leadershipService.removeListener(leadershipEventListener); |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 134 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 135 | log.info("SDN-IP Stopped"); |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 136 | } |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 137 | |
| 138 | @Override |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 139 | public void modifyPrimary(boolean isPrimary) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 140 | intentSynchronizer.leaderChanged(isPrimary); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 143 | @Override |
| 144 | public IntentSynchronizationService getIntentSynchronizationService() { |
| 145 | return intentSynchronizer; |
| 146 | } |
| 147 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 148 | /** |
| 149 | * Converts DPIDs of the form xx:xx:xx:xx:xx:xx:xx to OpenFlow provider |
| 150 | * device URIs. |
| 151 | * |
| 152 | * @param dpid the DPID string to convert |
| 153 | * @return the URI string for this device |
| 154 | */ |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 155 | static String dpidToUri(String dpid) { |
| 156 | return "of:" + dpid.replace(":", ""); |
| 157 | } |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 158 | |
| 159 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 160 | * A listener for Leadership Events. |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 161 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 162 | private class InnerLeadershipEventListener |
| 163 | implements LeadershipEventListener { |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 164 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 165 | @Override |
| 166 | public void event(LeadershipEvent event) { |
| 167 | log.debug("Leadership Event: time = {} type = {} event = {}", |
| 168 | event.time(), event.type(), event); |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 169 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 170 | if (!event.subject().topic().equals(appId.name())) { |
| 171 | return; // Not our topic: ignore |
| 172 | } |
Jonathan Hart | 75c470a | 2015-05-22 10:26:22 -0700 | [diff] [blame] | 173 | if (!Objects.equals(event.subject().leader(), localControllerNode.id())) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 174 | return; // The event is not about this instance: ignore |
| 175 | } |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 176 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 177 | switch (event.type()) { |
| 178 | case LEADER_ELECTED: |
Madan Jampani | 71582ed | 2014-11-18 10:06:01 -0800 | [diff] [blame] | 179 | log.info("SDN-IP Leader Elected"); |
| 180 | intentSynchronizer.leaderChanged(true); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 181 | break; |
| 182 | case LEADER_BOOTED: |
| 183 | log.info("SDN-IP Leader Lost Election"); |
| 184 | intentSynchronizer.leaderChanged(false); |
| 185 | break; |
| 186 | case LEADER_REELECTED: |
| 187 | break; |
| 188 | default: |
| 189 | break; |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 192 | } |
Jonathan Hart | 039d2b1 | 2014-10-10 09:33:04 -0700 | [diff] [blame] | 193 | } |