[AAI-178 Amsterdam] Make Edge Properties to be
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / parsers / query / UniqueURIQueryParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.aai.parsers.query;
22
23 import org.openecomp.aai.exceptions.AAIException;
24 import org.openecomp.aai.introspection.Introspector;
25 import org.openecomp.aai.introspection.Loader;
26 import org.openecomp.aai.parsers.uri.Parsable;
27 import org.openecomp.aai.parsers.uri.URIParser;
28 import org.openecomp.aai.parsers.uri.URIToDBKey;
29 import org.openecomp.aai.query.builder.QueryBuilder;
30 import org.openecomp.aai.serialization.db.EdgeType;
31
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.UriBuilder;
34 import java.io.UnsupportedEncodingException;
35 import java.net.URI;
36
37
38 /**
39  * The Class UniqueURIQueryParser.
40  */
41 public class UniqueURIQueryParser extends QueryParser implements Parsable {
42
43         
44         private URIToDBKey dbKeyParser = null;
45         
46         private Introspector previous = null;
47         
48         private boolean endsInContainer = false;
49         
50         private Introspector finalContainer = null;
51         
52         private String parentName = "";
53         
54         /**
55          * Instantiates a new unique URI query parser.
56          *
57          * @param loader the loader
58          * @param queryBuilder the query builder
59          * @param uri the uri
60          * @throws UnsupportedEncodingException the unsupported encoding exception
61          * @throws IllegalArgumentException the illegal argument exception
62          * @throws AAIException the AAI exception
63          */
64         public UniqueURIQueryParser(Loader loader, QueryBuilder queryBuilder, URI uri) throws UnsupportedEncodingException, IllegalArgumentException, AAIException {
65                 super(loader, queryBuilder, uri);
66                 URIParser parser = new URIParser(loader, uri);
67                 parser.parse(this);
68                 
69                 if (!endsInContainer) {
70                         this.dbKeyParser = new URIToDBKey(loader, uri);
71                         String dbKey = (String)dbKeyParser.getResult();
72                         queryBuilder.getVerticesByIndexedProperty("aai-unique-key", dbKey);
73                         queryBuilder.markParentBoundary();
74                         
75                         if (!(parentName.equals("") || parentName.equals(this.resultResource))) {
76                                 URI parentUri = UriBuilder.fromPath(uri.getRawPath().substring(0, uri.getRawPath().indexOf(containerResource))).build();
77                                 this.dbKeyParser = new URIToDBKey(loader, parentUri);
78                                 this.parentQueryBuilder = queryBuilder.newInstance().getVerticesByIndexedProperty("aai-unique-key", (String)dbKeyParser.getResult());
79                                 this.parentResourceType = parentName;
80                         } 
81                         this.containerResource = "";
82                 } else {
83                         URI parentUri = UriBuilder.fromPath(uri.getRawPath().substring(0, uri.getRawPath().indexOf(this.finalContainer.getDbName()))).build();
84                         this.dbKeyParser = new URIToDBKey(loader, parentUri);
85                         String dbKey = (String)dbKeyParser.getResult();
86                         this.parentResourceType = parentName;
87
88                         if (!dbKey.equals("")) {
89                                 queryBuilder.getVerticesByIndexedProperty("aai-unique-key", dbKey);
90                                 queryBuilder.markParentBoundary();
91                                 queryBuilder.createEdgeTraversal(EdgeType.TREE, previous, finalContainer);
92
93                         } 
94                         
95                         queryBuilder.createContainerQuery(finalContainer);
96                                 
97                         
98                 }
99         }
100         
101         
102         /**
103          * Instantiates a new unique URI query parser.
104          *
105          * @param loader the loader
106          * @param queryBuilder the query builder
107          */
108         public UniqueURIQueryParser(Loader loader, QueryBuilder queryBuilder) {
109                 super(loader, queryBuilder);
110         }
111         
112         /**
113          * @{inheritDoc}
114          */
115         @Override
116         public void processNamespace(Introspector obj) {
117         
118         }
119
120         /**
121          * @{inheritDoc}
122          */
123         @Override
124         public String getCloudRegionTransform() {
125                 return "add";
126         }
127
128         /**
129          * @{inheritDoc}
130          */
131         @Override
132         public boolean useOriginalLoader() {
133                 return false;
134         }
135
136
137         @Override
138         public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
139                         throws AAIException {
140                 this.resultResource = obj.getDbName();
141                 if (previous != null) {
142                         this.parentName = previous.getDbName();
143                 }
144                 this.previous  = obj;
145                                 
146         }
147
148
149         @Override
150         public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys,
151                         boolean isFinalContainer) throws AAIException {
152                 this.containerResource = obj.getName();
153                 if (previous != null) {
154                         this.parentName = previous.getDbName();
155                 }
156                 if (isFinalContainer) {
157                         this.endsInContainer = true;
158                         this.resultResource = obj.getChildDBName();
159                         
160                         this.finalContainer = obj;
161                 }               
162         }
163         
164 }