Github user miguelaferreira commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/1094#discussion_r46392407 --- Diff: plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureSharedNetworkUuidCommandWrapper.java --- @@ -0,0 +1,110 @@ +// +// 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.NAME_MAX_LEN; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.ConfigureSharedNetworkUuidAnswer; +import com.cloud.agent.api.ConfigureSharedNetworkUuidCommand; +import com.cloud.network.nicira.LogicalRouterPort; +import com.cloud.network.nicira.LogicalSwitchPort; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.NiciraNvpTag; +import com.cloud.network.nicira.PatchAttachment; +import com.cloud.network.resource.NiciraNvpResource; +import com.cloud.resource.CommandWrapper; +import com.cloud.resource.ResourceWrapper; + +@ResourceWrapper(handles = ConfigureSharedNetworkUuidCommand.class) +public final class NiciraNvpConfigureSharedNetworkUuidCommandWrapper extends CommandWrapper<ConfigureSharedNetworkUuidCommand, Answer, NiciraNvpResource>{ + + private static final Logger s_logger = Logger.getLogger(NiciraNvpConfigureSharedNetworkUuidCommandWrapper.class); + + @Override + public Answer execute(ConfigureSharedNetworkUuidCommand command, NiciraNvpResource niciraNvpResource) { + final String logicalRouterUuid = command.getLogicalRouterUuid(); + final String logicalSwitchUuid = command.getLogicalSwitchUuid(); + final String portIpAddress = command.getPortIpAddress(); + final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>(); + tags.add(new NiciraNvpTag("cs_account", command.getOwnerName())); + final long networkId = command.getNetworkId(); + + final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi(); + + s_logger.debug("Attaching Logical Switch " + logicalSwitchUuid + " on Logical Router " + logicalRouterUuid + " for Shared Network" + networkId); + + //Stored for rollback + LogicalRouterPort lrpi = null; + LogicalSwitchPort lsp = null; + try { + // Create the inside port for the router + lrpi = new LogicalRouterPort(); + lrpi.setAdminStatusEnabled(true); + lrpi.setDisplayName(niciraNvpResource.truncate(networkId + "-shared-att-port", NAME_MAX_LEN)); + lrpi.setTags(tags); + final List<String> ipAddresses = new ArrayList<String>(); + ipAddresses.add(portIpAddress); + lrpi.setIpAddresses(ipAddresses); + lrpi = niciraNvpApi.createLogicalRouterPort(logicalRouterUuid, lrpi); + + try { + // Create the inside port on the lswitch + lsp = new LogicalSwitchPort(niciraNvpResource.truncate(networkId + "-shared-att-port", NAME_MAX_LEN), tags, true); + lsp = niciraNvpApi.createLogicalSwitchPort(logicalSwitchUuid, lsp); + + // Attach the inside router port to the lswitch port with a PatchAttachment + niciraNvpApi.updateLogicalRouterPortAttachment(logicalRouterUuid, lrpi.getUuid(), new PatchAttachment(lsp.getUuid())); + + // Attach the inside lswitch port to the router with a PatchAttachment + niciraNvpApi.updateLogicalSwitchPortAttachment(logicalSwitchUuid, lsp.getUuid(), new PatchAttachment(lrpi.getUuid())); + } + catch (final NiciraNvpApiException e){ + try { + niciraNvpApi.deleteLogicalRouterPort(logicalRouterUuid, lrpi.getUuid()); + if (lsp != null){ + niciraNvpApi.deleteLogicalSwitchPort(logicalSwitchUuid, lsp.getUuid()); + } + } catch (NiciraNvpApiException ex) { --- End diff -- This is indeed better. However, now you are logging it as an info message, and then making a new exception. If you log something, and then throw an exception, I would say the log level should be warn because you are delegating the handling of the exception to the callers. In addition, when you make a new exception out of an existing exception you should include the original exception as the cause of the new exception. In this case you are using two exception as the cause `e and ex`. Let us think of a way of doing this better: * The first exception `ex` is being handled here. So I would say, first thing is to log it as an error (`s_logger.error("failed to do something", ex)`). This way we explain what failed and present the cause in the logs. * Then we handle that exception (`ex`) and we might get a second one (`e`). * This second one we don't want to handle here, and we will bubble it up wrapped in a new exception. So then we log `e` as a warning (`s_logger.warn("failed to do something else, namely handing the previous excpetion", e)`), and then we wrap it in a new exception and throw it (`throw new NiciraNvpApiException("probably the same message we set in the warn log", e`). What do you think? Does this make sense to you?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---