2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aaiclient.client.aai;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
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.onap.aaiclient.client.graphinventory.entities.uri.Depth;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.ObjectMapper;
45 public class AAIRestClientImpl implements AAIRestClientI {
47 private final Logger log = LoggerFactory.getLogger(getClass());
48 private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
51 public List<Pserver> getPhysicalServerByVnfId(String vnfId) throws IOException {
52 List<AAIResourceUri> startNodes = new ArrayList<>();
53 startNodes.add(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId)));
54 String jsonInput = new AAIQueryClient().query(Format.RESOURCE, new CustomQuery(startNodes, PSERVER_VNF_QUERY));
56 return this.getListOfPservers(jsonInput);
60 protected List<Pserver> getListOfPservers(String jsonInput) throws IOException {
61 ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
62 Results<Map<String, Pserver>> resultsFromJson =
63 mapper.readValue(jsonInput, new TypeReference<Results<Map<String, Pserver>>>() {});
64 List<Pserver> results = new ArrayList<>();
65 for (Map<String, Pserver> m : resultsFromJson.getResult()) {
66 results.add(m.get("pserver"));
72 public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint) {
73 GenericVnf genericVnf = new GenericVnf();
74 genericVnf.setInMaint(inMaint);
75 new AAIResourcesClient()
76 .update(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId)), genericVnf);
81 public GenericVnf getVnfByName(String vnfId) {
82 return new AAIResourcesClient().get(GenericVnf.class,
83 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId))).orElse(null);
87 public Optional<Pnf> getPnfByName(String pnfId) {
88 Response response = new AAIResourcesClient()
89 .getFullResponse(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfId)));
90 if (response.getStatus() != 200) {
91 return Optional.empty();
93 return Optional.of(response.readEntity(Pnf.class));
98 public void createPnf(String pnfId, Pnf pnf) {
99 new AAIResourcesClient().createIfNotExists(
100 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfId)), Optional.of(pnf));
104 public void updatePnf(String pnfId, Pnf pnf) {
105 new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfId)),
110 public Optional<ServiceInstance> getServiceInstanceById(String serviceInstanceId, String serviceType,
111 String globalSubscriberId) {
112 Response response = new AAIResourcesClient().getFullResponse(
113 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId)
114 .serviceSubscription(serviceType).serviceInstance(serviceInstanceId)));
115 return Optional.ofNullable(response.readEntity(ServiceInstance.class));
118 public Optional<ServiceInstance> getServiceInstanceByIdWithDepth(String serviceInstanceId, String serviceType,
119 String globalSubscriberId) {
121 new AAIResourcesClient()
122 .getFullResponse(AAIUriFactory
123 .createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId)
124 .serviceSubscription(serviceType).serviceInstance(serviceInstanceId))
126 return Optional.ofNullable(response.readEntity(ServiceInstance.class));
130 public void updateServiceInstance(String serviceInstanceId, String serviceType, String globalSubscriberId,
131 ServiceInstance serviceInstance) {
133 new AAIResourcesClient().update(AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
134 .customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(serviceInstanceId)),
136 } catch (Throwable ex) {
137 log.error("Exception happened while updating ServiceInstance, Exception: {}", ex.getLocalizedMessage());
138 throw new RuntimeException(ex);