diff options
author | 2017-09-08 13:27:46 -0400 | |
---|---|---|
committer | 2017-09-08 13:32:24 -0400 | |
commit | 62cd6aaaf74aa91ee0037c0e155c8e7284f07567 (patch) | |
tree | 68c0c53c9156f5aa3c6b3599ac940770f986633d /bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/aai/AAIRestClientImpl.java | |
parent | fa1a211d28a912892fcd888569df033900eb01ee (diff) |
1710 Rebase - Second Attempt
This commit rebases changes from openecomp-mso/internal-staging-1710 up to and including this codecloud commit:
54483fc6606ddb1591a2e9da61bff8712325f924
Wed Sep 6 18:12:56 2017 -0400
Rebasing was done on a branch on top of this commit in so/master in ONAP:
93fbdfbe46104f8859d4754040f979cb7997c157
Thu Sep 7 16:42:59 2017 +0000
Change-Id: I4ad9abf40da32bf5bdca43e868b8fa2dbcd9dc59
Issue-id: SO-107
Signed-off-by: Arthur Martella <amartell@research.att.com>
Diffstat (limited to 'bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/aai/AAIRestClientImpl.java')
-rw-r--r-- | bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/aai/AAIRestClientImpl.java | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/aai/AAIRestClientImpl.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/aai/AAIRestClientImpl.java new file mode 100644 index 0000000000..99024490a3 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/aai/AAIRestClientImpl.java @@ -0,0 +1,167 @@ +package org.openecomp.mso.client.aai; + +import java.io.File; +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import javax.net.ssl.SSLContext; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.openecomp.aai.domain.yang.GenericVnf; +import org.openecomp.aai.domain.yang.GenericVnfs; +import org.openecomp.aai.domain.yang.Pserver; +import org.openecomp.aai.domain.yang.Pservers; +import org.openecomp.mso.bpmn.core.PropertyConfiguration; +import org.openecomp.mso.client.aai.entities.CustomQuery; +import org.openecomp.mso.client.aai.entities.Results; +import org.openecomp.mso.logger.MsoLogger; +import org.openecomp.mso.properties.MsoPropertiesFactory; +import org.springframework.stereotype.Service; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Service +public class AAIRestClientImpl implements AAIRestClient { + + private final WebTarget webTarget; + + private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory(); + + private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL); + + private static final String ENDPOINT_VERSION = "v10"; + private static final String ENDPOINT_GET_ALL = ENDPOINT_VERSION + "/cloud-infrastructure/pservers"; + private static final String ENDPOINT_GET_ALL_VNFS = ENDPOINT_VERSION + "/network/generic-vnfs"; + private static final String ENDPOINT_CUSTOM_QUERY = ENDPOINT_VERSION + "/query"; + private static final String PSERVER_BY_VNF_QUERY = "g.V().has('aai-node-type', 'generic-vnf').has('vnf-name','USAUTOUFTIL2001UJDM02').out('runsOnPserver').has('aai-node-type', 'pserver')"; + private static final String PSERVER_VNF_QUERY = "pservers-fromVnf"; + private static final String GENERIC_VNF_PATH = ENDPOINT_VERSION + "/network/generic-vnfs/generic-vnf"; + 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(" + + ".out(‘has’).has(‘aai-node-type’,’vf-module’).store(‘x’),out(‘uses’).has(‘aai-node-type’,’volume-group’).store(‘x’)," + + ".out(‘hasLInterface’).has(‘aai-node-type’,’l-interface’).union(" + + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv4-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)," + + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv6-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)" + + ")," + ".out(‘runsOnVserver’).has(‘aai-node-type’,’vserver’).store(‘x’).union(" + + ".in(‘owns’).has(‘aai-node-type’,’tenant’).store(‘x’).in(‘has’).has(‘aai-node-type’,’cloud-region’).store(‘x’)," + + ".out(‘runsOnPserver’).has(‘aai-node-type’,’pserver’).store(‘x’)," + + ".out(‘hasLInterface’).has(‘aai-node-type’,’l-interface’).union(" + + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv4-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)," + + ".out(‘hasIpAddress’).has(‘aai-node-type’,’l3-interface-ipv6-address’).store(‘x’).out(‘isMemberOf’).has(‘aai-node-type’,’l3-network’).store(‘x’)" + + ")" + ")" + ")" + ").cap(‘x’).unfold().dedup()"; + + public AAIRestClientImpl() throws NoSuchAlgorithmException { + + Logger logger = Logger.getLogger(getClass().getName()); + Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("mso.bpmn.urn.properties"); + Client client = this.getSSLClient(); + webTarget = client.register(logger).register(new AAIClientResponseExceptionMapper()) + .target(properties.get("aai.endpoint") + "/aai"); + } + + public AAIRestClientImpl(final String host) throws NoSuchAlgorithmException { + Logger logger = Logger.getLogger(getClass().getName()); + Client client = this.getSSLClient(); + Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("mso.bpmn.urn.properties"); + webTarget = client.register(logger).register(new AAIClientResponseExceptionMapper()).target(host + "/aai"); + } + + @Override + public Pservers getPhysicalServers(String hostName, String uuid) { + return webTarget.register(AAIResourcesObjectMapperProvider.class).path(ENDPOINT_GET_ALL).request() + .header("X-FromAppId", "MSO").header("X-TransactionId", uuid) + .header("Content-Type", MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).get() + .readEntity(Pservers.class); + } + + @Override + public List<Pserver> getPhysicalServerByVnfId(String vnfId, String transactionLoggingUuid) + throws JsonParseException, JsonMappingException, IOException { + List<String> startNodes = new ArrayList<String>(); + startNodes.add("network/generic-vnfs/generic-vnf/" + vnfId); + String jsonInput = webTarget.register(AAIQueryObjectMapperProvider.class).path(ENDPOINT_CUSTOM_QUERY) + .queryParam("format", "resource").request().header("X-FromAppId", "MSO") + .header("X-TransactionId", transactionLoggingUuid) + .header("Content-Type", MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE) + .put(Entity.entity(new CustomQuery(startNodes, PSERVER_VNF_QUERY), MediaType.APPLICATION_JSON)) + .readEntity(String.class); + + + return this.getListOfPservers(jsonInput); + } + + protected List<Pserver> getListOfPservers(String jsonInput) throws JsonParseException, JsonMappingException, IOException + { + ObjectMapper mapper = new AAIQueryObjectMapperProvider().getContext(Object.class); + Results<Map<String, Pserver>> resultsFromJson = mapper.readValue(jsonInput, + new TypeReference<Results<Map<String, Pserver>>>() { + }); + List<Pserver> results = new ArrayList<>(); + for (Map<String, Pserver> m : resultsFromJson.getResult()) { + results.add(m.get("pserver")); + } + return results; + } + + protected List<Pserver> getListOfPservers(File jsonInput) throws JsonParseException, JsonMappingException, IOException + { + ObjectMapper mapper = new AAIQueryObjectMapperProvider().getContext(Object.class); + Results<Map<String, Pserver>> resultsFromJson = mapper.readValue(jsonInput, + new TypeReference<Results<Map<String, Pserver>>>() { + }); + List<Pserver> results = new ArrayList<>(); + for (Map<String, Pserver> m : resultsFromJson.getResult()) { + results.add(m.get("pserver")); + } + return results; + } + + @Override + public void updateMaintenceFlag(String vnfName, boolean inMaint, String transactionLoggingUuid) throws Exception { + GenericVnfs genericVnfs = webTarget.register(AAIResourcesObjectMapperProvider.class).path(ENDPOINT_GET_ALL_VNFS) + .queryParam("vnf-name", vnfName).request().header("X-FromAppId", "MSO") + .header("X-TransactionId", transactionLoggingUuid).header("Content-Type", "application/json") + .accept(MediaType.APPLICATION_JSON_TYPE).get().readEntity(GenericVnfs.class); + + if (genericVnfs.getGenericVnf().size() > 1) + throw new Exception("Multiple Generic Vnfs Returned"); + + GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0); + updateMaintenceFlagVnfId(genericVnf.getVnfId(), inMaint, transactionLoggingUuid); + } + + @Override + public void updateMaintenceFlagVnfId(String vnfId, boolean inMaint, String transactionLoggingUuid) + throws Exception { + GenericVnf genericVnf = new GenericVnf(); + genericVnf.setInMaint(inMaint); + webTarget.register(AAIResourcesObjectMapperProvider.class).path(GENERIC_VNF_PATH + "/" + vnfId).request() + .header("X-FromAppId", "MSO").header("X-TransactionId", transactionLoggingUuid) + .header("Content-Type", "application/merge-patch+json") + .header("Accept", MediaType.APPLICATION_JSON_TYPE).header("X-HTTP-Method-Override", "PATCH") + .put(Entity.entity(genericVnf, MediaType.valueOf("application/merge-patch+json"))); + } + + @Override + public GenericVnf getVnfByName(String vnfId, String transactionLoggingUuid) throws Exception { + return webTarget.register(AAIResourcesObjectMapperProvider.class).path(GENERIC_VNF_PATH + "/" + vnfId).request() + .header("X-FromAppId", "MSO").header("X-TransactionId", transactionLoggingUuid) + .header("Content-Type", "application/json").accept(MediaType.APPLICATION_JSON_TYPE).get() + .readEntity(GenericVnf.class); + } + + protected Client getSSLClient() throws NoSuchAlgorithmException { + return ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build(); + } + +} |