Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -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 | */ |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl.phase; |
Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 17 | |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 18 | import org.onosproject.net.intent.IntentData; |
| 19 | import org.onosproject.net.intent.impl.IntentProcessor; |
| 20 | |
Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 21 | import java.util.Optional; |
| 22 | |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 23 | import static org.onlab.util.Tools.isNullOrEmpty; |
| 24 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
| 25 | |
Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Represents a phase of processing an intent. |
| 28 | */ |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 29 | public interface IntentProcessPhase { |
Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Execute the procedure represented by the instance |
| 33 | * and generates the next update instance. |
| 34 | * |
| 35 | * @return next update |
| 36 | */ |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 37 | Optional<IntentProcessPhase> execute(); |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Create a starting intent process phase according to intent data this class holds. |
| 41 | * |
| 42 | * @param processor intent processor to be passed to intent process phases |
| 43 | * generated while this instance is working |
| 44 | * @param data intent data to be processed |
| 45 | * @param current intent date that is stored in the store |
| 46 | * @return starting intent process phase |
| 47 | */ |
| 48 | static IntentProcessPhase newInitialPhase(IntentProcessor processor, |
| 49 | IntentData data, IntentData current) { |
| 50 | switch (data.state()) { |
| 51 | case INSTALL_REQ: |
| 52 | return new InstallRequest(processor, data, Optional.ofNullable(current)); |
| 53 | case WITHDRAW_REQ: |
| 54 | if (current == null || isNullOrEmpty(current.installables())) { |
| 55 | return new Withdrawn(data, WITHDRAWN); |
| 56 | } else { |
| 57 | return new WithdrawRequest(processor, data, current); |
| 58 | } |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 59 | case PURGE_REQ: |
| 60 | return new PurgeRequest(data, current); |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 61 | default: |
| 62 | // illegal state |
| 63 | return new CompilingFailed(data); |
| 64 | } |
| 65 | } |
| 66 | |
Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 67 | } |