blob: a49c6fc1ae703c683fc7464989eb6edfe01330e0 [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
71 static final NodeKey<?> ROOT_NODE =
72 NodeKey.builder().schemaId(ROOT_NAME, DCS_NAMESPACE).build();
73
74 /**
75 * nodeKeys index for root node.
76 */
77 static final int ROOT_INDEX = 0;
78 /**
79 * nodeKeys index for devices node.
80 */
81 static final int DEVICES_INDEX = 1;
82 /**
83 * nodeKeys index for device node.
84 */
85 static final int DEVICE_INDEX = 2;
86
87 /**
88 * Tests if specified path points to root node of a Device.
89 *
90 * @param path to test.
91 * @return true if path points to root node of a Device.
92 */
93 public static boolean isDeviceRootNode(ResourceId path) {
94 return path.nodeKeys().size() == 3 &&
95 isUnderDeviceRootNode(path);
96 }
97
98 /**
99 * Tests if specified path points to root node of a Device.
100 *
101 * @param path to test.
102 * @return true if path points to root node of a Device.
103 */
104 public static boolean isUnderDeviceRootNode(ResourceId path) {
105 return path.nodeKeys().size() >= 3 &&
106 // TODO Would be better to test whole schemeId
107 DEVICE_NAME.equals(path.nodeKeys().get(DEVICE_INDEX).schemaId().name()) &&
108 (path.nodeKeys().get(DEVICE_INDEX) instanceof ListKey) &&
109 // TODO Would be better to test whole schemeId
110 DEVICES_NAME.equals(path.nodeKeys().get(DEVICES_INDEX).schemaId().name()) &&
111 ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX));
112 }
113
114 /**
115 * Tests if specified path points to root or devices node.
116 *
117 * @param path to test.
118 * @return true if path points to root node of a Device.
119 */
120 public static boolean isRootOrDevicesNode(ResourceId path) {
121 return isDevicesNode(path) ||
122 isRootNode(path);
123 }
124
125 public static boolean isDevicesNode(ResourceId path) {
126 return path.nodeKeys().size() == 2 &&
127 // TODO Would be better to test whole schemeId
128 DEVICES_NAME.equals(path.nodeKeys().get(DEVICES_INDEX).schemaId().name()) &&
129 ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX));
130 }
131
132 public static boolean isRootNode(ResourceId path) {
133 return path.nodeKeys().size() == 1 &&
134 ROOT_NODE.equals(path.nodeKeys().get(ROOT_INDEX));
135 }
136
137 /**
138 * Transforms device resource path to DeviceId.
139 *
140 * @param path pointing to somewhere in the subtree of a device
141 * @return DeviceId
142 * @throws IllegalArgumentException if the path was not part of devices tree
143 */
144 public static DeviceId toDeviceId(ResourceId path) {
145 checkArgument(isUnderDeviceRootNode(path), path);
146 // FIXME if we decide to drop any of intermediate nodes
147 // "/" - "devices" - "device"
148 return toDeviceId(path.nodeKeys().get(DEVICE_INDEX));
149 }
150
151 /**
152 * Transforms root node of a device to corresponding DeviceId.
153 *
154 * @param deviceRoot NodeKey of a device root node
155 * @return DeviceId
156 * @throws IllegalArgumentException if not a device node
157 */
158 public static DeviceId toDeviceId(NodeKey<?> deviceRoot) {
159 // TODO Would be better to test whole schemeId
160 if (!DEVICE_NAME.equals(deviceRoot.schemaId().name())) {
161 throw new IllegalArgumentException(deviceRoot + " is not a device node");
162 }
163
164 if (deviceRoot instanceof ListKey) {
165 ListKey device = (ListKey) deviceRoot;
166 Optional<DeviceId> did = device.keyLeafs().stream()
167 // TODO If we decide to define ONOS schema for device ID,
168 // use whole schemaId to filter, not only by name
169 .filter(kl -> DEVICE_ID_KL_NAME.equals(kl.leafSchema().name()))
170 .map(KeyLeaf::leafValAsString)
171 .map(DeviceId::deviceId)
172 .findFirst();
173
174 if (did.isPresent()) {
175 return did.get();
176 }
177 throw new IllegalArgumentException("device-id not found in " + deviceRoot);
178 }
179 throw new IllegalArgumentException("Unexpected type " + deviceRoot.getClass());
180 }
181
182 /**
183 * Transforms DeviceId into a ResourceId pointing to device root node.
184 *
185 * @param deviceId to transform
186 * @return ResourceId
187 */
188 public static ResourceId toResourceId(DeviceId deviceId) {
189 return ResourceId.builder()
190 .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE)
191 .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE)
192 .addBranchPointSchema(DEVICE_NAME, DCS_NAMESPACE)
193 .addKeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, deviceId.toString())
194 .build();
195 }
196
197}