Merge "Developer guide for mso-openstack-adapters Issue-ID: SO-3154"
[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.aai.domain.yang.ServiceInstance;
35 import org.onap.aaiclient.client.aai.entities.CustomQuery;
36 import org.onap.aaiclient.client.aai.entities.Results;
37 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
38 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
39 import org.onap.aaiclient.client.graphinventory.Format;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class AAIRestClientImpl implements AAIRestClientI {
44
45     private final Logger log = LoggerFactory.getLogger(getClass());
46     private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
47
48     @Override
49     public List<Pserver> getPhysicalServerByVnfId(String vnfId) throws IOException {
50         List<AAIResourceUri> startNodes = new ArrayList<>();
51         startNodes.add(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
52         String jsonInput = new AAIQueryClient().query(Format.RESOURCE, new CustomQuery(startNodes, PSERVER_VNF_QUERY));
53
54         return this.getListOfPservers(jsonInput);
55
56     }
57
58     protected List<Pserver> getListOfPservers(String jsonInput) throws IOException {
59         ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
60         Results<Map<String, Pserver>> resultsFromJson =
61                 mapper.readValue(jsonInput, new TypeReference<Results<Map<String, Pserver>>>() {});
62         List<Pserver> results = new ArrayList<>();
63         for (Map<String, Pserver> m : resultsFromJson.getResult()) {
64             results.add(m.get("pserver"));
65         }
66         return results;
67     }
68
69     @Override
70     public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint) {
71         GenericVnf genericVnf = new GenericVnf();
72         genericVnf.setInMaint(inMaint);
73         new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId), genericVnf);
74
75     }
76
77     @Override
78     public GenericVnf getVnfByName(String vnfId) {
79         return new AAIResourcesClient()
80                 .get(GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)).orElse(null);
81     }
82
83     @Override
84     public Optional<Pnf> getPnfByName(String pnfId) {
85         Response response =
86                 new AAIResourcesClient().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, Pnf pnf) {
96         new AAIResourcesClient().createIfNotExists(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfId),
97                 Optional.of(pnf));
98     }
99
100     @Override
101     public void updatePnf(String pnfId, Pnf pnf) {
102         new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfId), pnf);
103     }
104
105     @Override
106     public Optional<ServiceInstance> getServiceInstanceById(String globalSubscriberId, String serviceType,
107             String serviceInstanceId) {
108         Response response = new AAIResourcesClient().getFullResponse(AAIUriFactory
109                 .createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, serviceInstanceId));
110         return Optional.ofNullable(response.readEntity(ServiceInstance.class));
111     }
112
113     @Override
114     public void updateServiceInstance(String globalSubscriberId, String serviceType, String serviceInstanceId,
115             ServiceInstance serviceInstance) {
116         try {
117             new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,
118                     globalSubscriberId, serviceType, serviceInstanceId), serviceInstance);
119         } catch (Throwable ex) {
120             log.error("Exception happened while updating ServiceInstance, Exception: {}", ex.getLocalizedMessage());
121             throw new RuntimeException(ex);
122         }
123     }
124 }