blob: 9bbcaa6c66aae329396f0df861d5c84ae5e54dc8 [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";
40 private static final String SUPPRESS_HOST = "suppressHost";
Charles Chan370a65b2016-05-10 17:29:47 -070041 private static final String HOST_LEARNING = "hostLearning";
Charles Chan82ab1932016-01-30 23:22:37 -080042
43 @Override
44 public boolean isValid() {
Charles Chan370a65b2016-05-10 17:29:47 -070045 return hasOnlyFields(VROUTER_MACS, VROUTER_ID, SUPPRESS_SUBNET,
46 SUPPRESS_HOST, HOST_LEARNING) &&
Charles Chan43547ca2016-02-10 20:46:58 -080047 vRouterMacs() != null && vRouterId() != null &&
Charles Chan57bd98c2016-02-22 19:27:29 -080048 suppressSubnet() != null && suppressHost() != null;
Charles Chan82ab1932016-01-30 23:22:37 -080049 }
50
51 /**
52 * Gets vRouters from the config.
53 *
Charles Chan57bd98c2016-02-22 19:27:29 -080054 * @return Set of vRouter MAC addresses, empty is not specified,
55 * or null if not valid
Charles Chan82ab1932016-01-30 23:22:37 -080056 */
57 public Set<MacAddress> vRouterMacs() {
58 if (!object.has(VROUTER_MACS)) {
Charles Chan57bd98c2016-02-22 19:27:29 -080059 return ImmutableSet.of();
Charles Chan82ab1932016-01-30 23:22:37 -080060 }
61
62 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
63 ArrayNode arrayNode = (ArrayNode) object.path(VROUTER_MACS);
64 for (JsonNode jsonNode : arrayNode) {
65 MacAddress mac;
66
67 String macStr = jsonNode.asText(null);
68 if (macStr == null) {
69 return null;
70 }
71 try {
72 mac = MacAddress.valueOf(macStr);
73 } catch (IllegalArgumentException e) {
74 return null;
75 }
76
77 builder.add(mac);
78 }
79 return builder.build();
80 }
81
82 /**
83 * Sets vRouters to the config.
84 *
85 * @param vRouterMacs a set of vRouter MAC addresses
86 * @return this {@link SegmentRoutingAppConfig}
87 */
88 public SegmentRoutingAppConfig setVRouterMacs(Set<MacAddress> vRouterMacs) {
89 if (vRouterMacs == null) {
90 object.remove(VROUTER_MACS);
91 } else {
92 ArrayNode arrayNode = mapper.createArrayNode();
93
94 vRouterMacs.forEach(mac -> {
95 arrayNode.add(mac.toString());
96 });
97
98 object.set(VROUTER_MACS, arrayNode);
99 }
100 return this;
101 }
102
Charles Chan43547ca2016-02-10 20:46:58 -0800103 /**
104 * Gets vRouter device ID.
105 *
Charles Chan57bd98c2016-02-22 19:27:29 -0800106 * @return Optional vRouter device ID,
107 * empty is not specified or null if not valid
Charles Chan43547ca2016-02-10 20:46:58 -0800108 */
Charles Chan57bd98c2016-02-22 19:27:29 -0800109 public Optional<DeviceId> vRouterId() {
Charles Chan43547ca2016-02-10 20:46:58 -0800110 if (!object.has(VROUTER_ID)) {
Charles Chan57bd98c2016-02-22 19:27:29 -0800111 return Optional.empty();
Charles Chan43547ca2016-02-10 20:46:58 -0800112 }
113
114 try {
Charles Chan57bd98c2016-02-22 19:27:29 -0800115 return Optional.of(DeviceId.deviceId(object.path(VROUTER_ID).asText()));
Charles Chan43547ca2016-02-10 20:46:58 -0800116 } catch (IllegalArgumentException e) {
117 return null;
118 }
119 }
120
121 /**
122 * Sets vRouter device ID.
123 *
124 * @param vRouterId vRouter device ID
125 * @return this {@link SegmentRoutingAppConfig}
126 */
127 public SegmentRoutingAppConfig setVRouterId(DeviceId vRouterId) {
128 if (vRouterId == null) {
129 object.remove(VROUTER_ID);
130 } else {
131 object.put(VROUTER_ID, vRouterId.toString());
132 }
133 return this;
134 }
135
136 /**
Charles Chan57bd98c2016-02-22 19:27:29 -0800137 * Gets names of ports to which SegmentRouting does not push subnet rules.
Charles Chan43547ca2016-02-10 20:46:58 -0800138 *
Charles Chan57bd98c2016-02-22 19:27:29 -0800139 * @return Set of port names, empty if not specified, or null
140 * if not valid
Charles Chan43547ca2016-02-10 20:46:58 -0800141 */
Charles Chan57bd98c2016-02-22 19:27:29 -0800142 public Set<ConnectPoint> suppressSubnet() {
143 if (!object.has(SUPPRESS_SUBNET)) {
144 return ImmutableSet.of();
Charles Chan43547ca2016-02-10 20:46:58 -0800145 }
146
Charles Chan57bd98c2016-02-22 19:27:29 -0800147 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
148 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_SUBNET);
Charles Chan43547ca2016-02-10 20:46:58 -0800149 for (JsonNode jsonNode : arrayNode) {
150 String portName = jsonNode.asText(null);
151 if (portName == null) {
152 return null;
153 }
Charles Chan57bd98c2016-02-22 19:27:29 -0800154 try {
155 builder.add(ConnectPoint.deviceConnectPoint(portName));
156 } catch (IllegalArgumentException e) {
157 return null;
158 }
Charles Chan43547ca2016-02-10 20:46:58 -0800159 }
160 return builder.build();
161 }
162
163 /**
Charles Chan57bd98c2016-02-22 19:27:29 -0800164 * Sets names of ports to which SegmentRouting does not push subnet rules.
Charles Chan43547ca2016-02-10 20:46:58 -0800165 *
Charles Chan57bd98c2016-02-22 19:27:29 -0800166 * @param suppressSubnet names of ports to which SegmentRouting does not push
167 * subnet rules
Charles Chan43547ca2016-02-10 20:46:58 -0800168 * @return this {@link SegmentRoutingAppConfig}
169 */
Charles Chan57bd98c2016-02-22 19:27:29 -0800170 public SegmentRoutingAppConfig setSuppressSubnet(Set<ConnectPoint> suppressSubnet) {
171 if (suppressSubnet == null) {
172 object.remove(SUPPRESS_SUBNET);
Charles Chan43547ca2016-02-10 20:46:58 -0800173 } else {
174 ArrayNode arrayNode = mapper.createArrayNode();
Charles Chan57bd98c2016-02-22 19:27:29 -0800175 suppressSubnet.forEach(connectPoint -> {
176 arrayNode.add(connectPoint.deviceId() + "/" + connectPoint.port());
Charles Chan43547ca2016-02-10 20:46:58 -0800177 });
Charles Chan57bd98c2016-02-22 19:27:29 -0800178 object.set(SUPPRESS_SUBNET, arrayNode);
179 }
180 return this;
181 }
182
183 /**
184 * Gets names of ports to which SegmentRouting does not push host rules.
185 *
186 * @return Set of port names, empty if not specified, or null
187 * if not valid
188 */
189 public Set<ConnectPoint> suppressHost() {
190 if (!object.has(SUPPRESS_HOST)) {
191 return ImmutableSet.of();
192 }
193
194 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
195 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_HOST);
196 for (JsonNode jsonNode : arrayNode) {
197 String portName = jsonNode.asText(null);
198 if (portName == null) {
199 return null;
200 }
201 try {
202 builder.add(ConnectPoint.deviceConnectPoint(portName));
203 } catch (IllegalArgumentException e) {
204 return null;
205 }
206 }
207 return builder.build();
208 }
209
210 /**
211 * Sets names of ports to which SegmentRouting does not push host rules.
212 *
213 * @param suppressHost names of ports to which SegmentRouting does not push
214 * host rules
215 * @return this {@link SegmentRoutingAppConfig}
216 */
217 public SegmentRoutingAppConfig setSuppressHost(Set<ConnectPoint> suppressHost) {
218 if (suppressHost == null) {
219 object.remove(SUPPRESS_HOST);
220 } else {
221 ArrayNode arrayNode = mapper.createArrayNode();
222 suppressHost.forEach(connectPoint -> {
223 arrayNode.add(connectPoint.deviceId() + "/" + connectPoint.port());
224 });
225 object.set(SUPPRESS_HOST, arrayNode);
Charles Chan43547ca2016-02-10 20:46:58 -0800226 }
227 return this;
228 }
229
Charles Chan370a65b2016-05-10 17:29:47 -0700230 /**
231 * Gets whether host learning is enabled or not.
232 *
233 * @return true if enabled. false if disabled or not configured
234 */
235 public boolean hostLearning() {
236 return object.has(HOST_LEARNING) && object.path(HOST_LEARNING).asBoolean();
237 }
238
239 /**
240 * Sets whether host learning is enabled or not.
241 *
242 * @param enabled true if enabled
243 * @return this {@link SegmentRoutingAppConfig}
244 */
245 public SegmentRoutingAppConfig setHostLearning(boolean enabled) {
246 object.put(HOST_LEARNING, enabled);
247 return this;
248 }
249
Charles Chan82ab1932016-01-30 23:22:37 -0800250 @Override
251 public String toString() {
252 return toStringHelper(this)
253 .add("vRouterMacs", vRouterMacs())
Charles Chan57bd98c2016-02-22 19:27:29 -0800254 .add("vRouterId", vRouterId())
255 .add("suppressSubnet", suppressSubnet())
256 .add("suppressHost", suppressHost())
Charles Chan370a65b2016-05-10 17:29:47 -0700257 .add("hostLearning", hostLearning())
Charles Chan82ab1932016-01-30 23:22:37 -0800258 .toString();
259 }
260}