yoonseon | e92748c | 2016-12-13 16:37:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.incubator.net.virtual.provider; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 21 | /** |
| 22 | * Base implementation of a virtual provider service, |
| 23 | * which tracks the provider to which it is issued and can be invalidated. |
| 24 | * |
| 25 | * @param <P> type of the information provider |
| 26 | */ |
yoonseon | e92748c | 2016-12-13 16:37:51 -0800 | [diff] [blame] | 27 | public abstract class AbstractVirtualProviderService<P extends VirtualProvider> |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame^] | 28 | implements VirtualProviderService<P> { |
yoonseon | e92748c | 2016-12-13 16:37:51 -0800 | [diff] [blame] | 29 | |
| 30 | private boolean isValid = true; |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 31 | private P provider = null; |
yoonseon | e92748c | 2016-12-13 16:37:51 -0800 | [diff] [blame] | 32 | |
yoonseon | 1f51857 | 2017-01-03 17:27:26 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Creates a virtual provider service on behalf of the specified provider. |
| 35 | * |
| 36 | * @param provider provider to which this service is being issued |
| 37 | */ |
| 38 | protected void setProvider(P provider) { |
yoonseon | e92748c | 2016-12-13 16:37:51 -0800 | [diff] [blame] | 39 | this.provider = provider; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Invalidates this provider service. |
| 44 | */ |
| 45 | public void invalidate() { |
| 46 | isValid = false; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Checks the validity of this provider service. |
| 51 | * |
| 52 | * @throws java.lang.IllegalStateException if the service is no longer valid |
| 53 | */ |
| 54 | public void checkValidity() { |
| 55 | checkState(isValid, "Provider service is no longer valid"); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public P provider() { |
| 60 | return provider; |
| 61 | } |
| 62 | } |