blob: fc875a9fdbd0ecf8cef148dee974cd6175644555 [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 static org.slf4j.LoggerFactory.getLogger;
20
21import org.onosproject.yang.model.DataNode;
22import org.onosproject.yang.model.KeyLeaf;
23import org.onosproject.yang.model.LeafListKey;
24import org.onosproject.yang.model.ListKey;
Yuta HIGUCHI24057822017-08-02 15:03:51 -070025import org.onosproject.yang.model.ResourceId;
Yuta HIGUCHI24057822017-08-02 15:03:51 -070026import org.onosproject.yang.model.SchemaId;
27import org.slf4j.Logger;
28
29import com.google.common.annotations.Beta;
30
31/**
32 * Utility related to ResourceId.
33 */
34@Beta
35public abstract class ResourceIds {
36
37 private static final Logger log = getLogger(ResourceIds.class);
38
39 /**
40 * Builds the ResourceId of specified {@code node}.
41 *
42 * @param parent ResourceId of {@code node} parent
43 * @param node to create ResourceId.
44 * @return ResourceId of {@code node}
45 */
46 public static ResourceId resourceId(ResourceId parent, DataNode node) {
47 final ResourceId.Builder builder;
48 if (parent == null) {
49 builder = ResourceId.builder();
50 } else {
51 try {
52 builder = parent.copyBuilder();
53 } catch (CloneNotSupportedException e) {
54 throw new IllegalArgumentException(e);
55 }
56 }
57
58 SchemaId sid = node.key().schemaId();
59 switch (node.type()) {
60 case MULTI_INSTANCE_LEAF_VALUE_NODE:
61 builder.addLeafListBranchPoint(sid.name(), sid.namespace(),
62 ((LeafListKey) node.key()).asString());
63 break;
64
65 case MULTI_INSTANCE_NODE:
66 builder.addBranchPointSchema(sid.name(), sid.namespace());
67 for (KeyLeaf keyLeaf : ((ListKey) node.key()).keyLeafs()) {
68 builder.addKeyLeaf(keyLeaf.leafSchema().name(),
69 keyLeaf.leafSchema().namespace(),
70 keyLeaf.leafValAsString());
71 }
72 break;
73
74 case SINGLE_INSTANCE_LEAF_VALUE_NODE:
75 case SINGLE_INSTANCE_NODE:
76 builder.addBranchPointSchema(sid.name(), sid.namespace());
77 break;
78
79 default:
80 throw new IllegalArgumentException("Unknown type " + node);
81
82 }
83
84 return builder.build();
85 }
86
87 /**
88 * Concats {@code path} after {@code prefix}.
89 *
90 * @param prefix path
91 * @param path to append after {@code path}
92 * @return concatenated ResouceId
93 */
94 public static ResourceId concat(ResourceId prefix, ResourceId path) {
95 checkArgument(!path.nodeKeys().contains(DeviceResourceIds.ROOT_NODE),
96 "%s was already absolute path", path);
97 try {
98 return prefix.copyBuilder().append(path).build();
99 } catch (CloneNotSupportedException e) {
100 log.error("Could not copy {}", path, e);
101 throw new IllegalArgumentException("Could not copy " + path, e);
102 }
103 }
104
105
106 /**
Yuta HIGUCHI4070c042017-08-28 13:47:20 -0700107 * Returns {@code child} as relative ResourceId against {@code base}.
Yuta HIGUCHI24057822017-08-02 15:03:51 -0700108 *
109 * @param base ResourceId
110 * @param child ResourceId to relativize
111 * @return relative ResourceId
112 */
113 public static ResourceId relativize(ResourceId base, ResourceId child) {
114 checkArgument(child.nodeKeys().size() >= base.nodeKeys().size(),
115 "%s path must be deeper than base prefix %s", child, base);
116 checkArgument(base.nodeKeys().equals(child.nodeKeys().subList(0, base.nodeKeys().size())),
117 "%s is not a prefix of %s", child, base);
118
Yuta HIGUCHI4070c042017-08-28 13:47:20 -0700119 return ResourceId.builder().append(child.nodeKeys().subList(base.nodeKeys().size(),
120 child.nodeKeys().size())).build();
Yuta HIGUCHI24057822017-08-02 15:03:51 -0700121 }
122
123 /**
124 * Tests if {@code child} starts with {@code prefix}.
125 *
126 * @param prefix expected
127 * @param child to test
128 * @return true if {@code child} starts with {@code prefix}
129 */
130 public static boolean isPrefix(ResourceId prefix, ResourceId child) {
131
132 return child.nodeKeys().size() >= prefix.nodeKeys().size() &&
133 prefix.nodeKeys().equals(child.nodeKeys().subList(0, prefix.nodeKeys().size()));
134 }
135
136}