blob: 641559d515f6b70c8a48c4858d51379c66b1938f [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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;
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080029import org.onosproject.config.NetworkConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.IntentService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080033import org.onosproject.routing.RoutingService;
34import org.onosproject.routing.config.RoutingConfigurationService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080035import org.slf4j.Logger;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080036
Jonathan Hart552e31f2015-02-06 11:11:59 -080037import static org.slf4j.LoggerFactory.getLogger;
38
Jonathan Hart039d2b12014-10-10 09:33:04 -070039/**
Jonathan Hart0b04bed2014-10-16 16:39:19 -070040 * Component for the SDN-IP peering application.
Jonathan Hart039d2b12014-10-10 09:33:04 -070041 */
42@Component(immediate = true)
Jonathan Hart0b04bed2014-10-16 16:39:19 -070043@Service
44public class SdnIp implements SdnIpService {
Jonathan Hart039d2b12014-10-10 09:33:04 -070045
Brian O'Connorabafb502014-12-02 22:26:20 -080046 private static final String SDN_IP_APP = "org.onosproject.sdnip";
Jonathan Hart039d2b12014-10-10 09:33:04 -070047 private final Logger log = getLogger(getClass());
48
Jonathan Hartdc711bd2014-10-15 11:24:23 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskab97cf282014-10-20 23:31:12 -070050 protected CoreService coreService;
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdc711bd2014-10-15 11:24:23 -070053 protected IntentService intentService;
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080056 protected ClusterService clusterService;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart949c2842014-11-28 23:44:09 -080059 protected LeadershipService leadershipService;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080060
Jonathan Hart41349e92015-02-09 14:14:02 -080061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected RoutingService routingService;
63
Jonathan Hart90a02c22015-02-13 11:52:07 -080064 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected RoutingConfigurationService config;
66
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080067 //
Jonathan Hart41349e92015-02-09 14:14:02 -080068 // NOTE: Unused reference - needed to guarantee that the
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080069 // NetworkConfigReader component is activated and the network configuration
70 // is read.
71 //
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected NetworkConfigService networkConfigService;
74
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080075 private IntentSynchronizer intentSynchronizer;
Jonathan Hartce430a42014-10-16 20:44:29 -070076 private PeerConnectivityManager peerConnectivity;
Jonathan Hart41349e92015-02-09 14:14:02 -080077
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080078 private LeadershipEventListener leadershipEventListener =
79 new InnerLeadershipEventListener();
Pavlin Radoslavovc91eebe2014-11-25 18:45:46 -080080 private ApplicationId appId;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080081 private ControllerNode localControllerNode;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080082
Jonathan Hart039d2b12014-10-10 09:33:04 -070083 @Activate
Jonathan Hartd24fafb2015-02-09 17:55:32 -080084 protected void activate() {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080085 log.info("SDN-IP started");
Jonathan Hartbac07a02014-10-13 21:29:54 -070086
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080087 appId = coreService.registerApplication(SDN_IP_APP);
Jonathan Hartdc711bd2014-10-15 11:24:23 -070088
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080089 localControllerNode = clusterService.getLocalNode();
90
Jonathan Hart552e31f2015-02-06 11:11:59 -080091 intentSynchronizer = new IntentSynchronizer(appId, intentService,
Jonathan Hart90a02c22015-02-13 11:52:07 -080092 config);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080093 intentSynchronizer.start();
Jonathan Hart31582d12014-10-22 13:52:41 -070094
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080095 peerConnectivity = new PeerConnectivityManager(appId,
96 intentSynchronizer,
Jonathan Hart90a02c22015-02-13 11:52:07 -080097 config);
Jonathan Hartdc711bd2014-10-15 11:24:23 -070098 peerConnectivity.start();
99
Jonathan Hart41349e92015-02-09 14:14:02 -0800100 routingService.start(intentSynchronizer);
Jonathan Hart335ef462014-10-16 08:20:46 -0700101
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800102 leadershipService.addListener(leadershipEventListener);
103 leadershipService.runForLeadership(appId.name());
Jonathan Hart039d2b12014-10-10 09:33:04 -0700104 }
105
106 @Deactivate
107 protected void deactivate() {
Jonathan Hart41349e92015-02-09 14:14:02 -0800108 routingService.stop();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800109 peerConnectivity.stop();
110 intentSynchronizer.stop();
Jonathan Hart739c8352014-10-29 17:49:26 -0700111
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800112 leadershipService.withdraw(appId.name());
113 leadershipService.removeListener(leadershipEventListener);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800114
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800115 log.info("SDN-IP Stopped");
Jonathan Hart039d2b12014-10-10 09:33:04 -0700116 }
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700117
118 @Override
Jonathan Hartec2df012014-10-23 16:40:24 -0700119 public void modifyPrimary(boolean isPrimary) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800120 intentSynchronizer.leaderChanged(isPrimary);
Jonathan Hartec2df012014-10-23 16:40:24 -0700121 }
122
Jonathan Hart51372182014-12-03 21:32:34 -0800123 /**
124 * Converts DPIDs of the form xx:xx:xx:xx:xx:xx:xx to OpenFlow provider
125 * device URIs.
126 *
127 * @param dpid the DPID string to convert
128 * @return the URI string for this device
129 */
Jonathan Hartce430a42014-10-16 20:44:29 -0700130 static String dpidToUri(String dpid) {
131 return "of:" + dpid.replace(":", "");
132 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800133
134 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800135 * A listener for Leadership Events.
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800136 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800137 private class InnerLeadershipEventListener
138 implements LeadershipEventListener {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800139
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800140 @Override
141 public void event(LeadershipEvent event) {
142 log.debug("Leadership Event: time = {} type = {} event = {}",
143 event.time(), event.type(), event);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800144
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800145 if (!event.subject().topic().equals(appId.name())) {
146 return; // Not our topic: ignore
147 }
Madan Jampani8d21c792014-12-01 16:31:07 -0800148 if (!event.subject().leader().equals(
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800149 localControllerNode.id())) {
150 return; // The event is not about this instance: ignore
151 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800152
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800153 switch (event.type()) {
154 case LEADER_ELECTED:
Madan Jampani71582ed2014-11-18 10:06:01 -0800155 log.info("SDN-IP Leader Elected");
156 intentSynchronizer.leaderChanged(true);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800157 break;
158 case LEADER_BOOTED:
159 log.info("SDN-IP Leader Lost Election");
160 intentSynchronizer.leaderChanged(false);
161 break;
162 case LEADER_REELECTED:
163 break;
164 default:
165 break;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800166 }
167 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800168 }
Jonathan Hart039d2b12014-10-10 09:33:04 -0700169}