private void populateExtensions() {
    for (NetworkServiceProviderFactory extp :
        pluginRgty.getExtensionList(NetworkServiceProviderFactory.class)) {
      NetworkServiceProviderFactory old = providerFactories.get(extp.getType().toString());
      if (old != null) {
        throw new CloudRuntimeException(
            String.format(
                "duplicate NetworkServiceProviderFactory[%s, %s] for type[%s]",
                extp.getClass().getName(), old.getClass().getName(), extp.getType()));
      }
      providerFactories.put(extp.getType().toString(), extp);
    }

    for (ApplyNetworkServiceExtensionPoint extp :
        pluginRgty.getExtensionList(ApplyNetworkServiceExtensionPoint.class)) {
      ApplyNetworkServiceExtensionPoint old = providerExts.get(extp.getProviderType().toString());
      if (old != null) {
        throw new CloudRuntimeException(
            String.format(
                "duplicate ApplyNetworkServiceExtensionPoint[%s, %s] for type[%s]",
                extp.getClass().getName(), old.getClass().getName(), extp.getProviderType()));
      }
      providerExts.put(extp.getProviderType().toString(), extp);
    }

    nsExts = pluginRgty.getExtensionList(NetworkServiceExtensionPoint.class);
  }
Esempio n. 2
0
 private void populateExtensions() {
   for (ZoneFactory f : pluginRgty.getExtensionList(ZoneFactory.class)) {
     ZoneFactory old = zoneFactories.get(f.getType().toString());
     if (old != null) {
       throw new CloudRuntimeException(
           String.format(
               "duplicate ZoneFactory[%s, %s] for type[%s]",
               f.getClass().getName(), old.getClass().getName(), f.getType()));
     }
     zoneFactories.put(f.getType().toString(), f);
   }
 }
Esempio n. 3
0
  @Override
  public boolean start() {
    for (LocalStorageBackupStorageMediator m :
        pluginRgty.getExtensionList(LocalStorageBackupStorageMediator.class)) {
      for (String hvType : m.getSupportedHypervisorTypes()) {
        String key = makeMediatorKey(hvType, m.getSupportedBackupStorageType().toString());
        LocalStorageBackupStorageMediator old = backupStorageMediatorMap.get(key);
        if (old != null) {
          throw new CloudRuntimeException(
              String.format(
                  "duplicate LocalStorageBackupStorageMediator[%s, %s] for hypervisor type[%s] and backup storage type[%s]",
                  m, old, hvType, m.getSupportedBackupStorageType()));
        }

        backupStorageMediatorMap.put(key, m);
      }
    }

    return true;
  }
Esempio n. 4
0
  protected void recoverVolume(Completion completion) {
    final VolumeInventory vol = getSelfInventory();
    List<RecoverDataVolumeExtensionPoint> exts =
        pluginRgty.getExtensionList(RecoverDataVolumeExtensionPoint.class);
    for (RecoverDataVolumeExtensionPoint ext : exts) {
      ext.preRecoverDataVolume(vol);
    }

    CollectionUtils.safeForEach(
        exts,
        new ForEachFunction<RecoverDataVolumeExtensionPoint>() {
          @Override
          public void run(RecoverDataVolumeExtensionPoint ext) {
            ext.beforeRecoverDataVolume(vol);
          }
        });

    VolumeStatus oldStatus = self.getStatus();

    if (self.getInstallPath() != null) {
      self.setStatus(VolumeStatus.Ready);
    } else {
      self.setStatus(VolumeStatus.NotInstantiated);
    }
    self = dbf.updateAndRefresh(self);

    new FireVolumeCanonicalEvent().fireVolumeStatusChangedEvent(oldStatus, getSelfInventory());

    CollectionUtils.safeForEach(
        exts,
        new ForEachFunction<RecoverDataVolumeExtensionPoint>() {
          @Override
          public void run(RecoverDataVolumeExtensionPoint ext) {
            ext.afterRecoverDataVolume(vol);
          }
        });

    completion.success();
  }
Esempio n. 5
0
  @Transactional(readOnly = true)
  private List<VmInstanceVO> getCandidateVmForAttaching(String accountUuid) {
    List<String> vmUuids =
        acntMgr.getResourceUuidsCanAccessByAccount(accountUuid, VmInstanceVO.class);

    if (vmUuids != null && vmUuids.isEmpty()) {
      return new ArrayList<VmInstanceVO>();
    }

    TypedQuery<VmInstanceVO> q = null;
    String sql;
    if (vmUuids == null) {
      // all vms
      if (self.getStatus() == VolumeStatus.Ready) {
        sql =
            "select vm from VmInstanceVO vm, PrimaryStorageClusterRefVO ref, VolumeVO vol where vm.state in (:vmStates) and vol.uuid = :volUuid and vm.hypervisorType in (:hvTypes) and vm.clusterUuid = ref.clusterUuid and ref.primaryStorageUuid = vol.primaryStorageUuid group by vm.uuid";
        q = dbf.getEntityManager().createQuery(sql, VmInstanceVO.class);
        q.setParameter("volUuid", self.getUuid());
        List<String> hvTypes =
            VolumeFormat.valueOf(self.getFormat())
                .getHypervisorTypesSupportingThisVolumeFormatInString();
        q.setParameter("hvTypes", hvTypes);
      } else if (self.getStatus() == VolumeStatus.NotInstantiated) {
        sql = "select vm from VmInstanceVO vm where vm.state in (:vmStates) group by vm.uuid";
        q = dbf.getEntityManager().createQuery(sql, VmInstanceVO.class);
      } else {
        DebugUtils.Assert(
            false, String.format("should not reach here, volume[uuid:%s]", self.getUuid()));
      }
    } else {
      if (self.getStatus() == VolumeStatus.Ready) {
        sql =
            "select vm from VmInstanceVO vm, PrimaryStorageClusterRefVO ref, VolumeVO vol where vm.uuid in (:vmUuids) and vm.state in (:vmStates) and vol.uuid = :volUuid and vm.hypervisorType in (:hvTypes) and vm.clusterUuid = ref.clusterUuid and ref.primaryStorageUuid = vol.primaryStorageUuid group by vm.uuid";
        q = dbf.getEntityManager().createQuery(sql, VmInstanceVO.class);
        q.setParameter("volUuid", self.getUuid());
        List<String> hvTypes =
            VolumeFormat.valueOf(self.getFormat())
                .getHypervisorTypesSupportingThisVolumeFormatInString();
        q.setParameter("hvTypes", hvTypes);
      } else if (self.getStatus() == VolumeStatus.NotInstantiated) {
        sql =
            "select vm from VmInstanceVO vm where vm.uuid in (:vmUuids) and vm.state in (:vmStates) group by vm.uuid";
        q = dbf.getEntityManager().createQuery(sql, VmInstanceVO.class);
      } else {
        DebugUtils.Assert(
            false, String.format("should not reach here, volume[uuid:%s]", self.getUuid()));
      }

      q.setParameter("vmUuids", vmUuids);
    }

    q.setParameter("vmStates", Arrays.asList(VmInstanceState.Running, VmInstanceState.Stopped));
    List<VmInstanceVO> vms = q.getResultList();
    if (vms.isEmpty()) {
      return vms;
    }

    VolumeInventory vol = getSelfInventory();
    for (VolumeGetAttachableVmExtensionPoint ext :
        pluginRgty.getExtensionList(VolumeGetAttachableVmExtensionPoint.class)) {
      vms = ext.returnAttachableVms(vol, vms);
    }

    return vms;
  }
Esempio n. 6
0
  private void deleteVolume(final VolumeDeletionMsg msg, final NoErrorCompletion completion) {
    final VolumeDeletionReply reply = new VolumeDeletionReply();
    for (VolumeDeletionExtensionPoint extp :
        pluginRgty.getExtensionList(VolumeDeletionExtensionPoint.class)) {
      extp.preDeleteVolume(getSelfInventory());
    }

    CollectionUtils.safeForEach(
        pluginRgty.getExtensionList(VolumeDeletionExtensionPoint.class),
        new ForEachFunction<VolumeDeletionExtensionPoint>() {
          @Override
          public void run(VolumeDeletionExtensionPoint arg) {
            arg.beforeDeleteVolume(getSelfInventory());
          }
        });

    FlowChain chain = FlowChainBuilder.newShareFlowChain();
    chain.setName(String.format("delete-volume-%s", self.getUuid()));
    // for NotInstantiated Volume, no flow to execute
    chain.allowEmptyFlow();
    chain
        .then(
            new ShareFlow() {
              VolumeDeletionPolicy deletionPolicy;

              {
                if (msg.getDeletionPolicy() == null) {
                  deletionPolicy = deletionPolicyMgr.getDeletionPolicy(self.getUuid());
                } else {
                  deletionPolicy = VolumeDeletionPolicy.valueOf(msg.getDeletionPolicy());
                }
              }

              @Override
              public void setup() {
                if (self.getVmInstanceUuid() != null
                    && self.getType() == VolumeType.Data
                    && msg.isDetachBeforeDeleting()) {
                  flow(
                      new NoRollbackFlow() {
                        String __name__ = "detach-volume-from-vm";

                        public void run(final FlowTrigger trigger, Map data) {
                          DetachDataVolumeFromVmMsg dmsg = new DetachDataVolumeFromVmMsg();
                          dmsg.setVolume(getSelfInventory());
                          bus.makeTargetServiceIdByResourceUuid(
                              dmsg, VmInstanceConstant.SERVICE_ID, dmsg.getVmInstanceUuid());
                          bus.send(
                              dmsg,
                              new CloudBusCallBack(trigger) {
                                @Override
                                public void run(MessageReply reply) {
                                  self.setVmInstanceUuid(null);
                                  self = dbf.updateAndRefresh(self);
                                  trigger.next();
                                }
                              });
                        }
                      });
                }

                if (deletionPolicy == VolumeDeletionPolicy.Direct) {
                  flow(
                      new NoRollbackFlow() {
                        String __name__ = "delete-data-volume-from-primary-storage";

                        @Override
                        public void run(final FlowTrigger trigger, Map data) {
                          if (self.getStatus() == VolumeStatus.Ready) {
                            DeleteVolumeOnPrimaryStorageMsg dmsg =
                                new DeleteVolumeOnPrimaryStorageMsg();
                            dmsg.setVolume(getSelfInventory());
                            dmsg.setUuid(self.getPrimaryStorageUuid());
                            bus.makeTargetServiceIdByResourceUuid(
                                dmsg,
                                PrimaryStorageConstant.SERVICE_ID,
                                self.getPrimaryStorageUuid());
                            logger.debug(
                                String.format(
                                    "Asking primary storage[uuid:%s] to remove data volume[uuid:%s]",
                                    self.getPrimaryStorageUuid(), self.getUuid()));
                            bus.send(
                                dmsg,
                                new CloudBusCallBack(trigger) {
                                  @Override
                                  public void run(MessageReply reply) {
                                    if (!reply.isSuccess()) {
                                      logger.warn(
                                          String.format(
                                              "failed to delete volume[uuid:%s, name:%s], %s",
                                              self.getUuid(), self.getName(), reply.getError()));
                                    }

                                    trigger.next();
                                  }
                                });
                          } else {
                            trigger.next();
                          }
                        }
                      });
                }

                if (self.getPrimaryStorageUuid() != null
                    && deletionPolicy == VolumeDeletionPolicy.Direct) {
                  flow(
                      new NoRollbackFlow() {
                        String __name__ = "return-primary-storage-capacity";

                        @Override
                        public void run(FlowTrigger trigger, Map data) {
                          ReturnPrimaryStorageCapacityMsg rmsg =
                              new ReturnPrimaryStorageCapacityMsg();
                          rmsg.setPrimaryStorageUuid(self.getPrimaryStorageUuid());
                          rmsg.setDiskSize(self.getSize());
                          bus.makeTargetServiceIdByResourceUuid(
                              rmsg,
                              PrimaryStorageConstant.SERVICE_ID,
                              self.getPrimaryStorageUuid());
                          bus.send(rmsg);
                          trigger.next();
                        }
                      });
                }

                done(
                    new FlowDoneHandler(msg) {
                      @Override
                      public void handle(Map data) {
                        VolumeStatus oldStatus = self.getStatus();

                        if (deletionPolicy == VolumeDeletionPolicy.Direct) {
                          dbf.remove(self);
                        } else if (deletionPolicy == VolumeDeletionPolicy.Delay) {
                          self.setStatus(VolumeStatus.Deleted);
                          self = dbf.updateAndRefresh(self);
                        } else if (deletionPolicy == VolumeDeletionPolicy.Never) {
                          self.setStatus(VolumeStatus.Deleted);
                          self = dbf.updateAndRefresh(self);
                        }

                        new FireVolumeCanonicalEvent()
                            .fireVolumeStatusChangedEvent(oldStatus, getSelfInventory());

                        CollectionUtils.safeForEach(
                            pluginRgty.getExtensionList(VolumeDeletionExtensionPoint.class),
                            new ForEachFunction<VolumeDeletionExtensionPoint>() {
                              @Override
                              public void run(VolumeDeletionExtensionPoint arg) {
                                arg.afterDeleteVolume(getSelfInventory());
                              }
                            });
                        bus.reply(msg, reply);
                      }
                    });

                error(
                    new FlowErrorHandler(msg) {
                      @Override
                      public void handle(final ErrorCode errCode, Map data) {
                        CollectionUtils.safeForEach(
                            pluginRgty.getExtensionList(VolumeDeletionExtensionPoint.class),
                            new ForEachFunction<VolumeDeletionExtensionPoint>() {
                              @Override
                              public void run(VolumeDeletionExtensionPoint arg) {
                                arg.failedToDeleteVolume(getSelfInventory(), errCode);
                              }
                            });

                        reply.setError(errCode);
                        bus.reply(msg, reply);
                      }
                    });

                Finally(
                    new FlowFinallyHandler() {
                      @Override
                      public void Finally() {
                        completion.done();
                      }
                    });
              }
            })
        .start();
  }
Esempio n. 7
0
  private void expunge(final Completion completion) {
    if (self.getStatus() != VolumeStatus.Deleted) {
      throw new OperationFailureException(
          errf.stringToOperationError(
              String.format(
                  "the volume[uuid:%s, name:%s] is not deleted yet, can't expunge it",
                  self.getUuid(), self.getName())));
    }

    final VolumeInventory inv = getSelfInventory();
    CollectionUtils.safeForEach(
        pluginRgty.getExtensionList(VolumeBeforeExpungeExtensionPoint.class),
        new ForEachFunction<VolumeBeforeExpungeExtensionPoint>() {
          @Override
          public void run(VolumeBeforeExpungeExtensionPoint arg) {
            arg.volumeBeforeExpunge(inv);
          }
        });

    if (self.getPrimaryStorageUuid() != null) {
      DeleteVolumeOnPrimaryStorageMsg dmsg = new DeleteVolumeOnPrimaryStorageMsg();
      dmsg.setVolume(getSelfInventory());
      dmsg.setUuid(self.getPrimaryStorageUuid());
      bus.makeTargetServiceIdByResourceUuid(
          dmsg, PrimaryStorageConstant.SERVICE_ID, self.getPrimaryStorageUuid());
      bus.send(
          dmsg,
          new CloudBusCallBack(completion) {
            @Override
            public void run(MessageReply r) {
              if (!r.isSuccess()) {
                completion.fail(r.getError());
              } else {
                ReturnPrimaryStorageCapacityMsg msg = new ReturnPrimaryStorageCapacityMsg();
                msg.setPrimaryStorageUuid(self.getPrimaryStorageUuid());
                msg.setDiskSize(self.getSize());
                bus.makeTargetServiceIdByResourceUuid(
                    msg, PrimaryStorageConstant.SERVICE_ID, self.getPrimaryStorageUuid());
                bus.send(msg);

                CollectionUtils.safeForEach(
                    pluginRgty.getExtensionList(VolumeAfterExpungeExtensionPoint.class),
                    new ForEachFunction<VolumeAfterExpungeExtensionPoint>() {
                      @Override
                      public void run(VolumeAfterExpungeExtensionPoint arg) {
                        arg.volumeAfterExpunge(inv);
                      }
                    });

                dbf.remove(self);
                completion.success();
              }
            }
          });
    } else {
      CollectionUtils.safeForEach(
          pluginRgty.getExtensionList(VolumeAfterExpungeExtensionPoint.class),
          new ForEachFunction<VolumeAfterExpungeExtensionPoint>() {
            @Override
            public void run(VolumeAfterExpungeExtensionPoint arg) {
              arg.volumeAfterExpunge(inv);
            }
          });

      dbf.remove(self);
      completion.success();
    }
  }
 public PrimaryStorageTagAllocatorFlow() {
   if (tagExtensions == null) {
     tagExtensions = pluginRgty.getExtensionList(PrimaryStorageTagAllocatorExtensionPoint.class);
   }
 }