
org.xlcloud.console.leases.controllers.LeaseCreateBean Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2012 AMG.lab, a Bull Group Company
*
* 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.xlcloud.console.leases.controllers;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import javax.inject.Inject;
import javax.inject.Named;
import org.xlcloud.console.controllers.i18n.MessageController;
import org.xlcloud.console.controllers.request.RequestParameters;
import org.xlcloud.console.controllers.request.path.ConsolePathParams;
import org.xlcloud.console.extensions.scope.ViewScoped;
import org.xlcloud.console.leases.HypervisorType;
import org.xlcloud.console.leases.TimeUnit;
import org.xlcloud.console.leases.repository.LeasesRepository;
import org.xlcloud.console.repository.DataRepository;
import org.xlcloud.rest.exception.BaseException;
import org.xlcloud.service.Account;
import org.xlcloud.service.Lease;
import org.xlcloud.service.Project;
import org.xlcloud.service.builders.ReservationBuilder;
import com.google.common.collect.HashMultimap;
/**
* Controlled used to create a lease.
*
* @author Krzysztof Szafrański, AMG.net
*/
@Named
@ViewScoped
public class LeaseCreateBean {
private static final long DEFAULT_VCPUS = 4;
private static final long DEFAULT_LOCAL_GB = 50;
private static final long DEFAULT_MEMORY_MB = 8192;
private static final String DEFAULT_RESOURCE_PROPERTIES = "[\"and\"," + "[\">=\",\"$vcpus\",\"" + DEFAULT_VCPUS + "\"]," + "[\"and\","
+ "[\">=\",\"$local_gb\",\"" + DEFAULT_LOCAL_GB + "\"]," + "[\">=\",\"$memory_mb\",\"" + DEFAULT_MEMORY_MB + "\"]" + "]" + "]";
private static final long TIME_OFFSET = 5L * 60L * 1000L; // suggested
// "Start now"
// date will be
// later than the
// actual current
// time by this
// offset.
@Inject
private DataRepository dataRepository;
@Inject
private LeasesRepository leasesRepository;
@Inject
private LeasesBean leasesBean;
@Inject
private RequestParameters requestParameters;
@Inject
private MessageController messages;
private Lease lease;
private List projects;
private Map projectIdToAccountName = new HashMap<>();
private boolean editing;
private boolean startNow;
private Long timeUnitMultipler;
private Long vcpus;
private Long localGB;
private Long memoryMB;
/**
* Inits creating a new lease.
*
* @param predefinedProject
*/
public void initNew() {
lease = new Lease();
lease.setDuration(1L * TimeUnit.DAYS.getMultiplier());
lease.setEndDate(new Date(System.currentTimeMillis() + TIME_OFFSET + lease.getDuration()));
lease.getReservations().add(
ReservationBuilder.newInstance().minSize(1).maxSize(10).resourceProperties(DEFAULT_RESOURCE_PROPERTIES).build());
timeUnitMultipler = TimeUnit.DAYS.getMultiplier();
editing = false;
startNow = true;
if (leasesBean.getPredefinedProject() != null) {
lease.setProjectId(leasesBean.getPredefinedProject().getId());
} else if (projects == null) { // the user will have to select a project
loadProjectsAndAccounts();
}
vcpus = DEFAULT_VCPUS;
localGB = DEFAULT_LOCAL_GB;
memoryMB = DEFAULT_MEMORY_MB;
}
public void initEdit(Lease lease) {
Lease leaseToEdit = new Lease();
leaseToEdit.setId(lease.getId());
leaseToEdit.setStatus(lease.getStatus());
leaseToEdit.setName(lease.getName());
leaseToEdit.setStartDate(lease.getStartDate());
leaseToEdit.setEndDate(lease.getEndDate());
leaseToEdit.setReservations(lease.getReservations());
leaseToEdit.setProjectId(lease.getProjectId());
this.lease = leaseToEdit; // always null duration
timeUnitMultipler = TimeUnit.DAYS.getMultiplier();
editing = true;
startNow = false;
if (leasesBean.getPredefinedProject() == null && projects == null) {
loadProjectsAndAccounts();
}
}
private void loadProjectsAndAccounts() {
projects = dataRepository.listProjects();
HashMultimap accountIdToProjectId = HashMultimap.create();
for (Project project : projects) {
accountIdToProjectId.put(project.getAccountId(), project.getId());
}
for (Long accountId : accountIdToProjectId.keySet()) {
Account account = dataRepository.getAccount(accountId);
for (Long projectId : accountIdToProjectId.get(accountId)) {
projectIdToAccountName.put(projectId, account.getName());
}
}
}
/**
* Creates the lease.
*/
public void createOrUpdate() {
Lease leaseToSend = new Lease();
leaseToSend.setName(lease.getName());
leaseToSend.setStartDate(startNow ? null : lease.getStartDate());
leaseToSend.setDuration(lease.getDuration());
leaseToSend.setEndDate(lease.getDuration() != null ? null : lease.getEndDate());
leaseToSend.setReservations(lease.getReservations());
if (editing) {
try {
Lease updatedLease = leasesRepository.update(lease.getProjectId(), lease.getId(), leaseToSend);
leasesBean.updateLease(updatedLease);
requestParameters.addCallbackParameter(ConsolePathParams.VALID_CALLBACK_RESPONSE, true);
} catch (BaseException e) {
messages.addLocalizedError(e);
}
} else {
try {
Lease createdLease = leasesRepository.create(lease.getProjectId(), leaseToSend);
leasesBean.addLease(createdLease);
requestParameters.addCallbackParameter(ConsolePathParams.VALID_CALLBACK_RESPONSE, true);
} catch (BaseException e) {
messages.addLocalizedError(e);
}
}
}
/**
* Returns account name of the account the specified project is in.
*
* @param projectId
* @return
*/
public String accountNameForProject(Long projectId) {
return projectIdToAccountName.get(projectId);
}
/**
* Gets the value of {@link #lease}.
*
* @return value of {@link #lease}
*/
public Lease getLease() {
return lease;
}
/**
* Sets the value of {@link #lease}.
*
* @param lease
* - value
*/
public void setLease(Lease lease) {
this.lease = lease;
}
/**
* Gets the value of {@link #projects}.
*
* @return value of {@link #projects}
*/
public List getProjects() {
return projects;
}
/**
* Gets the value of {@link #editing}.
*
* @return value of {@link #editing}
*/
public boolean isEditing() {
return editing;
}
public String getProjectName(Lease lease) {
for (Project project : projects) {
if (project.getId().equals(lease.getProjectId())) {
return project.getName();
}
}
return null;
}
/**
* Gets the value of {@link #startNow}.
*
* @return value of {@link #startNow}
*/
public boolean isStartNow() {
return startNow;
}
/**
* Sets the value of {@link #startNow}.
*
* @param startNow
* - value
*/
public void setStartNow(boolean startNow) {
this.startNow = startNow;
}
/**
* Returns a string representing the current time.
*
* @return
*/
public String getMinDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return format.format(new Date(System.currentTimeMillis() + TIME_OFFSET));
}
/**
* Returns a string representing the current time + TIME_OFFSET.
*
* @return
*/
public String getStartNowTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return format.format(new Date(System.currentTimeMillis() + TIME_OFFSET));
}
/**
* Returns milliseconds representing the current time + TIME_OFFSET + time
* zone and DST offset.
*
* @return
*/
public long getStartNowTimeMillisWithTimeZone() {
TimeZone timeZone = TimeZone.getDefault();
Date date = new Date();
return date.getTime() + timeZone.getRawOffset() + (timeZone.inDaylightTime(date) ? timeZone.getDSTSavings() : 0) + TIME_OFFSET;
}
public TimeUnit[] getTimeUnits() {
return TimeUnit.values();
}
public HypervisorType[] getHypervisorTypes() {
return HypervisorType.values();
}
/**
* Gets the value of {@link #timeUnitMultipler}.
*
* @return value of {@link #timeUnitMultipler}
*/
public Long getTimeUnitMultipler() {
return timeUnitMultipler;
}
/**
* Sets the value of {@link #timeUnitMultipler}.
*
* @param timeUnitMultipler
* - value
*/
public void setTimeUnitMultipler(Long timeUnitMultipler) {
this.timeUnitMultipler = timeUnitMultipler;
}
/**
* Gets the value of {@link #vcpus}.
*
* @return value of {@link #vcpus}
*/
public Long getVcpus() {
return vcpus;
}
/**
* Sets the value of {@link #vcpus}.
*
* @param vcpus
* - value
*/
public void setVcpus(Long vcpus) {
this.vcpus = vcpus;
}
/**
* Gets the value of {@link #localGB}.
*
* @return value of {@link #localGB}
*/
public Long getLocalGB() {
return localGB;
}
/**
* Sets the value of {@link #localGB}.
*
* @param localGB
* - value
*/
public void setLocalGB(Long localGB) {
this.localGB = localGB;
}
/**
* Gets the value of {@link #memoryMB}.
*
* @return value of {@link #memoryMB}
*/
public Long getMemoryMB() {
return memoryMB;
}
/**
* Sets the value of {@link #memoryMB}.
*
* @param memoryMB
* - value
*/
public void setMemoryMB(Long memoryMB) {
this.memoryMB = memoryMB;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy