AAI-1523 Batch reformat aai-core
[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
21 package org.onap.aai.parsers.query;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25
26 import javax.ws.rs.core.MultivaluedMap;
27 import javax.ws.rs.core.UriBuilder;
28
29 import org.onap.aai.edges.enums.EdgeType;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.Introspector;
32 import org.onap.aai.introspection.Loader;
33 import org.onap.aai.parsers.uri.Parsable;
34 import org.onap.aai.parsers.uri.URIParser;
35 import org.onap.aai.parsers.uri.URIToDBKey;
36 import org.onap.aai.query.builder.QueryBuilder;
37
38 /**
39  * The Class UniqueURIQueryParser.
40  */
41 public class UniqueURIQueryParser extends QueryParser implements Parsable {
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)
64             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
77                         .fromPath(uri.getRawPath().substring(0, uri.getRawPath().indexOf(containerResource))).build();
78                 this.dbKeyParser = new URIToDBKey(loader, parentUri);
79                 this.parentQueryBuilder = queryBuilder.newInstance().getVerticesByIndexedProperty("aai-unique-key",
80                         (String) dbKeyParser.getResult());
81                 this.parentResourceType = parentName;
82             }
83             this.containerResource = "";
84         } else {
85             URI parentUri = UriBuilder
86                     .fromPath(uri.getRawPath().substring(0, uri.getRawPath().indexOf(this.finalContainer.getDbName())))
87                     .build();
88             this.dbKeyParser = new URIToDBKey(loader, parentUri);
89             String dbKey = (String) dbKeyParser.getResult();
90             this.parentResourceType = parentName;
91
92             if (!dbKey.equals("")) {
93                 queryBuilder.getVerticesByIndexedProperty("aai-unique-key", dbKey);
94                 queryBuilder.markParentBoundary();
95                 queryBuilder.createEdgeTraversal(EdgeType.TREE, previous, finalContainer);
96
97             }
98
99             queryBuilder.createContainerQuery(finalContainer);
100
101         }
102     }
103
104     /**
105      * Instantiates a new unique URI query parser.
106      *
107      * @param loader the loader
108      * @param queryBuilder the query builder
109      */
110     public UniqueURIQueryParser(Loader loader, QueryBuilder queryBuilder) {
111         super(loader, queryBuilder);
112     }
113
114     /**
115      * @{inheritDoc}
116      */
117     @Override
118     public void processNamespace(Introspector obj) {
119
120     }
121
122     /**
123      * @{inheritDoc}
124      */
125     @Override
126     public String getCloudRegionTransform() {
127         return "add";
128     }
129
130     /**
131      * @{inheritDoc}
132      */
133     @Override
134     public boolean useOriginalLoader() {
135         return false;
136     }
137
138     @Override
139     public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys)
140             throws AAIException {
141         this.resultResource = obj.getDbName();
142         if (previous != null) {
143             this.parentName = previous.getDbName();
144         }
145         this.previous = obj;
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 }