Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.incubator.net.config.basics; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.incubator.net.config.Config; |
| 24 | import org.onosproject.incubator.net.intf.Interface; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.host.InterfaceIpAddress; |
| 27 | |
| 28 | import java.util.Set; |
| 29 | |
| 30 | /** |
| 31 | * Configuration for interfaces. |
| 32 | */ |
| 33 | public class InterfaceConfig extends Config<ConnectPoint> { |
| 34 | public static final String INTERFACES = "interfaces"; |
| 35 | public static final String IPS = "ips"; |
| 36 | public static final String MAC = "mac"; |
| 37 | public static final String VLAN = "vlan"; |
| 38 | |
| 39 | /** |
| 40 | * Retrieves all interfaces configured on this port. |
| 41 | * |
| 42 | * @return set of interfaces |
| 43 | */ |
| 44 | public Set<Interface> getInterfaces() { |
| 45 | Set<Interface> interfaces = Sets.newHashSet(); |
| 46 | |
| 47 | for (JsonNode intfNode : node.path(INTERFACES)) { |
| 48 | interfaces.add(new Interface(subject, |
| 49 | getIps(intfNode), |
| 50 | MacAddress.valueOf(intfNode.path(MAC).asText()), |
| 51 | VlanId.vlanId(Short.parseShort(intfNode.path(VLAN).asText())))); |
| 52 | } |
| 53 | |
| 54 | return interfaces; |
| 55 | } |
| 56 | |
| 57 | private Set<InterfaceIpAddress> getIps(JsonNode node) { |
| 58 | Set<InterfaceIpAddress> ips = Sets.newHashSet(); |
| 59 | |
| 60 | JsonNode ipsNode = node.get(IPS); |
| 61 | ipsNode.forEach(jsonNode -> ips.add(InterfaceIpAddress.valueOf(jsonNode.asText()))); |
| 62 | |
| 63 | return ips; |
| 64 | } |
| 65 | |
| 66 | } |