From 59cc382f20ad01e47524ca865ffe4b0847a76dfd Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Thu, 2 May 2013 17:38:27 +0200 Subject: [PATCH] Prevent Nicira NVP tags from exceeding the 40 character limit. --- .../cloud/network/nicira/NiciraNvpTag.java | 17 +++++- .../cloud/network/nicira/NiciraTagTest.java | 54 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 plugins/network-elements/nicira-nvp/test/com/cloud/network/nicira/NiciraTagTest.java diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpTag.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpTag.java index f8282b86d9c..157c3b522c7 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpTag.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpTag.java @@ -16,7 +16,10 @@ // under the License. package com.cloud.network.nicira; +import org.apache.log4j.Logger; + public class NiciraNvpTag { + private static final Logger s_logger = Logger.getLogger(NiciraNvpTag.class); private String scope; private String tag; @@ -24,7 +27,12 @@ public class NiciraNvpTag { public NiciraNvpTag(String scope, String tag) { this.scope = scope; - this.tag = tag; + if (tag.length() > 40) { + s_logger.warn("tag \"" + tag + "\" too long, truncating to 40 characters"); + this.tag = tag.substring(0, 40); + } else { + this.tag = tag; + } } public String getScope() { @@ -40,7 +48,12 @@ public class NiciraNvpTag { } public void setTag(String tag) { - this.tag = tag; + if (tag.length() > 40) { + s_logger.warn("tag \"" + tag + "\" too long, truncating to 40 characters"); + this.tag = tag.substring(0, 40); + } else { + this.tag = tag; + } } } diff --git a/plugins/network-elements/nicira-nvp/test/com/cloud/network/nicira/NiciraTagTest.java b/plugins/network-elements/nicira-nvp/test/com/cloud/network/nicira/NiciraTagTest.java new file mode 100644 index 00000000000..fd13e07ab59 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/test/com/cloud/network/nicira/NiciraTagTest.java @@ -0,0 +1,54 @@ +// 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 org.junit.Test; +import static org.junit.Assert.*; + +public class NiciraTagTest { + @Test + public void testCreateTag() { + NiciraNvpTag tag = new NiciraNvpTag("scope","tag"); + assertEquals("scope part set", "scope", tag.getScope()); + assertEquals("tag part set", "tag", tag.getTag()); + } + + @Test + public void testCreateLongTag() { + NiciraNvpTag tag = new NiciraNvpTag("scope","verylongtagthatshouldattheminimumexceedthefortycharacterlenght"); + assertEquals("scope part set", "scope", tag.getScope()); + assertEquals("tag part set", "verylongtagthatshouldattheminimumexceedt", tag.getTag()); + } + + @Test + public void testSetTag() { + NiciraNvpTag tag = new NiciraNvpTag(); + tag.setScope("scope"); + tag.setTag("tag"); + assertEquals("scope part set", "scope", tag.getScope()); + assertEquals("tag part set", "tag", tag.getTag()); + } + + @Test + public void testSetLongTag() { + NiciraNvpTag tag = new NiciraNvpTag(); + tag.setScope("scope"); + tag.setTag("verylongtagthatshouldattheminimumexceedthefortycharacterlenght"); + assertEquals("scope part set", "scope", tag.getScope()); + assertEquals("tag part set", "verylongtagthatshouldattheminimumexceedt", tag.getTag()); + } +} \ No newline at end of file