blob: 3d1fe65c34fb5c2d9b25eabd72476cf95bfef5ed [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
Jonathan Hart039d2b12014-10-10 09:33:04 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070023import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.ControllerNode;
26import org.onosproject.cluster.LeadershipEvent;
27import org.onosproject.cluster.LeadershipEventListener;
28import org.onosproject.cluster.LeadershipService;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040031import org.onosproject.net.config.NetworkConfigService;
32import org.onosproject.incubator.net.intf.InterfaceService;
Pingping Line28ae4c2015-03-13 11:37:03 -070033import org.onosproject.net.host.HostService;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.intent.IntentService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080035import org.onosproject.routing.RoutingService;
36import org.onosproject.routing.config.RoutingConfigurationService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080037import org.slf4j.Logger;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080038
Jonathan Hart3cc23302015-05-14 09:21:16 -070039import java.util.Objects;
40
Jonathan Hart552e31f2015-02-06 11:11:59 -080041import static org.slf4j.LoggerFactory.getLogger;
42
Jonathan Hart039d2b12014-10-10 09:33:04 -070043/**
Jonathan Hart0b04bed2014-10-16 16:39:19 -070044 * Component for the SDN-IP peering application.
Jonathan Hart039d2b12014-10-10 09:33:04 -070045 */
46@Component(immediate = true)
Jonathan Hart0b04bed2014-10-16 16:39:19 -070047@Service
48public class SdnIp implements SdnIpService {
Jonathan Hart039d2b12014-10-10 09:33:04 -070049
Brian O'Connorabafb502014-12-02 22:26:20 -080050 private static final String SDN_IP_APP = "org.onosproject.sdnip";
Jonathan Hart039d2b12014-10-10 09:33:04 -070051 private final Logger log = getLogger(getClass());
52
Jonathan Hartdc711bd2014-10-15 11:24:23 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskab97cf282014-10-20 23:31:12 -070054 protected CoreService coreService;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdc711bd2014-10-15 11:24:23 -070057 protected IntentService intentService;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pingping Line28ae4c2015-03-13 11:37:03 -070060 protected HostService hostService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080063 protected ClusterService clusterService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart949c2842014-11-28 23:44:09 -080066 protected LeadershipService leadershipService;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080067
Jonathan Hart41349e92015-02-09 14:14:02 -080068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected RoutingService routingService;
70
Jonathan Hart90a02c22015-02-13 11:52:07 -080071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected RoutingConfigurationService config;
73
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected NetworkConfigService networkConfigService;
76
Jonathan Hart4cb39882015-08-12 23:50:55 -040077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected InterfaceService interfaceService;
79
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080080 private IntentSynchronizer intentSynchronizer;
Jonathan Hartce430a42014-10-16 20:44:29 -070081 private PeerConnectivityManager peerConnectivity;
Jonathan Hart41349e92015-02-09 14:14:02 -080082
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080083 private LeadershipEventListener leadershipEventListener =
84 new InnerLeadershipEventListener();
Pavlin Radoslavovc91eebe2014-11-25 18:45:46 -080085 private ApplicationId appId;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080086 private ControllerNode localControllerNode;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080087
Jonathan Hart039d2b12014-10-10 09:33:04 -070088 @Activate
Jonathan Hartd24fafb2015-02-09 17:55:32 -080089 protected void activate() {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080090 log.info("SDN-IP started");
Jonathan Hartbac07a02014-10-13 21:29:54 -070091
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080092 appId = coreService.registerApplication(SDN_IP_APP);
Jonathan Hartdc711bd2014-10-15 11:24:23 -070093
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080094 localControllerNode = clusterService.getLocalNode();
95
Jonathan Hart552e31f2015-02-06 11:11:59 -080096 intentSynchronizer = new IntentSynchronizer(appId, intentService,
Pingping Line28ae4c2015-03-13 11:37:03 -070097 hostService,
Jonathan Hart4cb39882015-08-12 23:50:55 -040098 config,
99 interfaceService);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800100 intentSynchronizer.start();
Jonathan Hart31582d12014-10-22 13:52:41 -0700101
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800102 peerConnectivity = new PeerConnectivityManager(appId,
103 intentSynchronizer,
Jonathan Hart4cb39882015-08-12 23:50:55 -0400104 networkConfigService,
105 coreService.getAppId(RoutingService.ROUTER_APP_ID),
106 interfaceService);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700107 peerConnectivity.start();
108
Pingping Line28ae4c2015-03-13 11:37:03 -0700109 routingService.addFibListener(intentSynchronizer);
110 routingService.addIntentRequestListener(intentSynchronizer);
111 routingService.start();
Jonathan Hart335ef462014-10-16 08:20:46 -0700112
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800113 leadershipService.addListener(leadershipEventListener);
114 leadershipService.runForLeadership(appId.name());
Jonathan Hart039d2b12014-10-10 09:33:04 -0700115 }
116
117 @Deactivate
118 protected void deactivate() {
Jonathan Hart41349e92015-02-09 14:14:02 -0800119 routingService.stop();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800120 peerConnectivity.stop();
121 intentSynchronizer.stop();
Jonathan Hart739c8352014-10-29 17:49:26 -0700122
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800123 leadershipService.withdraw(appId.name());
124 leadershipService.removeListener(leadershipEventListener);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800125
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800126 log.info("SDN-IP Stopped");
Jonathan Hart039d2b12014-10-10 09:33:04 -0700127 }
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700128
129 @Override
Jonathan Hartec2df012014-10-23 16:40:24 -0700130 public void modifyPrimary(boolean isPrimary) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800131 intentSynchronizer.leaderChanged(isPrimary);
Jonathan Hartec2df012014-10-23 16:40:24 -0700132 }
133
Jonathan Hart51372182014-12-03 21:32:34 -0800134 /**
135 * Converts DPIDs of the form xx:xx:xx:xx:xx:xx:xx to OpenFlow provider
136 * device URIs.
137 *
138 * @param dpid the DPID string to convert
139 * @return the URI string for this device
140 */
Jonathan Hartce430a42014-10-16 20:44:29 -0700141 static String dpidToUri(String dpid) {
142 return "of:" + dpid.replace(":", "");
143 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800144
145 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800146 * A listener for Leadership Events.
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800147 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800148 private class InnerLeadershipEventListener
149 implements LeadershipEventListener {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800150
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800151 @Override
152 public void event(LeadershipEvent event) {
153 log.debug("Leadership Event: time = {} type = {} event = {}",
154 event.time(), event.type(), event);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800155
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800156 if (!event.subject().topic().equals(appId.name())) {
157 return; // Not our topic: ignore
158 }
Jonathan Hart75c470a2015-05-22 10:26:22 -0700159 if (!Objects.equals(event.subject().leader(), localControllerNode.id())) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800160 return; // The event is not about this instance: ignore
161 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800162
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800163 switch (event.type()) {
164 case LEADER_ELECTED:
Madan Jampani71582ed2014-11-18 10:06:01 -0800165 log.info("SDN-IP Leader Elected");
166 intentSynchronizer.leaderChanged(true);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800167 break;
168 case LEADER_BOOTED:
169 log.info("SDN-IP Leader Lost Election");
170 intentSynchronizer.leaderChanged(false);
171 break;
172 case LEADER_REELECTED:
173 break;
174 default:
175 break;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800176 }
177 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800178 }
Jonathan Hart039d2b12014-10-10 09:33:04 -0700179}