bf9ad56da724c75b74a279ca6e8593b0c5fa508c
[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 java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import javax.ws.rs.core.Response;
29 import org.onap.aai.domain.yang.GenericVnf;
30 import org.onap.aai.domain.yang.Pnf;
31 import org.onap.aai.domain.yang.Pserver;
32 import org.onap.aai.domain.yang.ServiceInstance;
33 import org.onap.aaiclient.client.aai.entities.CustomQuery;
34 import org.onap.aaiclient.client.aai.entities.Results;
35 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
36 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
37 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
38 import org.onap.aaiclient.client.graphinventory.Format;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import com.fasterxml.jackson.core.type.TypeReference;
42 import com.fasterxml.jackson.databind.ObjectMapper;
43
44 public class AAIRestClientImpl implements AAIRestClientI {
45
46     private final Logger log = LoggerFactory.getLogger(getClass());
47     private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
48
49     @Override
50     public List<Pserver> getPhysicalServerByVnfId(String vnfId) throws IOException {
51         List<AAIResourceUri> startNodes = new ArrayList<>();
52         startNodes.add(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId)));
53         String jsonInput = new AAIQueryClient().query(Format.RESOURCE, new CustomQuery(startNodes, PSERVER_VNF_QUERY));
54
55         return this.getListOfPservers(jsonInput);
56
57     }
58
59     protected List<Pserver> getListOfPservers(String jsonInput) throws IOException {
60         ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
61         Results<Map<String, Pserver>> resultsFromJson =
62                 mapper.readValue(jsonInput, new TypeReference<Results<Map<String, Pserver>>>() {});
63         List<Pserver> results = new ArrayList<>();
64         for (Map<String, Pserver> m : resultsFromJson.getResult()) {
65             results.add(m.get("pserver"));
66         }
67         return results;
68     }
69
70     @Override
71     public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint) {
72         GenericVnf genericVnf = new GenericVnf();
73         genericVnf.setInMaint(inMaint);
74         new AAIResourcesClient()
75                 .update(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId)), genericVnf);
76
77     }
78
79     @Override
80     public GenericVnf getVnfByName(String vnfId) {
81         return new AAIResourcesClient().get(GenericVnf.class,
82                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId))).orElse(null);
83     }
84
85     @Override
86     public Optional<Pnf> getPnfByName(String pnfId) {
87         Response response = new AAIResourcesClient()
88                 .getFullResponse(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfId)));
89         if (response.getStatus() != 200) {
90             return Optional.empty();
91         } else {
92             return Optional.of(response.readEntity(Pnf.class));
93         }
94     }
95
96     @Override
97     public void createPnf(String pnfId, Pnf pnf) {
98         new AAIResourcesClient().createIfNotExists(
99                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfId)), Optional.of(pnf));
100     }
101
102     @Override
103     public void updatePnf(String pnfId, Pnf pnf) {
104         new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfId)),
105                 pnf);
106     }
107
108     @Override
109     public Optional<ServiceInstance> getServiceInstanceById(String serviceInstanceId, String serviceType,
110             String globalSubscriberId) {
111         Response response = new AAIResourcesClient().getFullResponse(
112                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId)
113                         .serviceSubscription(serviceType).serviceInstance(serviceInstanceId)));
114         return Optional.ofNullable(response.readEntity(ServiceInstance.class));
115     }
116
117     @Override
118     public void updateServiceInstance(String serviceInstanceId, String serviceType, String globalSubscriberId,
119             ServiceInstance serviceInstance) {
120         try {
121             new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
122                     .customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(serviceInstanceId)),
123                     serviceInstance);
124         } catch (Throwable ex) {
125             log.error("Exception happened while updating ServiceInstance, Exception: {}", ex.getLocalizedMessage());
126             throw new RuntimeException(ex);
127         }
128     }
129 }