Committed by
Sho Shimizu
Refactor: make ResourcePath construction a bit type-safer
Change-Id: Ie10f2b873b2b5bd7c284abdb509f31605f750435
Showing
2 changed files
with
59 additions
and
20 deletions
... | @@ -18,6 +18,8 @@ package org.onosproject.net.newresource; | ... | @@ -18,6 +18,8 @@ package org.onosproject.net.newresource; |
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | import com.google.common.base.MoreObjects; | 19 | import com.google.common.base.MoreObjects; |
20 | import com.google.common.collect.ImmutableList; | 20 | import com.google.common.collect.ImmutableList; |
21 | +import org.onosproject.net.DeviceId; | ||
22 | +import org.onosproject.net.PortNumber; | ||
21 | 23 | ||
22 | import java.util.LinkedList; | 24 | import java.util.LinkedList; |
23 | import java.util.List; | 25 | import java.util.List; |
... | @@ -41,8 +43,8 @@ import static com.google.common.base.Preconditions.checkState; | ... | @@ -41,8 +43,8 @@ import static com.google.common.base.Preconditions.checkState; |
41 | * A double value is associated with a continuous type value. | 43 | * A double value is associated with a continuous type value. |
42 | * | 44 | * |
43 | * Users of this class must keep the semantics of resources regarding the hierarchical structure. | 45 | * Users of this class must keep the semantics of resources regarding the hierarchical structure. |
44 | - * For example, resource path, Link:1/VLAN ID:100, is valid, but resource path, VLAN ID:100/Link:1 | 46 | + * For example, resource path, Device:1/Port:1/VLAN ID:100, is valid, but resource path, |
45 | - * is not valid because a link is not a sub-component of a VLAN ID. | 47 | + * VLAN ID:100/Device:1/Port:1 is not valid because a link is not a sub-component of a VLAN ID. |
46 | */ | 48 | */ |
47 | @Beta | 49 | @Beta |
48 | public abstract class ResourcePath { | 50 | public abstract class ResourcePath { |
... | @@ -52,29 +54,73 @@ public abstract class ResourcePath { | ... | @@ -52,29 +54,73 @@ public abstract class ResourcePath { |
52 | 54 | ||
53 | public static final Discrete ROOT = new Discrete(); | 55 | public static final Discrete ROOT = new Discrete(); |
54 | 56 | ||
57 | + public static ResourcePath discrete(DeviceId device) { | ||
58 | + return new Discrete(ImmutableList.of(device)); | ||
59 | + } | ||
60 | + | ||
55 | /** | 61 | /** |
56 | * Creates an resource path which represents a discrete-type resource from the specified components. | 62 | * Creates an resource path which represents a discrete-type resource from the specified components. |
57 | * | 63 | * |
58 | - * @param components components of the path. The order represents hierarchical structure of the resource. | 64 | + * @param device device ID which is the first component of the path |
65 | + * @param components following components of the path. The order represents hierarchical structure of the resource. | ||
59 | * @return resource path instance | 66 | * @return resource path instance |
60 | */ | 67 | */ |
61 | - public static ResourcePath discrete(Object... components) { | 68 | + public static ResourcePath discrete(DeviceId device, Object... components) { |
62 | - if (components.length == 0) { | 69 | + return new Discrete(ImmutableList.builder() |
63 | - return ROOT; | 70 | + .add(device) |
64 | - } else { | 71 | + .add(components) |
65 | - return new Discrete(ImmutableList.copyOf(components)); | 72 | + .build()); |
66 | } | 73 | } |
74 | + | ||
75 | + /** | ||
76 | + * Creates an resource path which represents a discrete-type resource from the specified components. | ||
77 | + * | ||
78 | + * @param device device ID which is the first component of the path | ||
79 | + * @param port port number which is the second component of the path | ||
80 | + * @param components following components of the path. The order represents hierarchical structure of the resource. | ||
81 | + * @return resource path instance | ||
82 | + */ | ||
83 | + public static ResourcePath discrete(DeviceId device, PortNumber port, Object... components) { | ||
84 | + return new Discrete(ImmutableList.builder() | ||
85 | + .add(device) | ||
86 | + .add(port) | ||
87 | + .add(components) | ||
88 | + .build()); | ||
67 | } | 89 | } |
68 | 90 | ||
69 | /** | 91 | /** |
70 | * Creates an resource path which represents a continuous-type resource from the specified components. | 92 | * Creates an resource path which represents a continuous-type resource from the specified components. |
71 | * | 93 | * |
72 | * @param value amount of the resource | 94 | * @param value amount of the resource |
73 | - * @param components components of the path. The order represents hierarchical structure of the resource. | 95 | + * @param device device ID which is the first component of the path |
96 | + * @param components following components of the path. The order represents hierarchical structure of the resource. | ||
97 | + * @return resource path instance | ||
98 | + */ | ||
99 | + public static ResourcePath continuous(double value, DeviceId device, Object... components) { | ||
100 | + checkArgument(components.length > 0, | ||
101 | + "Length of components must be greater thant 0, but " + components.length); | ||
102 | + | ||
103 | + return new Continuous(ImmutableList.builder() | ||
104 | + .add(device) | ||
105 | + .add(components) | ||
106 | + .build(), value); | ||
107 | + } | ||
108 | + | ||
109 | + /** | ||
110 | + * Creates an resource path which represents a continuous-type resource from the specified components. | ||
111 | + * | ||
112 | + * @param value amount of the resource | ||
113 | + * @param device device ID which is the first component of the path. | ||
114 | + * @param port port number which is the second component of the path. | ||
115 | + * @param components following components of the path. The order represents hierarchical structure of the resource. | ||
74 | * @return resource path instance | 116 | * @return resource path instance |
75 | */ | 117 | */ |
76 | - public static ResourcePath continuous(double value, Object... components) { | 118 | + public static ResourcePath continuous(double value, DeviceId device, PortNumber port, Object... components) { |
77 | - return new Continuous(ImmutableList.copyOf(components), value); | 119 | + return new Continuous(ImmutableList.builder() |
120 | + .add(device) | ||
121 | + .add(port) | ||
122 | + .add(components) | ||
123 | + .build(), value); | ||
78 | } | 124 | } |
79 | 125 | ||
80 | /** | 126 | /** |
... | @@ -82,7 +128,7 @@ public abstract class ResourcePath { | ... | @@ -82,7 +128,7 @@ public abstract class ResourcePath { |
82 | * | 128 | * |
83 | * @param components components of the path. The order represents hierarchical structure of the resource. | 129 | * @param components components of the path. The order represents hierarchical structure of the resource. |
84 | */ | 130 | */ |
85 | - ResourcePath(List<Object> components) { | 131 | + protected ResourcePath(List<Object> components) { |
86 | checkNotNull(components); | 132 | checkNotNull(components); |
87 | checkArgument(!components.isEmpty()); | 133 | checkArgument(!components.isEmpty()); |
88 | 134 | ||
... | @@ -101,7 +147,7 @@ public abstract class ResourcePath { | ... | @@ -101,7 +147,7 @@ public abstract class ResourcePath { |
101 | * @param parent the parent of this resource | 147 | * @param parent the parent of this resource |
102 | * @param last a child of the parent | 148 | * @param last a child of the parent |
103 | */ | 149 | */ |
104 | - ResourcePath(Discrete parent, Object last) { | 150 | + protected ResourcePath(Discrete parent, Object last) { |
105 | checkNotNull(parent); | 151 | checkNotNull(parent); |
106 | checkNotNull(last); | 152 | checkNotNull(last); |
107 | 153 | ... | ... |
... | @@ -53,13 +53,6 @@ public class ResourcePathTest { | ... | @@ -53,13 +53,6 @@ public class ResourcePathTest { |
53 | } | 53 | } |
54 | 54 | ||
55 | @Test | 55 | @Test |
56 | - public void testCreateWithZeroComponent() { | ||
57 | - ResourcePath path = ResourcePath.discrete(); | ||
58 | - | ||
59 | - assertThat(path, is(ResourcePath.ROOT)); | ||
60 | - } | ||
61 | - | ||
62 | - @Test | ||
63 | public void testComponents() { | 56 | public void testComponents() { |
64 | ResourcePath port = ResourcePath.discrete(D1, P1); | 57 | ResourcePath port = ResourcePath.discrete(D1, P1); |
65 | 58 | ... | ... |
-
Please register or login to post a comment