2 * Copyright (c) 2014 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
9 package org.opendaylight.yangtools.yang.parser.repo;
11 import static junit.framework.Assert.assertEquals;
12 import static junit.framework.Assert.assertFalse;
13 import static junit.framework.Assert.assertNotNull;
14 import static junit.framework.Assert.assertSame;
15 import static junit.framework.Assert.assertTrue;
16 import static junit.framework.Assert.fail;
17 import static org.mockito.Mockito.spy;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
21 import org.junit.Ignore;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
32 import com.google.common.collect.Lists;
33 import com.google.common.util.concurrent.CheckedFuture;
35 public class SharedSchemaRepositoryTest {
38 public void testSimpleSchemaContext() throws Exception {
39 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
41 final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang");
42 remoteInetTypesYang.register(sharedSchemaRepository);
43 final CheckedFuture<ASTSchemaSource, SchemaSourceException> registeredSourceFuture = sharedSchemaRepository.getSchemaSource(remoteInetTypesYang.getId(), ASTSchemaSource.class);
44 assertFalse(registeredSourceFuture.isDone());
46 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
48 final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture
49 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId()));
51 assertFalse(schemaContextFuture.isDone());
54 remoteInetTypesYang.setResult();
55 assertEquals(remoteInetTypesYang.getSchemaSourceRepresentation(), registeredSourceFuture.get());
57 // Verify schema created successfully
58 assertTrue(schemaContextFuture.isDone());
59 final SchemaContext firstSchemaContext = schemaContextFuture.checkedGet();
60 assertSchemaContext(firstSchemaContext, 1);
62 // Try same schema second time
63 final CheckedFuture<SchemaContext, SchemaResolutionException> secondSchemaFuture =
64 sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT)
65 .createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId()));
67 // Verify second schema created successfully immediately
68 assertTrue(secondSchemaFuture.isDone());
69 // Assert same context instance is returned from first and second attempt
70 assertSame(firstSchemaContext, secondSchemaFuture.checkedGet());
74 public void testTwoSchemaContextsSharingSource() throws Exception {
75 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
77 final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang");
78 remoteInetTypesYang.register(sharedSchemaRepository);
79 remoteInetTypesYang.setResult();
80 final SettableSchemaProvider<ASTSchemaSource> remoteTopologyYang = getImmediateYangSourceProviderFromResource("/ietf/network-topology@2013-10-21.yang");
81 remoteTopologyYang.register(sharedSchemaRepository);
82 remoteTopologyYang.setResult();
83 final SettableSchemaProvider<ASTSchemaSource> remoteModuleNoRevYang = getImmediateYangSourceProviderFromResource("/no-revision/module-without-revision.yang");
84 remoteModuleNoRevYang.register(sharedSchemaRepository);
86 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
88 final CheckedFuture<SchemaContext, SchemaResolutionException> inetAndTopologySchemaContextFuture
89 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId(), remoteTopologyYang.getId()));
90 assertTrue(inetAndTopologySchemaContextFuture.isDone());
91 assertSchemaContext(inetAndTopologySchemaContextFuture.checkedGet(), 2);
93 final CheckedFuture<SchemaContext, SchemaResolutionException> inetAndNoRevSchemaContextFuture
94 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId(), remoteModuleNoRevYang.getId()));
95 assertFalse(inetAndNoRevSchemaContextFuture.isDone());
97 remoteModuleNoRevYang.setResult();
98 assertTrue(inetAndNoRevSchemaContextFuture.isDone());
99 assertSchemaContext(inetAndNoRevSchemaContextFuture.checkedGet(), 2);
103 public void testFailedSchemaContext() throws Exception {
104 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
106 final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang");
107 remoteInetTypesYang.register(sharedSchemaRepository);
109 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
111 // Make source appear
112 final Throwable ex = new IllegalStateException("failed schema");
113 remoteInetTypesYang.setException(ex);
115 final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture
116 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId()));
119 schemaContextFuture.checkedGet();
120 } catch (final SchemaResolutionException e) {
121 assertNotNull(e.getCause());
122 assertNotNull(e.getCause().getCause());
123 assertSame(ex, e.getCause().getCause());
127 fail("Schema context creation should have failed");
131 @Ignore("Costs are not considered when getting sources")
133 public void testDifferentCosts() throws Exception {
134 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
136 final SettableSchemaProvider<ASTSchemaSource> immediateInetTypesYang = spy(getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"));
137 immediateInetTypesYang.register(sharedSchemaRepository);
138 immediateInetTypesYang.setResult();
140 final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = spy(getRemoteYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"));
141 remoteInetTypesYang.register(sharedSchemaRepository);
142 remoteInetTypesYang.setResult();
144 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
146 final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture
147 = fact.createSchemaContext(Lists.newArrayList(remoteInetTypesYang.getId()));
149 assertSchemaContext(schemaContextFuture.checkedGet(), 1);
151 final SourceIdentifier id = immediateInetTypesYang.getId();
152 verify(remoteInetTypesYang, times(0)).getSource(id);
153 verify(immediateInetTypesYang).getSource(id);
156 private void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
157 assertNotNull(schemaContext);
158 assertEquals(moduleSize, schemaContext.getModules().size());
161 private SettableSchemaProvider<ASTSchemaSource> getRemoteYangSourceProviderFromResource(final String resourceName) throws Exception {
162 final ResourceYangSource yangSource = new ResourceYangSource(resourceName);
163 final CheckedFuture<ASTSchemaSource, SchemaSourceException> aSTSchemaSource = TextToASTTransformer.TRANSFORMATION.apply(yangSource);
164 return SettableSchemaProvider.createRemote(aSTSchemaSource.get(), ASTSchemaSource.class);
167 private SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(final String resourceName) throws Exception {
168 final ResourceYangSource yangSource = new ResourceYangSource(resourceName);
169 final CheckedFuture<ASTSchemaSource, SchemaSourceException> aSTSchemaSource = TextToASTTransformer.TRANSFORMATION.apply(yangSource);
170 return SettableSchemaProvider.createImmediate(aSTSchemaSource.get(), ASTSchemaSource.class);