32846166ea89eb682f12374de66addd0d250147b
[appc.git] /
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.*;
43
44 public class YangContextBuilderImpl implements ContextBuilder {
45
46     @Override
47     public Map<String, Object> buildContext(String sourceFile, String contextConf) throws FileNotFoundException {
48         InputStream source = new FileInputStream(sourceFile);
49         if (source == null) {
50             throw new FileNotFoundException("YANG file <" + sourceFile + ">not found");
51         }
52
53         YangTextSchemaContextResolver yangContextResolver = YangTextSchemaContextResolver
54                 .create("yang-context-resolver");
55         try {
56             yangContextResolver.registerSource(new URL("file:///" + sourceFile));
57         } catch (SchemaSourceException | IOException | YangSyntaxErrorException e) {
58             // TODO Auto-generated catch block
59             e.printStackTrace();
60         }
61         Optional<SchemaContext> sc = yangContextResolver.getSchemaContext();
62
63         Map<String, Object> map = new HashMap<>();
64         if (sc.isPresent()) {
65
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
74         return map;
75     }
76
77     // @Override
78     // public Map<String, Object> buildContext(String sourceFile, String
79     // contextConf) throws FileNotFoundException {
80     // InputStream source = new FileInputStream(sourceFile);
81     // if (source == null) {
82     // throw new FileNotFoundException("YANG file <" + sourceFile + ">not found");
83     // }
84     //
85     // SchemaContext mSchema = parse(Collections.singletonList(source));
86     //
87     // Map<String, Object> map = new HashMap<>();
88     // map.put("module", mSchema.getModules().iterator().next());
89     // return map;
90     // }
91     //
92     // private SchemaContext parse(List<InputStream> sources) {
93     // YangParserImpl parser = new YangParserImpl();
94     // Set<Module> modules = parser.parseYangModelsFromStreams(sources);
95     // return parser.resolveSchemaContext(modules);
96     // }
97
98 }