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