rsahot036 | 620655b | 2018-02-26 15:01:37 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 17 | package org.onosproject.dhcprelay; |
| 18 | |
| 19 | import org.onlab.packet.Ip4Address; |
| 20 | import org.onlab.packet.VlanId; |
rsahot036 | 620655b | 2018-02-26 15:01:37 -0500 | [diff] [blame] | 21 | import org.onlab.util.HexString; |
| 22 | import org.onosproject.dhcprelay.api.DhcpServerInfo; |
rsahot036 | 620655b | 2018-02-26 15:01:37 -0500 | [diff] [blame] | 23 | import org.onosproject.net.host.InterfaceIpAddress; |
| 24 | import org.onosproject.net.intf.Interface; |
| 25 | |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | import java.util.Set; |
| 29 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 30 | public final class Dhcp4HandlerUtil { |
| 31 | private static final Logger log = LoggerFactory.getLogger(Dhcp4HandlerUtil.class); |
rsahot036 | 620655b | 2018-02-26 15:01:37 -0500 | [diff] [blame] | 32 | |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 33 | private Dhcp4HandlerUtil() { |
| 34 | } |
rsahot036 | 620655b | 2018-02-26 15:01:37 -0500 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Returns the first v4 interface ip out of a set of interfaces or null. |
| 38 | * |
| 39 | * @param intfs set of interfaces |
| 40 | * @return Ip4Address / null if not present |
| 41 | */ |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 42 | public static Ip4Address getRelayAgentIPv4Address(Set<Interface> intfs) { |
rsahot036 | 620655b | 2018-02-26 15:01:37 -0500 | [diff] [blame] | 43 | for (Interface intf : intfs) { |
| 44 | for (InterfaceIpAddress ip : intf.ipAddressesList()) { |
| 45 | Ip4Address relayAgentIp = ip.ipAddress().getIp4Address(); |
| 46 | if (relayAgentIp != null) { |
| 47 | return relayAgentIp; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Determind if an Interface contains a vlan id. |
| 56 | * |
| 57 | * @param iface the Interface |
| 58 | * @param vlanId the vlan id |
| 59 | * @return true if the Interface contains the vlan id |
| 60 | */ |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 61 | public static boolean interfaceContainsVlan(Interface iface, VlanId vlanId) { |
rsahot036 | 620655b | 2018-02-26 15:01:37 -0500 | [diff] [blame] | 62 | if (vlanId.equals(VlanId.NONE)) { |
| 63 | // untagged packet, check if vlan untagged or vlan native is not NONE |
| 64 | return !iface.vlanUntagged().equals(VlanId.NONE) || |
| 65 | !iface.vlanNative().equals(VlanId.NONE); |
| 66 | } |
| 67 | // tagged packet, check if the interface contains the vlan |
| 68 | return iface.vlanTagged().contains(vlanId); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check if a given server info has v6 ipaddress. |
| 73 | * |
| 74 | * @param serverInfo server info to check |
| 75 | * @return true if server info has v6 ip address; false otherwise |
| 76 | */ |
Taras Lemkin | 96a0d34 | 2018-03-26 14:52:58 +0000 | [diff] [blame^] | 77 | public static boolean isServerIpEmpty(DhcpServerInfo serverInfo) { |
rsahot036 | 620655b | 2018-02-26 15:01:37 -0500 | [diff] [blame] | 78 | if (!serverInfo.getDhcpServerIp4().isPresent()) { |
| 79 | log.warn("DhcpServerIp not available, use default DhcpServerIp {}", |
| 80 | HexString.toHexString(serverInfo.getDhcpServerIp4().get().toOctets())); |
| 81 | return true; |
| 82 | } |
| 83 | return false; |
| 84 | } |
rsahot036 | 620655b | 2018-02-26 15:01:37 -0500 | [diff] [blame] | 85 | } |
| 86 | |