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