blob: 605ae63157d60a13b041f42e8ad947a5adf6426e [file] [log] [blame]
Yuta HIGUCHI24057822017-08-02 15:03:51 -07001/*
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 */
16package org.onosproject.d.config;
17
18import static com.google.common.base.Preconditions.checkArgument;
19import java.util.Optional;
20
21import org.onosproject.net.DeviceId;
22import org.onosproject.yang.model.KeyLeaf;
23import org.onosproject.yang.model.ListKey;
24import org.onosproject.yang.model.NodeKey;
25import org.onosproject.yang.model.ResourceId;
26
27import com.google.common.annotations.Beta;
28
29/**
30 * Utility related to device ResourceId.
31 */
32@Beta
33public abstract class DeviceResourceIds {
34
35 // assuming following device tree structure
36 // - "/"
37 // +- devices
38 // +- device (=device root node:ListKey)
39
40
41 // FIXME randomly defined namespace, replace with something appropriate
42 /**
43 * SchemaId namespace for DCS defined nodes.
44 */
45 public static final String DCS_NAMESPACE = "org.onosproject.dcs";
46
47 /**
48 * SchemaId name for root node.
49 */
50 public static final String ROOT_NAME = "/";
51 /**
52 * SchemaId name for devices node.
53 */
54 public static final String DEVICES_NAME = "devices";
55 /**
56 * SchemaId name for device node.
57 */
58 public static final String DEVICE_NAME = "device";
59 /**
60 * KeyLeaf {@code name}, which holds DeviceId information on device node.
61 */
62 public static final String DEVICE_ID_KL_NAME = "device-id";
63
64 /**
65 * ResourceId pointing at root node.
66 */
67 public static final ResourceId ROOT_ID = ResourceId.builder()
68 .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE)
69 .build();
70
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -070071 public static final ResourceId DEVICES_ID = ResourceId.builder()
72 .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE)
73 .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE)
74 .build();
75
76
Yuta HIGUCHI24057822017-08-02 15:03:51 -070077 static final NodeKey<?> ROOT_NODE =
78 NodeKey.builder().schemaId(ROOT_NAME, DCS_NAMESPACE).build();
79
80 /**
81 * nodeKeys index for root node.
82 */
83 static final int ROOT_INDEX = 0;
84 /**
85 * nodeKeys index for devices node.
86 */
87 static final int DEVICES_INDEX = 1;
88 /**
89 * nodeKeys index for device node.
90 */
91 static final int DEVICE_INDEX = 2;
92
93 /**
94 * Tests if specified path points to root node of a Device.
95 *
96 * @param path to test.
97 * @return true if path points to root node of a Device.
98 */
99 public static boolean isDeviceRootNode(ResourceId path) {
100 return path.nodeKeys().size() == 3 &&
101 isUnderDeviceRootNode(path);
102 }
103
104 /**
105 * Tests if specified path points to root node of a Device.
106 *
107 * @param path to test.
108 * @return true if path points to root node of a Device.
109 */
110 public static boolean isUnderDeviceRootNode(ResourceId path) {
111 return path.nodeKeys().size() >= 3 &&
112 // TODO Would be better to test whole schemeId
113 DEVICE_NAME.equals(path.nodeKeys().get(DEVICE_INDEX).schemaId().name()) &&
114 (path.nodeKeys().get(DEVICE_INDEX) instanceof ListKey) &&
115 // TODO Would be better to test whole schemeId
116 DEVICES_NAME.equals(path.nodeKeys().get(DEVICES_INDEX).schemaId().name()) &&
117 ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX));
118 }
119
120 /**
121 * Tests if specified path points to root or devices node.
122 *
123 * @param path to test.
124 * @return true if path points to root node of a Device.
125 */
126 public static boolean isRootOrDevicesNode(ResourceId path) {
127 return isDevicesNode(path) ||
128 isRootNode(path);
129 }
130
131 public static boolean isDevicesNode(ResourceId path) {
132 return path.nodeKeys().size() == 2 &&
133 // TODO Would be better to test whole schemeId
134 DEVICES_NAME.equals(path.nodeKeys().get(DEVICES_INDEX).schemaId().name()) &&
135 ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX));
136 }
137
138 public static boolean isRootNode(ResourceId path) {
139 return path.nodeKeys().size() == 1 &&
140 ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX));
141 }
142
143 /**
144 * Transforms device resource path to DeviceId.
145 *
146 * @param path pointing to somewhere in the subtree of a device
147 * @return DeviceId
148 * @throws IllegalArgumentException if the path was not part of devices tree
149 */
150 public static DeviceId toDeviceId(ResourceId path) {
151 checkArgument(isUnderDeviceRootNode(path), path);
152 // FIXME if we decide to drop any of intermediate nodes
153 // "/" - "devices" - "device"
154 return toDeviceId(path.nodeKeys().get(DEVICE_INDEX));
155 }
156
157 /**
158 * Transforms root node of a device to corresponding DeviceId.
159 *
160 * @param deviceRoot NodeKey of a device root node
161 * @return DeviceId
162 * @throws IllegalArgumentException if not a device node
163 */
164 public static DeviceId toDeviceId(NodeKey<?> deviceRoot) {
165 // TODO Would be better to test whole schemeId
166 if (!DEVICE_NAME.equals(deviceRoot.schemaId().name())) {
167 throw new IllegalArgumentException(deviceRoot + " is not a device node");
168 }
169
170 if (deviceRoot instanceof ListKey) {
171 ListKey device = (ListKey) deviceRoot;
172 Optional<DeviceId> did = device.keyLeafs().stream()
173 // TODO If we decide to define ONOS schema for device ID,
174 // use whole schemaId to filter, not only by name
175 .filter(kl -> DEVICE_ID_KL_NAME.equals(kl.leafSchema().name()))
176 .map(KeyLeaf::leafValAsString)
177 .map(DeviceId::deviceId)
178 .findFirst();
179
180 if (did.isPresent()) {
181 return did.get();
182 }
183 throw new IllegalArgumentException("device-id not found in " + deviceRoot);
184 }
185 throw new IllegalArgumentException("Unexpected type " + deviceRoot.getClass());
186 }
187
188 /**
189 * Transforms DeviceId into a ResourceId pointing to device root node.
190 *
191 * @param deviceId to transform
192 * @return ResourceId
193 */
194 public static ResourceId toResourceId(DeviceId deviceId) {
195 return ResourceId.builder()
196 .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE)
197 .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE)
198 .addBranchPointSchema(DEVICE_NAME, DCS_NAMESPACE)
199 .addKeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, deviceId.toString())
200 .build();
201 }
202
203}