Skip to content

Commit 59cc382

Browse files
committed
Prevent Nicira NVP tags from exceeding the 40 character limit.
1 parent e56d2a4 commit 59cc382

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpTag.java‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@
1616
// under the License.
1717
package com.cloud.network.nicira;
1818

19+
import org.apache.log4j.Logger;
20+
1921
public class NiciraNvpTag {
22+
private static final Logger s_logger = Logger.getLogger(NiciraNvpTag.class);
2023
private String scope;
2124
private String tag;
2225

2326
public NiciraNvpTag() {}
2427

2528
public NiciraNvpTag(String scope, String tag) {
2629
this.scope = scope;
27-
this.tag = tag;
30+
if (tag.length() > 40) {
31+
s_logger.warn("tag \"" + tag + "\" too long, truncating to 40 characters");
32+
this.tag = tag.substring(0, 40);
33+
} else {
34+
this.tag = tag;
35+
}
2836
}
2937

3038
public String getScope() {
@@ -40,7 +48,12 @@ public String getTag() {
4048
}
4149

4250
public void setTag(String tag) {
43-
this.tag = tag;
51+
if (tag.length() > 40) {
52+
s_logger.warn("tag \"" + tag + "\" too long, truncating to 40 characters");
53+
this.tag = tag.substring(0, 40);
54+
} else {
55+
this.tag = tag;
56+
}
4457
}
4558

4659
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.network.nicira;
18+
19+
import org.junit.Test;
20+
import static org.junit.Assert.*;
21+
22+
public class NiciraTagTest {
23+
@Test
24+
public void testCreateTag() {
25+
NiciraNvpTag tag = new NiciraNvpTag("scope","tag");
26+
assertEquals("scope part set", "scope", tag.getScope());
27+
assertEquals("tag part set", "tag", tag.getTag());
28+
}
29+
30+
@Test
31+
public void testCreateLongTag() {
32+
NiciraNvpTag tag = new NiciraNvpTag("scope","verylongtagthatshouldattheminimumexceedthefortycharacterlenght");
33+
assertEquals("scope part set", "scope", tag.getScope());
34+
assertEquals("tag part set", "verylongtagthatshouldattheminimumexceedt", tag.getTag());
35+
}
36+
37+
@Test
38+
public void testSetTag() {
39+
NiciraNvpTag tag = new NiciraNvpTag();
40+
tag.setScope("scope");
41+
tag.setTag("tag");
42+
assertEquals("scope part set", "scope", tag.getScope());
43+
assertEquals("tag part set", "tag", tag.getTag());
44+
}
45+
46+
@Test
47+
public void testSetLongTag() {
48+
NiciraNvpTag tag = new NiciraNvpTag();
49+
tag.setScope("scope");
50+
tag.setTag("verylongtagthatshouldattheminimumexceedthefortycharacterlenght");
51+
assertEquals("scope part set", "scope", tag.getScope());
52+
assertEquals("tag part set", "verylongtagthatshouldattheminimumexceedt", tag.getTag());
53+
}
54+
}

0 commit comments

Comments
 (0)