2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.tools.generator.extensions;
27 import org.openecomp.appc.tools.generator.api.ContextBuilder;
28 import com.google.common.base.Optional;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
33 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
34 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ModuleEffectiveStatementImpl;
37 import java.io.FileInputStream;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import java.io.InputStream;
43 import java.util.HashMap;
46 public class YangContextBuilderImpl implements ContextBuilder {
49 public Map<String, Object> buildContext(String sourceFile, String contextConf) throws FileNotFoundException {
50 try ( InputStream source = new FileInputStream(sourceFile) ) {}
51 catch ( IOException ex) {
52 throw new FileNotFoundException("YANG file <" + sourceFile + ">not found");
55 Optional<SchemaContext> sc = null;
56 try ( YangTextSchemaContextResolver yangContextResolver =
57 YangTextSchemaContextResolver.create("yang-context-resolver")) {
58 yangContextResolver.registerSource(new URL("file:///" + sourceFile));
59 sc = yangContextResolver.getSchemaContext();
60 } catch (SchemaSourceException | IOException | YangSyntaxErrorException e) {
64 Map<String, Object> map = new HashMap<>();
65 if ( null != sc && sc.isPresent()) {
66 Set<Module> modules = sc.get().getModules();
67 for (Module module : modules) {
68 ModuleEffectiveStatementImpl impl = (ModuleEffectiveStatementImpl) module;
69 map.put("module", module);
77 // public Map<String, Object> buildContext(String sourceFile, String
78 // contextConf) throws FileNotFoundException {
79 // InputStream source = new FileInputStream(sourceFile);
80 // if (source == null) {
81 // throw new FileNotFoundException("YANG file <" + sourceFile + ">not found");
84 // SchemaContext mSchema = parse(Collections.singletonList(source));
86 // Map<String, Object> map = new HashMap<>();
87 // map.put("module", mSchema.getModules().iterator().next());
91 // private SchemaContext parse(List<InputStream> sources) {
92 // YangParserImpl parser = new YangParserImpl();
93 // Set<Module> modules = parser.parseYangModelsFromStreams(sources);
94 // return parser.resolveSchemaContext(modules);