Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 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.intent.impl.phase; |
| 17 | |
Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 18 | |
| 19 | import java.util.Optional; |
| 20 | import java.util.concurrent.Callable; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Worker to process a submitted intent. {@link #call()} method generates |
| 26 | */ |
| 27 | public final class IntentWorker implements Callable<FinalIntentProcessPhase> { |
| 28 | |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 29 | private final IntentProcessPhase initial; |
Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Create an instance with the specified arguments. |
| 33 | * |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 34 | * @param initial initial intent process phase |
Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 35 | */ |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 36 | public IntentWorker(IntentProcessPhase initial) { |
| 37 | this.initial = checkNotNull(initial); |
Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public FinalIntentProcessPhase call() throws Exception { |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 42 | IntentProcessPhase update = initial; |
Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 43 | Optional<IntentProcessPhase> currentPhase = Optional.of(update); |
| 44 | IntentProcessPhase previousPhase = update; |
| 45 | |
| 46 | while (currentPhase.isPresent()) { |
| 47 | previousPhase = currentPhase.get(); |
| 48 | currentPhase = previousPhase.execute(); |
| 49 | } |
| 50 | return (FinalIntentProcessPhase) previousPhase; |
| 51 | } |
Sho SHIMIZU | 662c3db | 2015-02-23 16:59:01 -0800 | [diff] [blame] | 52 | } |