CLOUDSTACK-9074: API add Gateway Service Find method

This commit is contained in:
nvazquez 2015-12-09 10:55:30 -08:00 committed by nvazquez
parent c67637180f
commit eb889c0c49
13 changed files with 346 additions and 18 deletions

View File

@ -0,0 +1,38 @@
//
// 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 com.cloud.agent.api;
public class FindL2GatewayServiceAnswer extends Answer {
private String gatewayServiceUuid;
public FindL2GatewayServiceAnswer(final Command command, final boolean success, final String details, final String gatewayServiceUuid) {
super(command, success, details);
this.gatewayServiceUuid = gatewayServiceUuid;
}
public FindL2GatewayServiceAnswer(final Command command, final Exception e) {
super(command, e);
}
public String getGatewayServiceUuid() {
return gatewayServiceUuid;
}
}

View File

@ -0,0 +1,46 @@
//
// 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 com.cloud.agent.api;
import com.cloud.network.nicira.GatewayServiceConfig;
import com.cloud.network.nicira.L2GatewayServiceConfig;
public class FindL2GatewayServiceCommand extends Command {
private L2GatewayServiceConfig gatewayServiceConfig;
public FindL2GatewayServiceCommand(L2GatewayServiceConfig config) {
this.gatewayServiceConfig = config;
}
@Override
public boolean executeInSequence() {
return false;
}
public GatewayServiceConfig getGatewayServiceConfig() {
return gatewayServiceConfig;
}
public void setGatewayServiceConfig(L2GatewayServiceConfig gatewayServiceConfig) {
this.gatewayServiceConfig = gatewayServiceConfig;
}
}

View File

@ -36,6 +36,8 @@ import com.cloud.agent.api.DeleteLogicalRouterPortAnswer;
import com.cloud.agent.api.DeleteLogicalRouterPortCommand;
import com.cloud.agent.api.DeleteLogicalSwitchAnswer;
import com.cloud.agent.api.DeleteLogicalSwitchCommand;
import com.cloud.agent.api.FindL2GatewayServiceAnswer;
import com.cloud.agent.api.FindL2GatewayServiceCommand;
import com.cloud.agent.api.FindLogicalRouterPortAnswer;
import com.cloud.agent.api.FindLogicalRouterPortCommand;
import com.cloud.dc.DataCenter;
@ -54,10 +56,10 @@ import com.cloud.network.Network.Service;
import com.cloud.network.Network.State;
import com.cloud.network.NetworkModel;
import com.cloud.network.NetworkProfile;
import com.cloud.network.NiciraNvpNicMappingVO;
import com.cloud.network.NiciraNvpRouterMappingVO;
import com.cloud.network.Networks.BroadcastDomainType;
import com.cloud.network.NiciraNvpDeviceVO;
import com.cloud.network.NiciraNvpNicMappingVO;
import com.cloud.network.NiciraNvpRouterMappingVO;
import com.cloud.network.PhysicalNetwork;
import com.cloud.network.PhysicalNetwork.IsolationMethod;
import com.cloud.network.dao.NetworkDao;
@ -67,6 +69,7 @@ import com.cloud.network.dao.NiciraNvpNicMappingDao;
import com.cloud.network.dao.NiciraNvpRouterMappingDao;
import com.cloud.network.dao.PhysicalNetworkDao;
import com.cloud.network.dao.PhysicalNetworkVO;
import com.cloud.network.nicira.L2GatewayServiceConfig;
import com.cloud.offering.NetworkOffering;
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
import com.cloud.resource.ResourceManager;
@ -209,6 +212,16 @@ public class NiciraNvpGuestNetworkGuru extends GuestNetworkGuru implements Netwo
final String transportzoneuuid = niciraNvpHost.getDetail("transportzoneuuid");
final String transportzoneisotype = niciraNvpHost.getDetail("transportzoneisotype");
if (offering.getGuestType().equals(GuestType.Shared)) {
try {
checkL2GatewayServiceSharedNetwork(niciraNvpHost);
}
catch (Exception e){
s_logger.error("L2 Gateway Service Issue: " + e.getMessage());
return null;
}
}
final CreateLogicalSwitchCommand cmd = new CreateLogicalSwitchCommand(transportzoneuuid, transportzoneisotype, name, context.getDomain().getName() + "-"
+ context.getAccount().getAccountName());
final CreateLogicalSwitchAnswer answer = (CreateLogicalSwitchAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd);
@ -230,6 +243,26 @@ public class NiciraNvpGuestNetworkGuru extends GuestNetworkGuru implements Netwo
return implemented;
}
private void checkL2GatewayServiceSharedNetwork(HostVO niciraNvpHost) throws Exception {
String l2GatewayServiceUuid = niciraNvpHost.getDetail("l2gatewayserviceuuid");
if (l2GatewayServiceUuid == null){
throw new Exception("No L2 Gateway Service found");
}
else {
final FindL2GatewayServiceCommand cmdL2GWService = new FindL2GatewayServiceCommand(new L2GatewayServiceConfig(l2GatewayServiceUuid));
final FindL2GatewayServiceAnswer answerL2GWService = (FindL2GatewayServiceAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmdL2GWService);
if (answerL2GWService == null || !answerL2GWService.getResult()){
throw new Exception("No L2 Gateway Service found with uuid " + l2GatewayServiceUuid);
}
else {
String uuidFound = answerL2GWService.getGatewayServiceUuid();
if (! uuidFound.equals(l2GatewayServiceUuid)){
throw new Exception("Found L2 Gateway Service " + uuidFound + " instead of " + l2GatewayServiceUuid);
}
}
}
}
@Override
public void reserve(final NicProfile nic, final Network network, final VirtualMachineProfile vm, final DeployDestination dest, final ReservationContext context)
throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {

View File

@ -0,0 +1,47 @@
//
// 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 com.cloud.network.nicira;
import java.io.Serializable;
public abstract class GatewayServiceConfig implements Serializable {
private String uuid;
private String displayName;
private String type;
public GatewayServiceConfig(String uuid, String type) {
this.uuid = uuid;
this.type = type;
}
public String getUuid() {
return uuid;
}
public String getDisplayName() {
return displayName;
}
public String getType() {
return type;
}
}

View File

@ -0,0 +1,30 @@
//
// 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 com.cloud.network.nicira;
public class L2GatewayServiceConfig extends GatewayServiceConfig {
private static final String L2GWTServiceConfigType = "L2GatewayServiceConfig";
public L2GatewayServiceConfig(String uuid) {
super(uuid, L2GWTServiceConfigType);
}
}

View File

@ -25,6 +25,7 @@ public class NiciraConstants {
public static final String ACL_URI_PREFIX = "/ws.v1/acl";
public static final String SWITCH_URI_PREFIX = "/ws.v1/lswitch";
public static final String ROUTER_URI_PREFIX = "/ws.v1/lrouter";
public static final String GATEWAY_SERVICE_PREFIX = "/ws.v1/gateway-service";
public static final String LOGIN_URL = "/ws.v1/login";
public static final String CONTROL_CLUSTER_STATUS_URL = "/ws.v1/control-cluster/status";
@ -39,5 +40,6 @@ public class NiciraConstants {
public static final String WILDCARD_QUERY_PARAMETER = "*";
public static final String UUID_QUERY_PARAMETER = "uuid";
public static final String FIELDS_QUERY_PARAMETER = "fields";
public static final String TYPES_QUERY_PARAMETER = "types";
}

View File

@ -42,6 +42,7 @@ public class NiciraNvpApi {
private static final String SWITCH_URI_PREFIX = NiciraConstants.SWITCH_URI_PREFIX;
private static final String ROUTER_URI_PREFIX = NiciraConstants.ROUTER_URI_PREFIX;
private static final String GATEWAY_SERVICE_PREFIX = NiciraConstants.GATEWAY_SERVICE_PREFIX;
private static final String ATTACHMENT_PATH_SEGMENT = NiciraConstants.ATTACHMENT_PATH_SEGMENT;
private static final String NAT_PATH_SEGMENT = NiciraConstants.NAT_PATH_SEGMENT;
@ -51,6 +52,7 @@ public class NiciraNvpApi {
private static final String WILDCARD_QUERY_PARAMETER = NiciraConstants.WILDCARD_QUERY_PARAMETER;
private static final String UUID_QUERY_PARAMETER = NiciraConstants.UUID_QUERY_PARAMETER;
private static final String FIELDS_QUERY_PARAMETER = NiciraConstants.FIELDS_QUERY_PARAMETER;
private static final String TYPES_QUERY_PARAMETER = NiciraConstants.TYPES_QUERY_PARAMETER;
private static final int DEFAULT_MAX_RETRIES = 5;
@ -131,7 +133,7 @@ public class NiciraNvpApi {
try {
createdEntity = restConnector.executeCreateObject(entity, uri, Collections.<String, String> emptyMap());
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
return createdEntity;
@ -157,7 +159,7 @@ public class NiciraNvpApi {
try {
entities = restConnector.executeRetrieveObject(listTypeMap.get(clazz), uri, params);
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
if (entities == null) {
@ -190,7 +192,7 @@ public class NiciraNvpApi {
try {
restConnector.executeUpdateObject(item, uri, Collections.<String, String> emptyMap());
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
@ -215,7 +217,7 @@ public class NiciraNvpApi {
try {
restConnector.executeDeleteObject(uri);
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
@ -394,7 +396,7 @@ public class NiciraNvpApi {
}.getType();
niciraList = restConnector.executeRetrieveObject(niciraListType, uri, params);
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
final List<LogicalSwitchPort> lspl = niciraList.getResults();
@ -413,7 +415,7 @@ public class NiciraNvpApi {
try {
return restConnector.executeRetrieveObject(ControlClusterStatus.class, uri, new HashMap<String, String>());
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
@ -427,7 +429,7 @@ public class NiciraNvpApi {
}.getType();
return restConnector.<NiciraNvpList<LogicalSwitchPort>> executeRetrieveObject(niciraListType, uri, params).getResults();
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
@ -441,7 +443,7 @@ public class NiciraNvpApi {
}.getType();
return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults();
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
@ -522,7 +524,7 @@ public class NiciraNvpApi {
}.getType();
return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults();
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
@ -536,7 +538,7 @@ public class NiciraNvpApi {
}.getType();
return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults();
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
@ -549,7 +551,7 @@ public class NiciraNvpApi {
}.getType();
return restConnector.<NiciraNvpList<NatRule>> executeRetrieveObject(niciraListType, uri, params).getResults();
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
@ -564,10 +566,30 @@ public class NiciraNvpApi {
}.getType();
return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults();
} catch (final CloudstackRESTException e) {
throw new NiciraNvpApiException(e);
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
public List<L2GatewayServiceConfig> findL2GatewayServiceByUuidAndType(final String gatewayServiceUuid, final String serviceConfigType) throws NiciraNvpApiException{
final String uri = buildGatewayServiceUri();
final Map<String, String> params = buildBasicParametersMap(WILDCARD_QUERY_PARAMETER);
params.put(UUID_QUERY_PARAMETER, gatewayServiceUuid);
params.put(TYPES_QUERY_PARAMETER, serviceConfigType);
try {
final Type niciraListType = new TypeToken<NiciraNvpList<L2GatewayServiceConfig>>() {
}.getType();
return restConnector.<NiciraNvpList<L2GatewayServiceConfig>> executeRetrieveObject(niciraListType, uri, params).getResults();
}
catch (final CloudstackRESTException e){
throw new NiciraNvpApiException(e, e.getErrorCode());
}
}
private static String buildGatewayServiceUri(){
return GATEWAY_SERVICE_PREFIX;
}
private static Map<String, String> buildBasicParametersMap(final String fieldsQueryValue) {
final Map<String, String> params = new HashMap<String, String>();
params.put(FIELDS_QUERY_PARAMETER, fieldsQueryValue);

View File

@ -21,6 +21,8 @@ package com.cloud.network.nicira;
public class NiciraNvpApiException extends Exception {
private int errorCode;
public NiciraNvpApiException() {
}
@ -36,4 +38,27 @@ public class NiciraNvpApiException extends Exception {
super(message, cause);
}
public NiciraNvpApiException(final String message, final int errorCode){
super(message);
this.errorCode = errorCode;
}
public NiciraNvpApiException(final Throwable cause, final int errorCode) {
super(cause);
this.errorCode = errorCode;
}
public NiciraNvpApiException(final String message, final Throwable cause, final int errorCode) {
super(message, cause);
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
}

View File

@ -92,9 +92,9 @@ public class NiciraRestClient extends BasicRestClient {
} else if (HttpStatusCodeHelper.isSuccess(statusCode)) {
return handleSuccessResponse(request, response);
} else if (HttpStatusCodeHelper.isConflict(statusCode)) {
throw new CloudstackRESTException("Conflict: " + statusLine.getReasonPhrase());
throw new CloudstackRESTException("Conflict: " + statusLine.getReasonPhrase(), statusCode);
} else {
throw new CloudstackRESTException("Unexpected status code: " + statusCode);
throw new CloudstackRESTException("Unexpected status code: " + statusCode, statusCode);
}
}

View File

@ -0,0 +1,69 @@
//
// 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 com.cloud.network.resource.wrapper;
import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES;
import java.util.List;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.FindL2GatewayServiceAnswer;
import com.cloud.agent.api.FindL2GatewayServiceCommand;
import com.cloud.network.nicira.GatewayServiceConfig;
import com.cloud.network.nicira.L2GatewayServiceConfig;
import com.cloud.network.nicira.NiciraNvpApi;
import com.cloud.network.nicira.NiciraNvpApiException;
import com.cloud.network.resource.NiciraNvpResource;
import com.cloud.network.utils.CommandRetryUtility;
import com.cloud.resource.CommandWrapper;
import com.cloud.resource.ResourceWrapper;
@ResourceWrapper(handles = FindL2GatewayServiceCommand.class)
public class NiciraNvpFindL2GatewayServiceCommandWrapper extends CommandWrapper<FindL2GatewayServiceCommand, Answer, NiciraNvpResource> {
private static final Logger s_logger = Logger.getLogger(NiciraNvpFindL2GatewayServiceCommandWrapper.class);
@Override
public Answer execute(FindL2GatewayServiceCommand command, NiciraNvpResource niciraNvpResource) {
final GatewayServiceConfig config = command.getGatewayServiceConfig();
final String uuid = config.getUuid();
final String type = config.getType();
final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
s_logger.info("Looking for L2 Gateway Service " + uuid + " of type " + type);
try {
List<L2GatewayServiceConfig> lstGW = niciraNvpApi.findL2GatewayServiceByUuidAndType(uuid, type);
if (lstGW.size() == 0) {
return new FindL2GatewayServiceAnswer(command, false, "L2 Gateway Service not found", null);
} else {
return new FindL2GatewayServiceAnswer(command, true, "L2 Gateway Service " + lstGW.get(0).getDisplayName()+ " found", lstGW.get(0).getUuid());
}
} catch (NiciraNvpApiException e) {
s_logger.error("Error finding Gateway Service due to: " + e.getMessage());
final CommandRetryUtility retryUtility = niciraNvpResource.getRetryUtility();
retryUtility.addRetry(command, NUM_RETRIES);
return retryUtility.retry(command, FindL2GatewayServiceAnswer.class, e);
}
}
}

View File

@ -473,4 +473,4 @@ public class NiciraNvpGuestNetworkGuruTest {
verify(agentmgr, times(1)).easySend(eq(NETWORK_ID), (Command)any());
verify(implementednetwork, times(1)).setBroadcastUri(null);
}
}
}

View File

@ -22,6 +22,8 @@ package com.cloud.utils.rest;
@SuppressWarnings("serial")
public class CloudstackRESTException extends Exception {
private int errorCode;
public CloudstackRESTException(final String message) {
super(message);
}
@ -30,4 +32,18 @@ public class CloudstackRESTException extends Exception {
super(message, cause);
}
public CloudstackRESTException(final String message, final int errorCode) {
super(message);
this.errorCode = errorCode;
}
public CloudstackRESTException(final String message, final Throwable cause, final int errorCode) {
super(message, cause);
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
}

View File

@ -127,7 +127,7 @@ public class RESTServiceConnector {
final HttpEntity entity = response.getEntity();
try {
final String stringEntity = EntityUtils.toString(entity);
s_logger.debug("Response entity: " + stringEntity);
//s_logger.debug("Response entity: " + stringEntity);
EntityUtils.consumeQuietly(entity);
return gson.fromJson(stringEntity, type);
} catch (final IOException e) {