blob: ace888d10a8c5e8b255e53d21317ef4e835d67ff [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip;
Jonathan Hart039d2b12014-10-10 09:33:04 -070017
Pingping Linc07781f2015-10-30 00:44:41 -070018import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.Objects;
21
Jonathan Hart039d2b12014-10-10 09:33:04 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070025import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070027import org.apache.felix.scr.annotations.Service;
Pingping Linc07781f2015-10-30 00:44:41 -070028import org.onosproject.app.ApplicationService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.cluster.ClusterService;
30import org.onosproject.cluster.ControllerNode;
31import org.onosproject.cluster.LeadershipEvent;
32import org.onosproject.cluster.LeadershipEventListener;
33import org.onosproject.cluster.LeadershipService;
34import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040036import org.onosproject.incubator.net.intf.InterfaceService;
Pingping Linc07781f2015-10-30 00:44:41 -070037import org.onosproject.net.config.NetworkConfigService;
Pingping Line28ae4c2015-03-13 11:37:03 -070038import org.onosproject.net.host.HostService;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.IntentService;
Jonathan Hart9a426f82015-09-03 15:43:13 +020040import org.onosproject.routing.IntentSynchronizationService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080041import org.onosproject.routing.RoutingService;
Jonathan Hart9a426f82015-09-03 15:43:13 +020042import org.onosproject.routing.SdnIpService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080043import org.onosproject.routing.config.RoutingConfigurationService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080044import org.slf4j.Logger;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080045
Jonathan Hart039d2b12014-10-10 09:33:04 -070046/**
Jonathan Hart0b04bed2014-10-16 16:39:19 -070047 * Component for the SDN-IP peering application.
Jonathan Hart039d2b12014-10-10 09:33:04 -070048 */
49@Component(immediate = true)
Jonathan Hart0b04bed2014-10-16 16:39:19 -070050@Service
51public class SdnIp implements SdnIpService {
Jonathan Hart039d2b12014-10-10 09:33:04 -070052
Brian O'Connorabafb502014-12-02 22:26:20 -080053 private static final String SDN_IP_APP = "org.onosproject.sdnip";
Jonathan Hart039d2b12014-10-10 09:33:04 -070054 private final Logger log = getLogger(getClass());
55
Jonathan Hartdc711bd2014-10-15 11:24:23 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskab97cf282014-10-20 23:31:12 -070057 protected CoreService coreService;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdc711bd2014-10-15 11:24:23 -070060 protected IntentService intentService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pingping Linc07781f2015-10-30 00:44:41 -070063 protected ApplicationService applicationService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pingping Line28ae4c2015-03-13 11:37:03 -070066 protected HostService hostService;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080069 protected ClusterService clusterService;
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart949c2842014-11-28 23:44:09 -080072 protected LeadershipService leadershipService;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080073
Jonathan Hart41349e92015-02-09 14:14:02 -080074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected RoutingService routingService;
76
Jonathan Hart90a02c22015-02-13 11:52:07 -080077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected RoutingConfigurationService config;
79
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected NetworkConfigService networkConfigService;
82
Jonathan Hart4cb39882015-08-12 23:50:55 -040083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected InterfaceService interfaceService;
85
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080086 private IntentSynchronizer intentSynchronizer;
Jonathan Hartce430a42014-10-16 20:44:29 -070087 private PeerConnectivityManager peerConnectivity;
Jonathan Hart9a426f82015-09-03 15:43:13 +020088 private SdnIpFib fib;
Jonathan Hart41349e92015-02-09 14:14:02 -080089
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080090 private LeadershipEventListener leadershipEventListener =
Pingping Linc07781f2015-10-30 00:44:41 -070091 new InnerLeadershipEventListener();
Pavlin Radoslavovc91eebe2014-11-25 18:45:46 -080092 private ApplicationId appId;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080093 private ControllerNode localControllerNode;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080094
Jonathan Hart039d2b12014-10-10 09:33:04 -070095 @Activate
Jonathan Hartd24fafb2015-02-09 17:55:32 -080096 protected void activate() {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080097 log.info("SDN-IP started");
Jonathan Hartbac07a02014-10-13 21:29:54 -070098
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080099 appId = coreService.registerApplication(SDN_IP_APP);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700100
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800101 localControllerNode = clusterService.getLocalNode();
102
Jonathan Hart9a426f82015-09-03 15:43:13 +0200103 intentSynchronizer = new IntentSynchronizer(appId, intentService);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800104 intentSynchronizer.start();
Jonathan Hart31582d12014-10-22 13:52:41 -0700105
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800106 peerConnectivity = new PeerConnectivityManager(appId,
107 intentSynchronizer,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400108 networkConfigService,
109 coreService.getAppId(RoutingService.ROUTER_APP_ID),
110 interfaceService);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700111 peerConnectivity.start();
112
Jonathan Hart9a426f82015-09-03 15:43:13 +0200113 fib = new SdnIpFib(appId, interfaceService, intentSynchronizer);
114
115 routingService.addFibListener(fib);
Pingping Line28ae4c2015-03-13 11:37:03 -0700116 routingService.start();
Jonathan Hart335ef462014-10-16 08:20:46 -0700117
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800118 leadershipService.addListener(leadershipEventListener);
119 leadershipService.runForLeadership(appId.name());
Pingping Linc07781f2015-10-30 00:44:41 -0700120
121 applicationService.registerDeactivateHook(appId,
122 intentSynchronizer::removeIntents);
123
Jonathan Hart039d2b12014-10-10 09:33:04 -0700124 }
125
126 @Deactivate
127 protected void deactivate() {
Jonathan Hart41349e92015-02-09 14:14:02 -0800128 routingService.stop();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800129 peerConnectivity.stop();
130 intentSynchronizer.stop();
Jonathan Hart739c8352014-10-29 17:49:26 -0700131
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800132 leadershipService.withdraw(appId.name());
133 leadershipService.removeListener(leadershipEventListener);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800134
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800135 log.info("SDN-IP Stopped");
Jonathan Hart039d2b12014-10-10 09:33:04 -0700136 }
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700137
138 @Override
Jonathan Hartec2df012014-10-23 16:40:24 -0700139 public void modifyPrimary(boolean isPrimary) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800140 intentSynchronizer.leaderChanged(isPrimary);
Jonathan Hartec2df012014-10-23 16:40:24 -0700141 }
142
Jonathan Hart9a426f82015-09-03 15:43:13 +0200143 @Override
144 public IntentSynchronizationService getIntentSynchronizationService() {
145 return intentSynchronizer;
146 }
147
Jonathan Hart51372182014-12-03 21:32:34 -0800148 /**
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 Hartce430a42014-10-16 20:44:29 -0700155 static String dpidToUri(String dpid) {
156 return "of:" + dpid.replace(":", "");
157 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800158
159 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800160 * A listener for Leadership Events.
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800161 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800162 private class InnerLeadershipEventListener
163 implements LeadershipEventListener {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800164
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800165 @Override
166 public void event(LeadershipEvent event) {
167 log.debug("Leadership Event: time = {} type = {} event = {}",
168 event.time(), event.type(), event);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800169
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800170 if (!event.subject().topic().equals(appId.name())) {
171 return; // Not our topic: ignore
172 }
Jonathan Hart75c470a2015-05-22 10:26:22 -0700173 if (!Objects.equals(event.subject().leader(), localControllerNode.id())) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800174 return; // The event is not about this instance: ignore
175 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800176
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800177 switch (event.type()) {
178 case LEADER_ELECTED:
Madan Jampani71582ed2014-11-18 10:06:01 -0800179 log.info("SDN-IP Leader Elected");
180 intentSynchronizer.leaderChanged(true);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800181 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 Radoslavov8b752442014-11-18 14:34:37 -0800190 }
191 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800192 }
Jonathan Hart039d2b12014-10-10 09:33:04 -0700193}