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