blob: 5cd674bf8c6ff38056c4b12f93ae0863a9e71765 [file] [log] [blame]
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -08003 *
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 SHIMIZU36a8a6e2015-02-13 15:38:45 -080016package org.onosproject.net.intent.impl.phase;
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -080017
Sho SHIMIZUce49b602015-02-23 19:15:49 -080018import org.onosproject.net.intent.IntentData;
19import org.onosproject.net.intent.impl.IntentProcessor;
20
Brian O'Connor6d8e3172015-04-30 15:43:57 -070021import java.util.Objects;
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -080022import java.util.Optional;
23
24/**
25 * Represents a phase of processing an intent.
26 */
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080027public interface IntentProcessPhase {
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -080028
29 /**
30 * Execute the procedure represented by the instance
31 * and generates the next update instance.
32 *
33 * @return next update
34 */
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080035 Optional<IntentProcessPhase> execute();
Sho SHIMIZUce49b602015-02-23 19:15:49 -080036
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'Connorf0c5a052015-04-27 00:34:53 -070048 switch (data.request()) {
Sho SHIMIZUce49b602015-02-23 19:15:49 -080049 case INSTALL_REQ:
50 return new InstallRequest(processor, data, Optional.ofNullable(current));
51 case WITHDRAW_REQ:
Brian O'Connorf0c5a052015-04-27 00:34:53 -070052 return new WithdrawRequest(processor, data, Optional.ofNullable(current));
Ray Milkey8c6d00e2015-03-13 14:14:34 -070053 case PURGE_REQ:
Brian O'Connora27ada72015-04-29 18:25:12 -070054 return new PurgeRequest(data, Optional.ofNullable(current));
Sho SHIMIZUce49b602015-02-23 19:15:49 -080055 default:
56 // illegal state
Brian O'Connorf0c5a052015-04-27 00:34:53 -070057 return new Failed(data);
Sho SHIMIZUce49b602015-02-23 19:15:49 -080058 }
59 }
60
Sho SHIMIZU4a141852016-01-14 18:55:40 -080061 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'Connor6d8e3172015-04-30 15:43:57 -070072 static void transferErrorCount(IntentData data, Optional<IntentData> stored) {
Sho SHIMIZUab6c1fe2016-01-13 14:07:25 -080073 stored.ifPresent(storedData -> {
Brian O'Connor6d8e3172015-04-30 15:43:57 -070074 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 SHIMIZUab6c1fe2016-01-13 14:07:25 -080080 });
Brian O'Connor6d8e3172015-04-30 15:43:57 -070081 }
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -080082}