blob: 089f8b02b9bffb920c25d845f1e15dc34957da7c [file] [log] [blame]
Charles Chanfc5c7802016-05-17 13:13:55 -07001/*
2 * Copyright 2016-present 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.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.
35 */
36public class XConnectConfig extends Config<ApplicationId> {
37 private static final String VLAN = "vlan";
38 private static final String PORTS = "ports";
39 private static final String NAME = "name"; // dummy field for naming
40
41 private static final String UNEXPECTED_FIELD_NAME = "Unexpected field name";
42
43 @Override
44 public boolean isValid() {
45 try {
46 getXconnects().forEach(this::getPorts);
47 } catch (IllegalArgumentException e) {
48 return false;
49 }
50 return true;
51 }
52
53 /**
54 * Returns all xconnect keys.
55 *
56 * @return all keys (device/vlan pairs)
57 * @throws IllegalArgumentException if wrong format
58 */
59 public Set<XConnectStoreKey> getXconnects() {
60 ImmutableSet.Builder<XConnectStoreKey> builder = ImmutableSet.builder();
61 object.fields().forEachRemaining(entry -> {
62 DeviceId deviceId = DeviceId.deviceId(entry.getKey());
63 builder.addAll(getXconnects(deviceId));
64 });
65 return builder.build();
66 }
67
68 /**
69 * Returns xconnect keys of given device.
70 *
71 * @param deviceId ID of the device from which we want to get XConnect info
72 * @return xconnect keys (device/vlan pairs) of given device
73 * @throws IllegalArgumentException if wrong format
74 */
75 public Set<XConnectStoreKey> getXconnects(DeviceId deviceId) {
76 ImmutableSet.Builder<XConnectStoreKey> builder = ImmutableSet.builder();
77 JsonNode vlanPortPair = object.get(deviceId.toString());
78 if (vlanPortPair != null) {
79 vlanPortPair.forEach(jsonNode -> {
80 if (!hasOnlyFields((ObjectNode) jsonNode, VLAN, PORTS, NAME)) {
81 throw new IllegalArgumentException(UNEXPECTED_FIELD_NAME);
82 }
83 VlanId vlanId = VlanId.vlanId((short) jsonNode.get(VLAN).asInt());
84 builder.add(new XConnectStoreKey(deviceId, vlanId));
85 });
86 }
87 return builder.build();
88 }
89
90 /**
91 * Returns ports of given xconnect key.
92 *
93 * @param xconnect xconnect key
94 * @return set of two ports associated with given xconnect key
95 * @throws IllegalArgumentException if wrong format
96 */
97 public Set<PortNumber> getPorts(XConnectStoreKey xconnect) {
98 ImmutableSet.Builder<PortNumber> builder = ImmutableSet.builder();
99 object.get(xconnect.deviceId().toString()).forEach(vlanPortsPair -> {
100 if (xconnect.vlanId().toShort() == vlanPortsPair.get(VLAN).asInt()) {
101 int portCount = vlanPortsPair.get(PORTS).size();
102 checkArgument(portCount == 2,
103 "Expect 2 ports but found " + portCount + " on " + xconnect);
104 vlanPortsPair.get(PORTS).forEach(portNode -> {
105 builder.add(PortNumber.portNumber(portNode.asInt()));
106 });
107 }
108 });
109 return builder.build();
110 }
111}