summaryrefslogtreecommitdiffstats
path: root/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/ModuleSource.java
blob: 96977dafdb38ee68d33988426cbe07a1084759c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright 2023 Huawei Technologies

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.onap.modeling.yangkit.compiler;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.onap.modeling.yangkit.catalog.ModuleInfo;
import org.onap.modeling.yangkit.compiler.util.YangCompilerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.yangcentral.yangkit.model.api.schema.ModuleId;
import org.yangcentral.yangkit.model.api.schema.YangSchemaContext;
import org.yangcentral.yangkit.model.api.stmt.Module;
import org.yangcentral.yangkit.parser.YangYinParser;
import org.yangcentral.yangkit.utils.file.FileUtil;


public class ModuleSource implements Source {
    private List<ModuleInfo> modules;

    private boolean importOnly;
    private static final Logger logger = LoggerFactory.getLogger(ModuleSource.class);

    public ModuleSource(List<ModuleInfo> modules) {
        this.modules = modules;
    }

    public ModuleSource(List<ModuleInfo> modules, boolean importOnly) {
        this.modules = modules;
        this.importOnly = importOnly;
    }

    public boolean isImportOnly() {
        return importOnly;
    }

    public void setImportOnly(boolean importOnly) {
        this.importOnly = importOnly;
    }

    public List<ModuleInfo> getModules() {
        return modules;
    }

    @Override
    public YangSchemaContext buildSource(Settings settings, YangSchemaContext yangSchemaContext)
            throws YangCompilerException {
        return buildSource(settings, yangSchemaContext, false);
    }

    @Override
    public YangSchemaContext buildSource(Settings settings, YangSchemaContext schemaContext, boolean withDependencies)
            throws YangCompilerException {
        ModuleInfo targetModuleInfo = null;
        for (ModuleInfo moduleInfo : modules) {
            if (schemaContext != null) {
                Optional<Module> oldModule = schemaContext.getModule(
                        new ModuleId(moduleInfo.getName(), moduleInfo.getRevision()));
                if (oldModule.isPresent()) {
                    if (!importOnly && schemaContext.isImportOnly(oldModule.get())) {
                        schemaContext.removeModule(oldModule.get().getModuleId());
                        schemaContext.addModule(oldModule.get());
                    }
                    continue;
                }
            }
            URI schema = moduleInfo.getSchema();
            if (schema == null) {
                try {
                    targetModuleInfo = YangCompilerUtil.getSchema(moduleInfo, settings);
                } catch (IOException e) {
                    throw new YangCompilerException(e.getMessage());
                }
                if (targetModuleInfo == null) {
                    throw new YangCompilerException("module="
                            + moduleInfo.getModuleInfo()
                            + " is not found.");
                }
                schema = targetModuleInfo.getSchema();
            }
            if (targetModuleInfo == null) {
                targetModuleInfo = moduleInfo;
            }


            try {
                logger.info("download yang from " + schema.toURL());
                String yangString = YangCompilerUtil.urlInvoke2String(schema.toURL().toString(), settings);

                InputStream inputStream = new ByteArrayInputStream(yangString.getBytes());
                String parseModuleInfo = schema.toURL().toString();
                schemaContext = YangYinParser.parse(inputStream,
                        parseModuleInfo, true, importOnly, schemaContext);
                //judge whether this module exists in local repository
                if (YangCompilerUtil.getSchemaFromLocal(targetModuleInfo, settings) == null) {
                    // if not found, install to local repository
                    String fileName = settings.getLocalRepository() + File.separator + targetModuleInfo.getModuleInfo()
                            + ".yang";
                    FileUtil.writeUtf8File(fileName, yangString);
                    logger.info("install " + targetModuleInfo.getModuleInfo() + ".yang to "
                            + settings.getLocalRepository());
                }
                if (withDependencies) {
                    logger.info("get dependencies for " + targetModuleInfo.getModuleInfo());
                    Module sourceModule =
                            schemaContext.getModule(targetModuleInfo.getName(), targetModuleInfo.getRevision())
                                    .get();

                    List<ModuleInfo> dependencies = YangCompilerUtil.getDependencies(sourceModule);
                    if (!dependencies.isEmpty()) {
                        List<ModuleInfo> extraDependencies = new ArrayList<>();
                        for (ModuleInfo dependency : dependencies) {
                            if (schemaContext.getModule(dependency.getName(), dependency.getRevision()).isPresent()) {
                                continue;
                            }
                            extraDependencies.add(dependency);
                        }
                        ModuleSource extraDependenciesSource = new ModuleSource(extraDependencies, true);
                        logger.info("start to build dependencies for " + targetModuleInfo.getModuleInfo());
                        schemaContext = extraDependenciesSource.buildSource(settings, schemaContext, true);
                        logger.info("end to build dependencies for " + targetModuleInfo.getModuleInfo());
                    }
                }

            } catch (Exception e) {
                throw new YangCompilerException(e.getMessage());
            }
        }
        return schemaContext;
    }
}