blob: b375852e6d1d4c95d953ffe260ae9ba74f73e517 [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 Hart9fd6bfc2014-12-03 15:30:33 -080021import org.apache.felix.scr.annotations.Modified;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070024import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cluster.ClusterService;
26import org.onosproject.cluster.ControllerNode;
27import org.onosproject.cluster.LeadershipEvent;
28import org.onosproject.cluster.LeadershipEventListener;
29import org.onosproject.cluster.LeadershipService;
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080030import org.onosproject.config.NetworkConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
33import org.onosproject.net.host.HostService;
34import org.onosproject.net.intent.IntentService;
Jonathan Hart41349e92015-02-09 14:14:02 -080035import org.onosproject.routingapi.RoutingService;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.sdnip.config.SdnIpConfigurationReader;
Jonathan Hart9fd6bfc2014-12-03 15:30:33 -080037import org.osgi.service.component.ComponentContext;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080038import org.slf4j.Logger;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080039
Jonathan Hart552e31f2015-02-06 11:11:59 -080040import java.util.Dictionary;
41
42import static org.slf4j.LoggerFactory.getLogger;
43
Jonathan Hart039d2b12014-10-10 09:33:04 -070044/**
Jonathan Hart0b04bed2014-10-16 16:39:19 -070045 * Component for the SDN-IP peering application.
Jonathan Hart039d2b12014-10-10 09:33:04 -070046 */
47@Component(immediate = true)
Jonathan Hart0b04bed2014-10-16 16:39:19 -070048@Service
49public class SdnIp implements SdnIpService {
Jonathan Hart039d2b12014-10-10 09:33:04 -070050
Brian O'Connorabafb502014-12-02 22:26:20 -080051 private static final String SDN_IP_APP = "org.onosproject.sdnip";
Jonathan Hart039d2b12014-10-10 09:33:04 -070052 private final Logger log = getLogger(getClass());
53
Jonathan Hartdc711bd2014-10-15 11:24:23 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskab97cf282014-10-20 23:31:12 -070055 protected CoreService coreService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hartdc711bd2014-10-15 11:24:23 -070058 protected IntentService intentService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected HostService hostService;
62
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080063 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080064 protected ClusterService clusterService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart949c2842014-11-28 23:44:09 -080067 protected LeadershipService leadershipService;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080068
Jonathan Hart41349e92015-02-09 14:14:02 -080069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected RoutingService routingService;
71
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080072 //
Jonathan Hart41349e92015-02-09 14:14:02 -080073 // NOTE: Unused reference - needed to guarantee that the
Pavlin Radoslavov492cc3a2015-01-22 18:46:26 -080074 // NetworkConfigReader component is activated and the network configuration
75 // is read.
76 //
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected NetworkConfigService networkConfigService;
79
Jonathan Hart9fd6bfc2014-12-03 15:30:33 -080080 private static final int DEFAULT_BGP_PORT = 2000;
81 private int bgpPort;
82
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080083 private IntentSynchronizer intentSynchronizer;
Jonathan Hart9965d772014-12-02 10:28:34 -080084 private SdnIpConfigurationReader config;
Jonathan Hartce430a42014-10-16 20:44:29 -070085 private PeerConnectivityManager peerConnectivity;
Jonathan Hart41349e92015-02-09 14:14:02 -080086
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080087 private LeadershipEventListener leadershipEventListener =
88 new InnerLeadershipEventListener();
Pavlin Radoslavovc91eebe2014-11-25 18:45:46 -080089 private ApplicationId appId;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080090 private ControllerNode localControllerNode;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080091
Jonathan Hart039d2b12014-10-10 09:33:04 -070092 @Activate
Jonathan Hart9fd6bfc2014-12-03 15:30:33 -080093 protected void activate(ComponentContext context) {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -080094 log.info("SDN-IP started");
Jonathan Hart9fd6bfc2014-12-03 15:30:33 -080095 readComponentConfiguration(context);
Jonathan Hartbac07a02014-10-13 21:29:54 -070096
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -080097 appId = coreService.registerApplication(SDN_IP_APP);
Jonathan Hart9965d772014-12-02 10:28:34 -080098 config = new SdnIpConfigurationReader();
99 config.readConfiguration();
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700100
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800101 localControllerNode = clusterService.getLocalNode();
102
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800103 InterfaceService interfaceService =
104 new HostToInterfaceAdaptor(hostService);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700105
Jonathan Hart552e31f2015-02-06 11:11:59 -0800106 intentSynchronizer = new IntentSynchronizer(appId, intentService,
107 config, interfaceService);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800108 intentSynchronizer.start();
Jonathan Hart31582d12014-10-22 13:52:41 -0700109
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800110 peerConnectivity = new PeerConnectivityManager(appId,
111 intentSynchronizer,
112 config,
113 interfaceService);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700114 peerConnectivity.start();
115
Jonathan Hart41349e92015-02-09 14:14:02 -0800116 routingService.start(intentSynchronizer);
Jonathan Hart335ef462014-10-16 08:20:46 -0700117
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800118 leadershipService.addListener(leadershipEventListener);
119 leadershipService.runForLeadership(appId.name());
Jonathan Hartec2df012014-10-23 16:40:24 -0700120
Jonathan Hart9fd6bfc2014-12-03 15:30:33 -0800121 log.info("Starting BGP with port {}", bgpPort);
Jonathan Hart41349e92015-02-09 14:14:02 -0800122 // TODO feed port information through to the BgpService
Jonathan Hart039d2b12014-10-10 09:33:04 -0700123 }
124
125 @Deactivate
126 protected void deactivate() {
Jonathan Hart41349e92015-02-09 14:14:02 -0800127 routingService.stop();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800128 peerConnectivity.stop();
129 intentSynchronizer.stop();
Jonathan Hart739c8352014-10-29 17:49:26 -0700130
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800131 leadershipService.withdraw(appId.name());
132 leadershipService.removeListener(leadershipEventListener);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800133
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800134 log.info("SDN-IP Stopped");
Jonathan Hart039d2b12014-10-10 09:33:04 -0700135 }
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700136
Jonathan Hart9fd6bfc2014-12-03 15:30:33 -0800137 /**
138 * Extracts properties from the component configuration context.
139 *
140 * @param context the component context
141 */
142 private void readComponentConfiguration(ComponentContext context) {
143 Dictionary<?, ?> properties = context.getProperties();
144 try {
145 String strPort = (String) properties.get("bgpPort");
146 if (strPort != null) {
147 bgpPort = Integer.parseInt(strPort);
148 } else {
149 bgpPort = DEFAULT_BGP_PORT;
150 }
151 } catch (Exception e) {
152 bgpPort = DEFAULT_BGP_PORT;
153 }
154 log.debug("BGP port is set to {}", bgpPort);
155 }
156
157 @Modified
158 public void modified(ComponentContext context) {
159 // Blank @Modified method to catch modifications to the context.
160 // If no @Modified method exists, it seems @Activate is called again
161 // when the context is modified.
162 }
163
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700164 @Override
Jonathan Hartec2df012014-10-23 16:40:24 -0700165 public void modifyPrimary(boolean isPrimary) {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800166 intentSynchronizer.leaderChanged(isPrimary);
Jonathan Hartec2df012014-10-23 16:40:24 -0700167 }
168
Jonathan Hart51372182014-12-03 21:32:34 -0800169 /**
170 * Converts DPIDs of the form xx:xx:xx:xx:xx:xx:xx to OpenFlow provider
171 * device URIs.
172 *
173 * @param dpid the DPID string to convert
174 * @return the URI string for this device
175 */
Jonathan Hartce430a42014-10-16 20:44:29 -0700176 static String dpidToUri(String dpid) {
177 return "of:" + dpid.replace(":", "");
178 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800179
180 /**
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800181 * A listener for Leadership Events.
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800182 */
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800183 private class InnerLeadershipEventListener
184 implements LeadershipEventListener {
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800185
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800186 @Override
187 public void event(LeadershipEvent event) {
188 log.debug("Leadership Event: time = {} type = {} event = {}",
189 event.time(), event.type(), event);
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800190
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800191 if (!event.subject().topic().equals(appId.name())) {
192 return; // Not our topic: ignore
193 }
Madan Jampani8d21c792014-12-01 16:31:07 -0800194 if (!event.subject().leader().equals(
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800195 localControllerNode.id())) {
196 return; // The event is not about this instance: ignore
197 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800198
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800199 switch (event.type()) {
200 case LEADER_ELECTED:
Madan Jampani71582ed2014-11-18 10:06:01 -0800201 log.info("SDN-IP Leader Elected");
202 intentSynchronizer.leaderChanged(true);
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800203 break;
204 case LEADER_BOOTED:
205 log.info("SDN-IP Leader Lost Election");
206 intentSynchronizer.leaderChanged(false);
207 break;
208 case LEADER_REELECTED:
209 break;
210 default:
211 break;
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800212 }
213 }
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800214 }
Jonathan Hart039d2b12014-10-10 09:33:04 -0700215}