Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / parameters / Output.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.parameters;
22
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
25
26 import java.util.LinkedHashMap;
27
28 public class Output {
29
30     private static final String DESCRIPTION = "description";
31     public static final String VALUE = "value";
32     private static final String[] OUTPUT_FIELD = {DESCRIPTION, VALUE};
33
34     private String name;
35     private LinkedHashMap<String, Object> attributes;
36
37     public Output(String name, LinkedHashMap<String, Object> attributes) {
38         this.name = name;
39         this.attributes = attributes;
40     }
41
42     public String getDescription() {
43         return (String) attributes.get(DESCRIPTION);
44     }
45
46     public Object getValue() {
47         return attributes.get(VALUE);
48     }
49
50     public void validate() {
51         validateField();
52     }
53
54     private void validateField() {
55         if (attributes == null) {
56             //TODO wrong error message...
57             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE216", String.format(
58                     "ValidationError: Output \"%s\" has wrong type. Expecting a dict",
59                     name)));
60         }
61
62         if (getValue() == null) {
63             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE217", String.format(
64                     "MissingRequiredFieldError: Output \"%s\" is missing required \"%s\"",
65                     name, VALUE)));
66         }
67         for (String key : attributes.keySet()) {
68             boolean bFound = false;
69             for (String of : OUTPUT_FIELD) {
70                 if (key.equals(of)) {
71                     bFound = true;
72                     break;
73                 }
74             }
75             if (!bFound) {
76                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE218", String.format(
77                         "UnknownFieldError: Output \"%s\" contains unknown field \"%s\"",
78                         name, key)));
79             }
80         }
81     }
82
83     // getter/setter
84
85     public String getName() {
86         return name;
87     }
88
89     public void setAttr(String name, Object value) {
90         attributes.put(name, value);
91     }
92 }
93
94 /*python
95
96 class Output(object):
97
98     OUTPUT_FIELD = (DESCRIPTION, VALUE) = ('description', 'value')
99
100     def __init__(self, name, attributes):
101         self.name = name
102         self.attributes = attributes
103
104     @property
105     def description(self):
106         return self.attributes.get(self.DESCRIPTION)
107
108     @property
109     def value(self):
110         return self.attributes.get(self.VALUE)
111
112     def validate(self):
113         self._validate_field()
114
115     def _validate_field(self):
116         if not isinstance(self.attributes, dict):
117             ValidationIssueCollector.appendException(
118                 MissingRequiredFieldError(what='Output "%s"' % self.name,
119                                           required=self.VALUE))
120         if self.value is None:
121             ValidationIssueCollector.appendException(
122                 MissingRequiredFieldError(what='Output "%s"' % self.name,
123                                           required=self.VALUE))
124         for name in self.attributes:
125             if name not in self.OUTPUT_FIELD:
126                 ValidationIssueCollector.appendException(
127                     UnknownFieldError(what='Output "%s"' % self.name,
128                                       field=name))
129 */