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.openecomp.mso.client.aai;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import java.util.UUID;
29 import org.onap.aai.domain.yang.GenericVnf;
30 import org.onap.aai.domain.yang.GenericVnfs;
31 import org.onap.aai.domain.yang.Pserver;
32 import org.onap.aai.domain.yang.Pservers;
33 import org.openecomp.mso.client.aai.entities.CustomQuery;
34 import org.openecomp.mso.client.aai.entities.Results;
35 import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;
36 import org.openecomp.mso.client.aai.entities.uri.AAIUriFactory;
37 import org.springframework.stereotype.Service;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.fasterxml.jackson.core.JsonParseException;
42 import com.fasterxml.jackson.core.type.TypeReference;
43 import com.fasterxml.jackson.databind.JsonMappingException;
44 import com.fasterxml.jackson.databind.ObjectMapper;
48 public class AAIRestClientImpl implements AAIRestClientI {
50 private static final EELFLogger logger = EELFManager.getInstance().getMetricsLogger();
51 private static final AAIVersion ENDPOINT_VERSION = AAIVersion.V10;
52 private static final String ENDPOINT_GET_ALL = ENDPOINT_VERSION + "/cloud-infrastructure/pservers";
53 private static final String ENDPOINT_GET_ALL_VNFS = ENDPOINT_VERSION + "/network/generic-vnfs";
54 private static final String ENDPOINT_CUSTOM_QUERY = ENDPOINT_VERSION + "/query";
55 private static final String PSERVER_VNF_QUERY = "pservers-fromVnf";
56 private static final String GENERIC_VNF_PATH = ENDPOINT_VERSION + "/network/generic-vnfs/generic-vnf";
57 private static final String SERVICE_TOPOLOGY_BY_SERVICE_INSTANCE_ID = "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("
58 + ".out(‘has’).has(‘aai-node-type’,’vf-module’).store(‘x’),out(‘uses’).has(‘aai-node-type’,’volume-group’).store(‘x’),"
59 + ".out(‘hasLInterface’).has(‘aai-node-type’,’l-interface’).union("
60 + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv4-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’),"
61 + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv6-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)"
62 + ")," + ".out(‘runsOnVserver’).has(‘aai-node-type’,’vserver’).store(‘x’).union("
63 + ".in(‘owns’).has(‘aai-node-type’,’tenant’).store(‘x’).in(‘has’).has(‘aai-node-type’,’cloud-region’).store(‘x’),"
64 + ".out(‘runsOnPserver’).has(‘aai-node-type’,’pserver’).store(‘x’),"
65 + ".out(‘hasLInterface’).has(‘aai-node-type’,’l-interface’).union("
66 + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv4-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’),"
67 + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv6-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)"
68 + ")" + ")" + ")" + ").cap(‘x’).unfold().dedup()";
70 public AAIRestClientImpl() {
75 public AAIRestClientImpl(final String host) {
81 public Pservers getPhysicalServers(String hostName, String uuid) {
84 requestId = UUID.fromString(uuid);
85 } catch (IllegalArgumentException e) {
86 logger.warn("could not parse uuid: " + uuid + " creating valid uuid automatically");
87 requestId = UUID.randomUUID();
89 return new AAIResourcesClient(ENDPOINT_VERSION, requestId).get(Pservers.class, AAIUriFactory.createResourceUri(AAIObjectPlurals.PSERVER));
93 public List<Pserver> getPhysicalServerByVnfId(String vnfId, String transactionLoggingUuid)
94 throws JsonParseException, JsonMappingException, IOException {
97 requestId = UUID.fromString(transactionLoggingUuid);
98 } catch (IllegalArgumentException e) {
99 logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
100 requestId = UUID.randomUUID();
102 List<AAIResourceUri> startNodes = new ArrayList<>();
103 startNodes.add(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
104 String jsonInput = new AAIQueryClient(ENDPOINT_VERSION, requestId).query(Format.RESOURCE, new CustomQuery(startNodes,PSERVER_VNF_QUERY));
106 return this.getListOfPservers(jsonInput);
110 protected List<Pserver> getListOfPservers(String jsonInput) throws JsonParseException, JsonMappingException, IOException {
111 ObjectMapper mapper = new AAICommonObjectMapperProvider().getContext(Object.class);
112 Results<Map<String, Pserver>> resultsFromJson = mapper.readValue(jsonInput,
113 new TypeReference<Results<Map<String, Pserver>>>() {
115 List<Pserver> results = new ArrayList<>();
116 for (Map<String, Pserver> m : resultsFromJson.getResult()) {
117 results.add(m.get("pserver"));
122 public void updateMaintenceFlag(String vnfName, boolean inMaint, String transactionLoggingUuid) throws JsonParseException, JsonMappingException, IOException {
125 requestId = UUID.fromString(transactionLoggingUuid);
126 } catch (IllegalArgumentException e) {
127 logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
128 requestId = UUID.randomUUID();
130 GenericVnfs genericVnfs = new AAIResourcesClient(ENDPOINT_VERSION, requestId).get(GenericVnfs.class, AAIUriFactory.createResourceUri(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName));
131 if(genericVnfs.getGenericVnf().size() > 1)
132 throw new IndexOutOfBoundsException("Multiple Generic Vnfs Returned");
134 GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
135 updateMaintenceFlagVnfId(genericVnf.getVnfId(), inMaint, transactionLoggingUuid);
139 public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint, String transactionLoggingUuid) throws JsonParseException, JsonMappingException, IOException {
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, requestId).update(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId), genericVnf);
154 public GenericVnf getVnfByName(String vnfId, String transactionLoggingUuid) throws JsonParseException, JsonMappingException, IOException {
157 requestId = UUID.fromString(transactionLoggingUuid);
158 } catch (IllegalArgumentException e) {
159 logger.warn("could not parse uuid: " + transactionLoggingUuid + " creating valid uuid automatically");
160 requestId = UUID.randomUUID();
162 return new AAIResourcesClient(ENDPOINT_VERSION, requestId).get(GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));