blob: b3e3cf46c2e88e525fb8f902c4b93e4a5291df36 [file] [log] [blame]
Amit Ghosh47243cb2017-07-26 05:08:53 +01001/*
Brian O'Connor10570622017-08-03 22:45:53 -07002 * Copyright 2017-present Open Networking Foundation
Amit Ghosh47243cb2017-07-26 05:08:53 +01003 *
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.opencord.dhcpl2relay;
17
Amit Ghosh8951f042017-08-10 13:48:10 +010018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20
21import com.google.common.collect.ImmutableSet;
22
Amit Ghosh47243cb2017-07-26 05:08:53 +010023import org.onosproject.core.ApplicationId;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.config.Config;
26
Amit Ghosh8951f042017-08-10 13:48:10 +010027import java.util.HashSet;
28import java.util.Set;
29
Amit Ghosh47243cb2017-07-26 05:08:53 +010030
31/**
32 * DHCP Relay Config class.
33 */
34public class DhcpL2RelayConfig extends Config<ApplicationId> {
35
Amit Ghosh8951f042017-08-10 13:48:10 +010036 private static final String DHCP_CONNECT_POINTS = "dhcpServerConnectPoints";
Amit Ghosh1125d932017-09-25 21:08:31 +010037 private static final String MODIFY_SRC_DST_MAC = "modifyUlPacketsSrcDstMacAddresses";
38
39 private static final Boolean DEFAULT_MODIFY_SRC_DST_MAC = false;
Amit Ghosh47243cb2017-07-26 05:08:53 +010040
41 @Override
42 public boolean isValid() {
43
Amit Ghosh1125d932017-09-25 21:08:31 +010044 return hasOnlyFields(DHCP_CONNECT_POINTS, MODIFY_SRC_DST_MAC);
45 }
46
47 /**
48 * Returns whether the app would modify MAC address of uplink packets.
49 *
50 * @return whether app would modify src and dst MAC addresses or not of packets
51 * sent to the DHCP server
52 */
53 public boolean getModifySrcDstMacAddresses() {
54 if (object == null) {
55 return DEFAULT_MODIFY_SRC_DST_MAC;
56 }
57 if (!object.has(MODIFY_SRC_DST_MAC)) {
58 return DEFAULT_MODIFY_SRC_DST_MAC;
59 }
60
61 return object.path(MODIFY_SRC_DST_MAC).asBoolean();
Amit Ghosh47243cb2017-07-26 05:08:53 +010062 }
63
64 /**
Amit Ghosh8951f042017-08-10 13:48:10 +010065 * Returns the dhcp server connect points.
Amit Ghosh47243cb2017-07-26 05:08:53 +010066 *
Amit Ghosh8951f042017-08-10 13:48:10 +010067 * @return dhcp server connect points
Amit Ghosh47243cb2017-07-26 05:08:53 +010068 */
Amit Ghosh8951f042017-08-10 13:48:10 +010069 public Set<ConnectPoint> getDhcpServerConnectPoint() {
70 if (object == null) {
71 return new HashSet<ConnectPoint>();
72 }
73
74 if (!object.has(DHCP_CONNECT_POINTS)) {
75 return ImmutableSet.of();
76 }
77
78 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
79 ArrayNode arrayNode = (ArrayNode) object.path(DHCP_CONNECT_POINTS);
80 for (JsonNode jsonNode : arrayNode) {
81 String portName = jsonNode.asText(null);
82 if (portName == null) {
83 return null;
84 }
85 try {
86 builder.add(ConnectPoint.deviceConnectPoint(portName));
87 } catch (IllegalArgumentException e) {
88 return null;
89 }
90 }
91 return builder.build();
Amit Ghosh47243cb2017-07-26 05:08:53 +010092 }
93}