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.so.client.aai;
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
30 import java.util.Optional;
31 import java.util.UUID;
32 import javax.ws.rs.core.Response;
33 import org.onap.aai.domain.yang.GenericVnf;
34 import org.onap.aai.domain.yang.GenericVnfs;
35 import org.onap.aai.domain.yang.Pnf;
36 import org.onap.aai.domain.yang.Pserver;
37 import org.onap.aai.domain.yang.Pservers;
38 import org.onap.so.client.aai.entities.CustomQuery;
39 import org.onap.so.client.aai.entities.Results;
40 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
41 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
42 import org.onap.so.client.graphinventory.Format;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.stereotype.Service;
49 public class AAIRestClientImpl implements AAIRestClientI {
51 private static Logger logger = LoggerFactory.getLogger(AAIClient.class);
52 private static final AAIVersion ENDPOINT_VERSION = AAIVersion.V10;
53 private static final String ENDPOINT_GET_ALL = ENDPOINT_VERSION + "/cloud-infrastructure/pservers";
54 private static final String ENDPOINT_GET_ALL_VNFS = ENDPOINT_VERSION + "/network/generic-vnfs";
55 private static final String ENDPOINT_CUSTOM_QUERY = ENDPOINT_VERSION + "/query";
56 private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
57 private static final String GENERIC_VNF_PATH = ENDPOINT_VERSION + "/network/generic-vnfs/generic-vnf";
58 private static final String SERVICE_TOPOLOGY_BY_SERVICE_INSTANCE_ID =
59 "store(‘x’).union(__.in(‘subscribesTo’).has(‘aai-node-type’,’customer’).store(‘x’),__.out(‘uses’).has(‘aai-node-type’,’allotted-resource’).store(‘x’),__.in(‘hasInstance’).has(‘aai-node-type’,’generic-vnf’).store(‘x’).union("
60 + ".out(‘has’).has(‘aai-node-type’,’vf-module’).store(‘x’),out(‘uses’).has(‘aai-node-type’,’volume-group’).store(‘x’),"
61 + ".out(‘hasLInterface’).has(‘aai-node-type’,’l-interface’).union("
62 + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv4-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’),"
63 + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv6-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)"
64 + ")," + ".out(‘runsOnVserver’).has(‘aai-node-type’,’vserver’).store(‘x’).union("
65 + ".in(‘owns’).has(‘aai-node-type’,’tenant’).store(‘x’).in(‘has’).has(‘aai-node-type’,’cloud-region’).store(‘x’),"
66 + ".out(‘runsOnPserver’).has(‘aai-node-type’,’pserver’).store(‘x’),"
67 + ".out(‘hasLInterface’).has(‘aai-node-type’,’l-interface’).union("
68 + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv4-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’),"
69 + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv6-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)"
70 + ")" + ")" + ")" + ").cap(‘x’).unfold().dedup()";
72 public AAIRestClientImpl() {
76 public Pservers getPhysicalServers(String hostName, String uuid) {
79 requestId = UUID.fromString(uuid);
80 } catch (IllegalArgumentException e) {
81 logger.warn("could not parse uuid: " + uuid + " creating valid uuid automatically");
82 requestId = UUID.randomUUID();
84 return new AAIResourcesClient(ENDPOINT_VERSION)
85 .get(Pservers.class, AAIUriFactory.createResourceUri(AAIObjectPlurals.PSERVER)).orElse(null);
89 public List<Pserver> getPhysicalServerByVnfId(String vnfId, String transactionLoggingUuid) throws IOException {
92 requestId = UUID.fromString(transactionLoggingUuid);
93 } catch (IllegalArgumentException e) {
94 logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
95 requestId = UUID.randomUUID();
97 List<AAIResourceUri> startNodes = new ArrayList<>();
98 startNodes.add(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
99 String jsonInput = new AAIQueryClient(ENDPOINT_VERSION)
100 .query(Format.RESOURCE, new CustomQuery(startNodes, PSERVER_VNF_QUERY));
102 return this.getListOfPservers(jsonInput);
106 protected List<Pserver> getListOfPservers(String jsonInput) throws IOException {
107 ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
108 Results<Map<String, Pserver>> resultsFromJson = mapper.readValue(jsonInput,
109 new TypeReference<Results<Map<String, Pserver>>>() {
111 List<Pserver> results = new ArrayList<>();
112 for (Map<String, Pserver> m : resultsFromJson.getResult()) {
113 results.add(m.get("pserver"));
119 public void updateMaintenceFlag(String vnfName, boolean inMaint, String transactionLoggingUuid) throws IOException {
122 requestId = UUID.fromString(transactionLoggingUuid);
123 } catch (IllegalArgumentException e) {
124 logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
125 requestId = UUID.randomUUID();
127 GenericVnfs genericVnfs = new AAIResourcesClient(ENDPOINT_VERSION).get(GenericVnfs.class,
128 AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName)).orElse(null);
129 if (genericVnfs.getGenericVnf().size() > 1) {
130 throw new IndexOutOfBoundsException("Multiple Generic Vnfs Returned");
133 GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
134 updateMaintenceFlagVnfId(genericVnf.getVnfId(), inMaint, transactionLoggingUuid);
138 public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint, String transactionLoggingUuid)
142 requestId = UUID.fromString(transactionLoggingUuid);
143 } catch (IllegalArgumentException e) {
144 logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
145 requestId = UUID.randomUUID();
147 GenericVnf genericVnf = new GenericVnf();
148 genericVnf.setInMaint(inMaint);
149 new AAIResourcesClient(ENDPOINT_VERSION)
150 .update(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId), genericVnf);
155 public GenericVnf getVnfByName(String vnfId, String transactionLoggingUuid) throws IOException {
158 requestId = UUID.fromString(transactionLoggingUuid);
159 } catch (IllegalArgumentException e) {
160 logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
161 requestId = UUID.randomUUID();
163 return new AAIResourcesClient(ENDPOINT_VERSION)
164 .get(GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)).orElse(null);
168 public Optional<Pnf> getPnfByName(String pnfId, String transactionLoggingUuid) throws IOException {
171 requestId = UUID.fromString(transactionLoggingUuid);
172 } catch (IllegalArgumentException e) {
173 logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically", e);
174 requestId = UUID.randomUUID();
176 Response response = new AAIResourcesClient(ENDPOINT_VERSION)
177 .getFullResponse(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfId));
178 if (response.getStatus() != 200) {
179 return Optional.empty();
181 return Optional.of(response.readEntity(Pnf.class));
186 public void createPnf(String pnfId, String transactionLoggingUuid, Pnf pnf) throws IOException {
189 requestId = UUID.fromString(transactionLoggingUuid);
190 } catch (IllegalArgumentException e) {
191 logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically", e);
192 requestId = UUID.randomUUID();
194 new AAIResourcesClient(ENDPOINT_VERSION)
195 .createIfNotExists(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfId), Optional.of(pnf));