use config value instead hard code of url
[so.git] / common / src / main / java / org / onap / so / client / aai / AAIRestClientImpl.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.onap.so.client.aai;
22
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import javax.ws.rs.core.Response;
31 import org.onap.aai.domain.yang.GenericVnf;
32 import org.onap.aai.domain.yang.Pnf;
33 import org.onap.aai.domain.yang.Pserver;
34 import org.onap.so.client.aai.entities.CustomQuery;
35 import org.onap.so.client.aai.entities.Results;
36 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
37 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
38 import org.onap.so.client.graphinventory.Format;
39
40 public class AAIRestClientImpl implements AAIRestClientI {
41
42     private static final AAIVersion ENDPOINT_VERSION = AAIVersion.V10;
43     private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
44
45     @Override
46     public List<Pserver> getPhysicalServerByVnfId(String vnfId) throws IOException {
47         List<AAIResourceUri> startNodes = new ArrayList<>();
48         startNodes.add(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
49         String jsonInput = new AAIQueryClient(ENDPOINT_VERSION)
50                 .query(Format.RESOURCE, new CustomQuery(startNodes, PSERVER_VNF_QUERY));
51
52         return this.getListOfPservers(jsonInput);
53
54     }
55
56     protected List<Pserver> getListOfPservers(String jsonInput) throws IOException {
57         ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
58         Results<Map<String, Pserver>> resultsFromJson = mapper.readValue(jsonInput,
59                 new TypeReference<Results<Map<String, Pserver>>>() {
60                 });
61         List<Pserver> results = new ArrayList<>();
62         for (Map<String, Pserver> m : resultsFromJson.getResult()) {
63             results.add(m.get("pserver"));
64         }
65         return results;
66     }
67
68     @Override
69     public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint, String transactionLoggingUuid) {
70         GenericVnf genericVnf = new GenericVnf();
71         genericVnf.setInMaint(inMaint);
72         new AAIResourcesClient(ENDPOINT_VERSION)
73                 .update(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId), genericVnf);
74
75     }
76
77     @Override
78     public GenericVnf getVnfByName(String vnfId, String transactionLoggingUuid) {
79         return new AAIResourcesClient(ENDPOINT_VERSION)
80                 .get(GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)).orElse(null);
81     }
82
83     @Override
84     public Optional<Pnf> getPnfByName(String pnfId, String transactionLoggingUuid) {
85         Response response = new AAIResourcesClient(ENDPOINT_VERSION)
86                 .getFullResponse(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfId));
87         if (response.getStatus() != 200) {
88             return Optional.empty();
89         } else {
90             return Optional.of(response.readEntity(Pnf.class));
91         }
92     }
93
94     @Override
95     public void createPnf(String pnfId, String transactionLoggingUuid, Pnf pnf) {
96         new AAIResourcesClient(ENDPOINT_VERSION)
97                 .createIfNotExists(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfId), Optional.of(pnf));
98     }
99 }