blob: 66f7882f2d3dddddc5a154aa65de9497a12ce52d [file] [log] [blame]
Luca Prete092e8952016-10-26 16:25:56 +02001/*
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 */
16package org.onosproject.vpls.config;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.JsonNodeFactory;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.collect.Sets;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.net.EncapsulationType;
25import org.onosproject.net.config.Config;
Luca Prete092e8952016-10-26 16:25:56 +020026
27import java.util.Set;
28
29/**
30 * Represents the VPLS application configuration.
31 */
32public class VplsAppConfig extends Config<ApplicationId> {
33 private static final String VPLS = "vplsList";
34 private static final String NAME = "name";
35 private static final String INTERFACE = "interfaces";
36 private static final String ENCAPSULATION = "encapsulation";
37
Luca Prete092e8952016-10-26 16:25:56 +020038 /**
39 * Returns a set of configured VPLSs.
40 *
41 * @return set of VPLSs
42 */
43 public Set<VplsConfig> vplss() {
44 Set<VplsConfig> vplss = Sets.newHashSet();
45
46 JsonNode vplsNode = object.get(VPLS);
47
48 if (vplsNode == null) {
49 return vplss;
50 }
51
52 vplsNode.forEach(jsonNode -> {
53 String name = jsonNode.get(NAME).asText();
54
55 Set<String> ifaces = Sets.newHashSet();
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +010056 JsonNode vplsIfaces = jsonNode.path(INTERFACE);
57 if (vplsIfaces.toString().isEmpty()) {
58 vplsIfaces = ((ObjectNode) jsonNode).putArray(INTERFACE);
59 }
60 vplsIfaces.forEach(ifacesNode -> ifaces.add(ifacesNode.asText()));
Luca Prete092e8952016-10-26 16:25:56 +020061
62 String encap = null;
63 if (jsonNode.hasNonNull(ENCAPSULATION)) {
64 encap = jsonNode.get(ENCAPSULATION).asText();
65 }
66 vplss.add(new VplsConfig(name,
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +010067 ifaces,
68 EncapsulationType.enumFromString(encap)));
Luca Prete092e8952016-10-26 16:25:56 +020069 });
70
71 return vplss;
72 }
73
74 /**
75 * Returns the VPLS configuration given a VPLS name.
76 *
77 * @param name the name of the VPLS
78 * @return the VPLS configuration if it exists; null otherwise
79 */
80 public VplsConfig getVplsWithName(String name) {
81 return vplss().stream()
82 .filter(vpls -> vpls.name().equals(name))
83 .findFirst()
84 .orElse(null);
85 }
86
87 /**
88 * Adds a VPLS to the configuration.
89 *
90 * @param vpls the name of the VPLS
91 */
92 public void addVpls(VplsConfig vpls) {
93 ObjectNode vplsNode = JsonNodeFactory.instance.objectNode();
94
95 vplsNode.put(NAME, vpls.name());
96
97 ArrayNode ifacesNode = vplsNode.putArray(INTERFACE);
98 vpls.ifaces().forEach(ifacesNode::add);
99
100 vplsNode.put(ENCAPSULATION, vpls.encap().toString());
101
102 ArrayNode vplsArray = vplss().isEmpty() ?
103 initVplsConfiguration() : (ArrayNode) object.get(VPLS);
104 vplsArray.add(vplsNode);
105 }
106
107 /**
108 * Removes a VPLS from the configuration.
109 *
110 * @param vplsName the vplsName of the VPLS to be removed
111 */
112 public void removeVpls(String vplsName) {
113 ArrayNode configuredVpls = (ArrayNode) object.get(VPLS);
114
115 for (int i = 0; i < configuredVpls.size(); i++) {
116 if (configuredVpls.get(i).hasNonNull(NAME) &&
117 configuredVpls.get(i).get(NAME).asText().equals(vplsName)) {
118 configuredVpls.remove(i);
119 return;
120 }
121 }
122 }
123
124 /**
125 * Finds a VPLS with a given network interface.
126 *
127 * @param iface the network interface
128 * @return the VPLS if found; null otherwise
129 */
130 public VplsConfig vplsFromIface(String iface) {
131 for (VplsConfig vpls : vplss()) {
132 if (vpls.isAttached(iface)) {
133 return vpls;
134 }
135 }
136 return null;
137 }
138
139 /**
140 * Adds a network interface to a VPLS.
141 *
142 * @param vplsName the vplsName of the VPLS
143 * @param iface the network interface to be added
144 */
145 public void addIface(String vplsName, String iface) {
146 JsonNode vplsNodes = object.get(VPLS);
147 vplsNodes.forEach(vplsNode -> {
148 if (hasNamedNode(vplsNode, vplsName)) {
149 ArrayNode ifacesNode = (ArrayNode) vplsNode.get(INTERFACE);
150 for (int i = 0; i < ifacesNode.size(); i++) {
151 if (ifacesNode.get(i).asText().equals(iface)) {
152 return; // Interface already exists.
153 }
154 }
155 ifacesNode.add(iface);
156 }
157 });
158 }
159
160 /**
161 * Removes a network interface from a VPLS.
162 *
163 * @param name the name of the VPLS
164 * @param iface the network interface to be removed
165 */
166 public void removeIface(VplsConfig name, String iface) {
167 JsonNode vplsNodes = object.get(VPLS);
168 vplsNodes.forEach(vplsNode -> {
169 if (hasNamedNode(vplsNode, name.name())) {
170 ArrayNode ifacesNode = (ArrayNode) vplsNode.get(INTERFACE);
171 for (int i = 0; i < ifacesNode.size(); i++) {
172 if (ifacesNode.get(i).asText().equals(iface)) {
173 ifacesNode.remove(i);
174 return;
175 }
176 }
177 }
178 });
179 }
180
181 /**
182 * Activate, deactivates, sets the encapsulation type for a given VPLS.
183 *
184 * @param vplsName the vplsName of the VPLS
185 * @param encap the encapsulation type, if set
186 */
187 public void setEncap(String vplsName, EncapsulationType encap) {
188 JsonNode vplsNodes = object.get(VPLS);
189 vplsNodes.forEach(vplsNode -> {
190 if (hasNamedNode(vplsNode, vplsName)) {
191 ((ObjectNode) vplsNode).put(ENCAPSULATION, encap.toString());
192 }
193 });
194 }
195
196 /**
197 * States if a JSON node has a "name" attribute and if the value is equal to
198 * the name given.
199 *
200 * @param jsonNode the JSON node
201 * @param name the node name
202 * @return true if the JSON node has a "name" attribute with value equal to
203 * the name given; false otherwise
204 */
205 private boolean hasNamedNode(JsonNode jsonNode, String name) {
206 return jsonNode.hasNonNull(NAME) &&
207 jsonNode.get(NAME).asText().equals(name);
208 }
209
210 /**
211 * Creates an empty VPLS configuration.
212 *
213 * @return empty ArrayNode to store the VPLS configuration
214 */
215 private ArrayNode initVplsConfiguration() {
216 return object.putArray(VPLS);
217 }
218}