Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | 1057062 | 2017-08-03 22:45:53 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 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 | package org.opencord.dhcpl2relay; |
| 17 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.config.Config; |
| 26 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 27 | import java.util.HashSet; |
| 28 | import java.util.Set; |
| 29 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * DHCP Relay Config class. |
| 33 | */ |
| 34 | public class DhcpL2RelayConfig extends Config<ApplicationId> { |
| 35 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 36 | private static final String DHCP_CONNECT_POINTS = "dhcpServerConnectPoints"; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 37 | |
| 38 | @Override |
| 39 | public boolean isValid() { |
| 40 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 41 | return hasOnlyFields(DHCP_CONNECT_POINTS); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 45 | * Returns the dhcp server connect points. |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 46 | * |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 47 | * @return dhcp server connect points |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 48 | */ |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 49 | public Set<ConnectPoint> getDhcpServerConnectPoint() { |
| 50 | if (object == null) { |
| 51 | return new HashSet<ConnectPoint>(); |
| 52 | } |
| 53 | |
| 54 | if (!object.has(DHCP_CONNECT_POINTS)) { |
| 55 | return ImmutableSet.of(); |
| 56 | } |
| 57 | |
| 58 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 59 | ArrayNode arrayNode = (ArrayNode) object.path(DHCP_CONNECT_POINTS); |
| 60 | for (JsonNode jsonNode : arrayNode) { |
| 61 | String portName = jsonNode.asText(null); |
| 62 | if (portName == null) { |
| 63 | return null; |
| 64 | } |
| 65 | try { |
| 66 | builder.add(ConnectPoint.deviceConnectPoint(portName)); |
| 67 | } catch (IllegalArgumentException e) { |
| 68 | return null; |
| 69 | } |
| 70 | } |
| 71 | return builder.build(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 72 | } |
| 73 | } |