Create relationship while register VIM. 77/33577/2
authorLiZi <li.zi30@zte.com.cn>
Thu, 1 Mar 2018 09:06:52 +0000 (04:06 -0500)
committerLiZi <li.zi30@zte.com.cn>
Thu, 1 Mar 2018 12:12:40 +0000 (07:12 -0500)
Create relationship between cloud region and complex object while register VIM.

Change-Id: I8bd121ad653cb27fb8a571d4544c10464e2a1fc4
Issue-ID: AAI-827
Signed-off-by: LiZi <li.zi30@zte.com.cn>
esr-mgr/src/main/java/org/onap/aai/esr/entity/rest/VimRegisterInfo.java
esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/CloudRegionProxy.java
esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/ICloudRegion.java
esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/RelationshipProvider.java [new file with mode: 0644]
esr-mgr/src/main/java/org/onap/aai/esr/util/VimManagerUtil.java
esr-mgr/src/main/java/org/onap/aai/esr/wrapper/VimManagerWrapper.java
esr-mgr/src/test/java/org/onap/aai/esr/entity/rest/VimRegisterInfoTest.java
esr-mgr/src/test/java/org/onap/aai/esr/util/VimManagerUtilTest.java
esr-mgr/src/test/java/org/onap/aai/esr/wrapper/VimManagerWrapperTest.java

index 0850354..3d6a305 100644 (file)
@@ -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;
+    }
+
 }
index bcfedb3..a162cd0 100644 (file)
@@ -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);
+        }
+    }
 }
index e5f6531..f2e47b6 100644 (file)
@@ -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 (file)
index 0000000..248cb3f
--- /dev/null
@@ -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<Relationship> {
+
+    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<String, Object> 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"));
+    }
+}
index b37e319..bbcc5fe 100644 (file)
@@ -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.
index 7545b01..7c3c8d7 100644 (file)
@@ -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<RelationshipData> 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<String> complexId = new ArrayList<>();
+        List<String> 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<complexList.getComplex().size(); i++) {
-                complexId.add(complexList.getComplex().get(i).getPhysicalLocationId());
+                physicalLocationIdList.add(complexList.getComplex().get(i).getPhysicalLocationId());
             }
         } catch (ExtsysException e) {
             LOG.error("Query vim details by ID failed !", e);
         }
-        return Response.ok(complexId).build();
+        return Response.ok(physicalLocationIdList).build();
     }
 }
index 74761f9..121d702 100644 (file)
@@ -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.
@@ -69,6 +69,14 @@ public class VimRegisterInfoTest {
         assertEquals(vimRegisterInfo.getCloudZone(), cloudZone);
     }
 
+    @Test
+    public void getterAndSetter4physicalLocationId() {
+        final String physicalLocationId = "test";
+        VimRegisterInfo vimRegisterInfo = new VimRegisterInfo();
+        vimRegisterInfo.setPhysicalLocationId(physicalLocationId);
+        assertEquals(vimRegisterInfo.getPhysicalLocationId(), physicalLocationId);
+    }
+    
     @Test
     public void getterAndSetter4complexName() {
         final String complexName = "test";
index 9426753..160de58 100644 (file)
@@ -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.
@@ -47,6 +47,7 @@ public class VimManagerUtilTest {
         vimRegisterInfo.setCloudType("openstack");
         vimRegisterInfo.setCloudZone("default");
         vimRegisterInfo.setComplexName("complex");
+        vimRegisterInfo.setPhysicalLocationId("complex");
         vimRegisterInfo.setOwnerDefinedType("test");
         vimRegisterInfo.setStatus("normal");
         vimAuthInfos.add(vimAuthInfo);
index e1df1bb..7387ddb 100644 (file)
@@ -45,6 +45,7 @@ public class VimManagerWrapperTest {
         vimRegisterInfo.setCloudType("openstack");
         vimRegisterInfo.setCloudZone("default");
         vimRegisterInfo.setComplexName("complex");
+        vimRegisterInfo.setPhysicalLocationId("complex");
         vimRegisterInfo.setOwnerDefinedType("test");
         vimRegisterInfo.setStatus("normal");
         VimAuthInfo vimAuthInfo = new VimAuthInfo();
@@ -132,6 +133,7 @@ public class VimManagerWrapperTest {
         vimRegisterInfo.setCloudType("openstack");
         vimRegisterInfo.setCloudZone("default");
         vimRegisterInfo.setComplexName("complex");
+        vimRegisterInfo.setPhysicalLocationId("complex");
         vimRegisterInfo.setOwnerDefinedType("test");
         vimRegisterInfo.setStatus("normal");
         vimAuthInfos.add(vimAuthInfo);