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 | |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 19 | import org.onosproject.yang.model.DataNode; |
| 20 | import org.onosproject.yang.model.KeyLeaf; |
| 21 | import org.onosproject.yang.model.LeafListKey; |
| 22 | import org.onosproject.yang.model.ListKey; |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 23 | import org.onosproject.yang.model.NodeKey; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 24 | import org.onosproject.yang.model.ResourceId; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 25 | import org.onosproject.yang.model.SchemaId; |
| 26 | import org.slf4j.Logger; |
| 27 | |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 28 | import java.util.Iterator; |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Utility related to ResourceId. |
| 36 | */ |
| 37 | @Beta |
| 38 | public abstract class ResourceIds { |
| 39 | |
| 40 | private static final Logger log = getLogger(ResourceIds.class); |
| 41 | |
| 42 | /** |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 43 | * Absolute ResourceId pointing at root node. |
| 44 | */ |
| 45 | public static final ResourceId ROOT_ID = ResourceId.builder() |
| 46 | .addBranchPointSchema(DeviceResourceIds.ROOT_NAME, |
| 47 | DeviceResourceIds.DCS_NAMESPACE) |
| 48 | .build(); |
| 49 | |
| 50 | /** |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 51 | * Builds the ResourceId of specified {@code node}. |
| 52 | * |
| 53 | * @param parent ResourceId of {@code node} parent |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 54 | * @param node to create ResourceId. |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 55 | * @return ResourceId of {@code node} |
| 56 | */ |
| 57 | public static ResourceId resourceId(ResourceId parent, DataNode node) { |
| 58 | final ResourceId.Builder builder; |
| 59 | if (parent == null) { |
| 60 | builder = ResourceId.builder(); |
| 61 | } else { |
| 62 | try { |
| 63 | builder = parent.copyBuilder(); |
| 64 | } catch (CloneNotSupportedException e) { |
| 65 | throw new IllegalArgumentException(e); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | SchemaId sid = node.key().schemaId(); |
| 70 | switch (node.type()) { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 71 | case MULTI_INSTANCE_LEAF_VALUE_NODE: |
| 72 | builder.addLeafListBranchPoint(sid.name(), sid.namespace(), |
| 73 | ((LeafListKey) node.key()).asString()); |
| 74 | break; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 75 | |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 76 | case MULTI_INSTANCE_NODE: |
| 77 | builder.addBranchPointSchema(sid.name(), sid.namespace()); |
| 78 | for (KeyLeaf keyLeaf : ((ListKey) node.key()).keyLeafs()) { |
| 79 | builder.addKeyLeaf(keyLeaf.leafSchema().name(), |
| 80 | keyLeaf.leafSchema().namespace(), |
| 81 | keyLeaf.leafValAsString()); |
| 82 | } |
| 83 | break; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 84 | |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 85 | case SINGLE_INSTANCE_LEAF_VALUE_NODE: |
| 86 | case SINGLE_INSTANCE_NODE: |
| 87 | builder.addBranchPointSchema(sid.name(), sid.namespace()); |
| 88 | break; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 89 | |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 90 | default: |
| 91 | throw new IllegalArgumentException("Unknown type " + node); |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 92 | |
| 93 | } |
| 94 | |
| 95 | return builder.build(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Concats {@code path} after {@code prefix}. |
| 100 | * |
| 101 | * @param prefix path |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 102 | * @param path to append after {@code path} |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 103 | * @return concatenated ResouceId |
| 104 | */ |
| 105 | public static ResourceId concat(ResourceId prefix, ResourceId path) { |
| 106 | checkArgument(!path.nodeKeys().contains(DeviceResourceIds.ROOT_NODE), |
| 107 | "%s was already absolute path", path); |
| 108 | try { |
| 109 | return prefix.copyBuilder().append(path).build(); |
| 110 | } catch (CloneNotSupportedException e) { |
| 111 | log.error("Could not copy {}", path, e); |
| 112 | throw new IllegalArgumentException("Could not copy " + path, e); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
Yuta HIGUCHI | 4070c04 | 2017-08-28 13:47:20 -0700 | [diff] [blame] | 118 | * Returns {@code child} as relative ResourceId against {@code base}. |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 119 | * |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 120 | * @param base ResourceId |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 121 | * @param child ResourceId to relativize |
| 122 | * @return relative ResourceId |
| 123 | */ |
| 124 | public static ResourceId relativize(ResourceId base, ResourceId child) { |
| 125 | checkArgument(child.nodeKeys().size() >= base.nodeKeys().size(), |
| 126 | "%s path must be deeper than base prefix %s", child, base); |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 127 | @SuppressWarnings("rawtypes") |
| 128 | Iterator<NodeKey> bIt = base.nodeKeys().iterator(); |
| 129 | @SuppressWarnings("rawtypes") |
| 130 | Iterator<NodeKey> cIt = child.nodeKeys().iterator(); |
| 131 | while (bIt.hasNext()) { |
| 132 | NodeKey<?> b = bIt.next(); |
| 133 | NodeKey<?> c = cIt.next(); |
| 134 | |
| 135 | checkArgument(Objects.equals(b, c), |
| 136 | "%s is not a prefix of %s.\n" + |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 137 | "b:%s != c:%s", |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 138 | base, child, |
| 139 | b, c); |
| 140 | } |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 141 | |
Yuta HIGUCHI | 4070c04 | 2017-08-28 13:47:20 -0700 | [diff] [blame] | 142 | return ResourceId.builder().append(child.nodeKeys().subList(base.nodeKeys().size(), |
| 143 | child.nodeKeys().size())).build(); |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 147 | * Removes the root node from {@code path}. |
| 148 | * |
| 149 | * @param path given resource ID |
| 150 | * @return resource ID without root node |
| 151 | */ |
| 152 | public static ResourceId removeRootNode(ResourceId path) { |
| 153 | if (!startsWithRootNode(path)) { |
| 154 | return path; |
| 155 | } |
| 156 | |
| 157 | return ResourceId.builder().append(path.nodeKeys().subList(1, |
| 158 | path.nodeKeys().size())).build(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns the resource ID of the parent data node pointed by {@code path}. |
| 163 | * |
| 164 | * @param path resource ID of the given data node |
| 165 | * @return resource ID of the parent data node |
| 166 | */ |
| 167 | public static ResourceId parentOf(ResourceId path) { |
| 168 | try { |
| 169 | return path.copyBuilder().removeLastKey().build(); |
| 170 | } catch (CloneNotSupportedException e) { |
| 171 | log.error("Could not copy {}", path, e); |
| 172 | throw new IllegalArgumentException("Could not copy " + path, e); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 177 | * Tests if {@code child} starts with {@code prefix}. |
| 178 | * |
| 179 | * @param prefix expected |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 180 | * @param child to test |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 181 | * @return true if {@code child} starts with {@code prefix} |
| 182 | */ |
| 183 | public static boolean isPrefix(ResourceId prefix, ResourceId child) { |
| 184 | |
| 185 | return child.nodeKeys().size() >= prefix.nodeKeys().size() && |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 186 | prefix.nodeKeys().equals(child.nodeKeys().subList(0, prefix.nodeKeys().size())); |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 189 | public static boolean startsWithRootNode(ResourceId path) { |
| 190 | return !path.nodeKeys().isEmpty() && |
| 191 | DeviceResourceIds.ROOT_NAME.equals(path.nodeKeys().get(0).schemaId().name()); |
| 192 | } |
| 193 | |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 194 | } |