AT&T 1712 and 1802 release code
[so.git] / common / src / main / java / org / openecomp / mso / client / aai / AAIQueryClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.mso.client.aai;
22
23 import java.util.Optional;
24 import java.util.UUID;
25
26 import org.openecomp.mso.client.aai.entities.CustomQuery;
27 import org.openecomp.mso.client.aai.entities.uri.AAIUri;
28 import org.openecomp.mso.client.aai.entities.uri.AAIUriFactory;
29 import org.openecomp.mso.client.policy.RestClient;
30 import org.openecomp.mso.client.aai.AAIVersion;
31
32 public class AAIQueryClient extends AAIClient {
33
34
35         private final AAIVersion version;
36         private Optional<String> depth = Optional.empty();
37         private boolean nodesOnly = false;
38         private Optional<AAISubgraphType> subgraph = Optional.empty();
39         
40         public AAIQueryClient() {
41                 super(UUID.randomUUID());
42                 this.version = super.getVersion();
43         }
44         
45         public AAIQueryClient(AAIVersion version, UUID requestId) {
46                 super(requestId);
47                 this.version = version;
48         }
49         
50         public AAIQueryClient(AAIVersion version) {
51                 this(version, UUID.randomUUID());
52         }
53         
54         public String query(Format format, CustomQuery query) {
55                 return this.createClient(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY).queryParam("format", format.toString()))
56                 .addRequestId(requestId).put(query, String.class);
57         }
58         
59         @Override
60         protected AAIVersion getVersion() {
61                 return this.version;
62         }
63         
64         public AAIQueryClient depth (String depth) {
65                 this.depth = Optional.of(depth);
66                 return this;
67         }
68         public AAIQueryClient nodesOnly() {
69                 this.nodesOnly = true;
70                 return this;
71         }
72         public AAIQueryClient subgraph(AAISubgraphType type){
73                 
74                 subgraph =  Optional.of(type);
75
76                 return this;
77         }
78         
79         @Override
80         public RestClient createClient(AAIUri uri) {
81                 AAIUri clone = uri.clone();
82                 if (this.depth.isPresent()) {
83                         clone.queryParam("depth", depth.get());
84                 }
85                 if (this.nodesOnly) {
86                         clone.queryParam("nodesOnly", "");
87                 }
88                 if (this.subgraph.isPresent()) {
89                         clone.queryParam("subgraph", this.subgraph.get().toString());
90                 }
91                 return super.createClient(clone);
92         }
93 }