summaryrefslogtreecommitdiffstats
path: root/yang-compiler/src/main/java/org/onap/modeling/yangkit/plugin/yangpackage/YangPackageMeta.java
blob: 40da4baf9f70479db138aea300ca199f84e1ef13 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
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.plugin.yangpackage;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;


public class YangPackageMeta {
    private String name;
    private String revision;
    private String organization;
    private String contact;
    private String description;
    private String reference;
    private boolean local;
    private final List<String> tags = new ArrayList<>();
    private final List<String> mandatoryFeatures = new ArrayList<>();
    private final List<PackageInfo> includePackages = new ArrayList<>();

    /**
     * get the package name.
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * get revision of package.
     * @return revision
     */
    public String getRevision() {
        return revision;
    }

    /**
     * get organization.
     * @return organization.
     */
    public String getOrganization() {
        return organization;
    }

    /**
     * get contact.
     * @return contact.
     */
    public String getContact() {
        return contact;
    }

    /**
     * get description.
     * @return description.
     */
    public String getDescription() {
        return description;
    }

    /**
     * get reference.
     * @return reference.
     */
    public String getReference() {
        return reference;
    }

    /**
     * whether the package is local.
     * @return true or false
     */
    public boolean isLocal() {
        return local;
    }

    /**
     * get tags of package.
     * @return list of tags.
     */
    public List<String> getTags() {
        return tags;
    }

    /**
     * get mandatory features.
     * @return list of features/
     */
    public List<String> getMandatoryFeatures() {
        return mandatoryFeatures;
    }

    /**
     * get include packages.
     * @return list of packages.
     */
    public List<PackageInfo> getIncludePackages() {
        return includePackages;
    }

    /**
     * deserialized package meta from json.
     * @param metaDoc json element
     */
    public void deserialize(JsonElement metaDoc) {
        JsonObject jsonObject = metaDoc.getAsJsonObject();
        JsonElement nameElement = jsonObject.get("name");
        if (nameElement != null) {
            this.name = nameElement.getAsString();
        }
        JsonElement versionElement = jsonObject.get("version");
        if (versionElement != null) {
            this.revision = versionElement.getAsString();
        }
        JsonElement organizationElement = jsonObject.get("organization");
        if (organizationElement != null) {
            this.organization = organizationElement.getAsString();
        }
        JsonElement contactElement = jsonObject.get("contact");
        if (contactElement != null) {
            this.contact = contactElement.getAsString();
        }
        JsonElement descElement = jsonObject.get("description");
        if (descElement != null) {
            this.description = descElement.getAsString();
        }
        JsonElement referElement = jsonObject.get("reference");
        if (referElement != null) {
            this.reference = referElement.getAsString();
        }

        JsonElement localElement = jsonObject.get("local");
        if (localElement != null) {
            this.local = referElement.getAsBoolean();
        }
        JsonElement tagElement = jsonObject.get("tag");
        if (tagElement != null) {
            JsonArray tagArray = tagElement.getAsJsonArray();
            int size = tagArray.size();
            for (int i = 0; i < size; i++) {
                JsonElement tagIns = tagArray.get(i);
                this.tags.add(tagIns.getAsString());
            }
        }

        JsonElement mandatoryFeaturesElement = jsonObject.get("mandatory-feature");
        if (mandatoryFeaturesElement != null) {
            JsonArray mandatoryFeaturesArray = mandatoryFeaturesElement.getAsJsonArray();
            int size = mandatoryFeaturesArray.size();
            for (int i = 0; i < size; i++) {
                JsonElement featureIns = mandatoryFeaturesArray.get(i);
                this.mandatoryFeatures.add(featureIns.getAsString());
            }
        }
        JsonElement includePackagesElement = jsonObject.get("include-package");
        if (includePackagesElement != null) {
            JsonArray includePackageArray = includePackagesElement.getAsJsonArray();
            int size = includePackageArray.size();
            for (int i = 0; i < size; i++) {
                JsonElement includePackageIns = includePackageArray.get(i);
                JsonObject includePackageObj = includePackageIns.getAsJsonObject();
                PackageInfo packageInfo = new PackageInfo(includePackageObj.get("name").getAsString(),
                        includePackageObj.get("version").getAsString());
                this.includePackages.add(packageInfo);
            }
        }

    }
}