Add plugin to check coverage
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / 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  *
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 package org.onap.aai.util.genxsd;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class EdgeDescription {
27         private static final Logger logger = LoggerFactory.getLogger("EdgeDescription.class");
28
29         public static enum LineageType {
30                 PARENT, CHILD, UNRELATED;
31         }
32         private String ruleKey;
33         private String to;
34         private String from;
35         private LineageType type = LineageType.UNRELATED;
36         private String direction;
37         private String multiplicity;
38         private String preventDelete;
39         private String deleteOtherV;
40         private String label;
41         private String description;
42         /**
43          * @return the deleteOtherV
44          */
45         public String getDeleteOtherV() {
46                 return deleteOtherV;
47         }
48         /**
49          * @param deleteOtherV the deleteOtherV to set
50          */
51         public void setDeleteOtherV(String deleteOtherV) {
52                 logger.debug("Edge: "+this.getRuleKey());
53                 logger.debug("Truth: "+(("${direction}".equals(deleteOtherV)) ? "true" : "false"));
54                 logger.debug("Truth: "+(("!${direction}".equals(deleteOtherV)) ? "true" : "false"));
55
56                 if("${direction}".equals(deleteOtherV) ) {
57                         this.deleteOtherV = this.direction;
58                 } else if("!${direction}".equals(deleteOtherV) ) {
59                         this.deleteOtherV = this.direction.equals("IN") ? "OUT" : ((this.direction.equals("OUT")) ? "IN" : deleteOtherV);
60                 } else {
61                         this.deleteOtherV = deleteOtherV;
62                 }
63                 logger.debug("DeleteOtherV="+deleteOtherV+"/"+this.direction+"="+this.deleteOtherV);
64         }
65         /**
66          * @return the preventDelete
67          */
68         public String getPreventDelete() {
69                 return preventDelete;
70         }
71         /**
72          * @param preventDelete the preventDelete to set
73          */
74         public void setPreventDelete(String preventDelete) {
75                 if(this.getTo().equals("flavor") || this.getFrom().equals("flavor") ){
76                         logger.debug("Edge: "+this.getRuleKey());
77                         logger.debug("Truth: "+(("${direction}".equals(preventDelete)) ? "true" : "false"));
78                         logger.debug("Truth: "+(("!${direction}".equals(preventDelete)) ? "true" : "false"));
79                 }
80
81                 if("${direction}".equals(preventDelete) ) {
82                         this.preventDelete = this.direction;
83                 } else if("!${direction}".equals(preventDelete) ) {
84                         this.preventDelete = this.direction.equals("IN") ? "OUT" : ((this.direction.equals("OUT")) ? "IN" : preventDelete);
85                 } else {
86                         this.preventDelete = preventDelete;
87                 }
88         }
89         public String getAlsoDeleteFootnote(String targetNode) {
90                 String returnVal = "";
91                 if(this.deleteOtherV.equals("IN") && this.to.equals(targetNode) ) {
92                         logger.debug("Edge: "+this.getRuleKey());
93                         logger.debug("IF this "+targetNode+" node is deleted, this FROM node is DELETED also");
94                         returnVal = "(1)";
95                 }
96                 if(this.deleteOtherV.equals("OUT") && this.from.equals(targetNode) ) {
97                         logger.debug("Edge: "+this.getRuleKey());
98                         logger.debug("IF this "+targetNode+" is deleted, this TO node is DELETED also");
99                         returnVal = "(2)";
100                 }
101                 if(this.deleteOtherV.equals("OUT") && this.to.equals(targetNode) ) {
102                         logger.debug("Edge: "+this.getRuleKey());
103                         logger.debug("IF this FROM node is deleted, this "+targetNode+" is DELETED also");
104                         returnVal = "(3)";
105                 }
106                 if(this.deleteOtherV.equals("IN") && this.from.equals(targetNode) ) {
107                         logger.debug("Edge: "+this.getRuleKey());
108                         logger.debug("IF this TO node is deleted, this "+targetNode+" node is DELETED also");
109                         returnVal = "(4)";
110                 }
111                 return returnVal;
112         }
113         /**
114          * @return the to
115          */
116         public String getTo() {
117                 return to;
118         }
119         /**
120          * @param to the to to set
121          */
122         public void setTo(String to) {
123                 this.to = to;
124         }
125         /**
126          * @return the from
127          */
128         public String getFrom() {
129                 return from;
130         }
131         /**
132          * @param from the from to set
133          */
134         public void setFrom(String from) {
135                 this.from = from;
136         }
137
138
139         public String getRuleKey() {
140                 return ruleKey;
141         }
142         public String getMultiplicity() {
143                 return multiplicity;
144         }
145         public String getDirection() {
146                 return direction;
147         }
148         public String getDescription() {
149                 return this.description;
150         }
151         public void setRuleKey(String val) {
152                 this.ruleKey=val;
153         }
154         public void setType(LineageType val) {
155                 this.type=val;
156         }
157         public void setDirection(String val) {
158                 this.direction = val;
159         }
160         public void setMultiplicity(String val) {
161                 this.multiplicity=val;
162         }
163         
164         public void setDescription(String val) {
165                 this.description = val;
166         }
167
168         public String getRelationshipDescription(String fromTo, String otherNodeName) {
169                 
170                 String result = "";             
171
172                 if ("FROM".equals(fromTo)) {
173                         if ("OUT".equals(direction)) {
174                                 if (LineageType.PARENT == type) {
175                                         result = " (PARENT of "+otherNodeName;
176                                         result = String.join(" ", result+",", this.from, this.getShortLabel(), this.to);
177                                 } 
178                         } 
179                         else {
180                                 if (LineageType.CHILD == type) {
181                                         result = " (CHILD of "+otherNodeName;
182                                         result = String.join(" ", result+",",  this.from, this.getShortLabel(), this.to);
183                                 } 
184                                 else if (LineageType.PARENT == type) {
185                                         result = " (PARENT of "+otherNodeName;
186                                         result = String.join(" ", result+",", this.from, this.getShortLabel(), this.to);
187                                 }
188                         }
189                         if (result.length() == 0) result = String.join(" ", "(", this.from, this.getShortLabel(), this.to+",", this.getMultiplicity());
190                 } else {
191                 //if ("TO".equals(fromTo)
192                         if ("OUT".equals(direction)) {
193                                 if (LineageType.PARENT == type) {
194                                         result = " (CHILD of "+otherNodeName;
195                                         result = String.join(" ", result+",", this.from, this.getShortLabel(), this.to+",", this.getMultiplicity());
196                                 } 
197                         } else {
198                                 if (LineageType.PARENT == type) {
199                                         result = " (PARENT of "+otherNodeName;
200                                         result = String.join(" ", result+",", this.from, this.getShortLabel(), this.to+",", this.getMultiplicity());
201                                 }
202                         }
203                         if (result.length() == 0) result = String.join(" ", "(", this.from, this.getShortLabel(), this.to+",", this.getMultiplicity());
204                 }
205 //      Confusing...
206 //              if (hasDelTarget()) result = result + ", will delete target node";
207
208                 if (result.length() > 0) result = result + ")";
209                 
210                 if (description != null && description.length() > 0) result = result + "\n      "+ description; // 6 spaces is important for yaml
211                 
212                 return result;
213         }
214
215         /**
216          * @return the hasDelTarget
217          */
218         
219         public boolean hasDelTarget() {
220                 return StringUtils.isNotEmpty(this.deleteOtherV) && (! "NONE".equalsIgnoreCase(this.deleteOtherV));
221         }
222         
223         /**
224          * @return the type
225          */
226         public LineageType getType() {
227                 return type;
228         }
229         /**
230          * @return the label
231          */
232         public String getLabel() {
233                 return label;
234         }
235         public String getShortLabel() {
236                 String[] pieces = this.getLabel().split("[.]");
237                 return pieces[pieces.length-1];
238         }
239         public void setLabel(String string) {
240                 this.label=string;
241         }
242 }