Fix sonar issues + merge
[aai/schema-service.git] / aai-schema-gen / src / main / java / org / onap / aai / schemagen / genxsd / EdgeDescription.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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  * <p>
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * <p>
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.aai.schemagen.genxsd;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.aai.edges.EdgeRule;
25 import org.onap.aai.edges.enums.AAIDirection;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class EdgeDescription {
30
31     private static final Logger logger = LoggerFactory.getLogger(EdgeDescription.class);
32     EdgeRule ed;
33
34     public enum LineageType {
35         PARENT, CHILD, UNRELATED
36     }
37
38     private final String ruleKey;
39     private LineageType lineageType = LineageType.UNRELATED;
40
41     public EdgeDescription(EdgeRule ed) {
42         super();
43         if (ed.getDirection().toString().equals(ed.getContains()) &&
44             AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) {
45             this.lineageType = LineageType.PARENT;
46         } else if (AAIDirection.getValue("IN").equals(AAIDirection.getValue(ed.getContains())) &&
47             ed.getDirection().toString().equals(ed.getContains())) {
48             this.lineageType = LineageType.CHILD;
49         } else if (AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getContains())) &&
50             AAIDirection.getValue("IN").equals(AAIDirection.getValue(ed.getDirection()))) {
51             this.lineageType = LineageType.PARENT;
52         } else if (AAIDirection.getValue("IN").equals(AAIDirection.getValue(ed.getContains())) &&
53             AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) {
54             this.lineageType = LineageType.PARENT;
55         } else {
56             this.lineageType = LineageType.UNRELATED;
57         }
58         this.ruleKey = ed.getFrom() + "|" + ed.getTo();
59         this.ed = ed;
60     }
61
62     /**
63      * @return the deleteOtherV
64      */
65     public String getDeleteOtherV() {
66         return ed.getDeleteOtherV();
67     }
68
69     /**
70      * @return the preventDelete
71      */
72     public String getPreventDelete() {
73         return ed.getPreventDelete();
74     }
75
76     public String getAlsoDeleteFootnote(String targetNode) {
77         String returnVal = "";
78         if (ed.getDeleteOtherV().equals("IN") && ed.getTo().equals(targetNode)) {
79             logger.debug("Edge: " + this.ruleKey);
80             logger.debug(
81                 "IF this " + targetNode + " node is deleted, this FROM node is DELETED also");
82             returnVal = "(1)";
83         }
84         if (ed.getDeleteOtherV().equals("OUT") && ed.getFrom().equals(targetNode)) {
85             logger.debug("Edge: " + this.ruleKey);
86             logger.debug("IF this " + targetNode + " is deleted, this TO node is DELETED also");
87             returnVal = "(2)";
88         }
89         if (ed.getDeleteOtherV().equals("OUT") && ed.getTo().equals(targetNode)) {
90             logger.debug("Edge: " + this.ruleKey);
91             logger.debug("IF this FROM node is deleted, this " + targetNode + " is DELETED also");
92             returnVal = "(3)";
93         }
94         if (ed.getDeleteOtherV().equals("IN") && ed.getFrom().equals(targetNode)) {
95             logger.debug("Edge: " + this.ruleKey);
96             logger
97                 .debug("IF this TO node is deleted, this " + targetNode + " node is DELETED also");
98             returnVal = "(4)";
99         }
100         return returnVal;
101     }
102
103     /**
104      * @return the to
105      */
106     public String getTo() {
107         return ed.getTo();
108     }
109
110     /**
111      * @return the from
112      */
113     public String getFrom() {
114         return ed.getFrom();
115     }
116
117     public String getRuleKey() {
118         return ruleKey;
119     }
120
121     public String getMultiplicity() {
122         return ed.getMultiplicityRule().toString();
123     }
124
125     public AAIDirection getDirection() {
126         return AAIDirection.getValue(ed.getDirection());
127     }
128
129     public String getDescription() {
130         return ed.getDescription();
131     }
132
133     public String getRelationshipDescription(String fromTo, String otherNodeName) {
134
135         String result = "";
136
137         if ("FROM".equals(fromTo)) {
138             if (AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) {
139                 if (LineageType.PARENT == lineageType) {
140                     result = " (PARENT of " + otherNodeName;
141                     result = String.join(" ", result + ",", ed.getFrom(), this.getShortLabel(),
142                         ed.getTo() + ",", this.getMultiplicity());
143                 }
144             } else {
145                 if (LineageType.CHILD == lineageType) {
146                     result = " (CHILD of " + otherNodeName;
147                     result = String.join(" ", result + ",", ed.getFrom(), this.getShortLabel(),
148                         ed.getTo() + ",", this.getMultiplicity());
149                 } else if (LineageType.PARENT == lineageType) {
150                     result = " (PARENT of " + otherNodeName;
151                     result = String.join(" ", result + ",", ed.getFrom(), this.getShortLabel(),
152                         ed.getTo() + ",", this.getMultiplicity());
153                 }
154             }
155             if (result.length() == 0) {
156                 result = String
157                     .join(" ", "(", ed.getFrom(), this.getShortLabel(), ed.getTo() + ",",
158                         this.getMultiplicity());
159             }
160         } else {
161             if (LineageType.PARENT == lineageType) {
162                 result = " (PARENT of " + otherNodeName;
163                 result = String
164                     .join(" ", result + ",", ed.getFrom(), this.getShortLabel(), ed.getTo() + ",",
165                         this.getMultiplicity());
166             }
167             if (result.length() == 0) {
168                 result = String
169                     .join(" ", "(", ed.getFrom(), this.getShortLabel(), ed.getTo() + ",",
170                         this.getMultiplicity());
171             }
172         }
173
174         if (result.length() > 0) {
175             result = result + ")";
176         }
177
178         if (ed.getDescription() != null && ed.getDescription().length() > 0) {
179             result = result + "\n      " + ed.getDescription(); // 6 spaces is important for yaml
180         }
181
182         return result;
183     }
184
185     /**
186      * @return the hasDelTarget
187      */
188
189     public boolean hasDelTarget() {
190         return StringUtils.isNotEmpty(ed.getDeleteOtherV()) &&
191             (!"NONE".equalsIgnoreCase(ed.getDeleteOtherV()));
192     }
193
194     /**
195      * @return the type
196      */
197     public LineageType getType() {
198
199         return lineageType;
200     }
201
202     /**
203      * @return the label
204      */
205     public String getLabel() {
206         return ed.getLabel();
207     }
208
209     public String getShortLabel() {
210         String[] pieces = this.getLabel().split("[.]");
211         return pieces[pieces.length - 1];
212     }
213 }