blob: 80846541262b52d053904cd5537e0e59a2c3a224 [file] [log] [blame]
samanwita palf28207b2015-09-04 10:41:56 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
16package org.onosproject.dhcp.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onlab.packet.ARP;
26import org.onlab.packet.DHCP;
27import org.onlab.packet.DHCPOption;
samanwita pal8969cbe2015-09-04 13:31:30 -070028import org.onlab.packet.DHCPPacketType;
samanwita palf28207b2015-09-04 10:41:56 -070029import org.onlab.packet.Ethernet;
30import org.onlab.packet.IPv4;
31import org.onlab.packet.Ip4Address;
32import org.onlab.packet.IpAddress;
33import org.onlab.packet.MacAddress;
34import org.onlab.packet.TpPort;
35import org.onlab.packet.UDP;
36import org.onlab.packet.VlanId;
37import org.onosproject.core.ApplicationId;
38import org.onosproject.core.CoreService;
39import org.onosproject.dhcp.DHCPService;
40import org.onosproject.dhcp.DHCPStore;
41import org.onosproject.net.config.ConfigFactory;
42import org.onosproject.net.config.NetworkConfigEvent;
43import org.onosproject.net.config.NetworkConfigListener;
44import org.onosproject.net.config.NetworkConfigRegistry;
45
46import org.onosproject.net.ConnectPoint;
47import org.onosproject.net.Host;
48import org.onosproject.net.HostId;
49import org.onosproject.net.HostLocation;
50import org.onosproject.net.flow.DefaultTrafficSelector;
51import org.onosproject.net.flow.DefaultTrafficTreatment;
52import org.onosproject.net.flow.TrafficSelector;
53import org.onosproject.net.flow.TrafficTreatment;
54import org.onosproject.net.host.DefaultHostDescription;
55import org.onosproject.net.host.HostProvider;
56import org.onosproject.net.host.HostProviderRegistry;
57import org.onosproject.net.host.HostProviderService;
58import org.onosproject.net.packet.DefaultOutboundPacket;
59import org.onosproject.net.packet.PacketContext;
60import org.onosproject.net.packet.PacketPriority;
61import org.onosproject.net.packet.PacketProcessor;
62import org.onosproject.net.packet.PacketService;
63import org.onosproject.net.provider.AbstractProvider;
64import org.onosproject.net.provider.ProviderId;
65import org.slf4j.Logger;
66import org.slf4j.LoggerFactory;
67
68import java.nio.ByteBuffer;
69import java.util.ArrayList;
70import java.util.HashSet;
71import java.util.List;
72import java.util.Map;
73import java.util.Set;
74
75import static org.onlab.packet.MacAddress.valueOf;
76import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
77
78/**
79 * Skeletal ONOS DHCP Server application.
80 */
81@Component(immediate = true)
82@Service
83public class DHCPManager implements DHCPService {
84
85 private static final ProviderId PID = new ProviderId("of", "org.onosproject.dhcp", true);
86 private final Logger log = LoggerFactory.getLogger(getClass());
87
88 private final NetworkConfigListener cfgListener = new InternalConfigListener();
89
90 private final Set<ConfigFactory> factories = ImmutableSet.of(
91 new ConfigFactory<ApplicationId, DHCPConfig>(APP_SUBJECT_FACTORY,
92 DHCPConfig.class,
93 "dhcp") {
94 @Override
95 public DHCPConfig createConfig() {
96 return new DHCPConfig();
97 }
98 },
99 new ConfigFactory<ApplicationId, DHCPStoreConfig>(APP_SUBJECT_FACTORY,
100 DHCPStoreConfig.class,
101 "dhcpstore") {
102 @Override
103 public DHCPStoreConfig createConfig() {
104 return new DHCPStoreConfig();
105 }
106 }
107 );
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected NetworkConfigRegistry cfgService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected PacketService packetService;
113
114 private DHCPPacketProcessor processor = new DHCPPacketProcessor();
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
117 protected CoreService coreService;
118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected DHCPStore dhcpStore;
121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected HostProviderRegistry hostProviderRegistry;
124
125 protected HostProviderService hostProviderService;
126
127 private ApplicationId appId;
128
129 // Hardcoded values are default values.
130
131 private static String myIP = "10.0.0.2";
132
133 private static MacAddress myMAC = valueOf("4f:4f:4f:4f:4f:4f");
134
135 /**
136 * leaseTime - 10 mins or 600s.
137 * renewalTime - 5 mins or 300s.
138 * rebindingTime - 6 mins or 360s.
139 */
140
141 private static int leaseTime = 600;
142
143 private static int renewalTime = 300;
144
145 private static int rebindingTime = 360;
146
147 private static byte packetTTL = (byte) 127;
148
149 private static String subnetMask = "255.0.0.0";
150
151 private static String broadcastAddress = "10.255.255.255";
152
153 private static String routerAddress = "10.0.0.2";
154
155 private static String domainServer = "10.0.0.2";
156 private final HostProvider hostProvider = new InternalHostProvider();
157
158 @Activate
159 protected void activate() {
160 // start the dhcp server
161 appId = coreService.registerApplication("org.onosproject.dhcp");
162
163 cfgService.addListener(cfgListener);
164 factories.forEach(cfgService::registerConfigFactory);
165 hostProviderService = hostProviderRegistry.register(hostProvider);
166 packetService.addProcessor(processor, PacketProcessor.observer(1));
167 requestPackets();
168 log.info("Started");
169 }
170
171 @Deactivate
172 protected void deactivate() {
173 cfgService.removeListener(cfgListener);
174 factories.forEach(cfgService::unregisterConfigFactory);
175 packetService.removeProcessor(processor);
176 hostProviderRegistry.unregister(hostProvider);
177 hostProviderService = null;
178 cancelPackets();
179 log.info("Stopped");
180 }
181
182 /**
183 * Request packet in via PacketService.
184 */
185 private void requestPackets() {
186
187 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
188 .matchEthType(Ethernet.TYPE_IPV4)
189 .matchIPProtocol(IPv4.PROTOCOL_UDP)
190 .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT))
191 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT));
192 packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
193
194 selectorServer = DefaultTrafficSelector.builder()
195 .matchEthType(Ethernet.TYPE_ARP);
196 packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
197 }
198
199 /**
200 * Cancel requested packets in via packet service.
201 */
202 private void cancelPackets() {
203 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
204 .matchEthType(Ethernet.TYPE_IPV4)
205 .matchIPProtocol(IPv4.PROTOCOL_UDP)
206 .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT))
207 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT));
208 packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
209
210 selectorServer = DefaultTrafficSelector.builder()
211 .matchEthType(Ethernet.TYPE_ARP);
212 packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
213 }
214
215 @Override
216 public Map<MacAddress, Ip4Address> listMapping() {
217
218 return dhcpStore.listMapping();
219 }
220
221 @Override
222 public int getLeaseTime() {
223 return leaseTime;
224 }
225
226 @Override
227 public int getRenewalTime() {
228 return renewalTime;
229 }
230
231 @Override
232 public int getRebindingTime() {
233 return rebindingTime;
234 }
235
236 @Override
237 public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress) {
238 return dhcpStore.assignStaticIP(macID, ipAddress);
239 }
240
241 @Override
242 public boolean removeStaticMapping(MacAddress macID) {
243 return dhcpStore.removeStaticIP(macID);
244 }
245
246 @Override
247 public Iterable<Ip4Address> getAvailableIPs() {
248 return dhcpStore.getAvailableIPs();
249 }
250
251 private class DHCPPacketProcessor implements PacketProcessor {
252
253 /**
254 * Builds the DHCP Reply packet.
255 *
256 * @param packet the incoming Ethernet frame
257 * @param ipOffered the IP offered by the DHCP Server
258 * @param outgoingMessageType the message type of the outgoing packet
259 * @return the Ethernet reply frame
260 */
261 private Ethernet buildReply(Ethernet packet, String ipOffered, byte outgoingMessageType) {
262 Ip4Address myIPAddress = Ip4Address.valueOf(myIP);
263 Ip4Address ipAddress;
264
265 // Ethernet Frame.
266 Ethernet ethReply = new Ethernet();
267 ethReply.setSourceMACAddress(myMAC);
268 ethReply.setDestinationMACAddress(packet.getSourceMAC());
269 ethReply.setEtherType(Ethernet.TYPE_IPV4);
270 ethReply.setVlanID(packet.getVlanID());
271
272 // IP Packet
273 IPv4 ipv4Packet = (IPv4) packet.getPayload();
274 IPv4 ipv4Reply = new IPv4();
275 ipv4Reply.setSourceAddress(myIPAddress.toInt());
276 ipAddress = Ip4Address.valueOf(ipOffered);
277 ipv4Reply.setDestinationAddress(ipAddress.toInt());
278 ipv4Reply.setTtl(packetTTL);
279
280 // UDP Datagram.
281 UDP udpPacket = (UDP) ipv4Packet.getPayload();
282 UDP udpReply = new UDP();
283 udpReply.setSourcePort((byte) UDP.DHCP_SERVER_PORT);
284 udpReply.setDestinationPort((byte) UDP.DHCP_CLIENT_PORT);
285
286 // DHCP Payload.
287 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
288 DHCP dhcpReply = new DHCP();
289 dhcpReply.setOpCode(DHCP.OPCODE_REPLY);
290
291 ipAddress = Ip4Address.valueOf(ipOffered);
292 dhcpReply.setYourIPAddress(ipAddress.toInt());
293 dhcpReply.setServerIPAddress(myIPAddress.toInt());
294
295 dhcpReply.setTransactionId(dhcpPacket.getTransactionId());
296 dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress());
297 dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET);
298 dhcpReply.setHardwareAddressLength((byte) 6);
299
300 // DHCP Options.
301 DHCPOption option = new DHCPOption();
302 List<DHCPOption> optionList = new ArrayList<>();
303
304 // DHCP Message Type.
305 option.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue());
306 option.setLength((byte) 1);
307 byte[] optionData = {outgoingMessageType};
308 option.setData(optionData);
309 optionList.add(option);
310
311 // DHCP Server Identifier.
312 option = new DHCPOption();
313 option.setCode(DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue());
314 option.setLength((byte) 4);
315 option.setData(myIPAddress.toOctets());
316 optionList.add(option);
317
318 // IP Address Lease Time.
319 option = new DHCPOption();
320 option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue());
321 option.setLength((byte) 4);
322 option.setData(ByteBuffer.allocate(4).putInt(leaseTime).array());
323 optionList.add(option);
324
325 // IP Address Renewal Time.
326 option = new DHCPOption();
327 option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue());
328 option.setLength((byte) 4);
329 option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array());
330 optionList.add(option);
331
332 // IP Address Rebinding Time.
333 option = new DHCPOption();
334 option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue());
335 option.setLength((byte) 4);
336 option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array());
337 optionList.add(option);
338
339 // Subnet Mask.
340 option = new DHCPOption();
341 option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue());
342 option.setLength((byte) 4);
343 ipAddress = Ip4Address.valueOf(subnetMask);
344 option.setData(ipAddress.toOctets());
345 optionList.add(option);
346
347 // Broadcast Address.
348 option = new DHCPOption();
349 option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue());
350 option.setLength((byte) 4);
351 ipAddress = Ip4Address.valueOf(broadcastAddress);
352 option.setData(ipAddress.toOctets());
353 optionList.add(option);
354
355 // Router Address.
356 option = new DHCPOption();
357 option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue());
358 option.setLength((byte) 4);
359 ipAddress = Ip4Address.valueOf(routerAddress);
360 option.setData(ipAddress.toOctets());
361 optionList.add(option);
362
363 // DNS Server Address.
364 option = new DHCPOption();
365 option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue());
366 option.setLength((byte) 4);
367 ipAddress = Ip4Address.valueOf(domainServer);
368 option.setData(ipAddress.toOctets());
369 optionList.add(option);
370
371 // End Option.
372 option = new DHCPOption();
373 option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue());
374 option.setLength((byte) 1);
375 optionList.add(option);
376
377 dhcpReply.setOptions(optionList);
378
379 udpReply.setPayload(dhcpReply);
380 ipv4Reply.setPayload(udpReply);
381 ethReply.setPayload(ipv4Reply);
382
383 return ethReply;
384 }
385
386 /**
387 * Sends the Ethernet reply frame via the Packet Service.
388 *
389 * @param context the context of the incoming frame
390 * @param reply the Ethernet reply frame
391 */
392 private void sendReply(PacketContext context, Ethernet reply) {
393 if (reply != null) {
394 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
395 ConnectPoint sourcePoint = context.inPacket().receivedFrom();
396 builder.setOutput(sourcePoint.port());
397
398 packetService.emit(new DefaultOutboundPacket(sourcePoint.deviceId(),
samanwita pal8969cbe2015-09-04 13:31:30 -0700399 builder.build(), ByteBuffer.wrap(reply.serialize())));
samanwita palf28207b2015-09-04 10:41:56 -0700400 }
401 }
402
403 /**
404 * Processes the DHCP Payload and initiates a reply to the client.
405 *
406 * @param context context of the incoming message
407 * @param dhcpPayload the extracted DHCP payload
408 */
409 private void processDHCPPacket(PacketContext context, DHCP dhcpPayload) {
410
411 Ethernet packet = context.inPacket().parsed();
412 boolean flagIfRequestedIP = false;
413 boolean flagIfServerIP = false;
414 Ip4Address requestedIP = Ip4Address.valueOf("0.0.0.0");
415 Ip4Address serverIP = Ip4Address.valueOf("0.0.0.0");
416
417 if (dhcpPayload != null) {
418
419 // TODO Convert this to enum value.
420 byte incomingPacketType = 0;
421 for (DHCPOption option : dhcpPayload.getOptions()) {
422 if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()) {
423 byte[] data = option.getData();
424 incomingPacketType = data[0];
425 }
426 if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()) {
427 byte[] data = option.getData();
428 requestedIP = Ip4Address.valueOf(data);
429 flagIfRequestedIP = true;
430 }
431 if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue()) {
432 byte[] data = option.getData();
433 serverIP = Ip4Address.valueOf(data);
434 flagIfServerIP = true;
435 }
436 }
437
438 String ipOffered = "";
samanwita pal8969cbe2015-09-04 13:31:30 -0700439 DHCPPacketType outgoingPacketType;
samanwita palf28207b2015-09-04 10:41:56 -0700440 MacAddress clientMAC = new MacAddress(dhcpPayload.getClientHardwareAddress());
441
samanwita pal8969cbe2015-09-04 13:31:30 -0700442 if (incomingPacketType == DHCPPacketType.DHCPDISCOVER.getValue()) {
samanwita palf28207b2015-09-04 10:41:56 -0700443
samanwita pal8969cbe2015-09-04 13:31:30 -0700444 outgoingPacketType = DHCPPacketType.DHCPOFFER;
samanwita palf28207b2015-09-04 10:41:56 -0700445 ipOffered = dhcpStore.suggestIP(clientMAC, requestedIP).toString();
446
samanwita pal8969cbe2015-09-04 13:31:30 -0700447 Ethernet ethReply = buildReply(packet, ipOffered, (byte) outgoingPacketType.getValue());
samanwita palf28207b2015-09-04 10:41:56 -0700448 sendReply(context, ethReply);
449
samanwita pal8969cbe2015-09-04 13:31:30 -0700450 } else if (incomingPacketType == DHCPPacketType.DHCPREQUEST.getValue()) {
samanwita palf28207b2015-09-04 10:41:56 -0700451
samanwita pal8969cbe2015-09-04 13:31:30 -0700452 outgoingPacketType = DHCPPacketType.DHCPACK;
samanwita palf28207b2015-09-04 10:41:56 -0700453
454 if (flagIfServerIP && flagIfRequestedIP) {
455 // SELECTING state
456 if (myIP.equals(serverIP.toString()) &&
457 dhcpStore.assignIP(clientMAC, requestedIP, leaseTime)) {
458
459 Ethernet ethReply = buildReply(packet, requestedIP.toString(),
samanwita pal8969cbe2015-09-04 13:31:30 -0700460 (byte) outgoingPacketType.getValue());
samanwita palf28207b2015-09-04 10:41:56 -0700461 sendReply(context, ethReply);
462 discoverHost(context, requestedIP);
463 }
464 } else if (flagIfRequestedIP) {
465 // INIT-REBOOT state
466 if (dhcpStore.assignIP(clientMAC, requestedIP, leaseTime)) {
467 Ethernet ethReply = buildReply(packet, requestedIP.toString(),
samanwita pal8969cbe2015-09-04 13:31:30 -0700468 (byte) outgoingPacketType.getValue());
samanwita palf28207b2015-09-04 10:41:56 -0700469 sendReply(context, ethReply);
470 discoverHost(context, requestedIP);
471 }
472 } else {
473 // RENEWING and REBINDING state
474 int ciaadr = dhcpPayload.getClientIPAddress();
475 if (ciaadr != 0) {
476 Ip4Address clientIaddr = Ip4Address.valueOf(ciaadr);
477 if (dhcpStore.assignIP(clientMAC, clientIaddr, leaseTime)) {
478 Ethernet ethReply = buildReply(packet, clientIaddr.toString(),
samanwita pal8969cbe2015-09-04 13:31:30 -0700479 (byte) outgoingPacketType.getValue());
samanwita palf28207b2015-09-04 10:41:56 -0700480 sendReply(context, ethReply);
481 discoverHost(context, clientIaddr);
482 }
483 }
484 }
samanwita pal8969cbe2015-09-04 13:31:30 -0700485 } else if (incomingPacketType == DHCPPacketType.DHCPRELEASE.getValue()) {
samanwita palf28207b2015-09-04 10:41:56 -0700486
487 dhcpStore.releaseIP(clientMAC);
488 }
489 }
490 }
491
492 /**
493 * Processes the ARP Payload and initiates a reply to the client.
494 *
495 * @param context context of the incoming message
496 * @param packet the ethernet payload
497 */
498 private void processARPPacket(PacketContext context, Ethernet packet) {
499
500 ARP arpPacket = (ARP) packet.getPayload();
501
502 ARP arpReply = (ARP) arpPacket.clone();
503 arpReply.setOpCode(ARP.OP_REPLY);
504
505 arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress());
506 arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress());
507 arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress());
508 arpReply.setSenderHardwareAddress(myMAC.toBytes());
509
510 // Ethernet Frame.
511 Ethernet ethReply = new Ethernet();
512 ethReply.setSourceMACAddress(myMAC);
513 ethReply.setDestinationMACAddress(packet.getSourceMAC());
514 ethReply.setEtherType(Ethernet.TYPE_ARP);
515 ethReply.setVlanID(packet.getVlanID());
516
517 ethReply.setPayload(arpReply);
518 sendReply(context, ethReply);
519 }
520
521 /**
522 * Integrates hosts learned through DHCP into topology.
523 * @param context context of the incoming message
524 * @param ipAssigned IP Address assigned to the host by DHCP Manager
525 */
526 private void discoverHost(PacketContext context, Ip4Address ipAssigned) {
527 Ethernet packet = context.inPacket().parsed();
528 MacAddress mac = packet.getSourceMAC();
529 VlanId vlanId = VlanId.vlanId(packet.getVlanID());
530 HostLocation hostLocation = new HostLocation(context.inPacket().receivedFrom(), 0);
531
532 Set<IpAddress> ips = new HashSet<>();
533 ips.add(ipAssigned);
534
535 HostId hostId = HostId.hostId(mac, vlanId);
536 DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips);
537 hostProviderService.hostDetected(hostId, desc);
538 }
539
540
541 @Override
542 public void process(PacketContext context) {
543
544 Ethernet packet = context.inPacket().parsed();
545 if (packet == null) {
546 return;
547 }
548
549 if (packet.getEtherType() == Ethernet.TYPE_IPV4) {
550 IPv4 ipv4Packet = (IPv4) packet.getPayload();
551
552 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
553 UDP udpPacket = (UDP) ipv4Packet.getPayload();
554
555 if (udpPacket.getDestinationPort() == UDP.DHCP_SERVER_PORT &&
556 udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT) {
557 // This is meant for the dhcp server so process the packet here.
558
559 DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
560 processDHCPPacket(context, dhcpPayload);
561 }
562 }
563 } else if (packet.getEtherType() == Ethernet.TYPE_ARP) {
564 ARP arpPacket = (ARP) packet.getPayload();
565
566 if ((arpPacket.getOpCode() == ARP.OP_REQUEST) &&
567 (Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()).toString().equals(myIP))) {
568
569 processARPPacket(context, packet);
570
571 }
572 }
573 }
574 }
575
576 private class InternalConfigListener implements NetworkConfigListener {
577
578 /**
579 * Reconfigures the DHCP Server according to the configuration parameters passed.
580 *
581 * @param cfg configuration object
582 */
583 private void reconfigureNetwork(DHCPConfig cfg) {
584
585 if (cfg.ip() != null) {
586 myIP = cfg.ip();
587 }
588 if (cfg.mac() != null) {
589 myMAC = MacAddress.valueOf(cfg.mac());
590 }
591 if (cfg.subnetMask() != null) {
592 subnetMask = cfg.subnetMask();
593 }
594 if (cfg.broadcastAddress() != null) {
595 broadcastAddress = cfg.broadcastAddress();
596 }
597 if (cfg.routerAddress() != null) {
598 routerAddress = cfg.routerAddress();
599 }
600 if (cfg.domainServer() != null) {
601 domainServer = cfg.domainServer();
602 }
603 if (cfg.ttl() != null) {
604 packetTTL = Byte.valueOf(cfg.ttl());
605 }
606 if (cfg.leaseTime() != null) {
607 leaseTime = Integer.valueOf(cfg.leaseTime());
608 }
609 if (cfg.renewTime() != null) {
610 renewalTime = Integer.valueOf(cfg.renewTime());
611 }
612 if (cfg.rebindTime() != null) {
613 rebindingTime = Integer.valueOf(cfg.rebindTime());
614 }
615 }
616
617 /**
618 * Reconfigures the DHCP Store according to the configuration parameters passed.
619 *
620 * @param cfg configuration object
621 */
622 private void reconfigureStore(DHCPStoreConfig cfg) {
623
624 if (cfg.defaultTimeout() != null) {
625 dhcpStore.setDefaultTimeoutForPurge(Integer.valueOf(cfg.defaultTimeout()));
626 }
627 if (cfg.timerDelay() != null) {
628 dhcpStore.setTimerDelay(Integer.valueOf(cfg.defaultTimeout()));
629 }
630 if ((cfg.startIP() != null) && (cfg.endIP() != null)) {
631 dhcpStore.populateIPPoolfromRange(Ip4Address.valueOf(cfg.startIP()),
samanwita pal8969cbe2015-09-04 13:31:30 -0700632 Ip4Address.valueOf(cfg.endIP()));
samanwita palf28207b2015-09-04 10:41:56 -0700633 }
634 }
635
636 @Override
637 public void event(NetworkConfigEvent event) {
638
639 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
640 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)) {
641 if (event.configClass().equals(DHCPConfig.class)) {
642 DHCPConfig cfg = cfgService.getConfig(appId, DHCPConfig.class);
643 reconfigureNetwork(cfg);
644 log.info("Reconfigured Manager");
645 }
646 if (event.configClass().equals(DHCPStoreConfig.class)) {
647 DHCPStoreConfig cfg = cfgService.getConfig(appId, DHCPStoreConfig.class);
648 reconfigureStore(cfg);
649 log.info("Reconfigured Store");
650 }
651 }
652 }
653 }
654
655 private class InternalHostProvider extends AbstractProvider implements HostProvider {
656
657 /**
658 * Creates a provider with the supplier identifier.
659 */
660 protected InternalHostProvider() {
661 super(PID);
662 }
663
664 @Override
665 public void triggerProbe(Host host) {
666 // nothing to do
667 }
668 }
669}