blob: 6689b93e0f8d693dc378601df1d1aef7c902c969 [file] [log] [blame]
Charles Chan82f19972016-05-17 13:13:55 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chan82f19972016-05-17 13:13:55 -07003 *
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.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.onlab.packet.VlanId;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.config.Config;
27import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
28
29import java.util.Set;
30
31import static com.google.common.base.Preconditions.checkArgument;
32
33/**
34 * Configuration object for cross-connect.
Charles Chan8d316332018-06-19 20:31:57 -070035 *
36 * @deprecated in ONOS 1.12. Replaced by {@link org.onosproject.segmentrouting.xconnect.impl.XconnectManager}
Charles Chan82f19972016-05-17 13:13:55 -070037 */
Charles Chan8d316332018-06-19 20:31:57 -070038@Deprecated
Charles Chan82f19972016-05-17 13:13:55 -070039public class XConnectConfig extends Config<ApplicationId> {
40 private static final String VLAN = "vlan";
41 private static final String PORTS = "ports";
42 private static final String NAME = "name"; // dummy field for naming
43
44 private static final String UNEXPECTED_FIELD_NAME = "Unexpected field name";
45
46 @Override
47 public boolean isValid() {
48 try {
49 getXconnects().forEach(this::getPorts);
50 } catch (IllegalArgumentException e) {
51 return false;
52 }
53 return true;
54 }
55
56 /**
57 * Returns all xconnect keys.
58 *
59 * @return all keys (device/vlan pairs)
60 * @throws IllegalArgumentException if wrong format
61 */
62 public Set<XConnectStoreKey> getXconnects() {
63 ImmutableSet.Builder<XConnectStoreKey> builder = ImmutableSet.builder();
64 object.fields().forEachRemaining(entry -> {
65 DeviceId deviceId = DeviceId.deviceId(entry.getKey());
66 builder.addAll(getXconnects(deviceId));
67 });
68 return builder.build();
69 }
70
71 /**
72 * Returns xconnect keys of given device.
73 *
74 * @param deviceId ID of the device from which we want to get XConnect info
75 * @return xconnect keys (device/vlan pairs) of given device
76 * @throws IllegalArgumentException if wrong format
77 */
78 public Set<XConnectStoreKey> getXconnects(DeviceId deviceId) {
79 ImmutableSet.Builder<XConnectStoreKey> builder = ImmutableSet.builder();
80 JsonNode vlanPortPair = object.get(deviceId.toString());
81 if (vlanPortPair != null) {
82 vlanPortPair.forEach(jsonNode -> {
83 if (!hasOnlyFields((ObjectNode) jsonNode, VLAN, PORTS, NAME)) {
84 throw new IllegalArgumentException(UNEXPECTED_FIELD_NAME);
85 }
86 VlanId vlanId = VlanId.vlanId((short) jsonNode.get(VLAN).asInt());
87 builder.add(new XConnectStoreKey(deviceId, vlanId));
88 });
89 }
90 return builder.build();
91 }
92
93 /**
94 * Returns ports of given xconnect key.
95 *
96 * @param xconnect xconnect key
97 * @return set of two ports associated with given xconnect key
98 * @throws IllegalArgumentException if wrong format
99 */
100 public Set<PortNumber> getPorts(XConnectStoreKey xconnect) {
101 ImmutableSet.Builder<PortNumber> builder = ImmutableSet.builder();
102 object.get(xconnect.deviceId().toString()).forEach(vlanPortsPair -> {
103 if (xconnect.vlanId().toShort() == vlanPortsPair.get(VLAN).asInt()) {
104 int portCount = vlanPortsPair.get(PORTS).size();
105 checkArgument(portCount == 2,
106 "Expect 2 ports but found " + portCount + " on " + xconnect);
107 vlanPortsPair.get(PORTS).forEach(portNode -> {
108 builder.add(PortNumber.portNumber(portNode.asInt()));
109 });
110 }
111 });
112 return builder.build();
113 }
114}