Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / Repository.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api;
22
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;
26
27 import java.util.LinkedHashMap;
28
29 public class Repository {
30
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};
35
36     private String name;
37     private Object reposit;
38     private String url;
39
40     @SuppressWarnings("unchecked")
41     public Repository(String repName, Object repValue) {
42         name = repName;
43         reposit = repValue;
44         if (reposit instanceof LinkedHashMap) {
45             url = (String) ((LinkedHashMap<String, Object>) reposit).get("url");
46             if (url == null) {
47                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE229", String.format(
48                         "MissingRequiredFieldError: Repository \"%s\" is missing required field \"url\"",
49                         name)));
50             }
51         }
52         loadAndValidate(name, reposit);
53     }
54
55     @SuppressWarnings("unchecked")
56     private void loadAndValidate(String val, Object repositDef) {
57         String keyname = val;
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)) {
63                         bFound = true;
64                         break;
65                     }
66                 }
67                 if (!bFound) {
68                     ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE230", String.format(
69                             "UnknownFieldError: repositories \"%s\" contains unknown field \"%s\"",
70                             keyname, key)));
71                 }
72             }
73
74             String repositUrl = (String) ((LinkedHashMap<String, Object>) repositDef).get("url");
75             if (repositUrl != null) {
76                 boolean urlVal = UrlUtils.validateUrl(repositUrl);
77                 if (!urlVal) {
78                     ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE231", String.format(
79                             "URLException: repsositories \"%s\" Invalid Url", keyname)));
80                 }
81             }
82         }
83     }
84
85     @Override
86     public String toString() {
87         return "Repository{" +
88                 "name='" + name + '\'' +
89                 ", reposit=" + reposit +
90                 ", url='" + url + '\'' +
91                 '}';
92     }
93 }
94
95 /*python
96
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
103
104 SECTIONS = (DESCRIPTION, URL, CREDENTIAL) = \
105            ('description', 'url', 'credential')
106
107
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)
119
120     def load_and_validate(self, val, reposit_def):
121         self.keyname = val
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))
128
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')
136                                      % self.keyname))
137 */