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 java.util.Collection; |
| 19 | import java.util.Optional; |
| 20 | |
| 21 | import org.onosproject.yang.model.DataNode; |
| 22 | import org.onosproject.yang.model.InnerNode; |
| 23 | import org.onosproject.yang.model.NodeKey; |
| 24 | import org.onosproject.yang.model.SchemaId; |
| 25 | |
| 26 | import com.google.common.annotations.Beta; |
| 27 | import com.google.common.collect.ImmutableList; |
| 28 | |
| 29 | /** |
| 30 | * Utility related to DataNode. |
| 31 | */ |
| 32 | @Beta |
| 33 | public abstract class DataNodes { |
| 34 | |
| 35 | // TODO it might make sense to turn these into DataNode methods |
| 36 | |
| 37 | // should avoid using this. use other child methods |
| 38 | @Deprecated |
| 39 | public static Optional<DataNode> childOnlyByName(DataNode node, String name) { |
| 40 | if (node instanceof InnerNode) { |
| 41 | InnerNode inode = (InnerNode) node; |
| 42 | return inode.childNodes().values().stream() |
| 43 | .filter(dn -> dn.key().schemaId().name().equals(name)) |
| 44 | .findFirst(); |
| 45 | } else { |
| 46 | return Optional.empty(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public static Optional<DataNode> child(DataNode node, String name, String namespace) { |
| 51 | if (node instanceof InnerNode) { |
| 52 | InnerNode inode = (InnerNode) node; |
| 53 | NodeKey<?> key = NodeKey.builder().schemaId(name, namespace).build(); |
| 54 | return Optional.ofNullable(inode.childNodes().get(key)); |
| 55 | } else { |
| 56 | return Optional.empty(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public static Optional<DataNode> child(DataNode node, SchemaId child) { |
| 61 | if (node instanceof InnerNode) { |
| 62 | InnerNode inode = (InnerNode) node; |
| 63 | NodeKey<?> key = NodeKey.builder().schemaId(child).build(); |
| 64 | return Optional.ofNullable(inode.childNodes().get(key)); |
| 65 | } else { |
| 66 | return Optional.empty(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public static Optional<DataNode> child(DataNode node, NodeKey<?> child) { |
| 71 | if (node instanceof InnerNode) { |
| 72 | InnerNode inode = (InnerNode) node; |
| 73 | return Optional.ofNullable(inode.childNodes().get(child)); |
| 74 | } else { |
| 75 | return Optional.empty(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public static Collection<DataNode> children(DataNode node) { |
| 80 | if (node instanceof InnerNode) { |
| 81 | InnerNode inode = (InnerNode) node; |
| 82 | return inode.childNodes().values(); |
| 83 | } else { |
| 84 | return ImmutableList.of(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | } |