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