blob: ea5a5906288c0796809c088eace60329845de6de [file] [log] [blame]
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -08001/*
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 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
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -080021import java.util.Optional;
22
Sho SHIMIZUce49b602015-02-23 19:15:49 -080023import static org.onlab.util.Tools.isNullOrEmpty;
Sho SHIMIZUce49b602015-02-23 19:15:49 -080024
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -080025/**
26 * Represents a phase of processing an intent.
27 */
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080028public interface IntentProcessPhase {
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -080029
30 /**
31 * Execute the procedure represented by the instance
32 * and generates the next update instance.
33 *
34 * @return next update
35 */
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080036 Optional<IntentProcessPhase> execute();
Sho SHIMIZUce49b602015-02-23 19:15:49 -080037
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 SHIMIZUee2aa652015-02-25 18:56:43 -080054 return new Withdrawn(data);
Sho SHIMIZUce49b602015-02-23 19:15:49 -080055 } else {
56 return new WithdrawRequest(processor, data, current);
57 }
Ray Milkey8c6d00e2015-03-13 14:14:34 -070058 case PURGE_REQ:
59 return new PurgeRequest(data, current);
Sho SHIMIZUce49b602015-02-23 19:15:49 -080060 default:
61 // illegal state
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080062 return new CompileFailed(data);
Sho SHIMIZUce49b602015-02-23 19:15:49 -080063 }
64 }
65
Sho SHIMIZUbdc17e72015-01-27 17:46:41 -080066}