8325ecb6dd41d031d691e256b9451772f1b446ac
[appc.git] / appc-client / code-generator / src / main / java / org / openecomp / appc / tools / generator / extensions / YangContextBuilderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.tools.generator.extensions;
26
27 import org.openecomp.appc.tools.generator.api.ContextBuilder;
28 import com.google.common.base.Optional;
29
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;
36
37 import java.io.FileInputStream;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.net.URL;
42 import java.util.Map;
43 import java.util.HashMap;
44 import java.util.Set;
45
46 public class YangContextBuilderImpl implements ContextBuilder {
47
48     @Override
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");
53         }
54
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) {
61             e.printStackTrace();
62         }
63
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);
70             }
71         }
72
73         return map;
74     }
75
76     // @Override
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");
82     // }
83     //
84     // SchemaContext mSchema = parse(Collections.singletonList(source));
85     //
86     // Map<String, Object> map = new HashMap<>();
87     // map.put("module", mSchema.getModules().iterator().next());
88     // return map;
89     // }
90     //
91     // private SchemaContext parse(List<InputStream> sources) {
92     // YangParserImpl parser = new YangParserImpl();
93     // Set<Module> modules = parser.parseYangModelsFromStreams(sources);
94     // return parser.resolveSchemaContext(modules);
95     // }
96
97 }