X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=artifactbroker%2Fplugins%2Freception-plugins%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fdistribution%2Freception%2Fhandling%2Fsdc%2FSdcReceptionHandler.java;h=48ee0ea5dbff728b5ff4ce44a2a87b8592a81e2d;hb=refs%2Fchanges%2F36%2F88236%2F1;hp=0d100f8f077edd94eba1532ce93354cad3253067;hpb=f0944b0dffa4904161cf40d5da13d5d2d34b0cbe;p=multicloud%2Fframework.git diff --git a/artifactbroker/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java b/artifactbroker/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java index 0d100f8..48ee0ea 100644 --- a/artifactbroker/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java +++ b/artifactbroker/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java @@ -20,16 +20,28 @@ package org.onap.policy.distribution.reception.handling.sdc; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import java.io.File; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.lang.reflect.Type; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.model.CloudArtifact; import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.model.GsonUtil; +import org.onap.policy.distribution.model.VfModuleModel; import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; import org.onap.policy.distribution.reception.handling.sdc.SdcClientHandler.SdcClientOperationType; @@ -41,6 +53,7 @@ import org.onap.sdc.api.consumer.IDistributionStatusMessage; import org.onap.sdc.api.consumer.INotificationCallback; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.notification.IResourceInstance; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.impl.DistributionClientFactory; @@ -87,7 +100,13 @@ public class SdcReceptionHandler extends AbstractReceptionHandler implements INo public void activateCallback(final INotificationData notificationData) { LOGGER.debug("Receieved the notification from SDC with ID: " + notificationData.getDistributionID()); changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.BUSY); - processCsarServiceArtifacts(notificationData); + //Process only the resource artifacts in MC + for (IResourceInstance resource : notificationData.getResources()) { + // We process only VNF resource in MC + if ("VF".equals(resource.getResourceType())) { + this.processVfModulesArtifacts(notificationData,resource); + } + } changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE); LOGGER.debug("Processed the notification from SDC with ID: " + notificationData.getDistributionID()); } @@ -185,40 +204,126 @@ public class SdcReceptionHandler extends AbstractReceptionHandler implements INo } /** - * Method to process csar service artifacts from incoming SDC notification. + * Method to process VF MODULES_METADATA artifacts from incoming SDC notification. + * + * @param artifact the item needs to check if it has been stored in DBB or not + */ + private boolean checkArtifactAlreadyStored(IArtifactInfo artifact) { + //TODO + return false; + + } + + /** + * Method to process VF MODULES_METADATA artifacts from incoming SDC notification. * * @param notificationData the notification from SDC + * @param resource every resource of the service from SDC */ - public void processCsarServiceArtifacts(final INotificationData notificationData) { + public void processVfModulesArtifacts(final INotificationData notificationData, IResourceInstance resource) { boolean artifactsProcessedSuccessfully = true; + LOGGER.debug("start process VF MODULES_METADATA: " ); DistributionStatisticsManager.updateTotalDistributionCount(); - for (final IArtifactInfo artifact : notificationData.getServiceArtifacts()) { + List relevantArtifactTypes = sdcConfig.getRelevantArtifactTypes(); + Path path = Paths.get("/data"); + ArrayList vfModuleModels = new ArrayList<>(); + HashMap artifactMap = new HashMap<>();//key is UUID, value is artifact for shared folder + String vfArtifactData = null; + IArtifactInfo vfModuleMetaDataArtifact = null; + + for (final IArtifactInfo artifact : resource.getArtifacts()) { + artifactMap.put(artifact.getArtifactUUID(),artifact); + + //extract the artifactlist and write them into MongoDB + if (artifact.getArtifactType().equals("VF_MODULES_METADATA")) { + try { + final IDistributionClientDownloadResult resultArtifact = + downloadTheArtifact(artifact,notificationData); + if (resultArtifact != null) { + vfArtifactData = new String(resultArtifact.getArtifactPayload(),"UTF-8"); + LOGGER.debug("VF_MODULE_ARTIFACT: " + new String(resultArtifact.getArtifactPayload(),"UTF-8")); + } + Type listType = new TypeToken>(){}.getType(); + vfModuleModels = new Gson().fromJson(vfArtifactData,listType); + LOGGER.debug("pass to process VF_MODULES_METADATA artifacts "); + vfModuleMetaDataArtifact = artifact; + } catch (final ArtifactDownloadException | UnsupportedEncodingException exp) { + LOGGER.error("Failed to process csar VF_MODULES_METADATA artifacts ", exp); + artifactsProcessedSuccessfully = false; + sendDistributionStatus(DistributionStatusType.DEPLOY, artifact.getArtifactURL(), + notificationData.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR, + "Failed to deploy the artifact due to: " + exp.getMessage()); + } + } + } + + //foreach(vf_module_metadata) + // 1. create a dir like /data/UUID1 + // 2. put the vfmodule-meta.json into it + // 3. put the service-meta.json into it + // 3. go through each aritfact uuid under artifact_list of vf_module and download + for (final VfModuleModel vfModule : vfModuleModels) { try { - final IDistributionClientDownloadResult resultArtifact = - downloadTheArtifact(artifact, notificationData); - final Path filePath = writeArtifactToFile(artifact, resultArtifact); - final Csar csarObject = new Csar(filePath.toString()); - inputReceived(csarObject); - sendDistributionStatus(DistributionStatusType.DEPLOY, artifact.getArtifactURL(), - notificationData.getDistributionID(), DistributionStatusEnum.DEPLOY_OK, null); - deleteArtifactFile(filePath); - } catch (final ArtifactDownloadException | PolicyDecodingException exp) { - LOGGER.error("Failed to process csar service artifacts ", exp); - artifactsProcessedSuccessfully = false; - sendDistributionStatus(DistributionStatusType.DEPLOY, artifact.getArtifactURL(), + //create the new dir + Path temp = Paths.get("/data",vfModule.getVfModuleModelCustomizationUUID()); + path = Files.createDirectory(temp);//create UUID path + //store the value to vfmodule-meta.json + String filePath = Paths.get("/data",vfModule.getVfModuleModelCustomizationUUID(), + "vfmodule-meta.json").toString(); + writeFileByFileWriter(filePath, vfArtifactData); + //store the service level info to serivce-meta.json + filePath = Paths.get("/data",vfModule.getVfModuleModelCustomizationUUID(), + "service-meta.json").toString(); + writeFileByFileWriter(filePath, notificationData.toString()); + LOGGER.debug("pass to create directory artifact file"); + } catch (final IOException exp) { + LOGGER.error("Failed to create directory artifact file", exp); + } + + for (final String uuid : vfModule.getArtifacts()) { + try { + IArtifactInfo artifact = artifactMap.get(uuid); + final IDistributionClientDownloadResult resultArtifact = downloadTheArtifact(artifact, notificationData); + writeArtifactToDir(artifact,resultArtifact,path); + LOGGER.debug("pass to write Artifact to dir "); + } catch (final ArtifactDownloadException exp) { + LOGGER.error("Failed to write artifact to dir ", exp); + artifactsProcessedSuccessfully = false; + sendDistributionStatus(DistributionStatusType.DEPLOY, artifactMap.get(uuid).getArtifactURL(), notificationData.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR, "Failed to deploy the artifact due to: " + exp.getMessage()); + } } } + + //for subplug work + try { + final CloudArtifact cloudArtifact = new CloudArtifact(vfModuleModels, artifactMap); + inputReceived(cloudArtifact); + LOGGER.debug("pass to process cloud artifacts "); + } catch ( final PolicyDecodingException exp) { + LOGGER.error("Failed to process cloud artifacts ", exp); + artifactsProcessedSuccessfully = false; + } + if (artifactsProcessedSuccessfully) { DistributionStatisticsManager.updateDistributionSuccessCount(); sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_OK, null); + if (vfModuleMetaDataArtifact != null) { + LOGGER.debug("one vfModuleMetaDataArtifact found" ); + sendDistributionStatus(DistributionStatusType.DEPLOY, vfModuleMetaDataArtifact.getArtifactURL(), + notificationData.getDistributionID(), DistributionStatusEnum.DEPLOY_OK, null); + } else { + LOGGER.debug("no vfModuleMetaDataArtifact found" ); + } + } else { DistributionStatisticsManager.updateDistributionFailureCount(); sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_ERROR, "Failed to process the artifact"); } + LOGGER.debug("end process VF MODULES_METADATA: " ); } /** @@ -248,6 +353,47 @@ public class SdcReceptionHandler extends AbstractReceptionHandler implements INo return downloadResult; } + /** + * Method to write a string to a file + * @Param filePath the file's path + * @Param content the data to be writen + * @throws IOException if error happends + */ + private static void writeFileByFileWriter(String filePath, String content) throws IOException { + File file = new File(filePath); + synchronized (file) { + try (FileWriter fw = new FileWriter(filePath)) { + fw.write(content); + } catch (final IOException exp) { + LOGGER.error("Failed to write File by File Writer ", exp); + } + } + } + /** + * Method to write the downloaded distribution artifact to local file system with specified dir. + * + * @param artifact the notification artifact + * @param resultArtifact the download result artifact + * @return the local path of written file + * @throws ArtifactDownloadException if error occurs while writing the artifact + */ + private Path writeArtifactToDir(final IArtifactInfo artifact, + final IDistributionClientDownloadResult resultArtifact, Path path) throws ArtifactDownloadException { + try { + final byte[] payloadBytes = resultArtifact.getArtifactPayload(); + //final File tempArtifactFile = File.createTempFile(artifact.getArtifactName(), ".csar"); + File tempArtifactFile = new File(path.toString() + "/" + artifact.getArtifactName()); + try (FileOutputStream fileOutputStream = new FileOutputStream(tempArtifactFile)) { + fileOutputStream.write(payloadBytes, 0, payloadBytes.length); + return tempArtifactFile.toPath(); + } + } catch (final Exception exp) { + final String message = "Failed to write artifact to local repository"; + LOGGER.error(message, exp); + throw new ArtifactDownloadException(message, exp); + } + } + /** * Method to write the downloaded distribution artifact to local file system. *