blob: d97e28a3b5c995a9f1817c8a98ed13873a968e05 [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;
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;
Pingping Line28ae4c2015-03-13 11:37:03 -070032import org.onosproject.net.host.HostService;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.intent.IntentService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080034import org.onosproject.routing.RoutingService;
35import org.onosproject.routing.config.RoutingConfigurationService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080036import org.slf4j.Logger;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080037
Jonathan Hart3cc23302015-05-14 09:21:16 -070038import java.util.Objects;
39
Jonathan Hart552e31f2015-02-06 11:11:59 -080040import static org.slf4j.LoggerFactory.getLogger;
41
Jonathan Hart039d2b12014-10-10 09:33:04 -070042/**
Jonathan Hart0b04bed2014-10-16 16:39:19 -070043 * Component for the SDN-IP peering application.
Jonathan Hart039d2b12014-10-10 09:33:04 -070044 */
45@Component(immediate = true)
Jonathan Hart0b04bed2014-10-16 16:39:19 -070046@Service
47public class SdnIp implements SdnIpService {
Jonathan Hart039d2b12014-10-10 09:33:04 -070048
Brian O'Connorabafb502014-12-02 22:26:20 -080049 private static final String SDN_IP_APP = "org.onosproject.sdnip";
Jonathan Hart039d2b12014-10-10 09:33:04 -070050 private final Logger log = getLogger(getClass());
51
Jonathan Hartdc711bd2014-10-15 11:24:23 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskab97cf282014-10-20 23:31:12 -070053 protected CoreService coreService;
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdc711bd2014-10-15 11:24:23 -070056 protected IntentService intentService;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pingping Line28ae4c2015-03-13 11:37:03 -070059 protected HostService hostService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080062 protected ClusterService clusterService;
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart949c2842014-11-28 23:44:09 -080065 protected LeadershipService leadershipService;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080066
Jonathan Hart41349e92015-02-09 14:14:02 -080067 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected RoutingService routingService;
69
Jonathan Hart90a02c22015-02-13 11:52:07 -080070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected RoutingConfigurationService config;
72
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080073 //
Jonathan Hart41349e92015-02-09 14:14:02 -080074 // NOTE: Unused reference - needed to guarantee that the
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080075 // NetworkConfigReader component is activated and the network configuration
76 // is read.
77 //
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected NetworkConfigService networkConfigService;
80
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080081 private IntentSynchronizer intentSynchronizer;
Jonathan Hartce430a42014-10-16 20:44:29 -070082 private PeerConnectivityManager peerConnectivity;
Jonathan Hart41349e92015-02-09 14:14:02 -080083
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080084 private LeadershipEventListener leadershipEventListener =
85 new InnerLeadershipEventListener();
Pavlin Radoslavovc91eebe2014-11-25 18:45:46 -080086 private ApplicationId appId;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080087 private ControllerNode localControllerNode;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080088
Jonathan Hart039d2b12014-10-10 09:33:04 -070089 @Activate
Jonathan Hartd24fafb2015-02-09 17:55:32 -080090 protected void activate() {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080091 log.info("SDN-IP started");
Jonathan Hartbac07a02014-10-13 21:29:54 -070092
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080093 appId = coreService.registerApplication(SDN_IP_APP);
Jonathan Hartdc711bd2014-10-15 11:24:23 -070094
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080095 localControllerNode = clusterService.getLocalNode();
96
Jonathan Hart552e31f2015-02-06 11:11:59 -080097 intentSynchronizer = new IntentSynchronizer(appId, intentService,
Pingping Line28ae4c2015-03-13 11:37:03 -070098 hostService,
Jonathan Hart90a02c22015-02-13 11:52:07 -080099 config);
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 Hart90a02c22015-02-13 11:52:07 -0800104 config);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700105 peerConnectivity.start();
106
Pingping Line28ae4c2015-03-13 11:37:03 -0700107 routingService.addFibListener(intentSynchronizer);
108 routingService.addIntentRequestListener(intentSynchronizer);
109 routingService.start();
Jonathan Hart335ef462014-10-16 08:20:46 -0700110
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800111 leadershipService.addListener(leadershipEventListener);
112 leadershipService.runForLeadership(appId.name());
Jonathan Hart039d2b12014-10-10 09:33:04 -0700113 }
114
115 @Deactivate
116 protected void deactivate() {
Jonathan Hart41349e92015-02-09 14:14:02 -0800117 routingService.stop();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800118 peerConnectivity.stop();
119 intentSynchronizer.stop();
Jonathan Hart739c8352014-10-29 17:49:26 -0700120
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800121 leadershipService.withdraw(appId.name());
122 leadershipService.removeListener(leadershipEventListener);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800123
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800124 log.info("SDN-IP Stopped");
Jonathan Hart039d2b12014-10-10 09:33:04 -0700125 }
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700126
127 @Override
Jonathan Hartec2df012014-10-23 16:40:24 -0700128 public void modifyPrimary(boolean isPrimary) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800129 intentSynchronizer.leaderChanged(isPrimary);
Jonathan Hartec2df012014-10-23 16:40:24 -0700130 }
131
Jonathan Hart51372182014-12-03 21:32:34 -0800132 /**
133 * Converts DPIDs of the form xx:xx:xx:xx:xx:xx:xx to OpenFlow provider
134 * device URIs.
135 *
136 * @param dpid the DPID string to convert
137 * @return the URI string for this device
138 */
Jonathan Hartce430a42014-10-16 20:44:29 -0700139 static String dpidToUri(String dpid) {
140 return "of:" + dpid.replace(":", "");
141 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800142
143 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800144 * A listener for Leadership Events.
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800145 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800146 private class InnerLeadershipEventListener
147 implements LeadershipEventListener {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800148
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800149 @Override
150 public void event(LeadershipEvent event) {
151 log.debug("Leadership Event: time = {} type = {} event = {}",
152 event.time(), event.type(), event);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800153
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800154 if (!event.subject().topic().equals(appId.name())) {
155 return; // Not our topic: ignore
156 }
Jonathan Hart75c470a2015-05-22 10:26:22 -0700157 if (!Objects.equals(event.subject().leader(), localControllerNode.id())) {
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800158 return; // The event is not about this instance: ignore
159 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800160
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800161 switch (event.type()) {
162 case LEADER_ELECTED:
Madan Jampani71582ed2014-11-18 10:06:01 -0800163 log.info("SDN-IP Leader Elected");
164 intentSynchronizer.leaderChanged(true);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800165 break;
166 case LEADER_BOOTED:
167 log.info("SDN-IP Leader Lost Election");
168 intentSynchronizer.leaderChanged(false);
169 break;
170 case LEADER_REELECTED:
171 break;
172 default:
173 break;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800174 }
175 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800176 }
Jonathan Hart039d2b12014-10-10 09:33:04 -0700177}