592336c2e32b25804a5be5992562600f39749c14
[onosfw.git] /
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.fasterxml.jackson.databind.node.ArrayNode;
21 import com.fasterxml.jackson.databind.node.ObjectNode;
22 import com.google.common.annotations.Beta;
23 import com.google.common.collect.Sets;
24 import org.onlab.packet.MacAddress;
25 import org.onlab.packet.VlanId;
26 import org.onosproject.incubator.net.intf.Interface;
27 import org.onosproject.net.ConnectPoint;
28 import org.onosproject.net.config.Config;
29 import org.onosproject.net.host.InterfaceIpAddress;
30
31 import java.util.Set;
32
33 /**
34  * Configuration for interfaces.
35  */
36 @Beta
37 public class InterfaceConfig extends Config<ConnectPoint> {
38     public static final String IPS = "ips";
39     public static final String MAC = "mac";
40     public static final String VLAN = "vlan";
41
42     public static final String MAC_MISSING_ERROR = "Must have a MAC address for each interface";
43     public static final String CONFIG_VALUE_ERROR = "Error parsing config value";
44
45     /**
46      * Retrieves all interfaces configured on this port.
47      *
48      * @return set of interfaces
49      * @throws ConfigException if there is any error in the JSON config
50      */
51     public Set<Interface> getInterfaces() throws ConfigException {
52         Set<Interface> interfaces = Sets.newHashSet();
53
54         try {
55             for (JsonNode intfNode : array) {
56                 Set<InterfaceIpAddress> ips = getIps(intfNode);
57
58                 if (intfNode.path(MAC).isMissingNode()) {
59                     throw new ConfigException(MAC_MISSING_ERROR);
60                 }
61
62                 MacAddress mac = MacAddress.valueOf(intfNode.path(MAC).asText());
63
64                 VlanId vlan = getVlan(intfNode);
65
66                 interfaces.add(new Interface(subject, ips, mac, vlan));
67             }
68         } catch (IllegalArgumentException e) {
69             throw new ConfigException(CONFIG_VALUE_ERROR, e);
70         }
71
72         return interfaces;
73     }
74
75     /**
76      * Adds an interface to the config.
77      *
78      * @param intf interface to add
79      */
80     public void addInterface(Interface intf) {
81         ObjectNode intfNode = array.addObject();
82         intfNode.put(MAC, intf.mac().toString());
83
84         if (!intf.ipAddresses().isEmpty()) {
85             intfNode.set(IPS, putIps(intf.ipAddresses()));
86         }
87
88         if (!intf.vlan().equals(VlanId.NONE)) {
89             intfNode.put(VLAN, intf.vlan().toString());
90         }
91     }
92
93     /**
94      * Removes an interface from the config.
95      *
96      * @param intf interface to remove
97      */
98     public void removeInterface(Interface intf) {
99         for (int i = 0; i < array.size(); i++) {
100             if (intf.vlan().equals(getVlan(node))) {
101                 array.remove(i);
102                 break;
103             }
104         }
105     }
106
107     private VlanId getVlan(JsonNode node) {
108         VlanId vlan = VlanId.NONE;
109         if (!node.path(VLAN).isMissingNode()) {
110             vlan = VlanId.vlanId(Short.valueOf(node.path(VLAN).asText()));
111         }
112         return vlan;
113     }
114
115     private Set<InterfaceIpAddress> getIps(JsonNode node) {
116         Set<InterfaceIpAddress> ips = Sets.newHashSet();
117
118         JsonNode ipsNode = node.get(IPS);
119         if (ipsNode != null) {
120             ipsNode.forEach(jsonNode ->
121                     ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
122         }
123
124         return ips;
125     }
126
127     private ArrayNode putIps(Set<InterfaceIpAddress> intfIpAddresses) {
128         ArrayNode ipArray = mapper.createArrayNode();
129
130         intfIpAddresses.forEach(i -> ipArray.add(i.toString()));
131
132         return ipArray;
133     }
134
135 }