Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 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 | |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 22 | import java.util.Optional; |
| 23 | |
| 24 | /** |
| 25 | * Represents a phase of processing an intent. |
| 26 | */ |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 27 | public interface IntentProcessPhase { |
Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Execute the procedure represented by the instance |
| 31 | * and generates the next update instance. |
| 32 | * |
| 33 | * @return next update |
| 34 | */ |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 35 | Optional<IntentProcessPhase> execute(); |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Create a starting intent process phase according to intent data this class holds. |
| 39 | * |
| 40 | * @param processor intent processor to be passed to intent process phases |
| 41 | * generated while this instance is working |
| 42 | * @param data intent data to be processed |
| 43 | * @param current intent date that is stored in the store |
| 44 | * @return starting intent process phase |
| 45 | */ |
| 46 | static IntentProcessPhase newInitialPhase(IntentProcessor processor, |
| 47 | IntentData data, IntentData current) { |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 48 | switch (data.request()) { |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 49 | case INSTALL_REQ: |
| 50 | return new InstallRequest(processor, data, Optional.ofNullable(current)); |
| 51 | case WITHDRAW_REQ: |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 52 | return new WithdrawRequest(processor, data, Optional.ofNullable(current)); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 53 | case PURGE_REQ: |
Brian O'Connor | a27ada7 | 2015-04-29 18:25:12 -0700 | [diff] [blame] | 54 | return new PurgeRequest(data, Optional.ofNullable(current)); |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 55 | default: |
| 56 | // illegal state |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 57 | return new Failed(data); |
Sho SHIMIZU | ce49b60 | 2015-02-23 19:15:49 -0800 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Sho SHIMIZU | 4a14185 | 2016-01-14 18:55:40 -0800 | [diff] [blame] | 61 | static FinalIntentProcessPhase process(IntentProcessPhase initial) { |
| 62 | Optional<IntentProcessPhase> currentPhase = Optional.of(initial); |
| 63 | IntentProcessPhase previousPhase = initial; |
| 64 | |
| 65 | while (currentPhase.isPresent()) { |
| 66 | previousPhase = currentPhase.get(); |
| 67 | currentPhase = previousPhase.execute(); |
| 68 | } |
| 69 | return (FinalIntentProcessPhase) previousPhase; |
| 70 | } |
| 71 | |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 72 | static void transferErrorCount(IntentData data, Optional<IntentData> stored) { |
Sho SHIMIZU | ab6c1fe | 2016-01-13 14:07:25 -0800 | [diff] [blame] | 73 | stored.ifPresent(storedData -> { |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 74 | if (Objects.equals(data.intent(), storedData.intent()) && |
| 75 | Objects.equals(data.request(), storedData.request())) { |
| 76 | data.setErrorCount(storedData.errorCount()); |
| 77 | } else { |
| 78 | data.setErrorCount(0); |
| 79 | } |
Sho SHIMIZU | ab6c1fe | 2016-01-13 14:07:25 -0800 | [diff] [blame] | 80 | }); |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 81 | } |
Sho SHIMIZU | bdc17e7 | 2015-01-27 17:46:41 -0800 | [diff] [blame] | 82 | } |