sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * 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 |
| 7 | * |
| 8 | * 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. |
| 15 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IPv4; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 26 | import org.onlab.packet.Ip4Prefix; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 28 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 29 | import org.onlab.util.KryoNamespace; |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 30 | import org.onosproject.cfg.ComponentConfigService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.event.Event; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 34 | import org.onosproject.incubator.net.config.basics.McastConfig; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 35 | import org.onosproject.mastership.MastershipService; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Link; |
| 39 | import org.onosproject.net.Port; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 40 | import org.onosproject.net.PortNumber; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 41 | import org.onosproject.net.config.ConfigFactory; |
| 42 | import org.onosproject.net.config.NetworkConfigEvent; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 43 | import org.onosproject.net.config.NetworkConfigListener; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 44 | import org.onosproject.net.config.NetworkConfigRegistry; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 45 | import org.onosproject.net.config.basics.SubjectFactories; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceEvent; |
| 47 | import org.onosproject.net.device.DeviceListener; |
| 48 | import org.onosproject.net.device.DeviceService; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.TrafficSelector; |
| 51 | import org.onosproject.net.flow.TrafficTreatment; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 53 | import org.onosproject.net.host.HostEvent; |
| 54 | import org.onosproject.net.host.HostListener; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 55 | import org.onosproject.net.mcast.McastEvent; |
| 56 | import org.onosproject.net.mcast.McastListener; |
| 57 | import org.onosproject.net.mcast.MulticastRouteService; |
| 58 | import org.onosproject.net.packet.PacketPriority; |
| 59 | import org.onosproject.net.topology.TopologyService; |
| 60 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 61 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
| 62 | import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig; |
| 63 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
| 64 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
| 65 | import org.onosproject.segmentrouting.grouphandler.NeighborSet; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 66 | import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey; |
| 67 | import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 68 | import org.onosproject.net.host.HostService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 69 | import org.onosproject.net.link.LinkEvent; |
| 70 | import org.onosproject.net.link.LinkListener; |
| 71 | import org.onosproject.net.link.LinkService; |
| 72 | import org.onosproject.net.packet.InboundPacket; |
| 73 | import org.onosproject.net.packet.PacketContext; |
| 74 | import org.onosproject.net.packet.PacketProcessor; |
| 75 | import org.onosproject.net.packet.PacketService; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 76 | import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey; |
| 77 | import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey; |
| 78 | import org.onosproject.segmentrouting.storekey.XConnectNextObjectiveStoreKey; |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 79 | import org.onosproject.store.serializers.KryoNamespaces; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 80 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 81 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 82 | import org.onosproject.store.service.StorageService; |
| 83 | import org.onosproject.store.service.WallClockTimestamp; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 84 | import org.slf4j.Logger; |
| 85 | import org.slf4j.LoggerFactory; |
| 86 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 87 | import java.util.Collections; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 88 | import java.util.HashSet; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 89 | import java.util.List; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 90 | import java.util.Map; |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 91 | import java.util.Optional; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 92 | import java.util.Set; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 93 | import java.util.concurrent.ConcurrentHashMap; |
| 94 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 95 | import java.util.concurrent.Executors; |
| 96 | import java.util.concurrent.ScheduledExecutorService; |
| 97 | import java.util.concurrent.ScheduledFuture; |
| 98 | import java.util.concurrent.TimeUnit; |
| 99 | |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 100 | import static com.google.common.base.Preconditions.checkState; |
| 101 | |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 102 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Segment routing manager. |
| 105 | */ |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 106 | @Service |
| 107 | @Component(immediate = true) |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 108 | public class SegmentRoutingManager implements SegmentRoutingService { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 109 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 110 | private static Logger log = LoggerFactory |
| 111 | .getLogger(SegmentRoutingManager.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 114 | protected CoreService coreService; |
| 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 117 | protected PacketService packetService; |
| 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 120 | protected HostService hostService; |
| 121 | |
| 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 123 | protected DeviceService deviceService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 126 | protected FlowObjectiveService flowObjectiveService; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected LinkService linkService; |
| 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 132 | protected MastershipService mastershipService; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 133 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 135 | protected StorageService storageService; |
| 136 | |
| 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 138 | protected NetworkConfigRegistry cfgService; |
| 139 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 141 | protected ComponentConfigService compCfgService; |
| 142 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 144 | protected MulticastRouteService multicastRouteService; |
| 145 | |
| 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 147 | protected TopologyService topologyService; |
| 148 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 149 | protected ArpHandler arpHandler = null; |
| 150 | protected IcmpHandler icmpHandler = null; |
| 151 | protected IpHandler ipHandler = null; |
| 152 | protected RoutingRulePopulator routingRulePopulator = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 153 | protected ApplicationId appId; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 154 | protected DeviceConfiguration deviceConfiguration = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 155 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 156 | protected DefaultRoutingHandler defaultRoutingHandler = null; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 157 | private TunnelHandler tunnelHandler = null; |
| 158 | private PolicyHandler policyHandler = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 159 | private InternalPacketProcessor processor = null; |
| 160 | private InternalLinkListener linkListener = null; |
| 161 | private InternalDeviceListener deviceListener = null; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 162 | private NetworkConfigEventHandler netcfgHandler = null; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 163 | private McastHandler mcastHandler = null; |
| 164 | private HostHandler hostHandler = null; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 165 | private InternalEventHandler eventHandler = new InternalEventHandler(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 166 | private final InternalHostListener hostListener = new InternalHostListener(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 167 | private final InternalConfigListener cfgListener = new InternalConfigListener(this); |
| 168 | private final InternalMcastListener mcastListener = new InternalMcastListener(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 169 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 170 | private ScheduledExecutorService executorService = Executors |
| 171 | .newScheduledThreadPool(1); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 172 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 173 | @SuppressWarnings("unused") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 174 | private static ScheduledFuture<?> eventHandlerFuture = null; |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 175 | @SuppressWarnings("rawtypes") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 176 | private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<Event>(); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 177 | private Map<DeviceId, DefaultGroupHandler> groupHandlerMap = |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 178 | new ConcurrentHashMap<>(); |
| 179 | /** |
| 180 | * Per device next objective ID store with (device id + neighbor set) as key. |
| 181 | */ |
| 182 | public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer> |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 183 | nsNextObjStore = null; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 184 | /** |
| 185 | * Per device next objective ID store with (device id + subnet) as key. |
| 186 | */ |
| 187 | public EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer> |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 188 | subnetNextObjStore = null; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 189 | /** |
| 190 | * Per device next objective ID store with (device id + port) as key. |
| 191 | */ |
| 192 | public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 193 | portNextObjStore = null; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 194 | /** |
| 195 | * Per cross-connect objective ID store with VLAN ID as key. |
| 196 | */ |
| 197 | public EventuallyConsistentMap<XConnectNextObjectiveStoreKey, Integer> |
| 198 | xConnectNextObjStore = null; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 199 | // Per device, per-subnet assigned-vlans store, with (device id + subnet |
| 200 | // IPv4 prefix) as key |
| 201 | private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId> |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 202 | subnetVidStore = null; |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 203 | private EventuallyConsistentMap<String, Tunnel> tunnelStore = null; |
| 204 | private EventuallyConsistentMap<String, Policy> policyStore = null; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 205 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 206 | private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory = |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 207 | new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(SubjectFactories.DEVICE_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 208 | SegmentRoutingDeviceConfig.class, "segmentrouting") { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 209 | @Override |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 210 | public SegmentRoutingDeviceConfig createConfig() { |
| 211 | return new SegmentRoutingDeviceConfig(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 212 | } |
| 213 | }; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 214 | private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory = |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 215 | new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(SubjectFactories.APP_SUBJECT_FACTORY, |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 216 | SegmentRoutingAppConfig.class, "segmentrouting") { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 217 | @Override |
| 218 | public SegmentRoutingAppConfig createConfig() { |
| 219 | return new SegmentRoutingAppConfig(); |
| 220 | } |
| 221 | }; |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 222 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 223 | private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory = |
| 224 | new ConfigFactory<ApplicationId, McastConfig>(SubjectFactories.APP_SUBJECT_FACTORY, |
| 225 | McastConfig.class, "multicast") { |
| 226 | @Override |
| 227 | public McastConfig createConfig() { |
| 228 | return new McastConfig(); |
| 229 | } |
| 230 | }; |
| 231 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 232 | private Object threadSchedulerLock = new Object(); |
| 233 | private static int numOfEventsQueued = 0; |
| 234 | private static int numOfEventsExecuted = 0; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 235 | private static int numOfHandlerExecution = 0; |
| 236 | private static int numOfHandlerScheduled = 0; |
| 237 | |
Charles Chan | 1963f4f | 2016-02-18 14:22:42 -0800 | [diff] [blame] | 238 | /** |
| 239 | * Segment Routing App ID. |
| 240 | */ |
| 241 | public static final String SR_APP_ID = "org.onosproject.segmentrouting"; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 242 | /** |
| 243 | * The starting value of per-subnet VLAN ID assignment. |
| 244 | */ |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 245 | private static final short ASSIGNED_VLAN_START = 4093; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 246 | /** |
| 247 | * The default VLAN ID assigned to the interfaces without subnet config. |
| 248 | */ |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 249 | public static final short ASSIGNED_VLAN_NO_SUBNET = 4094; |
| 250 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 251 | @Activate |
| 252 | protected void activate() { |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 253 | appId = coreService.registerApplication(SR_APP_ID); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 254 | |
| 255 | log.debug("Creating EC map nsnextobjectivestore"); |
| 256 | EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer> |
| 257 | nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 258 | nsNextObjStore = nsNextObjMapBuilder |
| 259 | .withName("nsnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 260 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 261 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 262 | .build(); |
| 263 | log.trace("Current size {}", nsNextObjStore.size()); |
| 264 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 265 | log.debug("Creating EC map subnetnextobjectivestore"); |
| 266 | EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer> |
| 267 | subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 268 | subnetNextObjStore = subnetNextObjMapBuilder |
| 269 | .withName("subnetnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 270 | .withSerializer(createSerializer()) |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 271 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 272 | .build(); |
| 273 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 274 | log.debug("Creating EC map subnetnextobjectivestore"); |
| 275 | EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer> |
| 276 | portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 277 | portNextObjStore = portNextObjMapBuilder |
| 278 | .withName("portnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 279 | .withSerializer(createSerializer()) |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 280 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 281 | .build(); |
| 282 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 283 | log.debug("Creating EC map xconnectnextobjectivestore"); |
| 284 | EventuallyConsistentMapBuilder<XConnectNextObjectiveStoreKey, Integer> |
| 285 | xConnectNextObjStoreBuilder = storageService.eventuallyConsistentMapBuilder(); |
| 286 | xConnectNextObjStore = xConnectNextObjStoreBuilder |
| 287 | .withName("xconnectnextobjectivestore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 288 | .withSerializer(createSerializer()) |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 289 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 290 | .build(); |
| 291 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 292 | EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder = |
| 293 | storageService.eventuallyConsistentMapBuilder(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 294 | tunnelStore = tunnelMapBuilder |
| 295 | .withName("tunnelstore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 296 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 297 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 298 | .build(); |
| 299 | |
| 300 | EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder = |
| 301 | storageService.eventuallyConsistentMapBuilder(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 302 | policyStore = policyMapBuilder |
| 303 | .withName("policystore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 304 | .withSerializer(createSerializer()) |
Madan Jampani | 675ae20 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 305 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 306 | .build(); |
| 307 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 308 | EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId> |
| 309 | subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder(); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 310 | subnetVidStore = subnetVidStoreMapBuilder |
| 311 | .withName("subnetvidstore") |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 312 | .withSerializer(createSerializer()) |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 313 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 314 | .build(); |
| 315 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 316 | compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager", |
| 317 | "purgeOnDisconnection", "true"); |
| 318 | compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager", |
| 319 | "purgeOnDisconnection", "true"); |
| 320 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 321 | processor = new InternalPacketProcessor(); |
| 322 | linkListener = new InternalLinkListener(); |
| 323 | deviceListener = new InternalDeviceListener(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 324 | netcfgHandler = new NetworkConfigEventHandler(this); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 325 | mcastHandler = new McastHandler(this); |
| 326 | hostHandler = new HostHandler(this); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 327 | |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 328 | cfgService.addListener(cfgListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 329 | cfgService.registerConfigFactory(deviceConfigFactory); |
| 330 | cfgService.registerConfigFactory(appConfigFactory); |
| 331 | cfgService.registerConfigFactory(mcastConfigFactory); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 332 | hostService.addListener(hostListener); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 333 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 334 | linkService.addListener(linkListener); |
| 335 | deviceService.addListener(deviceListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 336 | multicastRouteService.addListener(mcastListener); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 337 | |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 338 | // Request ARP packet-in |
| 339 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 340 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 341 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId, Optional.empty()); |
| 342 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 343 | cfgListener.configureNetwork(); |
| 344 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 345 | log.info("Started"); |
| 346 | } |
| 347 | |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 348 | private KryoNamespace.Builder createSerializer() { |
| 349 | return new KryoNamespace.Builder() |
| 350 | .register(KryoNamespaces.API) |
| 351 | .register(NeighborSetNextObjectiveStoreKey.class, |
| 352 | SubnetNextObjectiveStoreKey.class, |
| 353 | SubnetAssignedVidStoreKey.class, |
| 354 | NeighborSet.class, |
| 355 | Tunnel.class, |
| 356 | DefaultTunnel.class, |
| 357 | Policy.class, |
| 358 | TunnelPolicy.class, |
| 359 | Policy.Type.class, |
| 360 | PortNextObjectiveStoreKey.class, |
| 361 | XConnectNextObjectiveStoreKey.class |
| 362 | ); |
| 363 | } |
| 364 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 365 | @Deactivate |
| 366 | protected void deactivate() { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 367 | cfgService.removeListener(cfgListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 368 | cfgService.unregisterConfigFactory(deviceConfigFactory); |
| 369 | cfgService.unregisterConfigFactory(appConfigFactory); |
| 370 | cfgService.unregisterConfigFactory(mcastConfigFactory); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 371 | |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 372 | // Withdraw ARP packet-in |
| 373 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 374 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 375 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId, Optional.empty()); |
| 376 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 377 | packetService.removeProcessor(processor); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 378 | linkService.removeListener(linkListener); |
| 379 | deviceService.removeListener(deviceListener); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 380 | multicastRouteService.removeListener(mcastListener); |
| 381 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 382 | processor = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 383 | linkListener = null; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 384 | deviceListener = null; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 385 | groupHandlerMap.clear(); |
| 386 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 387 | nsNextObjStore.destroy(); |
| 388 | subnetNextObjStore.destroy(); |
| 389 | portNextObjStore.destroy(); |
| 390 | xConnectNextObjStore.destroy(); |
| 391 | tunnelStore.destroy(); |
| 392 | policyStore.destroy(); |
| 393 | subnetVidStore.destroy(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 394 | log.info("Stopped"); |
| 395 | } |
| 396 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 397 | |
| 398 | @Override |
| 399 | public List<Tunnel> getTunnels() { |
| 400 | return tunnelHandler.getTunnels(); |
| 401 | } |
| 402 | |
| 403 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 404 | public TunnelHandler.Result createTunnel(Tunnel tunnel) { |
| 405 | return tunnelHandler.createTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 409 | public TunnelHandler.Result removeTunnel(Tunnel tunnel) { |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 410 | for (Policy policy: policyHandler.getPolicies()) { |
| 411 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 412 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
| 413 | if (tunnelPolicy.tunnelId().equals(tunnel.id())) { |
| 414 | log.warn("Cannot remove the tunnel used by a policy"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 415 | return TunnelHandler.Result.TUNNEL_IN_USE; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 419 | return tunnelHandler.removeTunnel(tunnel); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 423 | public PolicyHandler.Result removePolicy(Policy policy) { |
| 424 | return policyHandler.removePolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 428 | public PolicyHandler.Result createPolicy(Policy policy) { |
| 429 | return policyHandler.createPolicy(policy); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public List<Policy> getPolicies() { |
| 434 | return policyHandler.getPolicies(); |
| 435 | } |
| 436 | |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 437 | @Override |
| 438 | public void rerouteNetwork() { |
| 439 | cfgListener.configureNetwork(); |
| 440 | for (Device device : deviceService.getDevices()) { |
| 441 | defaultRoutingHandler.populatePortAddressingRules(device.id()); |
| 442 | } |
| 443 | defaultRoutingHandler.startPopulationProcess(); |
| 444 | } |
| 445 | |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 446 | /** |
| 447 | * Returns the tunnel object with the tunnel ID. |
| 448 | * |
| 449 | * @param tunnelId Tunnel ID |
| 450 | * @return Tunnel reference |
| 451 | */ |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 452 | public Tunnel getTunnel(String tunnelId) { |
| 453 | return tunnelHandler.getTunnel(tunnelId); |
| 454 | } |
| 455 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 456 | /** |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 457 | * Returns the vlan-id assigned to the subnet configured for a device. |
| 458 | * If no vlan-id has been assigned, a new one is assigned out of a pool of ids, |
| 459 | * if and only if this controller instance is the master for the device. |
| 460 | * <p> |
| 461 | * USAGE: The assigned vlans are meant to be applied to untagged packets on those |
| 462 | * switches/pipelines that need this functionality. These vids are meant |
| 463 | * to be used internally within a switch, and thus need to be unique only |
| 464 | * on a switch level. Note that packets never go out on the wire with these |
| 465 | * vlans. Currently, vlan ids are assigned from value 4093 down. |
| 466 | * Vlan id 4094 expected to be used for all ports that are not assigned subnets. |
| 467 | * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned |
| 468 | * per subnet. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 469 | * |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 470 | * @param deviceId switch dpid |
| 471 | * @param subnet IPv4 prefix for which assigned vlan is desired |
| 472 | * @return VlanId assigned for the subnet on the device, or |
| 473 | * null if no vlan assignment was found and this instance is not |
| 474 | * the master for the device. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 475 | */ |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 476 | // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection. |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 477 | public VlanId getSubnetAssignedVlanId(DeviceId deviceId, Ip4Prefix subnet) { |
| 478 | VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey( |
| 479 | deviceId, subnet)); |
| 480 | if (assignedVid != null) { |
| 481 | log.debug("Query for subnet:{} on device:{} returned assigned-vlan " |
| 482 | + "{}", subnet, deviceId, assignedVid); |
| 483 | return assignedVid; |
| 484 | } |
| 485 | //check mastership for the right to assign a vlan |
| 486 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 487 | log.warn("This controller instance is not the master for device {}. " |
| 488 | + "Cannot assign vlan-id for subnet {}", deviceId, subnet); |
| 489 | return null; |
| 490 | } |
| 491 | // vlan assignment is expensive but done only once |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 492 | Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 493 | Set<Short> assignedVlans = new HashSet<>(); |
| 494 | Set<Ip4Prefix> unassignedSubnets = new HashSet<>(); |
| 495 | for (Ip4Prefix sub : configuredSubnets) { |
| 496 | VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, |
| 497 | sub)); |
| 498 | if (v != null) { |
| 499 | assignedVlans.add(v.toShort()); |
| 500 | } else { |
| 501 | unassignedSubnets.add(sub); |
| 502 | } |
| 503 | } |
| 504 | short nextAssignedVlan = ASSIGNED_VLAN_START; |
| 505 | if (!assignedVlans.isEmpty()) { |
| 506 | nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1); |
| 507 | } |
| 508 | for (Ip4Prefix unsub : unassignedSubnets) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 509 | // Special case for default route. Assign default VLAN ID to /32 and /0 subnets |
| 510 | if (unsub.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH || |
| 511 | unsub.prefixLength() == 0) { |
| 512 | subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub), |
| 513 | VlanId.vlanId(ASSIGNED_VLAN_NO_SUBNET)); |
| 514 | } else { |
| 515 | subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub), |
| 516 | VlanId.vlanId(nextAssignedVlan--)); |
| 517 | log.info("Assigned vlan: {} to subnet: {} on device: {}", |
| 518 | nextAssignedVlan + 1, unsub, deviceId); |
| 519 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 522 | return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 523 | } |
| 524 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 525 | /** |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 526 | * Returns the next objective ID for the given NeighborSet. |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 527 | * If the nextObjective does not exist, a new one is created and |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 528 | * its id is returned. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 529 | * |
sangho | 80f1f89 | 2015-05-19 11:57:42 -0700 | [diff] [blame] | 530 | * @param deviceId Device ID |
| 531 | * @param ns NegighborSet |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 532 | * @param meta metadata passed into the creation of a Next Objective |
| 533 | * @return next objective ID or -1 if an error was encountered during the |
| 534 | * creation of the nextObjective |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 535 | */ |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 536 | public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns, |
| 537 | TrafficSelector meta) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 538 | if (groupHandlerMap.get(deviceId) != null) { |
| 539 | log.trace("getNextObjectiveId query in device {}", deviceId); |
| 540 | return groupHandlerMap |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 541 | .get(deviceId).getNextObjectiveId(ns, meta); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 542 | } else { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 543 | log.warn("getNextObjectiveId query - groupHandler for device {} " |
| 544 | + "not found", deviceId); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 545 | return -1; |
| 546 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 549 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 550 | * Returns the next objective ID for the given subnet prefix. It is expected |
| 551 | * that the next-objective has been pre-created from configuration. |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 552 | * |
| 553 | * @param deviceId Device ID |
| 554 | * @param prefix Subnet |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 555 | * @return next objective ID or -1 if it was not found |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 556 | */ |
| 557 | public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) { |
| 558 | if (groupHandlerMap.get(deviceId) != null) { |
| 559 | log.trace("getSubnetNextObjectiveId query in device {}", deviceId); |
| 560 | return groupHandlerMap |
| 561 | .get(deviceId).getSubnetNextObjectiveId(prefix); |
| 562 | } else { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 563 | log.warn("getSubnetNextObjectiveId query - groupHandler for " |
| 564 | + "device {} not found", deviceId); |
| 565 | return -1; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Returns the next objective ID for the given portNumber, given the treatment. |
| 571 | * There could be multiple different treatments to the same outport, which |
| 572 | * would result in different objectives. If the next object |
| 573 | * does not exist, a new one is created and its id is returned. |
| 574 | * |
| 575 | * @param deviceId Device ID |
| 576 | * @param portNum port number on device for which NextObjective is queried |
| 577 | * @param treatment the actions to apply on the packets (should include outport) |
| 578 | * @param meta metadata passed into the creation of a Next Objective if necessary |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 579 | * @return next objective ID or -1 if an error occurred during retrieval or creation |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 580 | */ |
| 581 | public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum, |
| 582 | TrafficTreatment treatment, |
| 583 | TrafficSelector meta) { |
| 584 | DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId); |
| 585 | if (ghdlr != null) { |
| 586 | return ghdlr.getPortNextObjectiveId(portNum, treatment, meta); |
| 587 | } else { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 588 | log.warn("getPortNextObjectiveId query - groupHandler for device {}" |
| 589 | + " not found", deviceId); |
| 590 | return -1; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Returns the next objective ID of type broadcast associated with the VLAN |
| 596 | * cross-connection. |
| 597 | * |
| 598 | * @param deviceId Device ID for the cross-connection |
| 599 | * @param vlanId VLAN ID for the cross-connection |
| 600 | * @return next objective ID or -1 if it was not found |
| 601 | */ |
| 602 | public int getXConnectNextObjectiveId(DeviceId deviceId, VlanId vlanId) { |
| 603 | DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId); |
| 604 | if (ghdlr != null) { |
| 605 | return ghdlr.getXConnectNextObjectiveId(vlanId); |
| 606 | } else { |
| 607 | log.warn("getPortNextObjectiveId query - groupHandler for device {}" |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 608 | + " not found", deviceId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 609 | return -1; |
| 610 | } |
| 611 | } |
| 612 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 613 | private class InternalPacketProcessor implements PacketProcessor { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 614 | @Override |
| 615 | public void process(PacketContext context) { |
| 616 | |
| 617 | if (context.isHandled()) { |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | InboundPacket pkt = context.inPacket(); |
| 622 | Ethernet ethernet = pkt.parsed(); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 623 | log.trace("Rcvd pktin: {}", ethernet); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 624 | if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
| 625 | arpHandler.processPacketIn(pkt); |
| 626 | } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) { |
| 627 | IPv4 ipPacket = (IPv4) ethernet.getPayload(); |
| 628 | ipHandler.addToPacketBuffer(ipPacket); |
| 629 | if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 630 | icmpHandler.processPacketIn(pkt); |
| 631 | } else { |
| 632 | ipHandler.processPacketIn(pkt); |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | private class InternalLinkListener implements LinkListener { |
| 639 | @Override |
| 640 | public void event(LinkEvent event) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 641 | if (event.type() == LinkEvent.Type.LINK_ADDED |
| 642 | || event.type() == LinkEvent.Type.LINK_REMOVED) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 643 | log.debug("Event {} received from Link Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 644 | scheduleEventHandlerIfNotScheduled(event); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | private class InternalDeviceListener implements DeviceListener { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 650 | @Override |
| 651 | public void event(DeviceEvent event) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 652 | switch (event.type()) { |
| 653 | case DEVICE_ADDED: |
| 654 | case PORT_REMOVED: |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 655 | case DEVICE_UPDATED: |
| 656 | case DEVICE_AVAILABILITY_CHANGED: |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 657 | log.debug("Event {} received from Device Service", event.type()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 658 | scheduleEventHandlerIfNotScheduled(event); |
| 659 | break; |
| 660 | default: |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 665 | @SuppressWarnings("rawtypes") |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 666 | private void scheduleEventHandlerIfNotScheduled(Event event) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 667 | synchronized (threadSchedulerLock) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 668 | eventQueue.add(event); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 669 | numOfEventsQueued++; |
| 670 | |
| 671 | if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) { |
| 672 | //No pending scheduled event handling threads. So start a new one. |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 673 | eventHandlerFuture = executorService |
| 674 | .schedule(eventHandler, 100, TimeUnit.MILLISECONDS); |
| 675 | numOfHandlerScheduled++; |
| 676 | } |
Jonathan Hart | 54541d1 | 2016-04-12 15:39:44 -0700 | [diff] [blame] | 677 | log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}", |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 678 | numOfEventsQueued, |
| 679 | numOfHandlerScheduled); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 680 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | private class InternalEventHandler implements Runnable { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 684 | @Override |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 685 | public void run() { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 686 | try { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 687 | while (true) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 688 | @SuppressWarnings("rawtypes") |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 689 | Event event = null; |
| 690 | synchronized (threadSchedulerLock) { |
| 691 | if (!eventQueue.isEmpty()) { |
| 692 | event = eventQueue.poll(); |
| 693 | numOfEventsExecuted++; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 694 | } else { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 695 | numOfHandlerExecution++; |
| 696 | log.debug("numOfHandlerExecution {} numOfEventsExecuted {}", |
| 697 | numOfHandlerExecution, numOfEventsExecuted); |
| 698 | break; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 699 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 700 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 701 | if (event.type() == LinkEvent.Type.LINK_ADDED) { |
| 702 | processLinkAdded((Link) event.subject()); |
| 703 | } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { |
| 704 | processLinkRemoved((Link) event.subject()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 705 | } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 706 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 707 | event.type() == DeviceEvent.Type.DEVICE_UPDATED) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 708 | DeviceId deviceId = ((Device) event.subject()).id(); |
| 709 | if (deviceService.isAvailable(deviceId)) { |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 710 | log.info("Processing device event {} for available device {}", |
| 711 | event.type(), ((Device) event.subject()).id()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 712 | processDeviceAdded((Device) event.subject()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 713 | } else { |
| 714 | log.info("Processing device event {} for unavailable device {}", |
| 715 | event.type(), ((Device) event.subject()).id()); |
| 716 | processDeviceRemoved((Device) event.subject()); |
| 717 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 718 | } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) { |
| 719 | processPortRemoved((Device) event.subject(), |
| 720 | ((DeviceEvent) event).port()); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame^] | 721 | } else if (event.type() == DeviceEvent.Type.PORT_ADDED || |
| 722 | event.type() == DeviceEvent.Type.PORT_UPDATED) { |
| 723 | log.info("** PORT ADDED OR UPDATED {}/{} -> {}", |
| 724 | (Device) event.subject(), |
| 725 | ((DeviceEvent) event).port(), |
| 726 | event.type()); |
| 727 | /* XXX create method for single port filtering rules |
| 728 | if (defaultRoutingHandler != null) { |
| 729 | defaultRoutingHandler.populatePortAddressingRules( |
| 730 | ((Device) event.subject()).id()); |
| 731 | }*/ |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 732 | } else { |
| 733 | log.warn("Unhandled event type: {}", event.type()); |
| 734 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 735 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 736 | } catch (Exception e) { |
| 737 | log.error("SegmentRouting event handler " |
| 738 | + "thread thrown an exception: {}", e); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 739 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 743 | private void processLinkAdded(Link link) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame^] | 744 | log.info("** LINK ADDED {}", link.toString()); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 745 | if (!deviceConfiguration.isConfigured(link.src().deviceId())) { |
| 746 | log.warn("Source device of this link is not configured."); |
| 747 | return; |
| 748 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 749 | //Irrespective whether the local is a MASTER or not for this device, |
| 750 | //create group handler instance and push default TTP flow rules. |
| 751 | //Because in a multi-instance setup, instances can initiate |
| 752 | //groups for any devices. Also the default TTP rules are needed |
| 753 | //to be pushed before inserting any IP table entries for any device |
| 754 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src() |
| 755 | .deviceId()); |
| 756 | if (groupHandler != null) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 757 | groupHandler.linkUp(link, mastershipService.isLocalMaster( |
| 758 | link.src().deviceId())); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 759 | } else { |
| 760 | Device device = deviceService.getDevice(link.src().deviceId()); |
| 761 | if (device != null) { |
| 762 | log.warn("processLinkAdded: Link Added " |
| 763 | + "Notification without Device Added " |
| 764 | + "event, still handling it"); |
| 765 | processDeviceAdded(device); |
| 766 | groupHandler = groupHandlerMap.get(link.src() |
| 767 | .deviceId()); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 768 | groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id())); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 771 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 772 | log.trace("Starting optimized route population process"); |
| 773 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null); |
| 774 | //log.trace("processLinkAdded: re-starting route population process"); |
| 775 | //defaultRoutingHandler.startPopulationProcess(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 776 | |
| 777 | mcastHandler.init(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | private void processLinkRemoved(Link link) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame^] | 781 | log.info("** LINK REMOVED {}", link.toString()); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 782 | DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId()); |
| 783 | if (groupHandler != null) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 784 | groupHandler.portDown(link.src().port(), |
| 785 | mastershipService.isLocalMaster(link.src().deviceId())); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 786 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 787 | log.trace("Starting optimized route population process"); |
| 788 | defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link); |
| 789 | //log.trace("processLinkRemoved: re-starting route population process"); |
| 790 | //defaultRoutingHandler.startPopulationProcess(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 791 | |
| 792 | mcastHandler.processLinkDown(link); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | private void processDeviceAdded(Device device) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame^] | 796 | log.info("** DEVICE ADDED with ID {}", device.id()); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 797 | if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) { |
Saurav Das | 8ec0ec4 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 798 | log.warn("Device configuration uploading. Device {} will be " |
| 799 | + "processed after config completes.", device.id()); |
| 800 | return; |
| 801 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 802 | processDeviceAddedInternal(device.id()); |
| 803 | } |
| 804 | |
| 805 | private void processDeviceAddedInternal(DeviceId deviceId) { |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 806 | // Irrespective of whether the local is a MASTER or not for this device, |
| 807 | // we need to create a SR-group-handler instance. This is because in a |
| 808 | // multi-instance setup, any instance can initiate forwarding/next-objectives |
| 809 | // for any switch (even if this instance is a SLAVE or not even connected |
| 810 | // to the switch). To handle this, a default-group-handler instance is necessary |
| 811 | // per switch. |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 812 | log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet()); |
| 813 | if (groupHandlerMap.get(deviceId) == null) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 814 | DefaultGroupHandler groupHandler; |
| 815 | try { |
| 816 | groupHandler = DefaultGroupHandler. |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 817 | createGroupHandler(deviceId, |
| 818 | appId, |
| 819 | deviceConfiguration, |
| 820 | linkService, |
| 821 | flowObjectiveService, |
| 822 | this); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 823 | } catch (DeviceConfigNotFoundException e) { |
| 824 | log.warn(e.getMessage() + " Aborting processDeviceAdded."); |
| 825 | return; |
| 826 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 827 | log.debug("updating groupHandlerMap with new config for device: {}", |
| 828 | deviceId); |
| 829 | groupHandlerMap.put(deviceId, groupHandler); |
Saurav Das | 8ec0ec4 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 830 | } |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame^] | 831 | // Also, in some cases, drivers may need extra |
| 832 | // information to process rules (eg. Router IP/MAC); and so, we send |
| 833 | // port addressing rules to the driver as well irrespective of whether |
| 834 | // this instance is the master or not. |
| 835 | defaultRoutingHandler.populatePortAddressingRules(deviceId); |
| 836 | |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 837 | if (mastershipService.isLocalMaster(deviceId)) { |
| 838 | hostHandler.readInitialHosts(deviceId); |
| 839 | DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 840 | groupHandler.createGroupsFromSubnetConfig(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 841 | routingRulePopulator.populateSubnetBroadcastRule(deviceId); |
| 842 | groupHandler.createGroupsForXConnect(deviceId); |
| 843 | routingRulePopulator.populateXConnectBroadcastRule(deviceId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 844 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 845 | |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 846 | netcfgHandler.initVRouters(deviceId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 849 | private void processDeviceRemoved(Device device) { |
| 850 | nsNextObjStore.entrySet().stream() |
| 851 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 852 | .forEach(entry -> { |
| 853 | nsNextObjStore.remove(entry.getKey()); |
| 854 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 855 | subnetNextObjStore.entrySet().stream() |
| 856 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 857 | .forEach(entry -> { |
| 858 | subnetNextObjStore.remove(entry.getKey()); |
| 859 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 860 | portNextObjStore.entrySet().stream() |
| 861 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 862 | .forEach(entry -> { |
| 863 | portNextObjStore.remove(entry.getKey()); |
| 864 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 865 | xConnectNextObjStore.entrySet().stream() |
| 866 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 867 | .forEach(entry -> { |
| 868 | xConnectNextObjStore.remove(entry.getKey()); |
| 869 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 870 | subnetVidStore.entrySet().stream() |
| 871 | .filter(entry -> entry.getKey().deviceId().equals(device.id())) |
| 872 | .forEach(entry -> { |
| 873 | subnetVidStore.remove(entry.getKey()); |
| 874 | }); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 875 | groupHandlerMap.remove(device.id()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 876 | defaultRoutingHandler.purgeEcmpGraph(device.id()); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 877 | mcastHandler.removeDevice(device.id()); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 878 | } |
| 879 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 880 | private void processPortRemoved(Device device, Port port) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame^] | 881 | log.info("Port {} was removed", port.toString()); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 882 | DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id()); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 883 | if (groupHandler != null) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 884 | groupHandler.portDown(port.number(), |
| 885 | mastershipService.isLocalMaster(device.id())); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 888 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 889 | private class InternalConfigListener implements NetworkConfigListener { |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 890 | SegmentRoutingManager segmentRoutingManager; |
| 891 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 892 | /** |
| 893 | * Constructs the internal network config listener. |
| 894 | * |
| 895 | * @param srMgr segment routing manager |
| 896 | */ |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 897 | public InternalConfigListener(SegmentRoutingManager srMgr) { |
| 898 | this.segmentRoutingManager = srMgr; |
| 899 | } |
| 900 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 901 | /** |
| 902 | * Reads network config and initializes related data structure accordingly. |
| 903 | */ |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 904 | public void configureNetwork() { |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 905 | deviceConfiguration = new DeviceConfiguration(appId, |
| 906 | segmentRoutingManager.cfgService); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 907 | |
| 908 | arpHandler = new ArpHandler(segmentRoutingManager); |
| 909 | icmpHandler = new IcmpHandler(segmentRoutingManager); |
| 910 | ipHandler = new IpHandler(segmentRoutingManager); |
| 911 | routingRulePopulator = new RoutingRulePopulator(segmentRoutingManager); |
| 912 | defaultRoutingHandler = new DefaultRoutingHandler(segmentRoutingManager); |
| 913 | |
| 914 | tunnelHandler = new TunnelHandler(linkService, deviceConfiguration, |
| 915 | groupHandlerMap, tunnelStore); |
| 916 | policyHandler = new PolicyHandler(appId, deviceConfiguration, |
| 917 | flowObjectiveService, |
| 918 | tunnelHandler, policyStore); |
| 919 | |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 920 | for (Device device : deviceService.getDevices()) { |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 921 | processDeviceAddedInternal(device.id()); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | defaultRoutingHandler.startPopulationProcess(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 925 | mcastHandler.init(); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 928 | @Override |
| 929 | public void event(NetworkConfigEvent event) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 930 | // TODO move this part to NetworkConfigEventHandler |
| 931 | if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) { |
| 932 | switch (event.type()) { |
| 933 | case CONFIG_ADDED: |
| 934 | log.info("Segment Routing Config added."); |
| 935 | configureNetwork(); |
| 936 | break; |
| 937 | case CONFIG_UPDATED: |
| 938 | log.info("Segment Routing Config updated."); |
| 939 | // TODO support dynamic configuration |
| 940 | break; |
| 941 | default: |
| 942 | break; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 943 | } |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 944 | } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) { |
Charles Chan | d6d2533 | 2016-02-26 22:19:52 -0800 | [diff] [blame] | 945 | checkState(netcfgHandler != null, "NetworkConfigEventHandler is not initialized"); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 946 | switch (event.type()) { |
| 947 | case CONFIG_ADDED: |
| 948 | netcfgHandler.processVRouterConfigAdded(event); |
| 949 | break; |
| 950 | case CONFIG_UPDATED: |
| 951 | netcfgHandler.processVRouterConfigUpdated(event); |
| 952 | break; |
| 953 | case CONFIG_REMOVED: |
| 954 | netcfgHandler.processVRouterConfigRemoved(event); |
| 955 | break; |
| 956 | default: |
| 957 | break; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 958 | } |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | } |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 962 | |
| 963 | private class InternalHostListener implements HostListener { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 964 | @Override |
| 965 | public void event(HostEvent event) { |
| 966 | // Do not proceed without mastership |
| 967 | DeviceId deviceId = event.subject().location().deviceId(); |
| 968 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | switch (event.type()) { |
| 973 | case HOST_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 974 | hostHandler.processHostAddedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 975 | break; |
| 976 | case HOST_MOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 977 | hostHandler.processHostMovedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 978 | break; |
| 979 | case HOST_REMOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 980 | hostHandler.processHostRemoveEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 981 | break; |
| 982 | case HOST_UPDATED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 983 | hostHandler.processHostUpdatedEvent(event); |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 984 | break; |
| 985 | default: |
| 986 | log.warn("Unsupported host event type: {}", event.type()); |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 992 | private class InternalMcastListener implements McastListener { |
| 993 | @Override |
| 994 | public void event(McastEvent event) { |
| 995 | switch (event.type()) { |
| 996 | case SOURCE_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 997 | mcastHandler.processSourceAdded(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 998 | break; |
| 999 | case SINK_ADDED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1000 | mcastHandler.processSinkAdded(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1001 | break; |
| 1002 | case SINK_REMOVED: |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1003 | mcastHandler.processSinkRemoved(event); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1004 | break; |
| 1005 | case ROUTE_ADDED: |
| 1006 | case ROUTE_REMOVED: |
| 1007 | default: |
| 1008 | break; |
| 1009 | } |
| 1010 | } |
| 1011 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1012 | } |