2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.toscaparser.api;
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
25 import org.onap.sdc.toscaparser.api.utils.UrlUtils;
27 import java.util.LinkedHashMap;
29 public class Repository {
31 private static final String DESCRIPTION = "description";
32 private static final String URL = "url";
33 private static final String CREDENTIAL = "credential";
34 private static final String SECTIONS[] = {DESCRIPTION, URL, CREDENTIAL};
37 private Object reposit;
40 @SuppressWarnings("unchecked")
41 public Repository(String repName, Object repValue) {
44 if (reposit instanceof LinkedHashMap) {
45 url = (String) ((LinkedHashMap<String, Object>) reposit).get("url");
47 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE229", String.format(
48 "MissingRequiredFieldError: Repository \"%s\" is missing required field \"url\"",
52 loadAndValidate(name, reposit);
55 @SuppressWarnings("unchecked")
56 private void loadAndValidate(String val, Object repositDef) {
58 if (repositDef instanceof LinkedHashMap) {
59 for (String key : ((LinkedHashMap<String, Object>) reposit).keySet()) {
60 boolean bFound = false;
61 for (String sect : SECTIONS) {
62 if (key.equals(sect)) {
68 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE230", String.format(
69 "UnknownFieldError: repositories \"%s\" contains unknown field \"%s\"",
74 String repositUrl = (String) ((LinkedHashMap<String, Object>) repositDef).get("url");
75 if (repositUrl != null) {
76 boolean urlVal = UrlUtils.validateUrl(repositUrl);
78 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE231", String.format(
79 "URLException: repsositories \"%s\" Invalid Url", keyname)));
86 public String toString() {
87 return "Repository{" +
88 "name='" + name + '\'' +
89 ", reposit=" + reposit +
90 ", url='" + url + '\'' +
97 from toscaparser.common.exception import ValidationIssueCollector
98 from toscaparser.common.exception import MissingRequiredFieldError
99 from toscaparser.common.exception import UnknownFieldError
100 from toscaparser.common.exception import URLException
101 from toscaparser.utils.gettextutils import _
102 import org.openecomp.sdc.toscaparser.api.utils.urlutils
104 SECTIONS = (DESCRIPTION, URL, CREDENTIAL) = \
105 ('description', 'url', 'credential')
108 class Repository(object):
109 def __init__(self, repositories, values):
110 self.name = repositories
111 self.reposit = values
112 if isinstance(self.reposit, dict):
113 if 'url' not in self.reposit.keys():
114 ValidationIssueCollector.appendException(
115 MissingRequiredFieldError(what=_('Repository "%s"')
116 % self.name, required='url'))
117 self.url = self.reposit['url']
118 self.load_and_validate(self.name, self.reposit)
120 def load_and_validate(self, val, reposit_def):
122 if isinstance(reposit_def, dict):
123 for key in reposit_def.keys():
124 if key not in SECTIONS:
125 ValidationIssueCollector.appendException(
126 UnknownFieldError(what=_('repositories "%s"')
127 % self.keyname, field=key))
129 if URL in reposit_def.keys():
130 reposit_url = reposit_def.get(URL)
131 url_val = toscaparser.utils.urlutils.UrlUtils.\
132 validate_url(reposit_url)
133 if url_val is not True:
134 ValidationIssueCollector.appendException(
135 URLException(what=_('repsositories "%s" Invalid Url')