nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | package org.onosproject.vpls.config; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.config.Config; |
| 25 | |
| 26 | import java.util.Set; |
| 27 | |
| 28 | /** |
| 29 | * Configuration object for VPLS config. |
| 30 | */ |
| 31 | public class VplsConfig extends Config<ApplicationId> { |
| 32 | private static final String VPLS = "vplsNetworks"; |
| 33 | private static final String NAME = "name"; |
| 34 | private static final String INTERFACE = "interfaces"; |
| 35 | |
| 36 | /** |
| 37 | * Returns a set of configured VPLSs. |
| 38 | * |
| 39 | * @return set of VPLSs |
| 40 | */ |
| 41 | public Set<VplsNetworkConfig> vplsNetworks() { |
| 42 | Set<VplsNetworkConfig> vpls = Sets.newHashSet(); |
| 43 | |
| 44 | JsonNode vplsNode = object.get(VPLS); |
| 45 | |
| 46 | if (vplsNode == null) { |
| 47 | return vpls; |
| 48 | } |
| 49 | |
| 50 | vplsNode.forEach(jsonNode -> { |
| 51 | Set<String> ifaces = Sets.newHashSet(); |
| 52 | jsonNode.path(INTERFACE).forEach(ifacesNode -> |
| 53 | ifaces.add(ifacesNode.asText()) |
| 54 | ); |
| 55 | |
| 56 | String name = jsonNode.get(NAME).asText(); |
| 57 | |
| 58 | vpls.add(new VplsNetworkConfig(name, ifaces)); |
| 59 | }); |
| 60 | |
| 61 | return vpls; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the VPLS configuration given a VPLS name. |
| 66 | * |
| 67 | * @param name the VPLS name |
| 68 | * @return the VPLS configuration if it exists; null otherwise |
| 69 | */ |
| 70 | public VplsNetworkConfig getVplsWithName(String name) { |
| 71 | for (VplsNetworkConfig vpls : vplsNetworks()) { |
| 72 | if (vpls.name().equals(name)) { |
| 73 | return vpls; |
| 74 | } |
| 75 | } |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Adds a VPLS to the configuration. |
| 81 | * |
| 82 | * @param name the name of the VPLS to be added |
| 83 | */ |
| 84 | public void addVpls(VplsNetworkConfig name) { |
| 85 | ObjectNode vplsNode = JsonNodeFactory.instance.objectNode(); |
| 86 | |
| 87 | vplsNode.put(NAME, name.name()); |
| 88 | |
| 89 | ArrayNode ifacesNode = vplsNode.putArray(INTERFACE); |
| 90 | name.ifaces().forEach(ifacesNode::add); |
| 91 | |
| 92 | ArrayNode vplsArray = vplsNetworks().isEmpty() ? |
| 93 | initVplsConfiguration() : (ArrayNode) object.get(VPLS); |
| 94 | vplsArray.add(vplsNode); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Removes a VPLS from the configuration. |
| 99 | * |
| 100 | * @param name the name of the VPLS to be removed |
| 101 | */ |
| 102 | public void removeVpls(String name) { |
| 103 | ArrayNode vplsArray = (ArrayNode) object.get(VPLS); |
| 104 | |
| 105 | for (int i = 0; i < vplsArray.size(); i++) { |
| 106 | if (vplsArray.get(i).hasNonNull(NAME) && |
| 107 | vplsArray.get(i).get(NAME).asText().equals(name)) { |
| 108 | vplsArray.remove(i); |
| 109 | return; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Finds a VPLS with a given network interface. |
| 116 | * |
| 117 | * @param iface the network interface |
| 118 | * @return the VPLS if found; null otherwise |
| 119 | */ |
| 120 | public VplsNetworkConfig getVplsFromInterface(String iface) { |
| 121 | for (VplsNetworkConfig vpls : vplsNetworks()) { |
| 122 | if (vpls.isAttached(iface)) { |
| 123 | return vpls; |
| 124 | } |
| 125 | } |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Adds a network interface to a VPLS. |
| 131 | * |
| 132 | * @param name the name of the VPLS |
| 133 | * @param iface the network interface to be added |
| 134 | */ |
| 135 | public void addInterfaceToVpls(String name, String iface) { |
| 136 | JsonNode vplsNode = object.get(VPLS); |
| 137 | vplsNode.forEach(jsonNode -> { |
| 138 | |
| 139 | if (hasNamedNode(jsonNode, name)) { |
| 140 | ArrayNode ifacesNode = (ArrayNode) jsonNode.get(INTERFACE); |
| 141 | for (int i = 0; i < ifacesNode.size(); i++) { |
| 142 | if (ifacesNode.get(i).asText().equals(iface)) { |
| 143 | return; // Interface already exists. |
| 144 | } |
| 145 | } |
| 146 | ifacesNode.add(iface); |
| 147 | } |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Removes a network interface from a VPLS. |
| 153 | * |
| 154 | * @param name the name of the VPLS |
| 155 | * @param iface the network interface to be removed |
| 156 | */ |
| 157 | public void removeInterfaceFromVpls(VplsNetworkConfig name, String iface) { |
| 158 | JsonNode vplsNode = object.get(VPLS); |
| 159 | vplsNode.forEach(jsonNode -> { |
| 160 | if (hasNamedNode(jsonNode, name.name())) { |
| 161 | ArrayNode ifacesNode = (ArrayNode) jsonNode.get(INTERFACE); |
| 162 | for (int i = 0; i < ifacesNode.size(); i++) { |
| 163 | if (ifacesNode.get(i).asText().equals(iface)) { |
| 164 | ifacesNode.remove(i); |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | }); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * States if a JSON node has a "name" attribute and if the value is equal to |
| 174 | * the name given. |
| 175 | * |
| 176 | * @param jsonNode the JSON node |
| 177 | * @param name the node name |
| 178 | * @return true if the JSON node has a "name" attribute with value equal to |
| 179 | * the name given; false otherwise |
| 180 | */ |
| 181 | private boolean hasNamedNode(JsonNode jsonNode, String name) { |
| 182 | return jsonNode.hasNonNull(NAME) && |
| 183 | jsonNode.get(NAME).asText().equals(name); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Creates an empty VPLS configuration. |
| 188 | * |
| 189 | * @return empty ArrayNode to store the VPLS configuration |
| 190 | */ |
| 191 | private ArrayNode initVplsConfiguration() { |
| 192 | return object.putArray(VPLS); |
| 193 | } |
| 194 | } |