blob: a88bda806e9b492a120efcffffa6c39a338a4ed7 [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;
rsahot036620655b2018-02-26 15:01:37 -050021import org.onlab.util.HexString;
22import org.onosproject.dhcprelay.api.DhcpServerInfo;
rsahot036620655b2018-02-26 15:01:37 -050023import org.onosproject.net.host.InterfaceIpAddress;
24import org.onosproject.net.intf.Interface;
25
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28import java.util.Set;
29
Taras Lemkin96a0d342018-03-26 14:52:58 +000030public final class Dhcp4HandlerUtil {
31 private static final Logger log = LoggerFactory.getLogger(Dhcp4HandlerUtil.class);
rsahot036620655b2018-02-26 15:01:37 -050032
Taras Lemkin96a0d342018-03-26 14:52:58 +000033 private Dhcp4HandlerUtil() {
34 }
rsahot036620655b2018-02-26 15:01:37 -050035
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 Lemkin96a0d342018-03-26 14:52:58 +000042 public static Ip4Address getRelayAgentIPv4Address(Set<Interface> intfs) {
rsahot036620655b2018-02-26 15:01:37 -050043 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 Lemkin96a0d342018-03-26 14:52:58 +000061 public static boolean interfaceContainsVlan(Interface iface, VlanId vlanId) {
rsahot036620655b2018-02-26 15:01:37 -050062 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 Lemkin96a0d342018-03-26 14:52:58 +000077 public static boolean isServerIpEmpty(DhcpServerInfo serverInfo) {
rsahot036620655b2018-02-26 15:01:37 -050078 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 }
rsahot036620655b2018-02-26 15:01:37 -050085}
86