blob: aa7ef6a05272f0b9163446b6685066aed804b4da [file] [log] [blame]
Sho SHIMIZU662c3db2015-02-23 16:59:01 -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 */
16package org.onosproject.net.intent.impl.phase;
17
18import org.onosproject.net.intent.IntentData;
19import org.onosproject.net.intent.impl.IntentProcessor;
20
21import java.util.Optional;
22import java.util.concurrent.Callable;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import static org.onlab.util.Tools.isNullOrEmpty;
26import static org.onosproject.net.intent.IntentState.WITHDRAWN;
27
28/**
29 * Worker to process a submitted intent. {@link #call()} method generates
30 */
31public final class IntentWorker implements Callable<FinalIntentProcessPhase> {
32
33 private final IntentProcessor processor;
34 private final IntentData data;
35 private final IntentData current;
36
37 /**
38 * Create an instance with the specified arguments.
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 */
45 public IntentWorker(IntentProcessor processor, IntentData data, IntentData current) {
46 this.processor = checkNotNull(processor);
47 this.data = checkNotNull(data);
48 this.current = current;
49 }
50
51 @Override
52 public FinalIntentProcessPhase call() throws Exception {
53 IntentProcessPhase update = createInitialPhase();
54 Optional<IntentProcessPhase> currentPhase = Optional.of(update);
55 IntentProcessPhase previousPhase = update;
56
57 while (currentPhase.isPresent()) {
58 previousPhase = currentPhase.get();
59 currentPhase = previousPhase.execute();
60 }
61 return (FinalIntentProcessPhase) previousPhase;
62 }
63
64 /**
65 * Create a starting intent process phase according to intent data this class holds.
66 *
67 * @return starting intent process phase
68 */
69 private IntentProcessPhase createInitialPhase() {
70 switch (data.state()) {
71 case INSTALL_REQ:
72 return new InstallRequest(processor, data, Optional.ofNullable(current));
73 case WITHDRAW_REQ:
74 if (current == null || isNullOrEmpty(current.installables())) {
75 return new Withdrawn(data, WITHDRAWN);
76 } else {
77 return new WithdrawRequest(processor, data, current);
78 }
79 default:
80 // illegal state
81 return new CompilingFailed(data);
82 }
83 }
84}