aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/toscaparser/api/Repository.java
blob: ee5e5bca3c4e9c54a1a2a7f0d8893a111c427f83 (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.toscaparser.api;

import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
import org.onap.sdc.toscaparser.api.utils.UrlUtils;

import java.util.LinkedHashMap;

public class Repository {

    private static final String DESCRIPTION = "description";
    private static final String URL = "url";
    private static final String CREDENTIAL = "credential";
    private static final String SECTIONS[] = {DESCRIPTION, URL, CREDENTIAL};

    private String name;
    private Object reposit;
    private String url;

    @SuppressWarnings("unchecked")
    public Repository(String repName, Object repValue) {
        name = repName;
        reposit = repValue;
        if (reposit instanceof LinkedHashMap) {
            url = (String) ((LinkedHashMap<String, Object>) reposit).get("url");
            if (url == null) {
                ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE229", String.format(
                        "MissingRequiredFieldError: Repository \"%s\" is missing required field \"url\"",
                        name)));
            }
        }
        loadAndValidate(name, reposit);
    }

    @SuppressWarnings("unchecked")
    private void loadAndValidate(String val, Object repositDef) {
        String keyname = val;
        if (repositDef instanceof LinkedHashMap) {
            for (String key : ((LinkedHashMap<String, Object>) reposit).keySet()) {
                boolean bFound = false;
                for (String sect : SECTIONS) {
                    if (key.equals(sect)) {
                        bFound = true;
                        break;
                    }
                }
                if (!bFound) {
                    ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE230", String.format(
                            "UnknownFieldError: repositories \"%s\" contains unknown field \"%s\"",
                            keyname, key)));
                }
            }

            String repositUrl = (String) ((LinkedHashMap<String, Object>) repositDef).get("url");
            if (repositUrl != null) {
                boolean urlVal = UrlUtils.validateUrl(repositUrl);
                if (!urlVal) {
                    ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE231", String.format(
                            "URLException: repsositories \"%s\" Invalid Url", keyname)));
                }
            }
        }
    }

    @Override
    public String toString() {
        return "Repository{" +
                "name='" + name + '\'' +
                ", reposit=" + reposit +
                ", url='" + url + '\'' +
                '}';
    }
}

/*python

from toscaparser.common.exception import ValidationIssueCollector
from toscaparser.common.exception import MissingRequiredFieldError
from toscaparser.common.exception import UnknownFieldError
from toscaparser.common.exception import URLException
from toscaparser.utils.gettextutils import _
import org.openecomp.sdc.toscaparser.api.utils.urlutils

SECTIONS = (DESCRIPTION, URL, CREDENTIAL) = \
           ('description', 'url', 'credential')


class Repository(object):
    def __init__(self, repositories, values):
        self.name = repositories
        self.reposit = values
        if isinstance(self.reposit, dict):
            if 'url' not in self.reposit.keys():
                ValidationIssueCollector.appendException(
                    MissingRequiredFieldError(what=_('Repository "%s"')
                                              % self.name, required='url'))
            self.url = self.reposit['url']
        self.load_and_validate(self.name, self.reposit)

    def load_and_validate(self, val, reposit_def):
        self.keyname = val
        if isinstance(reposit_def, dict):
            for key in reposit_def.keys():
                if key not in SECTIONS:
                    ValidationIssueCollector.appendException(
                        UnknownFieldError(what=_('repositories "%s"')
                                          % self.keyname, field=key))

            if URL in reposit_def.keys():
                reposit_url = reposit_def.get(URL)
                url_val = toscaparser.utils.urlutils.UrlUtils.\
                    validate_url(reposit_url)
                if url_val is not True:
                    ValidationIssueCollector.appendException(
                        URLException(what=_('repsositories "%s" Invalid Url')
                                     % self.keyname))
*/