blob: 29328001933138430af7555b38aabcbbff067126 [file] [log] [blame]
Charles Chan5270ed02016-01-30 23:22:37 -08001/*
2 * Copyright 2016 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 */
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 Chanf2565a92016-02-10 20:46:58 -080025import org.onosproject.net.DeviceId;
Charles Chan5270ed02016-01-30 23:22:37 -080026import org.onosproject.net.config.Config;
Charles Chand9681e72016-02-22 19:27:29 -080027
28import java.util.Optional;
Charles Chan5270ed02016-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 Chanf2565a92016-02-10 20:46:58 -080038 private static final String VROUTER_ID = "vRouterId";
Charles Chand9681e72016-02-22 19:27:29 -080039 private static final String SUPPRESS_SUBNET = "suppressSubnet";
40 private static final String SUPPRESS_HOST = "suppressHost";
Charles Chan5270ed02016-01-30 23:22:37 -080041
42 @Override
43 public boolean isValid() {
Charles Chand9681e72016-02-22 19:27:29 -080044 return hasOnlyFields(VROUTER_MACS, VROUTER_ID, SUPPRESS_SUBNET, SUPPRESS_HOST) &&
Charles Chanf2565a92016-02-10 20:46:58 -080045 vRouterMacs() != null && vRouterId() != null &&
Charles Chand9681e72016-02-22 19:27:29 -080046 suppressSubnet() != null && suppressHost() != null;
Charles Chan5270ed02016-01-30 23:22:37 -080047 }
48
49 /**
50 * Gets vRouters from the config.
51 *
Charles Chand9681e72016-02-22 19:27:29 -080052 * @return Set of vRouter MAC addresses, empty is not specified,
53 * or null if not valid
Charles Chan5270ed02016-01-30 23:22:37 -080054 */
55 public Set<MacAddress> vRouterMacs() {
56 if (!object.has(VROUTER_MACS)) {
Charles Chand9681e72016-02-22 19:27:29 -080057 return ImmutableSet.of();
Charles Chan5270ed02016-01-30 23:22:37 -080058 }
59
60 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
61 ArrayNode arrayNode = (ArrayNode) object.path(VROUTER_MACS);
62 for (JsonNode jsonNode : arrayNode) {
63 MacAddress mac;
64
65 String macStr = jsonNode.asText(null);
66 if (macStr == null) {
67 return null;
68 }
69 try {
70 mac = MacAddress.valueOf(macStr);
71 } catch (IllegalArgumentException e) {
72 return null;
73 }
74
75 builder.add(mac);
76 }
77 return builder.build();
78 }
79
80 /**
81 * Sets vRouters to the config.
82 *
83 * @param vRouterMacs a set of vRouter MAC addresses
84 * @return this {@link SegmentRoutingAppConfig}
85 */
86 public SegmentRoutingAppConfig setVRouterMacs(Set<MacAddress> vRouterMacs) {
87 if (vRouterMacs == null) {
88 object.remove(VROUTER_MACS);
89 } else {
90 ArrayNode arrayNode = mapper.createArrayNode();
91
92 vRouterMacs.forEach(mac -> {
93 arrayNode.add(mac.toString());
94 });
95
96 object.set(VROUTER_MACS, arrayNode);
97 }
98 return this;
99 }
100
Charles Chanf2565a92016-02-10 20:46:58 -0800101 /**
102 * Gets vRouter device ID.
103 *
Charles Chand9681e72016-02-22 19:27:29 -0800104 * @return Optional vRouter device ID,
105 * empty is not specified or null if not valid
Charles Chanf2565a92016-02-10 20:46:58 -0800106 */
Charles Chand9681e72016-02-22 19:27:29 -0800107 public Optional<DeviceId> vRouterId() {
Charles Chanf2565a92016-02-10 20:46:58 -0800108 if (!object.has(VROUTER_ID)) {
Charles Chand9681e72016-02-22 19:27:29 -0800109 return Optional.empty();
Charles Chanf2565a92016-02-10 20:46:58 -0800110 }
111
112 try {
Charles Chand9681e72016-02-22 19:27:29 -0800113 return Optional.of(DeviceId.deviceId(object.path(VROUTER_ID).asText()));
Charles Chanf2565a92016-02-10 20:46:58 -0800114 } catch (IllegalArgumentException e) {
115 return null;
116 }
117 }
118
119 /**
120 * Sets vRouter device ID.
121 *
122 * @param vRouterId vRouter device ID
123 * @return this {@link SegmentRoutingAppConfig}
124 */
125 public SegmentRoutingAppConfig setVRouterId(DeviceId vRouterId) {
126 if (vRouterId == null) {
127 object.remove(VROUTER_ID);
128 } else {
129 object.put(VROUTER_ID, vRouterId.toString());
130 }
131 return this;
132 }
133
134 /**
Charles Chand9681e72016-02-22 19:27:29 -0800135 * Gets names of ports to which SegmentRouting does not push subnet rules.
Charles Chanf2565a92016-02-10 20:46:58 -0800136 *
Charles Chand9681e72016-02-22 19:27:29 -0800137 * @return Set of port names, empty if not specified, or null
138 * if not valid
Charles Chanf2565a92016-02-10 20:46:58 -0800139 */
Charles Chand9681e72016-02-22 19:27:29 -0800140 public Set<ConnectPoint> suppressSubnet() {
141 if (!object.has(SUPPRESS_SUBNET)) {
142 return ImmutableSet.of();
Charles Chanf2565a92016-02-10 20:46:58 -0800143 }
144
Charles Chand9681e72016-02-22 19:27:29 -0800145 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
146 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_SUBNET);
Charles Chanf2565a92016-02-10 20:46:58 -0800147 for (JsonNode jsonNode : arrayNode) {
148 String portName = jsonNode.asText(null);
149 if (portName == null) {
150 return null;
151 }
Charles Chand9681e72016-02-22 19:27:29 -0800152 try {
153 builder.add(ConnectPoint.deviceConnectPoint(portName));
154 } catch (IllegalArgumentException e) {
155 return null;
156 }
Charles Chanf2565a92016-02-10 20:46:58 -0800157 }
158 return builder.build();
159 }
160
161 /**
Charles Chand9681e72016-02-22 19:27:29 -0800162 * Sets names of ports to which SegmentRouting does not push subnet rules.
Charles Chanf2565a92016-02-10 20:46:58 -0800163 *
Charles Chand9681e72016-02-22 19:27:29 -0800164 * @param suppressSubnet names of ports to which SegmentRouting does not push
165 * subnet rules
Charles Chanf2565a92016-02-10 20:46:58 -0800166 * @return this {@link SegmentRoutingAppConfig}
167 */
Charles Chand9681e72016-02-22 19:27:29 -0800168 public SegmentRoutingAppConfig setSuppressSubnet(Set<ConnectPoint> suppressSubnet) {
169 if (suppressSubnet == null) {
170 object.remove(SUPPRESS_SUBNET);
Charles Chanf2565a92016-02-10 20:46:58 -0800171 } else {
172 ArrayNode arrayNode = mapper.createArrayNode();
Charles Chand9681e72016-02-22 19:27:29 -0800173 suppressSubnet.forEach(connectPoint -> {
174 arrayNode.add(connectPoint.deviceId() + "/" + connectPoint.port());
Charles Chanf2565a92016-02-10 20:46:58 -0800175 });
Charles Chand9681e72016-02-22 19:27:29 -0800176 object.set(SUPPRESS_SUBNET, arrayNode);
177 }
178 return this;
179 }
180
181 /**
182 * Gets names of ports to which SegmentRouting does not push host rules.
183 *
184 * @return Set of port names, empty if not specified, or null
185 * if not valid
186 */
187 public Set<ConnectPoint> suppressHost() {
188 if (!object.has(SUPPRESS_HOST)) {
189 return ImmutableSet.of();
190 }
191
192 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
193 ArrayNode arrayNode = (ArrayNode) object.path(SUPPRESS_HOST);
194 for (JsonNode jsonNode : arrayNode) {
195 String portName = jsonNode.asText(null);
196 if (portName == null) {
197 return null;
198 }
199 try {
200 builder.add(ConnectPoint.deviceConnectPoint(portName));
201 } catch (IllegalArgumentException e) {
202 return null;
203 }
204 }
205 return builder.build();
206 }
207
208 /**
209 * Sets names of ports to which SegmentRouting does not push host rules.
210 *
211 * @param suppressHost names of ports to which SegmentRouting does not push
212 * host rules
213 * @return this {@link SegmentRoutingAppConfig}
214 */
215 public SegmentRoutingAppConfig setSuppressHost(Set<ConnectPoint> suppressHost) {
216 if (suppressHost == null) {
217 object.remove(SUPPRESS_HOST);
218 } else {
219 ArrayNode arrayNode = mapper.createArrayNode();
220 suppressHost.forEach(connectPoint -> {
221 arrayNode.add(connectPoint.deviceId() + "/" + connectPoint.port());
222 });
223 object.set(SUPPRESS_HOST, arrayNode);
Charles Chanf2565a92016-02-10 20:46:58 -0800224 }
225 return this;
226 }
227
Charles Chan5270ed02016-01-30 23:22:37 -0800228 @Override
229 public String toString() {
230 return toStringHelper(this)
231 .add("vRouterMacs", vRouterMacs())
Charles Chand9681e72016-02-22 19:27:29 -0800232 .add("vRouterId", vRouterId())
233 .add("suppressSubnet", suppressSubnet())
234 .add("suppressHost", suppressHost())
Charles Chan5270ed02016-01-30 23:22:37 -0800235 .toString();
236 }
237}