chengfan | 0c32d71 | 2016-10-11 21:03:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.protocol.restconf.server.utils.parser.api; |
| 18 | |
| 19 | import org.onosproject.yms.ydt.YdtBuilder; |
| 20 | import org.onosproject.yms.ydt.YdtContext; |
| 21 | import org.onosproject.yms.ydt.YdtContextOperationType; |
| 22 | import org.onosproject.yms.ydt.YdtType; |
| 23 | import org.onosproject.yms.ydt.YmsOperationType; |
| 24 | |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import static org.onosproject.yms.ydt.YdtContextOperationType.NONE; |
| 30 | import static org.onosproject.yms.ydt.YdtType.*; |
| 31 | |
| 32 | /** |
| 33 | * A test class which represents YANG request work bench which contains all |
| 34 | * parameters for request handling and methods to build and obtain YANG data |
| 35 | * tree which is data (sub)instance representation, abstract of protocol. |
| 36 | */ |
| 37 | public class TestYdtBuilder implements YdtBuilder { |
| 38 | |
| 39 | private TestYdtNode curNode; |
| 40 | |
| 41 | private TestYdtNode rootNode; |
| 42 | |
| 43 | private final YmsOperationType ymsOperationType; |
| 44 | |
| 45 | private YdtContextOperationType ydtDefaultOpType; |
| 46 | |
| 47 | /** |
| 48 | * Creates an instance of YANG request work bench which is use to initialize |
| 49 | * logical rootNode and and schema registry. |
| 50 | * |
| 51 | * @param name name of logical container of a protocol |
| 52 | * which is a holder of the complete tree |
| 53 | * @param namespace namespace of logical container |
| 54 | * @param opType type of operation done by using YANG |
| 55 | * interface |
| 56 | */ |
| 57 | public TestYdtBuilder(String name, String namespace, |
| 58 | YmsOperationType opType) { |
| 59 | TestYangSchemaId nodeId = new TestYangSchemaId(); |
| 60 | nodeId.setName(name); |
| 61 | nodeId.setNameSpace(namespace); |
| 62 | setRootNode(new TestYdtNode(nodeId, SINGLE_INSTANCE_NODE)); |
| 63 | ymsOperationType = opType; |
| 64 | } |
| 65 | |
| 66 | private void setRootNode(TestYdtNode node) { |
| 67 | rootNode = node; |
| 68 | curNode = node; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public YdtContext getRootNode() { |
| 73 | return rootNode; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public YmsOperationType getYmsOperationType() { |
| 78 | return ymsOperationType; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void setRootTagAttributeMap(Map<String, String> attributeTag) { |
| 83 | |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Map<String, String> getRootTagAttributeMap() { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void addChild(String name, String namespace) { |
| 93 | addChild(name, namespace, SINGLE_INSTANCE_NODE, NONE); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void addChild(String name, String namespace, YdtType ydtType) { |
| 98 | addChild(name, namespace, ydtType, NONE); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void addChild(String name, String namespace, |
| 104 | YdtContextOperationType opType) { |
| 105 | addChild(name, namespace, SINGLE_INSTANCE_NODE, opType); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void addChild(String name, String namespace, YdtType ydtType, |
| 110 | YdtContextOperationType opType) { |
| 111 | TestYangSchemaId id = new TestYangSchemaId(); |
| 112 | id.setName(name); |
| 113 | String ns = namespace != null ? namespace : |
| 114 | curNode.getYdtNodeIdentifier().getNameSpace(); |
| 115 | id.setNameSpace(ns); |
| 116 | TestYdtNode childNode = new TestYdtNode(id, ydtType); |
| 117 | |
| 118 | YdtContextOperationType type = opType == null ? NONE : opType; |
| 119 | childNode.setYdtContextOperationType(type); |
| 120 | |
| 121 | curNode.addChild(childNode); |
| 122 | |
| 123 | // Updating the curNode. |
| 124 | curNode = childNode; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void addLeaf(String name, String namespace, String value) { |
| 129 | addLeaf(name, namespace, value, null, SINGLE_INSTANCE_LEAF_VALUE_NODE); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void addLeaf(String name, String namespace, Set<String> valueSet) { |
| 134 | addLeaf(name, namespace, null, valueSet, MULTI_INSTANCE_LEAF_VALUE_NODE); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void addMultiInstanceChild(String name, String namespace, |
| 139 | List<String> valueList) { |
| 140 | addChild(name, namespace, MULTI_INSTANCE_LEAF_VALUE_NODE, NONE); |
| 141 | if (curNode.getYdtType() == MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 142 | for (String value : valueList) { |
| 143 | curNode.addValue(value); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | private void addLeaf(String name, String namespace, String value, |
| 149 | Set<String> valueSet, YdtType ydtType) { |
| 150 | addChild(name, namespace, ydtType, NONE); |
| 151 | |
| 152 | if (value != null) { |
| 153 | curNode.addValue(value); |
| 154 | } else if (valueSet != null) { |
| 155 | curNode.addValueSet(valueSet); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void traverseToParent() { |
| 161 | curNode = curNode.getParent(); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public YdtContext getCurNode() { |
| 166 | return curNode; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void setDefaultEditOperationType(YdtContextOperationType opType) { |
| 171 | ydtDefaultOpType = opType; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Returns the default context operation type of a YDT builder. |
| 176 | * |
| 177 | * @return default context operation type |
| 178 | */ |
| 179 | public YdtContextOperationType getYdtDefaultOpType() { |
| 180 | return ydtDefaultOpType; |
| 181 | } |
| 182 | } |