From bb9bdf0173f7a225402a6640a0d8752155c9ee3c Mon Sep 17 00:00:00 2001 From: Prachi Damle Date: Mon, 25 Mar 2013 14:53:19 -0700 Subject: [PATCH] Changes to make affinity group types configurable. --- .../affinity/AffinityGroupTypeResponse.java | 48 +++++++++++++ .../ListAffinityGroupTypesCmd.java | 67 +++++++++++++++++++ client/tomcatconf/componentContext.xml.in | 9 +++ .../affinity/AffinityGroupServiceImpl.java | 64 +++++++++++++++++- 4 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 api/src/org/apache/cloudstack/affinity/AffinityGroupTypeResponse.java create mode 100644 api/src/org/apache/cloudstack/api/command/user/affinitygroup/ListAffinityGroupTypesCmd.java diff --git a/api/src/org/apache/cloudstack/affinity/AffinityGroupTypeResponse.java b/api/src/org/apache/cloudstack/affinity/AffinityGroupTypeResponse.java new file mode 100644 index 00000000000..2d1cd25edda --- /dev/null +++ b/api/src/org/apache/cloudstack/affinity/AffinityGroupTypeResponse.java @@ -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; + } + +} diff --git a/api/src/org/apache/cloudstack/api/command/user/affinitygroup/ListAffinityGroupTypesCmd.java b/api/src/org/apache/cloudstack/api/command/user/affinitygroup/ListAffinityGroupTypesCmd.java new file mode 100644 index 00000000000..ce6e89a078c --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/user/affinitygroup/ListAffinityGroupTypesCmd.java @@ -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 result = _affinityGroupService.listAffinityGroupTypes(); + ListResponse response = new ListResponse(); + ArrayList responses = new ArrayList(); + 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); + } +} diff --git a/client/tomcatconf/componentContext.xml.in b/client/tomcatconf/componentContext.xml.in index 1582e8bbc56..7f3e02d879d 100644 --- a/client/tomcatconf/componentContext.xml.in +++ b/client/tomcatconf/componentContext.xml.in @@ -247,4 +247,13 @@ + + + + + + + diff --git a/server/src/org/apache/cloudstack/affinity/AffinityGroupServiceImpl.java b/server/src/org/apache/cloudstack/affinity/AffinityGroupServiceImpl.java index 21052d0b3ab..853602231d1 100644 --- a/server/src/org/apache/cloudstack/affinity/AffinityGroupServiceImpl.java +++ b/server/src/org/apache/cloudstack/affinity/AffinityGroupServiceImpl.java @@ -1,7 +1,10 @@ package org.apache.cloudstack.affinity; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import javax.ejb.Local; 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.AffinityGroupVMMapDao; import org.apache.log4j.Logger; +import org.springframework.context.annotation.Primary; import com.cloud.event.ActionEvent; @@ -22,6 +26,7 @@ import com.cloud.user.AccountManager; import com.cloud.user.UserContext; import com.cloud.uservm.UserVm; import com.cloud.utils.Pair; +import com.cloud.utils.component.ComponentContext; import com.cloud.utils.component.Manager; import com.cloud.utils.component.ManagerBase; 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.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.fsm.StateListener; import com.cloud.vm.UserVmVO; import com.cloud.vm.VirtualMachine; @@ -69,7 +75,30 @@ public class AffinityGroupServiceImpl extends ManagerBase implements AffinityGro + " already exisits."); } - AffinityGroupVO group = new AffinityGroupVO(affinityGroupName, affinityGroupType, description, domainId, + + //validate the affinityGroupType + String internalAffinityType = null; + Map 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()); _affinityGroupDao.persist(group); @@ -188,8 +217,37 @@ public class AffinityGroupServiceImpl extends ManagerBase implements AffinityGro @Override public List listAffinityGroupTypes() { - // TODO Auto-generated method stub - return null; + List types = new ArrayList(); + Map componentMap = ComponentContext.getComponentsOfType(AffinityGroupProcessor.class); + + if (componentMap.size() > 0) { + for (Entry entry : componentMap.entrySet()) { + Map params = entry.getValue().getConfigParams(); + if (params.containsKey("type")) { + types.add((String) params.get("type")); + } + + } + + } + return types; + } + + protected Map getAffinityTypeToProcessorMap() { + Map typeProcessorMap = new HashMap(); + Map componentMap = ComponentContext.getComponentsOfType(AffinityGroupProcessor.class); + + if (componentMap.size() > 0) { + for (Entry entry : componentMap.entrySet()) { + Map params = entry.getValue().getConfigParams(); + if (params.containsKey("type")) { + typeProcessorMap.put((String) params.get("type"), entry.getValue()); + } + + } + + } + return typeProcessorMap; } @Override