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