Changes to make affinity group types configurable.

This commit is contained in:
Prachi Damle 2013-03-25 14:53:19 -07:00
parent 708157d33f
commit bb9bdf0173
4 changed files with 185 additions and 3 deletions

View File

@ -0,0 +1,48 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.apache.cloudstack.affinity;
import java.util.HashSet;
import java.util.Set;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;
import org.apache.cloudstack.api.EntityReference;
import org.apache.cloudstack.api.response.ControlledEntityResponse;
import org.apache.cloudstack.api.response.ControlledViewEntityResponse;
import com.cloud.network.security.SecurityGroup;
import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;
@SuppressWarnings("unused")
@EntityReference(value = AffinityGroup.class)
public class AffinityGroupTypeResponse extends BaseResponse {
@SerializedName(ApiConstants.TYPE)
@Param(description = "the type of the affinity group")
private String type;
public AffinityGroupTypeResponse() {
}
public void setType(String type) {
this.type = type;
}
}

View File

@ -0,0 +1,67 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.apache.cloudstack.api.command.user.affinitygroup;
import java.util.ArrayList;
import java.util.List;
import org.apache.cloudstack.affinity.AffinityGroupTypeResponse;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.BaseListCmd;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.log4j.Logger;
import com.cloud.user.Account;
@APICommand(name = "listAffinityGroupTypes", description = "Lists affinity group types available", responseObject = AffinityGroupTypeResponse.class)
public class ListAffinityGroupTypesCmd extends BaseListCmd {
public static final Logger s_logger = Logger.getLogger(ListAffinityGroupTypesCmd.class.getName());
private static final String s_name = "listaffinitygrouptypesresponse";
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public String getCommandName() {
return s_name;
}
public long getEntityOwnerId() {
return Account.ACCOUNT_ID_SYSTEM;
}
@Override
public void execute() {
List<String> result = _affinityGroupService.listAffinityGroupTypes();
ListResponse<AffinityGroupTypeResponse> response = new ListResponse<AffinityGroupTypeResponse>();
ArrayList<AffinityGroupTypeResponse> responses = new ArrayList<AffinityGroupTypeResponse>();
if (result != null) {
for (String type : result) {
AffinityGroupTypeResponse affinityTypeResponse = new AffinityGroupTypeResponse();
affinityTypeResponse.setType(type);
affinityTypeResponse.setObjectName("affinityGroupType");
responses.add(affinityTypeResponse);
}
}
response.setResponses(responses);
response.setResponseName(getCommandName());
this.setResponseObject(response);
}
}

View File

@ -247,4 +247,13 @@
</list> </list>
</property> </property>
</bean> </bean>
<!--
AffinityGroup Processors
-->
<bean id="HostAntiAffinityProcessor" class="org.apache.cloudstack.affinity.HostAntiAffinityProcessor">
<property name="name" value="HostAntiAffinityProcessor"/>
<property name="type" value="host anti-affinity"/>
</bean>
</beans> </beans>

View File

@ -1,7 +1,10 @@
package org.apache.cloudstack.affinity; package org.apache.cloudstack.affinity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import javax.ejb.Local; import javax.ejb.Local;
import javax.inject.Inject; import javax.inject.Inject;
@ -10,6 +13,7 @@ import javax.naming.ConfigurationException;
import org.apache.cloudstack.affinity.dao.AffinityGroupDao; import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao; import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.context.annotation.Primary;
import com.cloud.event.ActionEvent; import com.cloud.event.ActionEvent;
@ -22,6 +26,7 @@ import com.cloud.user.AccountManager;
import com.cloud.user.UserContext; import com.cloud.user.UserContext;
import com.cloud.uservm.UserVm; import com.cloud.uservm.UserVm;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
import com.cloud.utils.component.ComponentContext;
import com.cloud.utils.component.Manager; import com.cloud.utils.component.Manager;
import com.cloud.utils.component.ManagerBase; import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.db.DB; import com.cloud.utils.db.DB;
@ -30,6 +35,7 @@ import com.cloud.utils.db.JoinBuilder;
import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction; import com.cloud.utils.db.Transaction;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.fsm.StateListener; import com.cloud.utils.fsm.StateListener;
import com.cloud.vm.UserVmVO; import com.cloud.vm.UserVmVO;
import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachine;
@ -69,7 +75,30 @@ public class AffinityGroupServiceImpl extends ManagerBase implements AffinityGro
+ " already exisits."); + " already exisits.");
} }
AffinityGroupVO group = new AffinityGroupVO(affinityGroupName, affinityGroupType, description, domainId,
//validate the affinityGroupType
String internalAffinityType = null;
Map<String, AffinityGroupProcessor> typeProcessorMap = getAffinityTypeToProcessorMap();
if (typeProcessorMap != null && !typeProcessorMap.isEmpty()) {
if (!typeProcessorMap.containsKey(affinityGroupType)) {
throw new InvalidParameterValueException("Unable to create affinity group, invalid affinity group type"
+ affinityGroupType);
} else {
AffinityGroupProcessor processor = typeProcessorMap.get(affinityGroupType);
internalAffinityType = processor.getType();
}
} else {
throw new InvalidParameterValueException(
"Unable to create affinity group, no Affinity Group Types configured");
}
if (internalAffinityType == null) {
throw new InvalidParameterValueException(
"Unable to create affinity group, Affinity Group Processor for type " + affinityGroupType
+ "is wrongly configured");
}
AffinityGroupVO group = new AffinityGroupVO(affinityGroupName, internalAffinityType, description, domainId,
owner.getId()); owner.getId());
_affinityGroupDao.persist(group); _affinityGroupDao.persist(group);
@ -188,8 +217,37 @@ public class AffinityGroupServiceImpl extends ManagerBase implements AffinityGro
@Override @Override
public List<String> listAffinityGroupTypes() { public List<String> listAffinityGroupTypes() {
// TODO Auto-generated method stub List<String> types = new ArrayList<String>();
return null; Map<String, AffinityGroupProcessor> componentMap = ComponentContext.getComponentsOfType(AffinityGroupProcessor.class);
if (componentMap.size() > 0) {
for (Entry<String, AffinityGroupProcessor> entry : componentMap.entrySet()) {
Map<String, Object> params = entry.getValue().getConfigParams();
if (params.containsKey("type")) {
types.add((String) params.get("type"));
}
}
}
return types;
}
protected Map<String, AffinityGroupProcessor> getAffinityTypeToProcessorMap() {
Map<String, AffinityGroupProcessor> typeProcessorMap = new HashMap<String, AffinityGroupProcessor>();
Map<String, AffinityGroupProcessor> componentMap = ComponentContext.getComponentsOfType(AffinityGroupProcessor.class);
if (componentMap.size() > 0) {
for (Entry<String, AffinityGroupProcessor> entry : componentMap.entrySet()) {
Map<String, Object> params = entry.getValue().getConfigParams();
if (params.containsKey("type")) {
typeProcessorMap.put((String) params.get("type"), entry.getValue());
}
}
}
return typeProcessorMap;
} }
@Override @Override