Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [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. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yms.app.yob; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yang.gen.v1.urn.ip.topo.rev20140101.ymsiptopology.node.AugmentedTopoNode; |
| 21 | import org.onosproject.yang.gen.v1.urn.ip.topo.rev20140101.ymsiptopology.node.DefaultAugmentedTopoNode; |
| 22 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.YmsTopologyOpParam; |
| 23 | import org.onosproject.yang.gen.v1.urn.topo.rev20140101.ymstopology.DefaultNode; |
| 24 | import org.onosproject.yms.app.ydt.YangRequestWorkBench; |
| 25 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 26 | import org.onosproject.yms.ydt.YdtContext; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | import static org.hamcrest.core.Is.is; |
| 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.junit.Assert.assertNotNull; |
| 33 | import static org.junit.Assert.assertThat; |
| 34 | import static org.onosproject.yms.app.yob.YobTestUtils.NODE; |
| 35 | import static org.onosproject.yms.app.yob.YobTestUtils.ROOT_DATA_RESOURCE; |
| 36 | import static org.onosproject.yms.app.yob.YobTestUtils.ROUTER_ID; |
| 37 | import static org.onosproject.yms.app.yob.YobTestUtils.ROUTER_IP; |
| 38 | import static org.onosproject.yms.app.yob.YobTestUtils.STR_LEAF_VALUE; |
| 39 | import static org.onosproject.yms.app.yob.YobTestUtils.TOPOLOGY; |
| 40 | import static org.onosproject.yms.ydt.YdtContextOperationType.NONE; |
| 41 | |
| 42 | /** |
| 43 | * Test the YANG object building for the YANG data tree based on the non |
| 44 | * schema augmented nodes. |
| 45 | */ |
| 46 | public class YobAugmentTest { |
| 47 | |
| 48 | private YobTestUtils utils = YobTestUtils.instance(); |
| 49 | |
| 50 | @Test |
| 51 | public void augmentedLeaf() throws IOException { |
| 52 | |
| 53 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 54 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 55 | |
| 56 | ydtBuilder.addChild(TOPOLOGY, null, NONE); |
| 57 | ydtBuilder.addChild(NODE, null); |
| 58 | ydtBuilder.addLeaf(ROUTER_ID, "urn:ip:topo", STR_LEAF_VALUE); |
| 59 | |
| 60 | YdtContext logicalRoot = ydtBuilder.getRootNode(); |
| 61 | YdtExtendedContext appRoot = |
| 62 | (YdtExtendedContext) logicalRoot.getFirstChild(); |
| 63 | |
| 64 | DefaultYobBuilder yobBuilder = new DefaultYobBuilder(); |
| 65 | Object yangObject = yobBuilder.getYangObject(appRoot, |
| 66 | utils.schemaRegistry()); |
| 67 | assertNotNull("Fail to create augmented YANG object", yangObject); |
| 68 | |
| 69 | assertEquals("invalid augmented node created", YmsTopologyOpParam.class, |
| 70 | yangObject.getClass()); |
| 71 | |
| 72 | YmsTopologyOpParam topology = (YmsTopologyOpParam) yangObject; |
| 73 | assertNotNull("failed to build augmented node", topology.node()); |
| 74 | assertEquals("Single node entry is expected", 1, topology.node().size()); |
| 75 | assertEquals("Node type is not DefaultNode", DefaultNode.class, |
| 76 | topology.node().get(0).getClass()); |
| 77 | |
| 78 | DefaultNode node = (DefaultNode) topology.node().get(0); |
| 79 | assertNotNull("Augmented info is missing", node.yangAugmentedInfo( |
| 80 | AugmentedTopoNode.class)); |
| 81 | assertEquals("Augmented class is incorrect", |
| 82 | DefaultAugmentedTopoNode.class, |
| 83 | node.yangAugmentedInfo(AugmentedTopoNode.class) |
| 84 | .getClass()); |
| 85 | |
| 86 | DefaultAugmentedTopoNode augmentedNode = (DefaultAugmentedTopoNode) |
| 87 | node.yangAugmentedInfo(AugmentedTopoNode.class); |
| 88 | assertThat("Augmented leaf value is incorrect", |
| 89 | augmentedNode.routerId(), is(STR_LEAF_VALUE)); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void augmentedLeaves() throws IOException { |
| 94 | YangRequestWorkBench ydtBuilder = new YangRequestWorkBench( |
| 95 | ROOT_DATA_RESOURCE, null, null, utils.schemaRegistry(), true); |
| 96 | |
| 97 | ydtBuilder.addChild(TOPOLOGY, null, NONE); |
| 98 | ydtBuilder.addChild(NODE, null); |
| 99 | ydtBuilder.addLeaf(ROUTER_ID, "urn:ip:topo", STR_LEAF_VALUE); |
| 100 | ydtBuilder.traverseToParent(); |
| 101 | ydtBuilder.addLeaf(ROUTER_IP, "urn:ip:topo", STR_LEAF_VALUE); |
| 102 | |
| 103 | YdtContext logicalRoot = ydtBuilder.getRootNode(); |
| 104 | YdtExtendedContext appRoot = |
| 105 | (YdtExtendedContext) logicalRoot.getFirstChild(); |
| 106 | |
| 107 | DefaultYobBuilder yobBuilder = new DefaultYobBuilder(); |
| 108 | Object yangObject = yobBuilder.getYangObject(appRoot, |
| 109 | utils.schemaRegistry()); |
| 110 | assertNotNull("Fail to create augmented YANG object", yangObject); |
| 111 | |
| 112 | assertEquals("invalid augmented node created", |
| 113 | YmsTopologyOpParam.class, yangObject.getClass()); |
| 114 | |
| 115 | YmsTopologyOpParam topology = (YmsTopologyOpParam) yangObject; |
| 116 | assertNotNull("failed to build augmented node", topology.node()); |
| 117 | assertEquals("Single node entry is expected", 1, |
| 118 | topology.node().size()); |
| 119 | assertEquals("Node type is not DefaultNode", DefaultNode.class, |
| 120 | topology.node().get(0).getClass()); |
| 121 | |
| 122 | DefaultNode node = (DefaultNode) topology.node().get(0); |
| 123 | assertNotNull("Augmented info is missing", node.yangAugmentedInfo( |
| 124 | AugmentedTopoNode.class)); |
| 125 | assertEquals("Augmented class is incorrect", |
| 126 | DefaultAugmentedTopoNode.class, |
| 127 | node.yangAugmentedInfo(AugmentedTopoNode.class) |
| 128 | .getClass()); |
| 129 | |
| 130 | DefaultAugmentedTopoNode augmentedNode = (DefaultAugmentedTopoNode) |
| 131 | node.yangAugmentedInfo(AugmentedTopoNode.class); |
| 132 | assertThat("Augmented router id is incorrect", |
| 133 | augmentedNode.routerId(), is(STR_LEAF_VALUE)); |
| 134 | assertThat("Augmented router ip is incorrect", |
| 135 | augmentedNode.routerIp(), is(STR_LEAF_VALUE)); |
| 136 | } |
| 137 | } |