From 06fe3f5d62f9b986ebb8babf6fb140baed44769f Mon Sep 17 00:00:00 2001 From: LiZi Date: Thu, 1 Mar 2018 04:06:52 -0500 Subject: [PATCH] Create relationship while register VIM. Create relationship between cloud region and complex object while register VIM. Change-Id: I8bd121ad653cb27fb8a571d4544c10464e2a1fc4 Issue-ID: AAI-827 Signed-off-by: LiZi --- .../onap/aai/esr/entity/rest/VimRegisterInfo.java | 30 ++++++++---- .../esr/externalservice/aai/CloudRegionProxy.java | 15 +++++- .../aai/esr/externalservice/aai/ICloudRegion.java | 2 +- .../externalservice/aai/RelationshipProvider.java | 54 ++++++++++++++++++++++ .../java/org/onap/aai/esr/util/VimManagerUtil.java | 2 +- .../onap/aai/esr/wrapper/VimManagerWrapper.java | 47 +++++++++++++++---- .../aai/esr/entity/rest/VimRegisterInfoTest.java | 10 +++- .../org/onap/aai/esr/util/VimManagerUtilTest.java | 3 +- .../aai/esr/wrapper/VimManagerWrapperTest.java | 2 + 9 files changed, 140 insertions(+), 25 deletions(-) create mode 100644 esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/RelationshipProvider.java diff --git a/esr-mgr/src/main/java/org/onap/aai/esr/entity/rest/VimRegisterInfo.java b/esr-mgr/src/main/java/org/onap/aai/esr/entity/rest/VimRegisterInfo.java index 0850354..3d6a305 100644 --- a/esr-mgr/src/main/java/org/onap/aai/esr/entity/rest/VimRegisterInfo.java +++ b/esr-mgr/src/main/java/org/onap/aai/esr/entity/rest/VimRegisterInfo.java @@ -1,5 +1,5 @@ /** - * Copyright 2016-2017 ZTE Corporation. + * Copyright 2016-2018 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,9 +31,11 @@ public class VimRegisterInfo { private String ownerDefinedType; private String cloudZone; - + private String complexName; + private String physicalLocationId; + private String cloudExtraInfo; private String status; @@ -88,14 +90,6 @@ public class VimRegisterInfo { this.cloudZone = cloudZone; } - public String getComplexName() { - return complexName; - } - - public void setComplexName(String complexName) { - this.complexName = complexName; - } - public String getCloudExtraInfo() { return cloudExtraInfo; } @@ -120,4 +114,20 @@ public class VimRegisterInfo { this.status = status; } + public String getPhysicalLocationId() { + return physicalLocationId; + } + + public void setPhysicalLocationId(String physicalLocationId) { + this.physicalLocationId = physicalLocationId; + } + + public String getComplexName() { + return complexName; + } + + public void setComplexName(String complexName) { + this.complexName = complexName; + } + } diff --git a/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/CloudRegionProxy.java b/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/CloudRegionProxy.java index bcfedb3..a162cd0 100644 --- a/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/CloudRegionProxy.java +++ b/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/CloudRegionProxy.java @@ -1,5 +1,5 @@ /** - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2018 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.onap.aai.esr.externalservice.aai; import org.glassfish.jersey.client.ClientConfig; import org.onap.aai.esr.common.MsbConfig; import org.onap.aai.esr.entity.aai.CloudRegionDetail; +import org.onap.aai.esr.entity.aai.Relationship; import org.onap.aai.esr.exception.ExtsysException; import com.eclipsesource.jaxrs.consumer.ConsumerFactory; @@ -80,4 +81,16 @@ public class CloudRegionProxy { throw new ExtsysException("Query complexes from A&AI failed.", e); } } + + public void createCloudRegionRelationShip(String cloudOwner, String cloudRegionId, Relationship relationship) throws ExtsysException { + ClientConfig config = new ClientConfig(new RelationshipProvider()); + ICloudRegion createRelationshipProxy = + ConsumerFactory.createConsumer(MsbConfig.getCloudInfrastructureAddr(), config, ICloudRegion.class); + try { + createRelationshipProxy.createCloudRegionRelationship(transactionId, fromAppId, authorization, cloudOwner, + cloudRegionId, relationship); + } catch (Exception e) { + throw new ExtsysException("PUT cloud region to A&AI failed.", e); + } + } } diff --git a/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/ICloudRegion.java b/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/ICloudRegion.java index e5f6531..f2e47b6 100644 --- a/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/ICloudRegion.java +++ b/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/ICloudRegion.java @@ -71,7 +71,7 @@ public interface ICloudRegion { @Path("/cloud-regions/cloud-region/{cloud_owner}/{cloud_region_id}/relationship-list/relationship") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public void putRelationship(@HeaderParam("X-TransactionId") String transactionId, + public void createCloudRegionRelationship(@HeaderParam("X-TransactionId") String transactionId, @HeaderParam("X-FromAppId") String fromApp, @HeaderParam("Authorization") String authorization, @PathParam("cloud_owner") String cloud_owner, @PathParam("cloud_region_id") String cloud_region_id, Relationship relationship) throws ExtsysException; diff --git a/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/RelationshipProvider.java b/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/RelationshipProvider.java new file mode 100644 index 0000000..248cb3f --- /dev/null +++ b/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/RelationshipProvider.java @@ -0,0 +1,54 @@ +/** + * Copyright 2018 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.aai.esr.externalservice.aai; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import org.onap.aai.esr.entity.aai.Relationship; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.google.gson.Gson; + +public class RelationshipProvider implements MessageBodyWriter { + + private static final Logger logger = LoggerFactory.getLogger(EmsRegisterProvider.class); + + @Override + public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return Relationship.class.isAssignableFrom(type); + } + + @Override + public long getSize(Relationship t, Class type, Type genericType, Annotation[] annotations, + MediaType mediaType) { + return -1; + } + + @Override + public void writeTo(Relationship t, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, + MultivaluedMap httpHeaders, OutputStream entityStream) + throws IOException, WebApplicationException { + String json = new Gson().toJson(t, Relationship.class); + logger.info("the param to register EMS input is:" + json); + entityStream.write(json.getBytes("UTF-8")); + } +} diff --git a/esr-mgr/src/main/java/org/onap/aai/esr/util/VimManagerUtil.java b/esr-mgr/src/main/java/org/onap/aai/esr/util/VimManagerUtil.java index b37e319..bbcc5fe 100644 --- a/esr-mgr/src/main/java/org/onap/aai/esr/util/VimManagerUtil.java +++ b/esr-mgr/src/main/java/org/onap/aai/esr/util/VimManagerUtil.java @@ -1,5 +1,5 @@ /** - * Copyright 2017 ZTE Corporation. + * Copyright 2017-2018 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/esr-mgr/src/main/java/org/onap/aai/esr/wrapper/VimManagerWrapper.java b/esr-mgr/src/main/java/org/onap/aai/esr/wrapper/VimManagerWrapper.java index 7545b01..7c3c8d7 100644 --- a/esr-mgr/src/main/java/org/onap/aai/esr/wrapper/VimManagerWrapper.java +++ b/esr-mgr/src/main/java/org/onap/aai/esr/wrapper/VimManagerWrapper.java @@ -22,6 +22,8 @@ import org.onap.aai.esr.entity.aai.CloudRegionDetail; import org.onap.aai.esr.entity.aai.CloudRegionList; import org.onap.aai.esr.entity.aai.ComplexList; import org.onap.aai.esr.entity.aai.EsrSystemInfo; +import org.onap.aai.esr.entity.aai.Relationship; +import org.onap.aai.esr.entity.aai.RelationshipData; import org.onap.aai.esr.entity.rest.VimRegisterInfo; import org.onap.aai.esr.entity.rest.VimRegisterResponse; import org.onap.aai.esr.exception.ExceptionUtil; @@ -68,17 +70,14 @@ public class VimManagerWrapper { CloudRegionDetail cloudRegion = vimManagerUtil.vimRegisterInfo2CloudRegion(vimRegisterInfo); String cloudOwner = vimRegisterInfo.getCloudOwner(); String cloudRegionId = vimRegisterInfo.getCloudRegionId(); + String physicalLocationId = vimRegisterInfo.getPhysicalLocationId(); + //TODO query complex by complex id to get complex name. and put the name to vimRegisterInfo.complexName try { cloudRegionProxy.registerVim(cloudOwner, cloudRegionId, cloudRegion); result.setCloudOwner(cloudOwner); result.setCloudRegionId(cloudRegionId); - Tenant tenant = new Tenant(); - tenant.setDefaultTenant(cloudRegion.getEsrSystemInfoList().getEsrSystemInfo().get(0).getDefaultTenant()); - try { - VimManagerProxy.updateVim(cloudOwner, cloudRegionId, tenant); - } catch (ExtsysException e) { - LOG.error("Update VIM by Multi-cloud failed !", e); - } + createRelationship(cloudOwner, cloudRegionId, physicalLocationId); + updateVimWithMultiCloud(cloudRegion, cloudOwner, cloudRegionId); return Response.ok(result).build(); } catch (ExtsysException error) { LOG.error("Register VIM failed !", error); @@ -86,6 +85,34 @@ public class VimManagerWrapper { } } + private void updateVimWithMultiCloud(CloudRegionDetail cloudRegion, String cloudOwner, String cloudRegionId) { + Tenant tenant = new Tenant(); + tenant.setDefaultTenant(cloudRegion.getEsrSystemInfoList().getEsrSystemInfo().get(0).getDefaultTenant()); + try { + VimManagerProxy.updateVim(cloudOwner, cloudRegionId, tenant); + } catch (ExtsysException e) { + LOG.error("Update VIM by Multi-cloud failed !", e); + } + } + + private void createRelationship(String cloudOwner, String cloudRegionId, String physicalLocationId) { + Relationship relationship = new Relationship(); + RelationshipData relationshipData = new RelationshipData(); + List relationshipDatas = new ArrayList<>(); + String relatedLink = "/aai/v11/cloud-infrastructure/complexes/complex/" + physicalLocationId; + relationship.setRelatedTo("complex"); + relationship.setRelatedLink(relatedLink); + relationshipData.setRelationshipKey("complex.physical-location-id"); + relationshipData.setRelationshipValue(physicalLocationId); + relationshipDatas.add(relationshipData); + relationship.setRelationshipData(relationshipDatas); + try { + cloudRegionProxy.createCloudRegionRelationShip(cloudOwner, cloudRegionId, relationship); + } catch (ExtsysException e) { + LOG.error("Create relationship between cloudRegion and complex failed !", e); + } + } + public Response updateVim(String cloudOwner, String cloudRegionId, VimRegisterInfo vimRegisterInfo) { LOG.info("Start update VIM, input VIM info is: " + extsysUtil.objectToString(vimRegisterInfo)); VimRegisterResponse result = new VimRegisterResponse(); @@ -218,17 +245,17 @@ public class VimManagerWrapper { public Response queryComplexes() { ComplexList complexList = new ComplexList(); - List complexId = new ArrayList<>(); + List physicalLocationIdList = new ArrayList<>(); try { String complexesString = cloudRegionProxy.qureyComplexes(); LOG.info("The complex query result is: " + complexesString); complexList = new Gson().fromJson(complexesString, ComplexList.class); for (int i=0; i