blob: 0eb2c8e5ae3783c9fd0e754624554d8e6d4671aa [file] [log] [blame]
Charles Chan82ab1932016-01-30 23:22:37 -08001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Charles Chan82ab1932016-01-30 23:22:37 -08003 *
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
17package org.onosproject.segmentrouting.config;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.google.common.collect.ImmutableSet;
22import org.onlab.packet.MacAddress;
23import org.onosproject.core.ApplicationId;
Charles Chan57bd98c2016-02-22 19:27:29 -080024import org.onosproject.net.ConnectPoint;
Charles Chan43547ca2016-02-10 20:46:58 -080025import org.onosproject.net.DeviceId;
Charles Chan82ab1932016-01-30 23:22:37 -080026import org.onosproject.net.config.Config;
Charles Chan57bd98c2016-02-22 19:27:29 -080027
28import java.util.Optional;
Charles Chan82ab1932016-01-30 23:22:37 -080029import java.util.Set;
30
31import static com.google.common.base.MoreObjects.toStringHelper;
32
33/**
34 * App configuration object for Segment Routing.
35 */
36public class SegmentRoutingAppConfig extends Config<ApplicationId> {
37 private static final String VROUTER_MACS = "vRouterMacs";
Charles Chan43547ca2016-02-10 20:46:58 -080038 private static final String VROUTER_ID = "vRouterId";
Charles Chan57bd98c2016-02-22 19:27:29 -080039 private static final String SUPPRESS_SUBNET = "suppressSubnet";
Charles Chan3bf64b92016-05-20 10:55:40 -070040 private static final String SUPPRESS_HOST_BY_PORT = "suppressHostByPort";
41 // TODO We might want to move SUPPRESS_HOST_BY_PROVIDER to Component Config
42 private static final String SUPPRESS_HOST_BY_PROVIDER = "suppressHostByProvider";
Charles Chan82ab1932016-01-30 23:22:37 -080043
44 @Override
45 public boolean isValid() {
Charles Chan370a65b2016-05-10 17:29:47 -070046 return hasOnlyFields(VROUTER_MACS, VROUTER_ID, SUPPRESS_SUBNET,
Charles Chan3bf64b92016-05-20 10:55:40 -070047 SUPPRESS_HOST_BY_PORT, SUPPRESS_HOST_BY_PROVIDER) &&
Charles Chan43547ca2016-02-10 20:46:58 -080048 vRouterMacs() != null && vRouterId() != null &&
Charles Chan3bf64b92016-05-20 10:55:40 -070049 suppressSubnet() != null && suppressHostByPort() != null &&
50 suppressHostByProvider() != null;
Charles Chan82ab1932016-01-30 23:22:37 -080051 }
52
53 /**
54 * Gets vRouters from the config.
55 *
Charles Chan57bd98c2016-02-22 19:27:29 -080056 * @return Set of vRouter MAC addresses, empty is not specified,
57 * or null if not valid
Charles Chan82ab1932016-01-30 23:22:37 -080058 */
59 public Set<MacAddress> vRouterMacs() {
60 if (!object.has(VROUTER_MACS)) {
Charles Chan57bd98c2016-02-22 19:27:29 -080061 return ImmutableSet.of();
Charles Chan82ab1932016-01-30 23:22:37 -080062 }
63
64 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
65 ArrayNode arrayNode = (ArrayNode) object.path(VROUTER_MACS);
66 for (JsonNode jsonNode : arrayNode) {
67 MacAddress mac;
68
69 String macStr = jsonNode.asText(null);
70 if (macStr == null) {
71 return null;
72 }
73 try {
74 mac = MacAddress.valueOf(macStr);
75 } catch (IllegalArgumentException e) {
76 return null;
77 }
78
79 builder.add(mac);
80 }
81 return builder.build();
82 }
83
84 /**
85 * Sets vRouters to the config.
86 *
87 * @param vRouterMacs a set of vRouter MAC addresses
88 * @return this {@link SegmentRoutingAppConfig}
89 */
90 public SegmentRoutingAppConfig setVRouterMacs(Set<MacAddress> vRouterMacs) {
91 if (vRouterMacs == null) {
92 object.remove(VROUTER_MACS);
93 } else {
94 ArrayNode arrayNode = mapper.createArrayNode();
95
96 vRouterMacs.forEach(mac -> {
97 arrayNode.add(mac.toString());
98 });
99
100 object.set(VROUTER_MACS, arrayNode);
101 }
102 return this;
103 }
104
Charles Chan43547ca2016-02-10 20:46:58 -0800105 /**
106 * Gets vRouter device ID.
107 *
Charles Chan57bd98c2016-02-22 19:27:29 -0800108 * @return Optional vRouter device ID,
109 * empty is not specified or null if not valid
Charles Chan43547ca2016-02-10 20:46:58 -0800110 */
Charles Chan57bd98c2016-02-22 19:27:29 -0800111 public Optional<DeviceId> vRouterId() {
Charles Chan43547ca2016-02-10 20:46:58 -0800112 if (!object.has(VROUTER_ID)) {
Charles Chan57bd98c2016-02-22 19:27:29 -0800113 return Optional.empty();
Charles Chan43547ca2016-02-10 20:46:58 -0800114 }
115
116 try {
Charles Chan57bd98c2016-02-22 19:27:29 -0800117 return Optional.of(DeviceId.deviceId(object.path(VROUTER_ID).asText()));
Charles Chan43547ca2016-02-10 20:46:58 -0800118 } catch (IllegalArgumentException e) {
119 return null;
120 }
121 }
122
123 /**
124 * Sets vRouter device ID.
125 *
126 * @param vRouterId vRouter device ID
127 * @return this {@link SegmentRoutingAppConfig}
128 */
129 public SegmentRoutingAppConfig setVRouterId(DeviceId vRouterId) {
130 if (vRouterId == null) {
131 object.remove(VROUTER_ID);
132 } else {
133 object.put(VROUTER_ID, vRouterId.toString());
134 }
135 return this;
136 }
137
138 /**
Charles Chan57bd98c2016-02-22 19:27:29 -0800139 * Gets names of ports to which SegmentRouting does not push subnet rules.
Charles Chan43547ca2016-02-10 20:46:58 -0800140 *
Charles Chan57bd98c2016-02-22 19:27:29 -0800141 * @return Set of port names, empty if not specified, or null
142 * if not valid
Charles Chan43547ca2016-02-10 20:46:58 -0800143 */
Charles Chan57bd98c2016-02-22 19:27:29 -0800144 public Set<ConnectPoint> suppressSubnet() {
145 if (!object.has(SUPPRESS_SUBNET)) {
146 return ImmutableSet.of();
Charles Chan43547ca2016-02-10 20:46:58 -0800147 }
148
Charles Chan57bd98c2016-02-22 19:27:29 -0800149 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
150 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_SUBNET);
Charles Chan43547ca2016-02-10 20:46:58 -0800151 for (JsonNode jsonNode : arrayNode) {
152 String portName = jsonNode.asText(null);
153 if (portName == null) {
154 return null;
155 }
Charles Chan57bd98c2016-02-22 19:27:29 -0800156 try {
157 builder.add(ConnectPoint.deviceConnectPoint(portName));
158 } catch (IllegalArgumentException e) {
159 return null;
160 }
Charles Chan43547ca2016-02-10 20:46:58 -0800161 }
162 return builder.build();
163 }
164
165 /**
Charles Chan57bd98c2016-02-22 19:27:29 -0800166 * Sets names of ports to which SegmentRouting does not push subnet rules.
Charles Chan43547ca2016-02-10 20:46:58 -0800167 *
Charles Chan57bd98c2016-02-22 19:27:29 -0800168 * @param suppressSubnet names of ports to which SegmentRouting does not push
169 * subnet rules
Charles Chan43547ca2016-02-10 20:46:58 -0800170 * @return this {@link SegmentRoutingAppConfig}
171 */
Charles Chan57bd98c2016-02-22 19:27:29 -0800172 public SegmentRoutingAppConfig setSuppressSubnet(Set<ConnectPoint> suppressSubnet) {
173 if (suppressSubnet == null) {
174 object.remove(SUPPRESS_SUBNET);
Charles Chan43547ca2016-02-10 20:46:58 -0800175 } else {
176 ArrayNode arrayNode = mapper.createArrayNode();
Charles Chan57bd98c2016-02-22 19:27:29 -0800177 suppressSubnet.forEach(connectPoint -> {
178 arrayNode.add(connectPoint.deviceId() + "/" + connectPoint.port());
Charles Chan43547ca2016-02-10 20:46:58 -0800179 });
Charles Chan57bd98c2016-02-22 19:27:29 -0800180 object.set(SUPPRESS_SUBNET, arrayNode);
181 }
182 return this;
183 }
184
185 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700186 * Gets connect points to which SegmentRouting does not push host rules.
Charles Chan57bd98c2016-02-22 19:27:29 -0800187 *
Charles Chan3bf64b92016-05-20 10:55:40 -0700188 * @return Set of connect points, empty if not specified, or null
Charles Chan57bd98c2016-02-22 19:27:29 -0800189 * if not valid
190 */
Charles Chan3bf64b92016-05-20 10:55:40 -0700191 public Set<ConnectPoint> suppressHostByPort() {
192 if (!object.has(SUPPRESS_HOST_BY_PORT)) {
Charles Chan57bd98c2016-02-22 19:27:29 -0800193 return ImmutableSet.of();
194 }
195
196 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
Charles Chan3bf64b92016-05-20 10:55:40 -0700197 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_HOST_BY_PORT);
Charles Chan57bd98c2016-02-22 19:27:29 -0800198 for (JsonNode jsonNode : arrayNode) {
199 String portName = jsonNode.asText(null);
200 if (portName == null) {
201 return null;
202 }
203 try {
204 builder.add(ConnectPoint.deviceConnectPoint(portName));
205 } catch (IllegalArgumentException e) {
206 return null;
207 }
208 }
209 return builder.build();
210 }
211
212 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700213 * Sets connect points to which SegmentRouting does not push host rules.
Charles Chan57bd98c2016-02-22 19:27:29 -0800214 *
Charles Chan3bf64b92016-05-20 10:55:40 -0700215 * @param connectPoints connect points to which SegmentRouting does not push
Charles Chan57bd98c2016-02-22 19:27:29 -0800216 * host rules
217 * @return this {@link SegmentRoutingAppConfig}
218 */
Charles Chan3bf64b92016-05-20 10:55:40 -0700219 public SegmentRoutingAppConfig setSuppressHostByPort(Set<ConnectPoint> connectPoints) {
220 if (connectPoints == null) {
221 object.remove(SUPPRESS_HOST_BY_PORT);
Charles Chan57bd98c2016-02-22 19:27:29 -0800222 } else {
223 ArrayNode arrayNode = mapper.createArrayNode();
Charles Chan3bf64b92016-05-20 10:55:40 -0700224 connectPoints.forEach(connectPoint -> {
Charles Chan57bd98c2016-02-22 19:27:29 -0800225 arrayNode.add(connectPoint.deviceId() + "/" + connectPoint.port());
226 });
Charles Chan3bf64b92016-05-20 10:55:40 -0700227 object.set(SUPPRESS_HOST_BY_PORT, arrayNode);
Charles Chan43547ca2016-02-10 20:46:58 -0800228 }
229 return this;
230 }
231
Charles Chan370a65b2016-05-10 17:29:47 -0700232 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700233 * Gets provider names from which SegmentRouting does not learn host info.
Charles Chan370a65b2016-05-10 17:29:47 -0700234 *
Charles Chan3bf64b92016-05-20 10:55:40 -0700235 * @return array of provider names that need to be ignored
Charles Chan370a65b2016-05-10 17:29:47 -0700236 */
Charles Chan3bf64b92016-05-20 10:55:40 -0700237 public Set<String> suppressHostByProvider() {
238 if (!object.has(SUPPRESS_HOST_BY_PROVIDER)) {
239 return ImmutableSet.of();
240 }
241
242 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
243 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_HOST_BY_PROVIDER);
244 for (JsonNode jsonNode : arrayNode) {
245 String providerName = jsonNode.asText(null);
246 if (providerName == null) {
247 return null;
248 }
249 builder.add(providerName);
250 }
251 return builder.build();
Charles Chan370a65b2016-05-10 17:29:47 -0700252 }
253
254 /**
Charles Chan3bf64b92016-05-20 10:55:40 -0700255 * Sets provider names from which SegmentRouting does not learn host info.
Charles Chan370a65b2016-05-10 17:29:47 -0700256 *
Charles Chan3bf64b92016-05-20 10:55:40 -0700257 * @param providers set of provider names
Charles Chan370a65b2016-05-10 17:29:47 -0700258 * @return this {@link SegmentRoutingAppConfig}
259 */
Charles Chan3bf64b92016-05-20 10:55:40 -0700260 public SegmentRoutingAppConfig setSuppressHostByProvider(Set<String> providers) {
261 if (providers == null) {
262 object.remove(SUPPRESS_HOST_BY_PROVIDER);
263 } else {
264 ArrayNode arrayNode = mapper.createArrayNode();
265 providers.forEach(arrayNode::add);
266 object.set(SUPPRESS_HOST_BY_PROVIDER, arrayNode);
267 }
Charles Chan370a65b2016-05-10 17:29:47 -0700268 return this;
269 }
270
Charles Chan82ab1932016-01-30 23:22:37 -0800271 @Override
272 public String toString() {
273 return toStringHelper(this)
274 .add("vRouterMacs", vRouterMacs())
Charles Chan57bd98c2016-02-22 19:27:29 -0800275 .add("vRouterId", vRouterId())
276 .add("suppressSubnet", suppressSubnet())
Charles Chan3bf64b92016-05-20 10:55:40 -0700277 .add("suppressHostByPort", suppressHostByPort())
278 .add("suppressHostByProvider", suppressHostByProvider())
Charles Chan82ab1932016-01-30 23:22:37 -0800279 .toString();
280 }
281}