Sho SHIMIZU
Committed by Gerrit Code Review

ONOS-2382: Define a new resource management API

Change-Id: I8abbb30967a7f99ea96b22613b40b2bc0a0ce73c
...@@ -16,13 +16,14 @@ ...@@ -16,13 +16,14 @@
16 package org.onosproject.net.intent; 16 package org.onosproject.net.intent;
17 17
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 +import org.onosproject.net.newresource.ResourceConsumer;
19 20
20 /** 21 /**
21 * Intent identifier suitable as an external key. 22 * Intent identifier suitable as an external key.
22 * <p>This class is immutable.</p> 23 * <p>This class is immutable.</p>
23 */ 24 */
24 @Beta 25 @Beta
25 -public final class IntentId { 26 +public final class IntentId implements ResourceConsumer {
26 27
27 private final long value; 28 private final long value;
28 29
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +
20 +import java.util.Objects;
21 +
22 +import static com.google.common.base.Preconditions.checkNotNull;
23 +
24 +/**
25 + * Base implementation of a class representing resource which belongs to a particular subject.
26 + *
27 + * @param <S> type of the subject
28 + * @param <T> type of the resource
29 + */
30 +@Beta
31 +public abstract class AbstractResource<S, T> implements Resource<S, T> {
32 +
33 + private final S subject;
34 + private final T resource;
35 +
36 + /**
37 + * Constructor expected to be called by constructors of the sub-classes.
38 + *
39 + * @param subject identifier which this resource belongs to
40 + * @param resource resource of the subject
41 + */
42 + protected AbstractResource(S subject, T resource) {
43 + this.subject = checkNotNull(subject);
44 + this.resource = checkNotNull(resource);
45 + }
46 +
47 + @Override
48 + public S subject() {
49 + return subject;
50 + }
51 +
52 + @Override
53 + public T resource() {
54 + return resource;
55 + }
56 +
57 + @Override
58 + public int hashCode() {
59 + return Objects.hash(subject, resource);
60 + }
61 +
62 + @Override
63 + public boolean equals(Object obj) {
64 + if (this == obj) {
65 + return true;
66 + }
67 + if (!(obj instanceof AbstractResource)) {
68 + return false;
69 + }
70 + final AbstractResource that = (AbstractResource) obj;
71 + return Objects.equals(this.subject, that.subject)
72 + && Objects.equals(this.resource, that.resource);
73 + }
74 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +
20 +import java.util.Objects;
21 +
22 +import static com.google.common.base.Preconditions.checkNotNull;
23 +
24 +/**
25 + * Base implementation of a class representing allocation of resource which belongs to a particular subject.
26 + *
27 + * @param <S> type of the subject
28 + * @param <T> type of the resource
29 + */
30 +@Beta
31 +public abstract class AbstractResourceAllocation<S, T> implements ResourceAllocation<S, T> {
32 +
33 + private final S subject;
34 + private final T resource;
35 + private final ResourceConsumer consumer;
36 +
37 + /**
38 + * Constructor expected to be called by constructors of the sub-classes.
39 + *
40 + * @param subject identifier which this resource belongs to
41 + * @param resource resource of the subject
42 + * @param consumer consumer ot this resource
43 + */
44 + protected AbstractResourceAllocation(S subject, T resource, ResourceConsumer consumer) {
45 + this.subject = checkNotNull(subject);
46 + this.resource = checkNotNull(resource);
47 + this.consumer = consumer;
48 + }
49 +
50 + @Override
51 + public S subject() {
52 + return subject;
53 + }
54 +
55 + @Override
56 + public T resource() {
57 + return resource;
58 + }
59 +
60 + @Override
61 + public ResourceConsumer consumer() {
62 + return consumer;
63 + }
64 +
65 + @Override
66 + public int hashCode() {
67 + return Objects.hash(subject, resource, consumer);
68 + }
69 +
70 + @Override
71 + public boolean equals(Object obj) {
72 + if (this == obj) {
73 + return true;
74 + }
75 + if (!(obj instanceof AbstractResourceAllocation)) {
76 + return false;
77 + }
78 + final AbstractResourceAllocation that = (AbstractResourceAllocation) obj;
79 + return Objects.equals(this.subject, that.subject)
80 + && Objects.equals(this.resource, that.resource)
81 + && Objects.equals(this.consumer, that.consumer);
82 + }
83 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +import org.onosproject.net.DeviceId;
20 +
21 +/**
22 + * Represents resource which belongs to a particular device.
23 + *
24 + * @param <T> represents the type of the resource
25 + */
26 +@Beta
27 +public final class DeviceResource<T> extends AbstractResource<DeviceId, T> {
28 +
29 + /**
30 + * Creates a new device resource from the specified device identifier and resource.
31 + * (deviceId, resource) signifies a resource identifier which is globally unique.
32 + *
33 + * @param deviceId device identifier which this resource belongs to
34 + * @param resource resource of the device
35 + */
36 + public DeviceResource(DeviceId deviceId, T resource) {
37 + super(deviceId, resource);
38 + }
39 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +import org.onosproject.net.DeviceId;
20 +
21 +/**
22 + * Represents allocation of resource bound to device.
23 + *
24 + * @param <T> type of the resource
25 + */
26 +@Beta
27 +public final class DeviceResourceAllocation<T> extends AbstractResourceAllocation<DeviceId, T> {
28 +
29 + /**
30 + * Creates a new allocation of resource bound to the specified device and consumed by the specified user.
31 + *
32 + * @param device device identifier which this resource belongs to
33 + * @param resource resource of the device
34 + * @param consumer consumer of this resource
35 + */
36 + public DeviceResourceAllocation(DeviceId device, T resource, ResourceConsumer consumer) {
37 + super(device, resource, consumer);
38 + }
39 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +import org.onosproject.net.LinkKey;
20 +
21 +/**
22 + * Represents resource which belongs to a particular link.
23 + *
24 + * @param <T> represents the type of the resource
25 + */
26 +@Beta
27 +public final class LinkResource<T> extends AbstractResource<LinkKey, T> {
28 +
29 + /**
30 + * Creates a new link resource from the specified link identifier and resource.
31 + * (linkKey, resource) signifies a resource identifier which is globally unique.
32 + *
33 + * @param linkKey link identifier which this resource belongs to
34 + * @param resource resource of the link
35 + */
36 + public LinkResource(LinkKey linkKey, T resource) {
37 + super(linkKey, resource);
38 + }
39 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +import org.onosproject.net.LinkKey;
20 +
21 +/**
22 + * Represents allocation of resource bound to link.
23 + *
24 + * @param <T> type of the resource
25 + */
26 +@Beta
27 +public final class LinkResourceAllocation<T> extends AbstractResourceAllocation<LinkKey, T> {
28 +
29 + /**
30 + * Creates a new allocation of resource bound to the specified device and consumed by the specified user.
31 + *
32 + * @param link device identifier which this resource belongs to
33 + * @param resource resource of the device
34 + * @param consumer consumer of this resource
35 + */
36 + public LinkResourceAllocation(LinkKey link, T resource, ResourceConsumer consumer) {
37 + super(link, resource, consumer);
38 + }
39 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +
20 +/**
21 + * Represents resource which belongs to a particular subject.
22 + *
23 + * @param <S> type of the subject
24 + * @param <T> type of the resource
25 + */
26 +@Beta
27 +public interface Resource<S, T> {
28 + /**
29 + * Returns the subject of the resource.
30 + * The value is the identifier which this resource belongs to.
31 + *
32 + * @return the subject of the resource
33 + */
34 + S subject();
35 +
36 + /**
37 + * Returns the resource of the subject.
38 + *
39 + * @return the resource of the subject
40 + */
41 + T resource();
42 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +
20 +/**
21 + * Represents allocation of resource which belongs to a particular subject.
22 + *
23 + * @param <S> type of the subject
24 + * @param <T> type of the resource
25 + */
26 +@Beta
27 +public interface ResourceAllocation<S, T> {
28 + /**
29 + * Returns the subject of the resource.
30 + * The value is the identifier which this resource belongs to.
31 + *
32 + * @return the subject of the resource
33 + */
34 + S subject();
35 +
36 + /**
37 + * Returns the resource which belongs to the subject.
38 + *
39 + * @return the resource which belongs to the subject
40 + */
41 + T resource();
42 +
43 + /**
44 + * Returns the consumer of this resource.
45 + *
46 + * @return the consumer of this resource
47 + */
48 + ResourceConsumer consumer();
49 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +
20 +/**
21 + * Marker interface representing an entity using resource.
22 + */
23 +@Beta
24 +public interface ResourceConsumer {
25 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.newresource;
17 +
18 +import com.google.common.annotations.Beta;
19 +
20 +import java.util.Collection;
21 +import java.util.List;
22 +import java.util.Optional;
23 +
24 +/**
25 + * Service for allocating/releasing resource(s) and retrieving allocation(s) and availability.
26 + */
27 +@Beta
28 +public interface ResourceService {
29 + /**
30 + * Allocates the specified resource to the specified user.
31 + *
32 + * @param consumer resource user which the resource is allocated to
33 + * @param resource resource to be allocated
34 + * @param <S> type of the subject which this resource belongs to
35 + * @param <T> type of the resource
36 + * @return allocation information enclosed by Optional. If the allocation fails, the return value is empty
37 + */
38 + <S, T> Optional<ResourceAllocation<S, T>> allocate(ResourceConsumer consumer, Resource<S, T> resource);
39 +
40 + /**
41 + * Transactionally allocates the specified resources to the specified user.
42 + * All allocations are made when this method succeeds, or no allocation is made when this method fails.
43 + *
44 + * @param consumer resource user which the resources are allocated to
45 + * @param resources resources to be allocated
46 + * @return non-empty list of allocation information if succeeded, otherwise empty list
47 + */
48 + List<ResourceAllocation<?, ?>> allocate(ResourceConsumer consumer, List<? extends Resource<?, ?>> resources);
49 +
50 + /**
51 + * Transactionally allocates the specified resources to the specified user.
52 + * All allocations are made when this method succeeds, or no allocation is made when this method fails.
53 + *
54 + * @param consumer resource user which the resources are allocated to
55 + * @param resources resources to be allocated
56 + * @return non-empty list of allocation information if succeeded, otherwise empty list
57 + */
58 + List<ResourceAllocation<?, ?>> allocate(ResourceConsumer consumer, Resource<?, ?>... resources);
59 +
60 + /**
61 + * Releases the specified resource allocation.
62 + *
63 + * @param allocation resource allocation to be released
64 + * @param <S> type of the subject which this resource belongs to
65 + * @param <T> type of the device resource
66 + * @return true if succeeded, otherwise false
67 + */
68 + <S, T> boolean release(ResourceAllocation<S, T> allocation);
69 +
70 + /**
71 + * Transactionally releases the specified resource allocations.
72 + * All allocations are released when this method succeeded, or no allocation is released when this method fails.
73 + *
74 + * @param allocations resource allocations to be released
75 + * @return true if succeeded, otherwise false
76 + */
77 + boolean release(List<? extends ResourceAllocation<?, ?>> allocations);
78 +
79 + /**
80 + * Transactionally releases the specified resource allocations.
81 + * All allocations are released when this method succeeded, or no allocation is released when this method fails.
82 + *
83 + * @param allocations resource allocations to be released
84 + * @return true if succeeded, otherwise false
85 + */
86 + boolean release(ResourceAllocation<?, ?>... allocations);
87 +
88 + /**
89 + * Transactionally releases the resources allocated to the specified consumer.
90 + * All allocations are released when this method succeeded, or no allocation is released when this method fails.
91 + *
92 + * @param consumer consumer whose allocated resources are to be released
93 + * @return true if succeeded, otherwise false
94 + */
95 + boolean release(ResourceConsumer consumer);
96 +
97 + /**
98 + * Returns allocated resources in the specified subject regarding the specified resource type.
99 + *
100 + * @param subject subject where resource allocations are obtained
101 + * @param cls class to specify a type of resource
102 + * @param <S> type of the subject
103 + * @param <T> type of the resource
104 + * @return non-empty collection of resource allocations if resources are allocated with the subject and type,
105 + * empty collection if no resource is allocated with the subject and type
106 + */
107 + <S, T> Collection<ResourceAllocation<S, T>> getResourceAllocations(S subject, Class<T> cls);
108 +
109 + /**
110 + * Returns resources allocated to the specified consumer.
111 + *
112 + * @param consumer consumer whose allocated resources are to be returned
113 + * @return resources allocated to the consumer
114 + */
115 + Collection<ResourceAllocation<?, ?>> getResourceAllocations(ResourceConsumer consumer);
116 +
117 + /**
118 + * Returns the availability of the specified device resource.
119 + *
120 + * @param resource resource to check the availability
121 + * @param <S> type of the subject
122 + * @param <T> type of the resource
123 + * @return true if available, otherwise false
124 + */
125 + <S, T> boolean isAvailable(Resource<S, T> resource);
126 +
127 + // TODO: listener and event mechanism need to be considered
128 +}