blob: e39fb18f86066a0eb651600a22708931e59ec6b5 [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;
24import org.onosproject.net.config.Config;
25import java.util.Set;
26
27import static com.google.common.base.MoreObjects.toStringHelper;
28
29/**
30 * App configuration object for Segment Routing.
31 */
32public class SegmentRoutingAppConfig extends Config<ApplicationId> {
33 private static final String VROUTER_MACS = "vRouterMacs";
34
35 @Override
36 public boolean isValid() {
37 return hasOnlyFields(VROUTER_MACS) && vRouterMacs() != null;
38 }
39
40 /**
41 * Gets vRouters from the config.
42 *
43 * @return a set of vRouter MAC addresses
44 */
45 public Set<MacAddress> vRouterMacs() {
46 if (!object.has(VROUTER_MACS)) {
47 return null;
48 }
49
50 ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder();
51 ArrayNode arrayNode = (ArrayNode) object.path(VROUTER_MACS);
52 for (JsonNode jsonNode : arrayNode) {
53 MacAddress mac;
54
55 String macStr = jsonNode.asText(null);
56 if (macStr == null) {
57 return null;
58 }
59 try {
60 mac = MacAddress.valueOf(macStr);
61 } catch (IllegalArgumentException e) {
62 return null;
63 }
64
65 builder.add(mac);
66 }
67 return builder.build();
68 }
69
70 /**
71 * Sets vRouters to the config.
72 *
73 * @param vRouterMacs a set of vRouter MAC addresses
74 * @return this {@link SegmentRoutingAppConfig}
75 */
76 public SegmentRoutingAppConfig setVRouterMacs(Set<MacAddress> vRouterMacs) {
77 if (vRouterMacs == null) {
78 object.remove(VROUTER_MACS);
79 } else {
80 ArrayNode arrayNode = mapper.createArrayNode();
81
82 vRouterMacs.forEach(mac -> {
83 arrayNode.add(mac.toString());
84 });
85
86 object.set(VROUTER_MACS, arrayNode);
87 }
88 return this;
89 }
90
91 @Override
92 public String toString() {
93 return toStringHelper(this)
94 .add("vRouterMacs", vRouterMacs())
95 .toString();
96 }
97}