2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.yang.parser.impl;
10 import static org.junit.Assert.*;
12 import java.io.FileInputStream;
13 import java.io.InputStream;
15 import java.text.DateFormat;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.List;
22 import org.junit.Test;
23 import org.opendaylight.controller.yang.common.QName;
24 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
26 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
27 import org.opendaylight.controller.yang.model.api.IdentitySchemaNode;
28 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
30 import org.opendaylight.controller.yang.model.api.Module;
31 import org.opendaylight.controller.yang.model.api.MustDefinition;
32 import org.opendaylight.controller.yang.model.api.SchemaContext;
33 import org.opendaylight.controller.yang.model.api.SchemaNode;
34 import org.opendaylight.controller.yang.model.api.SchemaPath;
35 import org.opendaylight.controller.yang.model.api.TypeDefinition;
36 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
37 import org.opendaylight.controller.yang.model.api.UsesNode;
38 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
39 import org.opendaylight.controller.yang.model.util.ExtendedType;
41 import com.google.common.collect.Lists;
43 public class YangParserWithContextTest {
44 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
45 private final YangParserImpl parser = new YangParserImpl();
48 public void testTypeFromContext() throws Exception {
49 SchemaContext context = null;
50 String resource = "/types/ietf-inet-types@2010-09-24.yang";
51 InputStream stream = new FileInputStream(getClass().getResource(resource).getPath());
52 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
56 resource = "/context-test/test1.yang";
57 InputStream stream2 = new FileInputStream(getClass().getResource(resource).getPath());
58 module = TestUtils.loadModuleWithContext(stream2, context);
60 assertNotNull(module);
62 LeafSchemaNode leaf = (LeafSchemaNode) module.getDataChildByName("id");
64 ExtendedType leafType = (ExtendedType) leaf.getType();
65 QName qname = leafType.getQName();
66 assertEquals(URI.create("urn:simple.demo.test1"), qname.getNamespace());
67 assertEquals(simpleDateFormat.parse("2013-06-18"), qname.getRevision());
68 assertEquals("t1", qname.getPrefix());
69 assertEquals("port-number", qname.getLocalName());
71 ExtendedType leafBaseType = (ExtendedType) leafType.getBaseType();
72 qname = leafBaseType.getQName();
73 assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-inet-types"), qname.getNamespace());
74 assertEquals(simpleDateFormat.parse("2010-09-24"), qname.getRevision());
75 assertEquals("inet", qname.getPrefix());
76 assertEquals("port-number", qname.getLocalName());
78 ExtendedType dscpExt = (ExtendedType)TestUtils.findTypedef(module.getTypeDefinitions(), "dscp-ext");
79 List<RangeConstraint> ranges = dscpExt.getRanges();
80 assertEquals(1, ranges.size());
81 RangeConstraint range = ranges.get(0);
82 assertEquals(0L, range.getMin());
83 assertEquals(63L, range.getMax());
88 public void testUsesGroupingFromContext() throws Exception {
89 SchemaContext context = null;
90 try (InputStream stream = new FileInputStream(getClass().getResource("/model/testfile2.yang").getPath())) {
91 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
94 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test2.yang").getPath())) {
95 module = TestUtils.loadModuleWithContext(stream, context);
97 assertNotNull(module);
99 ContainerSchemaNode peer = (ContainerSchemaNode) module.getDataChildByName("peer");
100 ContainerSchemaNode destination = (ContainerSchemaNode) peer.getDataChildByName("destination");
101 Set<UsesNode> usesNodes = destination.getUses();
102 assertEquals(1, usesNodes.size());
103 UsesNode usesNode = usesNodes.iterator().next();
105 // test grouping path
106 List<QName> path = new ArrayList<QName>();
107 QName qname = new QName(URI.create("urn:simple.types.data.demo"), simpleDateFormat.parse("2013-02-27"), "t2",
110 SchemaPath expectedPath = new SchemaPath(path, true);
111 assertEquals(expectedPath, usesNode.getGroupingPath());
114 Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
115 assertEquals(5, refines.size());
117 LeafSchemaNode refineLeaf = null;
118 ContainerSchemaNode refineContainer = null;
119 ListSchemaNode refineList = null;
120 GroupingDefinition refineGrouping = null;
121 TypeDefinition<?> typedef = null;
122 for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
123 SchemaNode value = entry.getValue();
124 if (value instanceof LeafSchemaNode) {
125 refineLeaf = (LeafSchemaNode) value;
126 } else if (value instanceof ContainerSchemaNode) {
127 refineContainer = (ContainerSchemaNode) value;
128 } else if (value instanceof ListSchemaNode) {
129 refineList = (ListSchemaNode) value;
130 } else if (value instanceof GroupingDefinition) {
131 refineGrouping = (GroupingDefinition) value;
132 } else if (value instanceof TypeDefinition<?>) {
133 typedef = (TypeDefinition<?>) value;
138 assertNotNull(refineLeaf);
139 assertEquals("address", refineLeaf.getQName().getLocalName());
140 assertEquals("description of address defined by refine", refineLeaf.getDescription());
141 assertEquals("address reference added by refine", refineLeaf.getReference());
142 assertFalse(refineLeaf.isConfiguration());
143 assertTrue(refineLeaf.getConstraints().isMandatory());
144 Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints().getMustConstraints();
145 assertEquals(1, leafMustConstraints.size());
146 MustDefinition leafMust = leafMustConstraints.iterator().next();
147 assertEquals("\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"", leafMust.toString());
150 assertNotNull(refineContainer);
151 Set<MustDefinition> mustConstraints = refineContainer.getConstraints().getMustConstraints();
152 assertTrue(mustConstraints.isEmpty());
153 assertEquals("description of port defined by refine", refineContainer.getDescription());
154 assertEquals("port reference added by refine", refineContainer.getReference());
155 assertFalse(refineContainer.isConfiguration());
156 assertTrue(refineContainer.isPresenceContainer());
159 assertNotNull(refineList);
160 assertEquals("description of addresses defined by refine", refineList.getDescription());
161 assertEquals("addresses reference added by refine", refineList.getReference());
162 assertFalse(refineList.isConfiguration());
163 assertEquals(2, (int) refineList.getConstraints().getMinElements());
164 assertEquals(12, (int) refineList.getConstraints().getMaxElements());
166 // grouping target-inner
167 assertNotNull(refineGrouping);
168 Set<DataSchemaNode> refineGroupingChildren = refineGrouping.getChildNodes();
169 assertEquals(1, refineGroupingChildren.size());
170 LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode) refineGroupingChildren.iterator().next();
171 assertEquals("inner-grouping-id", refineGroupingLeaf.getQName().getLocalName());
172 assertEquals("new target-inner grouping description", refineGrouping.getDescription());
174 // typedef group-type
175 assertNotNull(typedef);
176 assertEquals("new group-type description", typedef.getDescription());
177 assertEquals("new group-type reference", typedef.getReference());
178 assertTrue(typedef.getBaseType() instanceof ExtendedType);
182 public void testIdentity() throws Exception {
183 SchemaContext context = null;
184 try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang")
186 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
188 Module module = null;
189 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
190 module = TestUtils.loadModuleWithContext(stream, context);
192 assertNotNull(module);
194 Set<IdentitySchemaNode> identities = module.getIdentities();
195 assertEquals(1, identities.size());
197 IdentitySchemaNode identity = identities.iterator().next();
198 QName idQName = identity.getQName();
199 assertEquals(URI.create("urn:simple.demo.test3"), idQName.getNamespace());
200 assertEquals(simpleDateFormat.parse("2013-06-18"), idQName.getRevision());
201 assertEquals("t3", idQName.getPrefix());
202 assertEquals("pt", idQName.getLocalName());
204 IdentitySchemaNode baseIdentity = identity.getBaseIdentity();
205 QName idBaseQName = baseIdentity.getQName();
206 assertEquals(URI.create("urn:simple.container.demo"), idBaseQName.getNamespace());
207 assertEquals(simpleDateFormat.parse("2012-04-16"), idBaseQName.getRevision());
208 assertEquals("iit", idBaseQName.getPrefix());
209 assertEquals("service-type", idBaseQName.getLocalName());
213 public void testUnknownNodes() throws Exception {
214 SchemaContext context = null;
215 try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath())) {
216 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
219 Module module = null;
220 try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
221 module = TestUtils.loadModuleWithContext(stream, context);
224 ContainerSchemaNode network = (ContainerSchemaNode) module.getDataChildByName("network");
225 List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
226 assertEquals(1, unknownNodes.size());
228 UnknownSchemaNode un = unknownNodes.iterator().next();
229 QName unType = un.getNodeType();
230 assertEquals(URI.create("urn:simple.container.demo"), unType.getNamespace());
231 assertEquals(simpleDateFormat.parse("2012-04-16"), unType.getRevision());
232 assertEquals("custom", unType.getPrefix());
233 assertEquals("mountpoint", unType.getLocalName());
234 assertEquals("point", un.getNodeParameter());
238 public void testAugment() throws Exception {
240 SchemaContext context = null;
241 String resource = "/context-augment-test/test4.yang";
243 try (InputStream stream = new FileInputStream(getClass().getResource(resource).getPath())) {
244 context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
247 Set<Module> contextModules = context.getModules();
248 Module t3 = TestUtils.findModule(contextModules, "test4");
249 ContainerSchemaNode interfaces = (ContainerSchemaNode) t3.getDataChildByName("interfaces");
250 ListSchemaNode ifEntry = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
252 // load another modules and parse them against already existing context
253 Set<Module> modules = null;
254 try (InputStream stream1 = new FileInputStream(getClass().getResource("/context-augment-test/test1.yang")
256 InputStream stream2 = new FileInputStream(getClass().getResource("/context-augment-test/test2.yang")
258 InputStream stream3 = new FileInputStream(getClass().getResource("/context-augment-test/test3.yang")
260 List<InputStream> input = Lists.newArrayList(stream1, stream2, stream3);
261 modules = TestUtils.loadModulesWithContext(input, context);
263 assertNotNull(modules);
265 // test augmentation process
266 ContainerSchemaNode augmentHolder = (ContainerSchemaNode) ifEntry.getDataChildByName("augment-holder");
267 assertNotNull(augmentHolder);
268 DataSchemaNode ds0 = augmentHolder.getDataChildByName("ds0ChannelNumber");
270 DataSchemaNode interfaceId = augmentHolder.getDataChildByName("interface-id");
271 assertNotNull(interfaceId);
272 DataSchemaNode higherLayerIf = augmentHolder.getDataChildByName("higher-layer-if");
273 assertNotNull(higherLayerIf);
274 ContainerSchemaNode schemas = (ContainerSchemaNode) augmentHolder.getDataChildByName("schemas");
275 assertNotNull(schemas);
276 assertNotNull(schemas.getDataChildByName("id"));
278 // test augment target after augmentation: check if it is same instance
279 ListSchemaNode ifEntryAfterAugment = (ListSchemaNode) interfaces.getDataChildByName("ifEntry");
280 assertTrue(ifEntry == ifEntryAfterAugment);