Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | package org.onosproject.d.config; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import java.util.Optional; |
| 20 | |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.yang.model.KeyLeaf; |
| 23 | import org.onosproject.yang.model.ListKey; |
| 24 | import org.onosproject.yang.model.NodeKey; |
| 25 | import org.onosproject.yang.model.ResourceId; |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 26 | import org.onosproject.yang.model.SchemaId; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 27 | |
| 28 | import com.google.common.annotations.Beta; |
| 29 | |
| 30 | /** |
| 31 | * Utility related to device ResourceId. |
| 32 | */ |
| 33 | @Beta |
| 34 | public abstract class DeviceResourceIds { |
| 35 | |
| 36 | // assuming following device tree structure |
| 37 | // - "/" |
| 38 | // +- devices |
| 39 | // +- device (=device root node:ListKey) |
| 40 | |
| 41 | |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 42 | /** |
| 43 | * SchemaId namespace for DCS defined nodes. |
| 44 | */ |
| 45 | public static final String DCS_NAMESPACE = "org.onosproject.dcs"; |
| 46 | |
Yuta HIGUCHI | 825401e | 2018-02-27 13:23:25 -0800 | [diff] [blame^] | 47 | // FIXME '/' is problematic name from RFC 7950/7951 point of view |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 48 | /** |
| 49 | * SchemaId name for root node. |
| 50 | */ |
| 51 | public static final String ROOT_NAME = "/"; |
| 52 | /** |
| 53 | * SchemaId name for devices node. |
| 54 | */ |
| 55 | public static final String DEVICES_NAME = "devices"; |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 56 | public static final SchemaId DEVICES_SCHEMA = new SchemaId(DEVICES_NAME, DCS_NAMESPACE); |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 57 | /** |
| 58 | * SchemaId name for device node. |
| 59 | */ |
| 60 | public static final String DEVICE_NAME = "device"; |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 61 | public static final SchemaId DEVICE_SCHEMA = new SchemaId(DEVICE_NAME, DCS_NAMESPACE); |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 62 | /** |
| 63 | * KeyLeaf {@code name}, which holds DeviceId information on device node. |
| 64 | */ |
| 65 | public static final String DEVICE_ID_KL_NAME = "device-id"; |
| 66 | |
| 67 | /** |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 68 | * Absolute ResourceId pointing at root node. |
| 69 | * @deprecated Use {@link ResourceIds#ROOT_ID} instead |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 70 | */ |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 71 | @Deprecated |
| 72 | public static final ResourceId ROOT_ID = ResourceIds.ROOT_ID; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 73 | |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Absolute ResourceId pointing at 'devices' node. |
| 76 | */ |
Yuta HIGUCHI | ea1fe52 | 2017-09-01 14:24:02 -0700 | [diff] [blame] | 77 | public static final ResourceId DEVICES_ID = ResourceId.builder() |
| 78 | .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE) |
| 79 | .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE) |
| 80 | .build(); |
| 81 | |
| 82 | |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 83 | static final NodeKey<?> ROOT_NODE = |
| 84 | NodeKey.builder().schemaId(ROOT_NAME, DCS_NAMESPACE).build(); |
| 85 | |
| 86 | /** |
| 87 | * nodeKeys index for root node. |
| 88 | */ |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 89 | @Deprecated |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 90 | static final int ROOT_INDEX = 0; |
| 91 | /** |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 92 | * nodeKeys index relative from root for devices node. |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 93 | */ |
| 94 | static final int DEVICES_INDEX = 1; |
| 95 | /** |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 96 | * nodeKeys index relative from root for device node. |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 97 | */ |
| 98 | static final int DEVICE_INDEX = 2; |
| 99 | |
| 100 | /** |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 101 | * Converts root relative ResourceId used by DynamicConfigEvent. |
| 102 | * |
| 103 | * @param rootRelative resource Id |
| 104 | * @return absolute ResourceId. |
| 105 | */ |
| 106 | @Beta |
| 107 | public static ResourceId toAbsolute(ResourceId rootRelative) { |
| 108 | if (ResourceIds.startsWithRootNode(rootRelative)) { |
| 109 | return rootRelative; |
| 110 | } |
| 111 | return ResourceIds.concat(ResourceIds.ROOT_ID, rootRelative); |
| 112 | } |
| 113 | |
| 114 | /** |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 115 | * Tests if specified path points to root node of a Device. |
| 116 | * |
| 117 | * @param path to test. |
| 118 | * @return true if path points to root node of a Device. |
| 119 | */ |
| 120 | public static boolean isDeviceRootNode(ResourceId path) { |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 121 | if (ResourceIds.startsWithRootNode(path)) { |
| 122 | return path.nodeKeys().size() == 3 && |
| 123 | isUnderDeviceRootNode(path); |
| 124 | } else { |
| 125 | return path.nodeKeys().size() == 2 && |
| 126 | isUnderDeviceRootNode(path); |
| 127 | } |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 131 | * Tests if specified path points to root node of a Device or it's subtree. |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 132 | * |
| 133 | * @param path to test. |
| 134 | * @return true if path points to root node of a Device. |
| 135 | */ |
| 136 | public static boolean isUnderDeviceRootNode(ResourceId path) { |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 137 | int rootIdx = ResourceIds.startsWithRootNode(path) ? 0 : -1; |
| 138 | return path.nodeKeys().size() >= rootIdx + 3 && |
| 139 | DEVICE_SCHEMA.equals(path.nodeKeys().get(rootIdx + DEVICE_INDEX).schemaId()) && |
| 140 | (path.nodeKeys().get(rootIdx + DEVICE_INDEX) instanceof ListKey) && |
| 141 | DEVICES_SCHEMA.equals(path.nodeKeys().get(rootIdx + DEVICES_INDEX).schemaId()); } |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Tests if specified path points to root or devices node. |
| 145 | * |
| 146 | * @param path to test. |
| 147 | * @return true if path points to root node of a Device. |
| 148 | */ |
| 149 | public static boolean isRootOrDevicesNode(ResourceId path) { |
| 150 | return isDevicesNode(path) || |
| 151 | isRootNode(path); |
| 152 | } |
| 153 | |
| 154 | public static boolean isDevicesNode(ResourceId path) { |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 155 | int rootIdx = ResourceIds.startsWithRootNode(path) ? 0 : -1; |
| 156 | return path.nodeKeys().size() == rootIdx + 2 && |
| 157 | DEVICES_SCHEMA.equals(path.nodeKeys().get(rootIdx + DEVICES_INDEX).schemaId()); |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | public static boolean isRootNode(ResourceId path) { |
| 161 | return path.nodeKeys().size() == 1 && |
| 162 | ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX)); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Transforms device resource path to DeviceId. |
| 167 | * |
| 168 | * @param path pointing to somewhere in the subtree of a device |
| 169 | * @return DeviceId |
| 170 | * @throws IllegalArgumentException if the path was not part of devices tree |
| 171 | */ |
| 172 | public static DeviceId toDeviceId(ResourceId path) { |
| 173 | checkArgument(isUnderDeviceRootNode(path), path); |
| 174 | // FIXME if we decide to drop any of intermediate nodes |
| 175 | // "/" - "devices" - "device" |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 176 | int rootIdx = ResourceIds.startsWithRootNode(path) ? 0 : -1; |
| 177 | return toDeviceId(path.nodeKeys().get(rootIdx + DEVICE_INDEX)); |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Transforms root node of a device to corresponding DeviceId. |
| 182 | * |
| 183 | * @param deviceRoot NodeKey of a device root node |
| 184 | * @return DeviceId |
| 185 | * @throws IllegalArgumentException if not a device node |
| 186 | */ |
| 187 | public static DeviceId toDeviceId(NodeKey<?> deviceRoot) { |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 188 | if (!DEVICE_SCHEMA.equals(deviceRoot.schemaId())) { |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 189 | throw new IllegalArgumentException(deviceRoot + " is not a device node"); |
| 190 | } |
| 191 | |
| 192 | if (deviceRoot instanceof ListKey) { |
| 193 | ListKey device = (ListKey) deviceRoot; |
| 194 | Optional<DeviceId> did = device.keyLeafs().stream() |
| 195 | // TODO If we decide to define ONOS schema for device ID, |
| 196 | // use whole schemaId to filter, not only by name |
| 197 | .filter(kl -> DEVICE_ID_KL_NAME.equals(kl.leafSchema().name())) |
| 198 | .map(KeyLeaf::leafValAsString) |
| 199 | .map(DeviceId::deviceId) |
| 200 | .findFirst(); |
| 201 | |
| 202 | if (did.isPresent()) { |
| 203 | return did.get(); |
| 204 | } |
| 205 | throw new IllegalArgumentException("device-id not found in " + deviceRoot); |
| 206 | } |
| 207 | throw new IllegalArgumentException("Unexpected type " + deviceRoot.getClass()); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Transforms DeviceId into a ResourceId pointing to device root node. |
| 212 | * |
| 213 | * @param deviceId to transform |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 214 | * @return absolute ResourceId |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 215 | */ |
| 216 | public static ResourceId toResourceId(DeviceId deviceId) { |
| 217 | return ResourceId.builder() |
| 218 | .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE) |
| 219 | .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE) |
| 220 | .addBranchPointSchema(DEVICE_NAME, DCS_NAMESPACE) |
| 221 | .addKeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, deviceId.toString()) |
| 222 | .build(); |
| 223 | } |
| 224 | |
| 225 | } |