blob: 2f24e15c494ce045b2215fc09b27952138cebe4c [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;
32import org.onosproject.net.host.HostService;
33import org.onosproject.net.intent.IntentService;
Jonathan Hart41349e92015-02-09 14:14:02 -080034import org.onosproject.routingapi.RoutingService;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.sdnip.config.SdnIpConfigurationReader;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080036import org.slf4j.Logger;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080037
Jonathan Hart552e31f2015-02-06 11:11:59 -080038import static org.slf4j.LoggerFactory.getLogger;
39
Jonathan Hart039d2b12014-10-10 09:33:04 -070040/**
Jonathan Hart0b04bed2014-10-16 16:39:19 -070041 * Component for the SDN-IP peering application.
Jonathan Hart039d2b12014-10-10 09:33:04 -070042 */
43@Component(immediate = true)
Jonathan Hart0b04bed2014-10-16 16:39:19 -070044@Service
45public class SdnIp implements SdnIpService {
Jonathan Hart039d2b12014-10-10 09:33:04 -070046
Brian O'Connorabafb502014-12-02 22:26:20 -080047 private static final String SDN_IP_APP = "org.onosproject.sdnip";
Jonathan Hart039d2b12014-10-10 09:33:04 -070048 private final Logger log = getLogger(getClass());
49
Jonathan Hartdc711bd2014-10-15 11:24:23 -070050 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskab97cf282014-10-20 23:31:12 -070051 protected CoreService coreService;
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdc711bd2014-10-15 11:24:23 -070054 protected IntentService intentService;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected HostService hostService;
58
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080059 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080060 protected ClusterService clusterService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart949c2842014-11-28 23:44:09 -080063 protected LeadershipService leadershipService;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080064
Jonathan Hart41349e92015-02-09 14:14:02 -080065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected RoutingService routingService;
67
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080068 //
Jonathan Hart41349e92015-02-09 14:14:02 -080069 // NOTE: Unused reference - needed to guarantee that the
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080070 // NetworkConfigReader component is activated and the network configuration
71 // is read.
72 //
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected NetworkConfigService networkConfigService;
75
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080076 private IntentSynchronizer intentSynchronizer;
Jonathan Hart9965d772014-12-02 10:28:34 -080077 private SdnIpConfigurationReader config;
Jonathan Hartce430a42014-10-16 20:44:29 -070078 private PeerConnectivityManager peerConnectivity;
Jonathan Hart41349e92015-02-09 14:14:02 -080079
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080080 private LeadershipEventListener leadershipEventListener =
81 new InnerLeadershipEventListener();
Pavlin Radoslavovc91eebe2014-11-25 18:45:46 -080082 private ApplicationId appId;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080083 private ControllerNode localControllerNode;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080084
Jonathan Hart039d2b12014-10-10 09:33:04 -070085 @Activate
Jonathan Hartd24fafb2015-02-09 17:55:32 -080086 protected void activate() {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080087 log.info("SDN-IP started");
Jonathan Hartbac07a02014-10-13 21:29:54 -070088
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080089 appId = coreService.registerApplication(SDN_IP_APP);
Jonathan Hart9965d772014-12-02 10:28:34 -080090 config = new SdnIpConfigurationReader();
91 config.readConfiguration();
Jonathan Hartdc711bd2014-10-15 11:24:23 -070092
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080093 localControllerNode = clusterService.getLocalNode();
94
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080095 InterfaceService interfaceService =
96 new HostToInterfaceAdaptor(hostService);
Jonathan Hartdc711bd2014-10-15 11:24:23 -070097
Jonathan Hart552e31f2015-02-06 11:11:59 -080098 intentSynchronizer = new IntentSynchronizer(appId, intentService,
99 config, 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,
104 config,
105 interfaceService);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700106 peerConnectivity.start();
107
Jonathan Hart41349e92015-02-09 14:14:02 -0800108 routingService.start(intentSynchronizer);
Jonathan Hart335ef462014-10-16 08:20:46 -0700109
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800110 leadershipService.addListener(leadershipEventListener);
111 leadershipService.runForLeadership(appId.name());
Jonathan Hart039d2b12014-10-10 09:33:04 -0700112 }
113
114 @Deactivate
115 protected void deactivate() {
Jonathan Hart41349e92015-02-09 14:14:02 -0800116 routingService.stop();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800117 peerConnectivity.stop();
118 intentSynchronizer.stop();
Jonathan Hart739c8352014-10-29 17:49:26 -0700119
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800120 leadershipService.withdraw(appId.name());
121 leadershipService.removeListener(leadershipEventListener);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800122
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800123 log.info("SDN-IP Stopped");
Jonathan Hart039d2b12014-10-10 09:33:04 -0700124 }
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700125
126 @Override
Jonathan Hartec2df012014-10-23 16:40:24 -0700127 public void modifyPrimary(boolean isPrimary) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800128 intentSynchronizer.leaderChanged(isPrimary);
Jonathan Hartec2df012014-10-23 16:40:24 -0700129 }
130
Jonathan Hart51372182014-12-03 21:32:34 -0800131 /**
132 * Converts DPIDs of the form xx:xx:xx:xx:xx:xx:xx to OpenFlow provider
133 * device URIs.
134 *
135 * @param dpid the DPID string to convert
136 * @return the URI string for this device
137 */
Jonathan Hartce430a42014-10-16 20:44:29 -0700138 static String dpidToUri(String dpid) {
139 return "of:" + dpid.replace(":", "");
140 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800141
142 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800143 * A listener for Leadership Events.
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800144 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800145 private class InnerLeadershipEventListener
146 implements LeadershipEventListener {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800147
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800148 @Override
149 public void event(LeadershipEvent event) {
150 log.debug("Leadership Event: time = {} type = {} event = {}",
151 event.time(), event.type(), event);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800152
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800153 if (!event.subject().topic().equals(appId.name())) {
154 return; // Not our topic: ignore
155 }
Madan Jampani8d21c792014-12-01 16:31:07 -0800156 if (!event.subject().leader().equals(
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800157 localControllerNode.id())) {
158 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}