blob: 2e9818cdb1189bfeb8a58b52fc358d63b3071e49 [file] [log] [blame]
samanwita pal18696f62015-07-17 13:15:43 -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 */
samanwita palf66ed8a2015-07-21 15:45:33 -070016package org.onosproject.dhcpserver.impl;
samanwita pal18696f62015-07-17 13:15:43 -070017
samanwita pal775f6192015-07-28 10:10:13 -070018import com.google.common.collect.ImmutableSet;
samanwita pal18696f62015-07-17 13:15:43 -070019import 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;
samanwita palf66ed8a2015-07-21 15:45:33 -070024import org.apache.felix.scr.annotations.Service;
samanwita palb7900e52015-07-30 11:47:14 -070025import org.onlab.packet.ARP;
samanwita pal18696f62015-07-17 13:15:43 -070026import org.onlab.packet.DHCP;
27import org.onlab.packet.DHCPOption;
28import org.onlab.packet.Ethernet;
29import org.onlab.packet.IPv4;
30import org.onlab.packet.Ip4Address;
31import org.onlab.packet.MacAddress;
32import org.onlab.packet.UDP;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
samanwita palf66ed8a2015-07-21 15:45:33 -070035import org.onosproject.dhcpserver.DHCPService;
36import org.onosproject.dhcpserver.DHCPStore;
samanwita pal775f6192015-07-28 10:10:13 -070037import org.onosproject.incubator.net.config.ConfigFactory;
38import org.onosproject.incubator.net.config.NetworkConfigEvent;
39import org.onosproject.incubator.net.config.NetworkConfigListener;
40import org.onosproject.incubator.net.config.NetworkConfigRegistry;
samanwita pal18696f62015-07-17 13:15:43 -070041import org.onosproject.net.ConnectPoint;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
46import org.onosproject.net.packet.DefaultOutboundPacket;
47import org.onosproject.net.packet.PacketContext;
48import org.onosproject.net.packet.PacketPriority;
49import org.onosproject.net.packet.PacketProcessor;
50import org.onosproject.net.packet.PacketService;
51import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
54import java.nio.ByteBuffer;
55import java.util.ArrayList;
56import java.util.List;
samanwita palf66ed8a2015-07-21 15:45:33 -070057import java.util.Map;
samanwita pal775f6192015-07-28 10:10:13 -070058import java.util.Set;
samanwita pal18696f62015-07-17 13:15:43 -070059
60import static org.onlab.packet.MacAddress.valueOf;
samanwita pal775f6192015-07-28 10:10:13 -070061import static org.onosproject.incubator.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
samanwita pal18696f62015-07-17 13:15:43 -070062
63/**
64 * Skeletal ONOS DHCP Server application.
65 */
66@Component(immediate = true)
samanwita palf66ed8a2015-07-21 15:45:33 -070067@Service
68public class DHCPManager implements DHCPService {
samanwita pal18696f62015-07-17 13:15:43 -070069
70 private final Logger log = LoggerFactory.getLogger(getClass());
71
samanwita pal775f6192015-07-28 10:10:13 -070072 private final NetworkConfigListener cfgListener = new InternalConfigListener();
73
74 private final Set<ConfigFactory> factories = ImmutableSet.of(
75 new ConfigFactory<ApplicationId, DHCPConfig>(APP_SUBJECT_FACTORY,
76 DHCPConfig.class,
77 "dhcp") {
78 @Override
79 public DHCPConfig createConfig() {
80 return new DHCPConfig();
81 }
82 },
83 new ConfigFactory<ApplicationId, DHCPStoreConfig>(APP_SUBJECT_FACTORY,
84 DHCPStoreConfig.class,
85 "dhcpstore") {
86 @Override
87 public DHCPStoreConfig createConfig() {
88 return new DHCPStoreConfig();
89 }
90 }
91 );
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected NetworkConfigRegistry cfgService;
94
samanwita pal18696f62015-07-17 13:15:43 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected PacketService packetService;
97
98 private DHCPPacketProcessor processor = new DHCPPacketProcessor();
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected CoreService coreService;
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected DHCPStore dhcpStore;
105
106 private ApplicationId appId;
107
samanwita pal775f6192015-07-28 10:10:13 -0700108 // Hardcoded values are default values.
samanwita pal18696f62015-07-17 13:15:43 -0700109
samanwita pal775f6192015-07-28 10:10:13 -0700110 private static String myIP = "10.0.0.2";
samanwita pal18696f62015-07-17 13:15:43 -0700111
samanwita pal775f6192015-07-28 10:10:13 -0700112 private static MacAddress myMAC = valueOf("4f:4f:4f:4f:4f:4f");
samanwita pal18696f62015-07-17 13:15:43 -0700113
114 /**
115 * leaseTime - 10 mins or 600s.
116 * renewalTime - 5 mins or 300s.
117 * rebindingTime - 6 mins or 360s.
118 */
119
120 private static int leaseTime = 600;
121
122 private static int renewalTime = 300;
123
124 private static int rebindingTime = 360;
125
126 private static byte packetTTL = (byte) 127;
127
128 private static String subnetMask = "255.0.0.0";
129
130 private static String broadcastAddress = "10.255.255.255";
131
samanwita pal792159a2015-07-28 14:07:01 -0700132 private static String routerAddress = "10.0.0.2";
133
134 private static String domainServer = "10.0.0.2";
135
samanwita pal18696f62015-07-17 13:15:43 -0700136 @Activate
137 protected void activate() {
138 // start the dhcp server
139 appId = coreService.registerApplication("org.onosproject.dhcpserver");
140
samanwita pal775f6192015-07-28 10:10:13 -0700141 cfgService.addListener(cfgListener);
142 factories.forEach(cfgService::registerConfigFactory);
samanwita palb7900e52015-07-30 11:47:14 -0700143 packetService.addProcessor(processor, PacketProcessor.observer(1));
samanwita pal18696f62015-07-17 13:15:43 -0700144 requestPackets();
145 log.info("Started");
146 }
147
148 @Deactivate
149 protected void deactivate() {
samanwita pal775f6192015-07-28 10:10:13 -0700150 cfgService.removeListener(cfgListener);
151 factories.forEach(cfgService::unregisterConfigFactory);
samanwita pal18696f62015-07-17 13:15:43 -0700152 packetService.removeProcessor(processor);
samanwita palb7900e52015-07-30 11:47:14 -0700153 cancelPackets();
samanwita pal18696f62015-07-17 13:15:43 -0700154 log.info("Stopped");
155 }
156
157 /**
158 * Request packet in via PacketService.
159 */
160 private void requestPackets() {
161
162 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
163 .matchEthType(Ethernet.TYPE_IPV4)
164 .matchIPProtocol(IPv4.PROTOCOL_UDP)
165 .matchUdpDst(UDP.DHCP_SERVER_PORT)
166 .matchUdpSrc(UDP.DHCP_CLIENT_PORT);
samanwita palb7900e52015-07-30 11:47:14 -0700167 packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
samanwita pal18696f62015-07-17 13:15:43 -0700168
samanwita palb7900e52015-07-30 11:47:14 -0700169 selectorServer = DefaultTrafficSelector.builder()
170 .matchEthType(Ethernet.TYPE_ARP);
171 packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
172 }
173
174 /**
175 * Cancel requested packets in via packet service.
176 */
177 private void cancelPackets() {
178 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
179 .matchEthType(Ethernet.TYPE_IPV4)
180 .matchIPProtocol(IPv4.PROTOCOL_UDP)
181 .matchUdpDst(UDP.DHCP_SERVER_PORT)
182 .matchUdpSrc(UDP.DHCP_CLIENT_PORT);
183 packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
184
185 selectorServer = DefaultTrafficSelector.builder()
186 .matchEthType(Ethernet.TYPE_ARP);
187 packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
samanwita pal18696f62015-07-17 13:15:43 -0700188 }
189
samanwita palf66ed8a2015-07-21 15:45:33 -0700190 @Override
191 public Map<MacAddress, Ip4Address> listMapping() {
192
193 return dhcpStore.listMapping();
194 }
195
196 @Override
197 public int getLeaseTime() {
198 return leaseTime;
199 }
200
201 @Override
202 public int getRenewalTime() {
203 return renewalTime;
204 }
205
206 @Override
207 public int getRebindingTime() {
208 return rebindingTime;
209 }
210
211 @Override
212 public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress) {
213 return dhcpStore.assignStaticIP(macID, ipAddress);
214 }
215
216 @Override
217 public boolean removeStaticMapping(MacAddress macID) {
218 return dhcpStore.removeStaticIP(macID);
219 }
220
221 @Override
222 public Iterable<Ip4Address> getAvailableIPs() {
223 return dhcpStore.getAvailableIPs();
224 }
225
samanwita pal18696f62015-07-17 13:15:43 -0700226 private class DHCPPacketProcessor implements PacketProcessor {
227
228 /**
229 * Builds the DHCP Reply packet.
230 *
231 * @param packet the incoming Ethernet frame
232 * @param ipOffered the IP offered by the DHCP Server
233 * @param outgoingMessageType the message type of the outgoing packet
234 * @return the Ethernet reply frame
235 */
236 private Ethernet buildReply(Ethernet packet, String ipOffered, byte outgoingMessageType) {
samanwita pal775f6192015-07-28 10:10:13 -0700237 Ip4Address myIPAddress = Ip4Address.valueOf(myIP);
samanwita pal18696f62015-07-17 13:15:43 -0700238 Ip4Address ipAddress;
239
240 // Ethernet Frame.
241 Ethernet ethReply = new Ethernet();
samanwita pal775f6192015-07-28 10:10:13 -0700242 ethReply.setSourceMACAddress(myMAC);
samanwita pal18696f62015-07-17 13:15:43 -0700243 ethReply.setDestinationMACAddress(packet.getSourceMAC());
244 ethReply.setEtherType(Ethernet.TYPE_IPV4);
245 ethReply.setVlanID(packet.getVlanID());
246
247 // IP Packet
248 IPv4 ipv4Packet = (IPv4) packet.getPayload();
249 IPv4 ipv4Reply = new IPv4();
250 ipv4Reply.setSourceAddress(myIPAddress.toInt());
251 ipAddress = Ip4Address.valueOf(ipOffered);
252 ipv4Reply.setDestinationAddress(ipAddress.toInt());
253 ipv4Reply.setTtl(packetTTL);
254
255 // UDP Datagram.
256 UDP udpPacket = (UDP) ipv4Packet.getPayload();
257 UDP udpReply = new UDP();
258 udpReply.setSourcePort((byte) UDP.DHCP_SERVER_PORT);
259 udpReply.setDestinationPort((byte) UDP.DHCP_CLIENT_PORT);
260
261 // DHCP Payload.
262 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
263 DHCP dhcpReply = new DHCP();
264 dhcpReply.setOpCode(DHCP.OPCODE_REPLY);
265
266 ipAddress = Ip4Address.valueOf(ipOffered);
267 dhcpReply.setYourIPAddress(ipAddress.toInt());
268 dhcpReply.setServerIPAddress(myIPAddress.toInt());
269
270 dhcpReply.setTransactionId(dhcpPacket.getTransactionId());
271 dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress());
272 dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET);
273 dhcpReply.setHardwareAddressLength((byte) 6);
274
275 // DHCP Options.
276 DHCPOption option = new DHCPOption();
277 List<DHCPOption> optionList = new ArrayList<>();
278
279 // DHCP Message Type.
280 option.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue());
281 option.setLength((byte) 1);
282 byte[] optionData = {outgoingMessageType};
283 option.setData(optionData);
284 optionList.add(option);
285
286 // DHCP Server Identifier.
287 option = new DHCPOption();
288 option.setCode(DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue());
289 option.setLength((byte) 4);
290 option.setData(myIPAddress.toOctets());
291 optionList.add(option);
292
293 // IP Address Lease Time.
294 option = new DHCPOption();
295 option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue());
296 option.setLength((byte) 4);
297 option.setData(ByteBuffer.allocate(4).putInt(leaseTime).array());
298 optionList.add(option);
299
300 // IP Address Renewal Time.
301 option = new DHCPOption();
302 option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue());
303 option.setLength((byte) 4);
304 option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array());
305 optionList.add(option);
306
307 // IP Address Rebinding Time.
308 option = new DHCPOption();
309 option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue());
310 option.setLength((byte) 4);
311 option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array());
312 optionList.add(option);
313
314 // Subnet Mask.
315 option = new DHCPOption();
316 option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue());
317 option.setLength((byte) 4);
318 ipAddress = Ip4Address.valueOf(subnetMask);
319 option.setData(ipAddress.toOctets());
320 optionList.add(option);
321
322 // Broadcast Address.
323 option = new DHCPOption();
324 option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue());
325 option.setLength((byte) 4);
326 ipAddress = Ip4Address.valueOf(broadcastAddress);
327 option.setData(ipAddress.toOctets());
328 optionList.add(option);
329
330 // Router Address.
331 option = new DHCPOption();
332 option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue());
333 option.setLength((byte) 4);
samanwita pal792159a2015-07-28 14:07:01 -0700334 ipAddress = Ip4Address.valueOf(routerAddress);
335 option.setData(ipAddress.toOctets());
samanwita pal18696f62015-07-17 13:15:43 -0700336 optionList.add(option);
337
338 // DNS Server Address.
339 option = new DHCPOption();
340 option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue());
341 option.setLength((byte) 4);
samanwita pal792159a2015-07-28 14:07:01 -0700342 ipAddress = Ip4Address.valueOf(domainServer);
343 option.setData(ipAddress.toOctets());
samanwita pal18696f62015-07-17 13:15:43 -0700344 optionList.add(option);
345
346 // End Option.
347 option = new DHCPOption();
348 option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue());
349 option.setLength((byte) 1);
350 optionList.add(option);
351
352 dhcpReply.setOptions(optionList);
353
354 udpReply.setPayload(dhcpReply);
355 ipv4Reply.setPayload(udpReply);
356 ethReply.setPayload(ipv4Reply);
357
358 return ethReply;
359 }
360
361 /**
362 * Sends the Ethernet reply frame via the Packet Service.
363 *
364 * @param context the context of the incoming frame
365 * @param reply the Ethernet reply frame
366 */
367 private void sendReply(PacketContext context, Ethernet reply) {
368 if (reply != null) {
369 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
370 ConnectPoint sourcePoint = context.inPacket().receivedFrom();
371 builder.setOutput(sourcePoint.port());
372
373 packetService.emit(new DefaultOutboundPacket(sourcePoint.deviceId(),
374 builder.build(), ByteBuffer.wrap(reply.serialize())));
375 }
376 }
377
378 /**
379 * Processes the DHCP Payload and initiates a reply to the client.
380 *
381 * @param context context of the incoming message
382 * @param dhcpPayload the extracted DHCP payload
383 */
384 private void processDHCPPacket(PacketContext context, DHCP dhcpPayload) {
385
386 Ethernet packet = context.inPacket().parsed();
387 boolean flagIfRequestedIP = false;
388 boolean flagIfServerIP = false;
389 Ip4Address requestedIP = Ip4Address.valueOf("0.0.0.0");
390 Ip4Address serverIP = Ip4Address.valueOf("0.0.0.0");
391
392 if (dhcpPayload != null) {
393
394 // TODO Convert this to enum value.
395 byte incomingPacketType = 0;
396 for (DHCPOption option : dhcpPayload.getOptions()) {
397 if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()) {
398 byte[] data = option.getData();
399 incomingPacketType = data[0];
400 }
401 if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()) {
402 byte[] data = option.getData();
403 requestedIP = Ip4Address.valueOf(data);
404 flagIfRequestedIP = true;
405 }
406 if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue()) {
407 byte[] data = option.getData();
408 serverIP = Ip4Address.valueOf(data);
409 flagIfServerIP = true;
410 }
411 }
412
413 String ipOffered = "";
414 DHCP.DHCPMessageType outgoingPacketType;
415 MacAddress clientMAC = new MacAddress(dhcpPayload.getClientHardwareAddress());
416
417 if (incomingPacketType == DHCP.DHCPMessageType.MessageType_Discover.getValue()) {
418
419 outgoingPacketType = DHCP.DHCPMessageType.MessageType_Offer;
samanwita palf09c09e2015-07-22 16:06:42 -0700420 ipOffered = dhcpStore.suggestIP(clientMAC, requestedIP).toString();
samanwita pal18696f62015-07-17 13:15:43 -0700421
422 Ethernet ethReply = buildReply(packet, ipOffered, outgoingPacketType.getValue());
423 sendReply(context, ethReply);
424
425 } else if (incomingPacketType == DHCP.DHCPMessageType.MessageType_Request.getValue()) {
426
427 outgoingPacketType = DHCP.DHCPMessageType.MessageType_ACK;
428
429 if (flagIfServerIP && flagIfRequestedIP) {
430 // SELECTING state
samanwita pal775f6192015-07-28 10:10:13 -0700431 if (myIP.equals(serverIP.toString()) &&
samanwita pal18696f62015-07-17 13:15:43 -0700432 dhcpStore.assignIP(clientMAC, requestedIP, leaseTime)) {
433
434 Ethernet ethReply = buildReply(packet, requestedIP.toString(),
435 outgoingPacketType.getValue());
436 sendReply(context, ethReply);
437 }
438 } else if (flagIfRequestedIP) {
439 // INIT-REBOOT state
440 if (dhcpStore.assignIP(clientMAC, requestedIP, leaseTime)) {
441 Ethernet ethReply = buildReply(packet, requestedIP.toString(),
442 outgoingPacketType.getValue());
443 sendReply(context, ethReply);
444 }
445 } else {
446 // RENEWING and REBINDING state
447 int ciaadr = dhcpPayload.getClientIPAddress();
448 if (ciaadr != 0) {
449 Ip4Address clientIaddr = Ip4Address.valueOf(ciaadr);
450 if (dhcpStore.assignIP(clientMAC, clientIaddr, leaseTime)) {
451 Ethernet ethReply = buildReply(packet, clientIaddr.toString(),
452 outgoingPacketType.getValue());
453 sendReply(context, ethReply);
454 }
455 }
456 }
457 } else if (incomingPacketType == DHCP.DHCPMessageType.MessageType_Release.getValue()) {
458
459 dhcpStore.releaseIP(clientMAC);
460 }
461 }
462 }
463
samanwita palb7900e52015-07-30 11:47:14 -0700464 /**
465 * Processes the ARP Payload and initiates a reply to the client.
466 *
467 * @param context context of the incoming message
468 * @param packet the ethernet payload
469 */
470 private void processARPPacket(PacketContext context, Ethernet packet) {
471
472 ARP arpPacket = (ARP) packet.getPayload();
473
474 ARP arpReply = (ARP) arpPacket.clone();
475 arpReply.setOpCode(ARP.OP_REPLY);
476
477 arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress());
478 arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress());
479 arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress());
480 arpReply.setSenderHardwareAddress(myMAC.toBytes());
481
482 // Ethernet Frame.
483 Ethernet ethReply = new Ethernet();
484 ethReply.setSourceMACAddress(myMAC);
485 ethReply.setDestinationMACAddress(packet.getSourceMAC());
486 ethReply.setEtherType(Ethernet.TYPE_ARP);
487 ethReply.setVlanID(packet.getVlanID());
488
489 ethReply.setPayload(arpReply);
490 sendReply(context, ethReply);
491 }
492
493
samanwita pal18696f62015-07-17 13:15:43 -0700494 @Override
495 public void process(PacketContext context) {
496
samanwita pal18696f62015-07-17 13:15:43 -0700497 Ethernet packet = context.inPacket().parsed();
498 if (packet == null) {
499 return;
500 }
501
502 if (packet.getEtherType() == Ethernet.TYPE_IPV4) {
503 IPv4 ipv4Packet = (IPv4) packet.getPayload();
504
505 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
506 UDP udpPacket = (UDP) ipv4Packet.getPayload();
507
508 if (udpPacket.getDestinationPort() == UDP.DHCP_SERVER_PORT &&
509 udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT) {
510 // This is meant for the dhcp server so process the packet here.
511
512 DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
513 processDHCPPacket(context, dhcpPayload);
514 }
515 }
samanwita palb7900e52015-07-30 11:47:14 -0700516 } else if (packet.getEtherType() == Ethernet.TYPE_ARP) {
517 ARP arpPacket = (ARP) packet.getPayload();
518
519 if ((arpPacket.getOpCode() == ARP.OP_REQUEST) &&
520 (Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()).toString().equals(myIP))) {
521
522 processARPPacket(context, packet);
523
524 }
samanwita pal18696f62015-07-17 13:15:43 -0700525 }
526 }
527 }
samanwita pal775f6192015-07-28 10:10:13 -0700528
529 private class InternalConfigListener implements NetworkConfigListener {
530
531 /**
532 * Reconfigures the DHCP Server according to the configuration parameters passed.
533 *
534 * @param cfg configuration object
535 */
536 private void reconfigureNetwork(DHCPConfig cfg) {
537
538 if (cfg.ip() != null) {
539 myIP = cfg.ip();
540 }
541 if (cfg.mac() != null) {
542 myMAC = MacAddress.valueOf(cfg.mac());
543 }
544 if (cfg.subnetMask() != null) {
545 subnetMask = cfg.subnetMask();
546 }
547 if (cfg.broadcastAddress() != null) {
548 broadcastAddress = cfg.broadcastAddress();
549 }
samanwita pal792159a2015-07-28 14:07:01 -0700550 if (cfg.routerAddress() != null) {
551 routerAddress = cfg.routerAddress();
552 }
553 if (cfg.domainServer() != null) {
554 domainServer = cfg.domainServer();
555 }
samanwita pal775f6192015-07-28 10:10:13 -0700556 if (cfg.ttl() != null) {
557 packetTTL = Byte.valueOf(cfg.ttl());
558 }
559 if (cfg.leaseTime() != null) {
560 leaseTime = Integer.valueOf(cfg.leaseTime());
561 }
562 if (cfg.renewTime() != null) {
563 renewalTime = Integer.valueOf(cfg.renewTime());
564 }
565 if (cfg.rebindTime() != null) {
566 rebindingTime = Integer.valueOf(cfg.rebindTime());
567 }
568 }
569
570 /**
571 * Reconfigures the DHCP Store according to the configuration parameters passed.
572 *
573 * @param cfg configuration object
574 */
575 private void reconfigureStore(DHCPStoreConfig cfg) {
576
577 if (cfg.defaultTimeout() != null) {
578 dhcpStore.setDefaultTimeoutForPurge(Integer.valueOf(cfg.defaultTimeout()));
579 }
580 if (cfg.timerDelay() != null) {
581 dhcpStore.setTimerDelay(Integer.valueOf(cfg.defaultTimeout()));
582 }
583 if ((cfg.startIP() != null) && (cfg.endIP() != null)) {
samanwita palb7900e52015-07-30 11:47:14 -0700584 dhcpStore.populateIPPoolfromRange(Ip4Address.valueOf(cfg.startIP()),
585 Ip4Address.valueOf(cfg.endIP()));
samanwita pal775f6192015-07-28 10:10:13 -0700586 }
587 }
588
589 @Override
590 public void event(NetworkConfigEvent event) {
591
592 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
593 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)) {
594 if (event.configClass().equals(DHCPConfig.class)) {
595 DHCPConfig cfg = cfgService.getConfig(appId, DHCPConfig.class);
596 reconfigureNetwork(cfg);
597 log.info("Reconfigured Manager");
598 }
599 if (event.configClass().equals(DHCPStoreConfig.class)) {
600 DHCPStoreConfig cfg = cfgService.getConfig(appId, DHCPStoreConfig.class);
601 reconfigureStore(cfg);
602 log.info("Reconfigured Store");
603 }
samanwita pal775f6192015-07-28 10:10:13 -0700604 }
samanwita pal775f6192015-07-28 10:10:13 -0700605 }
606 }
samanwita pal18696f62015-07-17 13:15:43 -0700607}