blob: 163cc1c9c1c8c32fcc1ffe880b0cdfc35e0f14ff [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 Chanf2565a92016-02-10 20:46:58 -080024import org.onosproject.net.DeviceId;
Charles Chan5270ed02016-01-30 23:22:37 -080025import org.onosproject.net.config.Config;
26import java.util.Set;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29
30/**
31 * App configuration object for Segment Routing.
32 */
33public class SegmentRoutingAppConfig extends Config<ApplicationId> {
34 private static final String VROUTER_MACS = "vRouterMacs";
Charles Chanf2565a92016-02-10 20:46:58 -080035 private static final String VROUTER_ID = "vRouterId";
36 private static final String EXCLUDE_PORTS = "excludePorts";
Charles Chan5270ed02016-01-30 23:22:37 -080037
38 @Override
39 public boolean isValid() {
Charles Chanf2565a92016-02-10 20:46:58 -080040 return hasOnlyFields(VROUTER_MACS, VROUTER_ID, EXCLUDE_PORTS) &&
41 vRouterMacs() != null && vRouterId() != null &&
42 excludePorts() != null;
Charles Chan5270ed02016-01-30 23:22:37 -080043 }
44
45 /**
46 * Gets vRouters from the config.
47 *
48 * @return a set of vRouter MAC addresses
49 */
50 public Set<MacAddress> vRouterMacs() {
51 if (!object.has(VROUTER_MACS)) {
52 return null;
53 }
54
55 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
56 ArrayNode arrayNode = (ArrayNode) object.path(VROUTER_MACS);
57 for (JsonNode jsonNode : arrayNode) {
58 MacAddress mac;
59
60 String macStr = jsonNode.asText(null);
61 if (macStr == null) {
62 return null;
63 }
64 try {
65 mac = MacAddress.valueOf(macStr);
66 } catch (IllegalArgumentException e) {
67 return null;
68 }
69
70 builder.add(mac);
71 }
72 return builder.build();
73 }
74
75 /**
76 * Sets vRouters to the config.
77 *
78 * @param vRouterMacs a set of vRouter MAC addresses
79 * @return this {@link SegmentRoutingAppConfig}
80 */
81 public SegmentRoutingAppConfig setVRouterMacs(Set<MacAddress> vRouterMacs) {
82 if (vRouterMacs == null) {
83 object.remove(VROUTER_MACS);
84 } else {
85 ArrayNode arrayNode = mapper.createArrayNode();
86
87 vRouterMacs.forEach(mac -> {
88 arrayNode.add(mac.toString());
89 });
90
91 object.set(VROUTER_MACS, arrayNode);
92 }
93 return this;
94 }
95
Charles Chanf2565a92016-02-10 20:46:58 -080096 /**
97 * Gets vRouter device ID.
98 *
99 * @return vRouter device ID, or null if not valid
100 */
101 public DeviceId vRouterId() {
102 if (!object.has(VROUTER_ID)) {
103 return null;
104 }
105
106 try {
107 return DeviceId.deviceId(object.path(VROUTER_ID).asText());
108 } catch (IllegalArgumentException e) {
109 return null;
110 }
111 }
112
113 /**
114 * Sets vRouter device ID.
115 *
116 * @param vRouterId vRouter device ID
117 * @return this {@link SegmentRoutingAppConfig}
118 */
119 public SegmentRoutingAppConfig setVRouterId(DeviceId vRouterId) {
120 if (vRouterId == null) {
121 object.remove(VROUTER_ID);
122 } else {
123 object.put(VROUTER_ID, vRouterId.toString());
124 }
125 return this;
126 }
127
128 /**
129 * Gets names of ports that are ignored by SegmentRouting.
130 *
131 * @return set of port names
132 */
133 public Set<String> excludePorts() {
134 if (!object.has(EXCLUDE_PORTS)) {
135 return null;
136 }
137
138 ImmutableSet.Builder<String> builder = ImmutableSet.builder();
139 ArrayNode arrayNode = (ArrayNode) object.path(EXCLUDE_PORTS);
140 for (JsonNode jsonNode : arrayNode) {
141 String portName = jsonNode.asText(null);
142 if (portName == null) {
143 return null;
144 }
145 builder.add(portName);
146 }
147 return builder.build();
148 }
149
150 /**
151 * Sets names of ports that are ignored by SegmentRouting.
152 *
153 * @paran excludePorts names of ports that are ignored by SegmentRouting
154 * @return this {@link SegmentRoutingAppConfig}
155 */
156 public SegmentRoutingAppConfig setExcludePorts(Set<String> excludePorts) {
157 if (excludePorts == null) {
158 object.remove(EXCLUDE_PORTS);
159 } else {
160 ArrayNode arrayNode = mapper.createArrayNode();
161 excludePorts.forEach(portName -> {
162 arrayNode.add(portName);
163 });
164 object.set(EXCLUDE_PORTS, arrayNode);
165 }
166 return this;
167 }
168
Charles Chan5270ed02016-01-30 23:22:37 -0800169 @Override
170 public String toString() {
171 return toStringHelper(this)
172 .add("vRouterMacs", vRouterMacs())
Charles Chanf2565a92016-02-10 20:46:58 -0800173 .add("excludePorts", excludePorts())
Charles Chan5270ed02016-01-30 23:22:37 -0800174 .toString();
175 }
176}