Merge "Added scriptName for deallocation of NSSI."
[so.git] / graph-inventory / aai-client / src / main / java / org / onap / aaiclient / 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.aaiclient.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.aaiclient.client.aai.entities.CustomQuery;
35 import org.onap.aaiclient.client.aai.entities.Results;
36 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
37 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
38 import org.onap.aaiclient.client.graphinventory.Format;
39
40 public class AAIRestClientImpl implements AAIRestClientI {
41
42     private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
43
44     @Override
45     public List<Pserver> getPhysicalServerByVnfId(String vnfId) throws IOException {
46         List<AAIResourceUri> startNodes = new ArrayList<>();
47         startNodes.add(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
48         String jsonInput = new AAIQueryClient().query(Format.RESOURCE, new CustomQuery(startNodes, PSERVER_VNF_QUERY));
49
50         return this.getListOfPservers(jsonInput);
51
52     }
53
54     protected List<Pserver> getListOfPservers(String jsonInput) throws IOException {
55         ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
56         Results<Map<String, Pserver>> resultsFromJson =
57                 mapper.readValue(jsonInput, new TypeReference<Results<Map<String, Pserver>>>() {});
58         List<Pserver> results = new ArrayList<>();
59         for (Map<String, Pserver> m : resultsFromJson.getResult()) {
60             results.add(m.get("pserver"));
61         }
62         return results;
63     }
64
65     @Override
66     public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint) {
67         GenericVnf genericVnf = new GenericVnf();
68         genericVnf.setInMaint(inMaint);
69         new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId), genericVnf);
70
71     }
72
73     @Override
74     public GenericVnf getVnfByName(String vnfId) {
75         return new AAIResourcesClient()
76                 .get(GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)).orElse(null);
77     }
78
79     @Override
80     public Optional<Pnf> getPnfByName(String pnfId) {
81         Response response =
82                 new AAIResourcesClient().getFullResponse(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfId));
83         if (response.getStatus() != 200) {
84             return Optional.empty();
85         } else {
86             return Optional.of(response.readEntity(Pnf.class));
87         }
88     }
89
90     @Override
91     public void createPnf(String pnfId, Pnf pnf) {
92         new AAIResourcesClient().createIfNotExists(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfId),
93                 Optional.of(pnf));
94     }
95
96     @Override
97     public void updatePnf(String pnfId, Pnf pnf) {
98         new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfId), pnf);
99     }
100 }