blob: 3e4d17cda3823b64291a98a247bfe83b822d1958 [file] [log] [blame]
Charles Chan5270ed02016-01-30 23:22:37 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chan5270ed02016-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 Chand9681e72016-02-22 19:27:29 -080024import org.onosproject.net.ConnectPoint;
Charles Chan5270ed02016-01-30 23:22:37 -080025import org.onosproject.net.config.Config;
Charles Chand9681e72016-02-22 19:27:29 -080026
Charles Chan5270ed02016-01-30 23:22:37 -080027import java.util.Set;
28
29import static com.google.common.base.MoreObjects.toStringHelper;
30
31/**
32 * App configuration object for Segment Routing.
33 */
34public class SegmentRoutingAppConfig extends Config<ApplicationId> {
35 private static final String VROUTER_MACS = "vRouterMacs";
Charles Chand9681e72016-02-22 19:27:29 -080036 private static final String SUPPRESS_SUBNET = "suppressSubnet";
Charles Chanb3007e12016-05-20 10:55:40 -070037 private static final String SUPPRESS_HOST_BY_PORT = "suppressHostByPort";
38 // TODO We might want to move SUPPRESS_HOST_BY_PROVIDER to Component Config
39 private static final String SUPPRESS_HOST_BY_PROVIDER = "suppressHostByProvider";
Pier Ventre98161782016-10-31 15:00:01 -070040 private static final String MPLS_ECMP = "MPLS-ECMP";
Charles Chan5270ed02016-01-30 23:22:37 -080041
42 @Override
43 public boolean isValid() {
Charles Chan03a73e02016-10-24 14:52:01 -070044 return hasOnlyFields(VROUTER_MACS, SUPPRESS_SUBNET,
Pier Ventre98161782016-10-31 15:00:01 -070045 SUPPRESS_HOST_BY_PORT, SUPPRESS_HOST_BY_PROVIDER, MPLS_ECMP) &&
Charles Chan03a73e02016-10-24 14:52:01 -070046 vRouterMacs() != null &&
Charles Chanb3007e12016-05-20 10:55:40 -070047 suppressSubnet() != null && suppressHostByPort() != null &&
48 suppressHostByProvider() != null;
Charles Chan5270ed02016-01-30 23:22:37 -080049 }
50
51 /**
Pier Ventre98161782016-10-31 15:00:01 -070052 * Gets MPLS-ECMP configuration from the config.
53 *
54 * @return the configuration of MPLS-ECMP. If it is not
55 * specified, the default behavior is false.
56 */
57 public boolean mplsEcmp() {
58 return get(MPLS_ECMP, false);
59 }
60
61 /**
62 * Sets MPLS-ECMP to the config.
63 *
64 * @param mplsEcmp the MPLS-ECMP configuration
65 * @return this {@link SegmentRoutingAppConfig}
66 */
67 public SegmentRoutingAppConfig setMplsEcmp(boolean mplsEcmp) {
68 object.put(MPLS_ECMP, mplsEcmp);
69 return this;
70 }
71
72 /**
Charles Chan5270ed02016-01-30 23:22:37 -080073 * Gets vRouters from the config.
74 *
Charles Chand9681e72016-02-22 19:27:29 -080075 * @return Set of vRouter MAC addresses, empty is not specified,
76 * or null if not valid
Charles Chan5270ed02016-01-30 23:22:37 -080077 */
78 public Set<MacAddress> vRouterMacs() {
79 if (!object.has(VROUTER_MACS)) {
Charles Chand9681e72016-02-22 19:27:29 -080080 return ImmutableSet.of();
Charles Chan5270ed02016-01-30 23:22:37 -080081 }
82
83 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
84 ArrayNode arrayNode = (ArrayNode) object.path(VROUTER_MACS);
85 for (JsonNode jsonNode : arrayNode) {
86 MacAddress mac;
87
88 String macStr = jsonNode.asText(null);
89 if (macStr == null) {
90 return null;
91 }
92 try {
93 mac = MacAddress.valueOf(macStr);
94 } catch (IllegalArgumentException e) {
95 return null;
96 }
97
98 builder.add(mac);
99 }
100 return builder.build();
101 }
102
103 /**
104 * Sets vRouters to the config.
105 *
106 * @param vRouterMacs a set of vRouter MAC addresses
107 * @return this {@link SegmentRoutingAppConfig}
108 */
109 public SegmentRoutingAppConfig setVRouterMacs(Set<MacAddress> vRouterMacs) {
110 if (vRouterMacs == null) {
111 object.remove(VROUTER_MACS);
112 } else {
113 ArrayNode arrayNode = mapper.createArrayNode();
114
115 vRouterMacs.forEach(mac -> {
116 arrayNode.add(mac.toString());
117 });
118
119 object.set(VROUTER_MACS, arrayNode);
120 }
121 return this;
122 }
123
Charles Chanf2565a92016-02-10 20:46:58 -0800124 /**
Charles Chand9681e72016-02-22 19:27:29 -0800125 * Gets names of ports to which SegmentRouting does not push subnet rules.
Charles Chanf2565a92016-02-10 20:46:58 -0800126 *
Charles Chand9681e72016-02-22 19:27:29 -0800127 * @return Set of port names, empty if not specified, or null
128 * if not valid
Charles Chanf2565a92016-02-10 20:46:58 -0800129 */
Charles Chand9681e72016-02-22 19:27:29 -0800130 public Set<ConnectPoint> suppressSubnet() {
131 if (!object.has(SUPPRESS_SUBNET)) {
132 return ImmutableSet.of();
Charles Chanf2565a92016-02-10 20:46:58 -0800133 }
134
Charles Chand9681e72016-02-22 19:27:29 -0800135 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
136 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_SUBNET);
Charles Chanf2565a92016-02-10 20:46:58 -0800137 for (JsonNode jsonNode : arrayNode) {
138 String portName = jsonNode.asText(null);
139 if (portName == null) {
140 return null;
141 }
Charles Chand9681e72016-02-22 19:27:29 -0800142 try {
143 builder.add(ConnectPoint.deviceConnectPoint(portName));
144 } catch (IllegalArgumentException e) {
145 return null;
146 }
Charles Chanf2565a92016-02-10 20:46:58 -0800147 }
148 return builder.build();
149 }
150
151 /**
Charles Chand9681e72016-02-22 19:27:29 -0800152 * Sets names of ports to which SegmentRouting does not push subnet rules.
Charles Chanf2565a92016-02-10 20:46:58 -0800153 *
Charles Chand9681e72016-02-22 19:27:29 -0800154 * @param suppressSubnet names of ports to which SegmentRouting does not push
155 * subnet rules
Charles Chanf2565a92016-02-10 20:46:58 -0800156 * @return this {@link SegmentRoutingAppConfig}
157 */
Charles Chand9681e72016-02-22 19:27:29 -0800158 public SegmentRoutingAppConfig setSuppressSubnet(Set<ConnectPoint> suppressSubnet) {
159 if (suppressSubnet == null) {
160 object.remove(SUPPRESS_SUBNET);
Charles Chanf2565a92016-02-10 20:46:58 -0800161 } else {
162 ArrayNode arrayNode = mapper.createArrayNode();
Charles Chand9681e72016-02-22 19:27:29 -0800163 suppressSubnet.forEach(connectPoint -> {
164 arrayNode.add(connectPoint.deviceId() + "/" + connectPoint.port());
Charles Chanf2565a92016-02-10 20:46:58 -0800165 });
Charles Chand9681e72016-02-22 19:27:29 -0800166 object.set(SUPPRESS_SUBNET, arrayNode);
167 }
168 return this;
169 }
170
171 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700172 * Gets connect points to which SegmentRouting does not push host rules.
Charles Chand9681e72016-02-22 19:27:29 -0800173 *
Charles Chanb3007e12016-05-20 10:55:40 -0700174 * @return Set of connect points, empty if not specified, or null
Charles Chand9681e72016-02-22 19:27:29 -0800175 * if not valid
176 */
Charles Chanb3007e12016-05-20 10:55:40 -0700177 public Set<ConnectPoint> suppressHostByPort() {
178 if (!object.has(SUPPRESS_HOST_BY_PORT)) {
Charles Chand9681e72016-02-22 19:27:29 -0800179 return ImmutableSet.of();
180 }
181
182 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
Charles Chanb3007e12016-05-20 10:55:40 -0700183 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_HOST_BY_PORT);
Charles Chand9681e72016-02-22 19:27:29 -0800184 for (JsonNode jsonNode : arrayNode) {
185 String portName = jsonNode.asText(null);
186 if (portName == null) {
187 return null;
188 }
189 try {
190 builder.add(ConnectPoint.deviceConnectPoint(portName));
191 } catch (IllegalArgumentException e) {
192 return null;
193 }
194 }
195 return builder.build();
196 }
197
198 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700199 * Sets connect points to which SegmentRouting does not push host rules.
Charles Chand9681e72016-02-22 19:27:29 -0800200 *
Charles Chanb3007e12016-05-20 10:55:40 -0700201 * @param connectPoints connect points to which SegmentRouting does not push
Charles Chand9681e72016-02-22 19:27:29 -0800202 * host rules
203 * @return this {@link SegmentRoutingAppConfig}
204 */
Charles Chanb3007e12016-05-20 10:55:40 -0700205 public SegmentRoutingAppConfig setSuppressHostByPort(Set<ConnectPoint> connectPoints) {
206 if (connectPoints == null) {
207 object.remove(SUPPRESS_HOST_BY_PORT);
Charles Chand9681e72016-02-22 19:27:29 -0800208 } else {
209 ArrayNode arrayNode = mapper.createArrayNode();
Charles Chanb3007e12016-05-20 10:55:40 -0700210 connectPoints.forEach(connectPoint -> {
Charles Chand9681e72016-02-22 19:27:29 -0800211 arrayNode.add(connectPoint.deviceId() + "/" + connectPoint.port());
212 });
Charles Chanb3007e12016-05-20 10:55:40 -0700213 object.set(SUPPRESS_HOST_BY_PORT, arrayNode);
Charles Chanf2565a92016-02-10 20:46:58 -0800214 }
215 return this;
216 }
217
Charles Chan6ea94fc2016-05-10 17:29:47 -0700218 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700219 * Gets provider names from which SegmentRouting does not learn host info.
Charles Chan6ea94fc2016-05-10 17:29:47 -0700220 *
Charles Chanb3007e12016-05-20 10:55:40 -0700221 * @return array of provider names that need to be ignored
Charles Chan6ea94fc2016-05-10 17:29:47 -0700222 */
Charles Chanb3007e12016-05-20 10:55:40 -0700223 public Set<String> suppressHostByProvider() {
224 if (!object.has(SUPPRESS_HOST_BY_PROVIDER)) {
225 return ImmutableSet.of();
226 }
227
228 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
229 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_HOST_BY_PROVIDER);
230 for (JsonNode jsonNode : arrayNode) {
231 String providerName = jsonNode.asText(null);
232 if (providerName == null) {
233 return null;
234 }
235 builder.add(providerName);
236 }
237 return builder.build();
Charles Chan6ea94fc2016-05-10 17:29:47 -0700238 }
239
240 /**
Charles Chanb3007e12016-05-20 10:55:40 -0700241 * Sets provider names from which SegmentRouting does not learn host info.
Charles Chan6ea94fc2016-05-10 17:29:47 -0700242 *
Charles Chanb3007e12016-05-20 10:55:40 -0700243 * @param providers set of provider names
Charles Chan6ea94fc2016-05-10 17:29:47 -0700244 * @return this {@link SegmentRoutingAppConfig}
245 */
Charles Chanb3007e12016-05-20 10:55:40 -0700246 public SegmentRoutingAppConfig setSuppressHostByProvider(Set<String> providers) {
247 if (providers == null) {
248 object.remove(SUPPRESS_HOST_BY_PROVIDER);
249 } else {
250 ArrayNode arrayNode = mapper.createArrayNode();
251 providers.forEach(arrayNode::add);
252 object.set(SUPPRESS_HOST_BY_PROVIDER, arrayNode);
253 }
Charles Chan6ea94fc2016-05-10 17:29:47 -0700254 return this;
255 }
256
Charles Chan5270ed02016-01-30 23:22:37 -0800257 @Override
258 public String toString() {
259 return toStringHelper(this)
260 .add("vRouterMacs", vRouterMacs())
Charles Chand9681e72016-02-22 19:27:29 -0800261 .add("suppressSubnet", suppressSubnet())
Charles Chanb3007e12016-05-20 10:55:40 -0700262 .add("suppressHostByPort", suppressHostByPort())
263 .add("suppressHostByProvider", suppressHostByProvider())
Pier Ventre98161782016-10-31 15:00:01 -0700264 .add("mplsEcmp", mplsEcmp())
Charles Chan5270ed02016-01-30 23:22:37 -0800265 .toString();
266 }
267}