sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 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. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package org.onosproject.yms.app.ydt; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yms.app.yob.DefaultYobBuilder; |
| 21 | import org.onosproject.yms.app.ytb.DefaultYangTreeBuilder; |
| 22 | import org.onosproject.yms.ydt.YdtContext; |
| 23 | |
| 24 | import java.util.HashSet; |
| 25 | import java.util.LinkedList; |
| 26 | import java.util.List; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import static org.onosproject.yms.app.ydt.YdtAppNodeOperationType.OTHER_EDIT; |
| 30 | import static org.onosproject.yms.app.ydt.YdtTestUtils.identityRefYdt; |
| 31 | import static org.onosproject.yms.app.ydt.YdtTestUtils.validateAppLogicalNodeContents; |
| 32 | import static org.onosproject.yms.app.ydt.YdtTestUtils.validateAppModuleNodeContents; |
| 33 | import static org.onosproject.yms.app.ydt.YdtTestUtils.validateLeafContents; |
| 34 | import static org.onosproject.yms.app.ydt.YdtTestUtils.validateLeafListContents; |
| 35 | import static org.onosproject.yms.app.ydt.YdtTestUtils.validateNodeContents; |
| 36 | import static org.onosproject.yms.app.ydt.YdtTestUtils.walkINTree; |
| 37 | import static org.onosproject.yms.ydt.YdtContextOperationType.MERGE; |
| 38 | import static org.onosproject.yms.ydt.YmsOperationType.EDIT_CONFIG_REPLY; |
| 39 | |
| 40 | public class IdentityTest { |
| 41 | |
| 42 | Set<String> valueSet = new HashSet(); |
| 43 | |
| 44 | private static final String[] EXPECTED = { |
| 45 | "Entry Node is identityref.", |
| 46 | "Entry Node is crypto-base.", |
| 47 | "Entry Node is crypto.", |
| 48 | "Exit Node is crypto.", |
| 49 | "Entry Node is abc-zeunion.", |
| 50 | "Exit Node is abc-zeunion.", |
| 51 | "Entry Node is level2.", |
| 52 | "Exit Node is level2.", |
| 53 | "Entry Node is level3.", |
| 54 | "Exit Node is level3.", |
| 55 | "Entry Node is level4.", |
| 56 | "Exit Node is level4.", |
| 57 | "Entry Node is abc-type.", |
| 58 | "Exit Node is abc-type.", |
| 59 | "Exit Node is crypto-base.", |
| 60 | "Exit Node is identityref.", |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * Creates and validates identity ref in ydt. |
| 65 | */ |
| 66 | @Test |
| 67 | public void identityRefTest() { |
| 68 | YangRequestWorkBench ydtBuilder = identityRefYdt(); |
| 69 | validateTree(ydtBuilder); |
| 70 | validateAppTree(ydtBuilder); |
| 71 | walkINTree(ydtBuilder, EXPECTED); |
| 72 | |
| 73 | //TODO need to be handled later |
| 74 | // validateYangObject(ydtBuilder); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Validates the given built ydt. |
| 79 | */ |
| 80 | private void validateTree(YangRequestWorkBench ydtBuilder) { |
| 81 | |
| 82 | valueSet.add("crypto-alg"); |
| 83 | // Assign root node to ydtNode for validating purpose. |
| 84 | YdtNode ydtNode = (YdtNode) ydtBuilder.getRootNode(); |
| 85 | // Logical root node does not have operation type |
| 86 | validateNodeContents(ydtNode, "identityref", null); |
| 87 | ydtNode = ydtNode.getFirstChild(); |
| 88 | validateNodeContents(ydtNode, "crypto-base", MERGE); |
| 89 | ydtNode = ydtNode.getFirstChild(); |
| 90 | |
| 91 | validateLeafContents(ydtNode, "crypto", "crypto-alg"); |
| 92 | ydtNode = ydtNode.getNextSibling(); |
| 93 | validateLeafContents(ydtNode, "abc-zeunion", "crypto-alg"); |
| 94 | ydtNode = ydtNode.getNextSibling(); |
| 95 | validateLeafContents(ydtNode, "level2", "crypto-alg2"); |
| 96 | ydtNode = ydtNode.getNextSibling(); |
| 97 | validateLeafContents(ydtNode, "level3", "crypto-alg3"); |
| 98 | ydtNode = ydtNode.getNextSibling(); |
| 99 | validateLeafContents(ydtNode, "level4", "crypto-alg3"); |
| 100 | ydtNode = ydtNode.getNextSibling(); |
| 101 | validateLeafListContents(ydtNode, "abc-type", valueSet); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Validates the given built ydt application tree. |
| 107 | */ |
| 108 | private void validateAppTree(YangRequestWorkBench ydtBuilder) { |
| 109 | |
| 110 | // Assign root node to ydtNode for validating purpose. |
| 111 | YdtAppContext ydtAppContext = ydtBuilder.getAppRootNode(); |
| 112 | // Logical root node does not have operation type |
| 113 | validateAppLogicalNodeContents(ydtAppContext); |
| 114 | ydtAppContext = ydtAppContext.getFirstChild(); |
| 115 | validateAppModuleNodeContents(ydtAppContext, "crypto-base", |
| 116 | OTHER_EDIT); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Creates Ydt from YO using YTB. |
| 121 | */ |
| 122 | private void validateYangObject(YangRequestWorkBench ydtBuilder) { |
| 123 | |
| 124 | YdtContext rootCtx = ydtBuilder.getRootNode(); |
| 125 | |
| 126 | YdtContext childCtx = rootCtx.getFirstChild(); |
| 127 | |
| 128 | DefaultYobBuilder builder = new DefaultYobBuilder(); |
| 129 | |
| 130 | Object yangObject = builder.getYangObject( |
| 131 | (YdtExtendedContext) childCtx, YdtTestUtils |
| 132 | .getSchemaRegistry()); |
| 133 | |
| 134 | List<Object> list = new LinkedList<>(); |
| 135 | list.add(yangObject); |
| 136 | // Builds YANG tree in YTB. |
| 137 | DefaultYangTreeBuilder treeBuilder = new DefaultYangTreeBuilder(); |
| 138 | YangRequestWorkBench defaultYdtBuilder = |
| 139 | (YangRequestWorkBench) treeBuilder.getYdtBuilderForYo( |
| 140 | list, "identityref", "ydt.crypto-base", |
| 141 | EDIT_CONFIG_REPLY, YdtTestUtils |
| 142 | .getSchemaRegistry()); |
| 143 | |
| 144 | // Validate the created YDT |
| 145 | walkINTree(defaultYdtBuilder, EXPECTED); |
| 146 | validateTree(defaultYdtBuilder); |
| 147 | } |
| 148 | } |