blob: 077c6ca5092fd5fdd2a83a7caa9391c71c87b114 [file] [log] [blame]
rsahot036620655b2018-02-26 15:01:37 -05001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
17package org.onosproject.dhcprelay;
18
19import org.onlab.packet.Ip4Address;
20import org.onlab.packet.VlanId;
21
22import org.onlab.packet.Ethernet;
23
24import org.onlab.util.HexString;
25import org.onosproject.dhcprelay.api.DhcpServerInfo;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.host.InterfaceIpAddress;
28import org.onosproject.net.intf.Interface;
29
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32import java.util.Set;
33
34public class Dhcp4HandlerUtil {
35
36
37 private final Logger log = LoggerFactory.getLogger(getClass());
38
39 /**
40 * Returns the first v4 interface ip out of a set of interfaces or null.
41 *
42 * @param intfs set of interfaces
43 * @return Ip4Address / null if not present
44 */
45 public Ip4Address getRelayAgentIPv4Address(Set<Interface> intfs) {
46 for (Interface intf : intfs) {
47 for (InterfaceIpAddress ip : intf.ipAddressesList()) {
48 Ip4Address relayAgentIp = ip.ipAddress().getIp4Address();
49 if (relayAgentIp != null) {
50 return relayAgentIp;
51 }
52 }
53 }
54 return null;
55 }
56
57 /**
58 * Determind if an Interface contains a vlan id.
59 *
60 * @param iface the Interface
61 * @param vlanId the vlan id
62 * @return true if the Interface contains the vlan id
63 */
64 public boolean interfaceContainsVlan(Interface iface, VlanId vlanId) {
65 if (vlanId.equals(VlanId.NONE)) {
66 // untagged packet, check if vlan untagged or vlan native is not NONE
67 return !iface.vlanUntagged().equals(VlanId.NONE) ||
68 !iface.vlanNative().equals(VlanId.NONE);
69 }
70 // tagged packet, check if the interface contains the vlan
71 return iface.vlanTagged().contains(vlanId);
72 }
73
74 /**
75 * Check if a given server info has v6 ipaddress.
76 *
77 * @param serverInfo server info to check
78 * @return true if server info has v6 ip address; false otherwise
79 */
80 public boolean isServerIpEmpty(DhcpServerInfo serverInfo) {
81 if (!serverInfo.getDhcpServerIp4().isPresent()) {
82 log.warn("DhcpServerIp not available, use default DhcpServerIp {}",
83 HexString.toHexString(serverInfo.getDhcpServerIp4().get().toOctets()));
84 return true;
85 }
86 return false;
87 }
88
89
90 /**
91 * the new class the contains Ethernet packet and destination port.
92 */
93 public class InternalPacket {
94 Ethernet packet;
95 ConnectPoint destLocation;
96 public InternalPacket(Ethernet newPacket, ConnectPoint newLocation) {
97 packet = newPacket;
98 destLocation = newLocation;
99 }
100 }
101
102
103}
104