Fix k8s artifact url for upload
[multicloud/framework.git] / artifactbroker / plugins / forwarding-plugins / src / main / java / org / onap / policy / distribution / forwarding / k8s / K8sArtifactForwarder.java
index d920907..8ad07df 100644 (file)
@@ -64,24 +64,24 @@ import org.onap.sdc.api.notification.IArtifactInfo;
 public class K8sArtifactForwarder implements ArtifactForwarder {
 
     private static final Logger LOGGER = FlexLogger.getLogger(K8sArtifactForwarder.class);
-    private static final String BASE_PATH = "http://localhost:8081/v1/rb/definition";
+    private static final String BASE_PATH = "http://localhost:9015/v1/rb/definition";
     private static final String CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT = "CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT";
     private Map<String, IArtifactInfo> artifactMap;
 
     private K8sArtifactForwarderParameterGroup configurationParameters = null;
-    
+
 
 
     @Override
     public void forward(PolicyInput  policyInput) {
         if (policyInput instanceof CloudArtifact) {
-            System.out.println("get a CloudArtifact !");  
+            System.out.println("get a CloudArtifact !");
             CloudArtifact cloudArtifact = (CloudArtifact) policyInput;
             artifactMap = cloudArtifact.getArtifactTypeMap();
-            System.out.println("the artifactMap = " + artifactMap);  
+            System.out.println("the artifactMap = " + artifactMap);
             ArrayList<VfModuleModel> vfModuleModels = cloudArtifact.getVfModulePayload();
-            System.out.println("the size of vfModule = " + vfModuleModels.size());  
-                   
+            System.out.println("the size of vfModule = " + vfModuleModels.size());
+
             for (VfModuleModel vfModule : vfModuleModels) {
                 forwardAndUpload(vfModule);
             }
@@ -92,7 +92,7 @@ public class K8sArtifactForwarder implements ArtifactForwarder {
     }
 
     private void forwardAndUpload(VfModuleModel vfModule) {
-        
+
         System.out.println("before create type !");
         boolean definitionCreated = createDefinition(vfModule);
         System.out.println(" after create type !");
@@ -111,8 +111,8 @@ public class K8sArtifactForwarder implements ArtifactForwarder {
                 .create();
 
             Map<String, Object> map = new LinkedHashMap<String, Object>();
-            map.put("rb-name", vfModule.getVfModuleModelName());
-            map.put("rb-version", vfModule.getVfModuleModelVersion());
+            map.put("rb-name", vfModule.getVfModuleModelInvariantUUID());
+            map.put("rb-version", vfModule.getVfModuleModelUUID());
             map.put("descritpion",vfModule.getVfModuleModelDescription());
             Map<String, String> labelMap = new LinkedHashMap<String, String>();
             labelMap.put("vnf_customization_uuid",vfModule.getVfModuleModelCustomizationUUID());
@@ -130,38 +130,64 @@ public class K8sArtifactForwarder implements ArtifactForwarder {
     }
 
     private boolean uploadArtifact(VfModuleModel vfModule) {
-        String url = BASE_PATH + "/" + vfModule.getVfModuleModelName() + "/" 
-            + vfModule.getVfModuleModelVersion() + "/content"; 
+        String url = BASE_PATH + "/" + vfModule.getVfModuleModelInvariantUUID() + "/"
+            + vfModule.getVfModuleModelUUID() + "/content";
         HttpPost httpPost = new HttpPost(url);
         httpPost.addHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
 
         List<String> artifacts = vfModule.getArtifacts();
         System.out.println("artifacts = " + artifacts);
 
-        String cloudUuid = null;
+        String vfNamePrefix = vfModule.getVfModuleModelName().toLowerCase();
+        if ( vfNamePrefix.indexOf("..") > 0 ) {
+            vfNamePrefix = vfNamePrefix.substring(vfNamePrefix.indexOf("..") + 2);
+            if ( vfNamePrefix.indexOf("..") > 0 )
+                vfNamePrefix = vfNamePrefix.substring(0, vfNamePrefix.indexOf("..")) + "_";
+            else
+                vfNamePrefix = "";
+        } else
+            vfNamePrefix = "";
+
         IArtifactInfo cloudArtifact = null;
+        IArtifactInfo firstCloudArtifact = null;
+        int cloudArtifactCount = 0;
         boolean found = false;
 
         for (String artifact: artifacts) {
-            if ( artifactMap.get(artifact) != null 
+            if ( artifactMap.get(artifact) != null
                 && artifactMap.get(artifact).getArtifactType().equals("CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT")) {
-                cloudArtifact = artifactMap.get(artifact);
-                cloudUuid = cloudArtifact.getArtifactUUID();
-                found = true;
-                break;
+                if ( cloudArtifactCount == 0 )
+                    firstCloudArtifact = artifactMap.get(artifact);
+                cloudArtifactCount++;
+                IArtifactInfo tmpArtifact = artifactMap.get(artifact);
+                if ( tmpArtifact.getArtifactName().toLowerCase().startsWith(vfNamePrefix) ) {
+                    cloudArtifact = tmpArtifact;
+                    found = true;
+                    break;
+                }
             }
         }
-        
-        if ( found == false ) {
-            System.out.println(" meets error , no CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT type found ");
-            return false;
+
+        if ( found == false  ) {
+            if ( firstCloudArtifact == null ) {
+                System.out.println(" meets error , no CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT type found ");
+                return false;
+            } else {
+                if ( cloudArtifactCount == 1 || vfNamePrefix == "" ) {
+                    cloudArtifact = firstCloudArtifact;
+                } else {
+                    System.out.println(" meets error , CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT artifacts mismatch ");
+                    return false;
+                }
+            }
         }
-        String cloudArtifactPath = "/data/" + vfModule.getVfModuleModelCustomizationUUID() 
+
+        String cloudArtifactPath = "/data/" + vfModule.getVfModuleModelCustomizationUUID()
             + "/" + cloudArtifact.getArtifactName();
         File file = new File(cloudArtifactPath);
         FileEntity entity = new FileEntity(file);
         httpPost.setEntity(entity);
-        
+
         return invokeHttpPost("uploading", httpPost);
     }
 
@@ -172,29 +198,31 @@ public class K8sArtifactForwarder implements ArtifactForwarder {
     }
 
     protected static boolean invokeHttpPost(String action, HttpPost httpPost)  {
-        System.out.println("httpPost begin!");
+        System.out.println("httpPost URI: " + httpPost);
         boolean ret = false;
 
         String errorMsg;
         label1: {
             try ( CloseableHttpClient httpClient = HttpClients.createDefault() ) {
-                System.out.println("result1") ;
+                System.out.println("Execute Post Body: " + EntityUtils.toString(httpPost.getEntity()));
                 CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpPost);
-                System.out.println("result2") ;
+                System.out.println("result2");
                 String result = EntityUtils.toString(closeableHttpResponse.getEntity());
                 System.out.println("result = {}" + result);
                 System.out.println("status  = {}" + closeableHttpResponse.getStatusLine().getStatusCode());
-                if ( closeableHttpResponse.getStatusLine().getStatusCode() != 200 ) {
-                    System.out.println("exception: ret= " + closeableHttpResponse.getStatusLine().getStatusCode());
-                } else {
+                int status = closeableHttpResponse.getStatusLine().getStatusCode();
+                // [200, 300] means pass
+                if ( (status >=200) && (status <= 300) ) {
                     ret = true;
+                } else {
+                    System.out.println("exception: ret= " + status);
                 }
 
                 closeableHttpResponse.close();
                 break label1;
-            } catch (IOException var) {
-                errorMsg = action + ":httpPostWithJSON connect faild";
-                System.out.println("exception: POST_CONNECT_FAILD : {}" + errorMsg);
+            } catch (IOException exp) {
+                errorMsg = action + " :invokeHttpPost failed with: " + exp.getMessage();
+                System.out.println("exception: POST FAILED : {}" + errorMsg);
             }
         }