Sho SHIMIZU
Committed by Gerrit Code Review

Pull out IntentWorker from IntentManager

- IntentWorker is placed in phase package
- Make public IntentProcessPhase subclasses package private now

Change-Id: Ie7d218fe5d8a516f3913ff8881d9d17cfd8e4c46
...@@ -41,20 +41,14 @@ import org.onosproject.net.intent.IntentState; ...@@ -41,20 +41,14 @@ import org.onosproject.net.intent.IntentState;
41 import org.onosproject.net.intent.IntentStore; 41 import org.onosproject.net.intent.IntentStore;
42 import org.onosproject.net.intent.IntentStoreDelegate; 42 import org.onosproject.net.intent.IntentStoreDelegate;
43 import org.onosproject.net.intent.Key; 43 import org.onosproject.net.intent.Key;
44 -import org.onosproject.net.intent.impl.phase.CompilingFailed;
45 import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase; 44 import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase;
46 -import org.onosproject.net.intent.impl.phase.InstallRequest; 45 +import org.onosproject.net.intent.impl.phase.IntentWorker;
47 -import org.onosproject.net.intent.impl.phase.IntentProcessPhase;
48 -import org.onosproject.net.intent.impl.phase.WithdrawRequest;
49 -import org.onosproject.net.intent.impl.phase.Withdrawn;
50 import org.slf4j.Logger; 46 import org.slf4j.Logger;
51 47
52 import java.util.Collection; 48 import java.util.Collection;
53 import java.util.EnumSet; 49 import java.util.EnumSet;
54 import java.util.List; 50 import java.util.List;
55 import java.util.Map; 51 import java.util.Map;
56 -import java.util.Optional;
57 -import java.util.concurrent.Callable;
58 import java.util.concurrent.ExecutionException; 52 import java.util.concurrent.ExecutionException;
59 import java.util.concurrent.ExecutorService; 53 import java.util.concurrent.ExecutorService;
60 import java.util.concurrent.Future; 54 import java.util.concurrent.Future;
...@@ -64,10 +58,8 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -64,10 +58,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
64 import static java.util.concurrent.Executors.newFixedThreadPool; 58 import static java.util.concurrent.Executors.newFixedThreadPool;
65 import static java.util.concurrent.Executors.newSingleThreadExecutor; 59 import static java.util.concurrent.Executors.newSingleThreadExecutor;
66 import static org.onlab.util.Tools.groupedThreads; 60 import static org.onlab.util.Tools.groupedThreads;
67 -import static org.onlab.util.Tools.isNullOrEmpty;
68 import static org.onosproject.net.intent.IntentState.FAILED; 61 import static org.onosproject.net.intent.IntentState.FAILED;
69 import static org.onosproject.net.intent.IntentState.INSTALL_REQ; 62 import static org.onosproject.net.intent.IntentState.INSTALL_REQ;
70 -import static org.onosproject.net.intent.IntentState.WITHDRAWN;
71 import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ; 63 import static org.onosproject.net.intent.IntentState.WITHDRAW_REQ;
72 import static org.slf4j.LoggerFactory.getLogger; 64 import static org.slf4j.LoggerFactory.getLogger;
73 65
...@@ -364,48 +356,6 @@ public class IntentManager ...@@ -364,48 +356,6 @@ public class IntentManager
364 } 356 }
365 } 357 }
366 358
367 - private final class IntentWorker implements Callable<FinalIntentProcessPhase> {
368 -
369 - private final IntentProcessor processor;
370 - private final IntentData data;
371 - private final IntentData current;
372 -
373 - private IntentWorker(IntentProcessor processor, IntentData data, IntentData current) {
374 - this.processor = checkNotNull(processor);
375 - this.data = checkNotNull(data);
376 - this.current = current;
377 - }
378 -
379 - @Override
380 - public FinalIntentProcessPhase call() throws Exception {
381 - IntentProcessPhase update = createIntentUpdate();
382 - Optional<IntentProcessPhase> currentPhase = Optional.of(update);
383 - IntentProcessPhase previousPhase = update;
384 -
385 - while (currentPhase.isPresent()) {
386 - previousPhase = currentPhase.get();
387 - currentPhase = previousPhase.execute();
388 - }
389 - return (FinalIntentProcessPhase) previousPhase;
390 - }
391 -
392 - private IntentProcessPhase createIntentUpdate() {
393 - switch (data.state()) {
394 - case INSTALL_REQ:
395 - return new InstallRequest(processor, data, Optional.ofNullable(current));
396 - case WITHDRAW_REQ:
397 - if (current == null || isNullOrEmpty(current.installables())) {
398 - return new Withdrawn(data, WITHDRAWN);
399 - } else {
400 - return new WithdrawRequest(processor, data, current);
401 - }
402 - default:
403 - // illegal state
404 - return new CompilingFailed(data);
405 - }
406 - }
407 - }
408 -
409 private class InternalBatchDelegate implements IntentBatchDelegate { 359 private class InternalBatchDelegate implements IntentBatchDelegate {
410 @Override 360 @Override
411 public void execute(Collection<IntentData> operations) { 361 public void execute(Collection<IntentData> operations) {
......
...@@ -25,13 +25,13 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -25,13 +25,13 @@ import static com.google.common.base.Preconditions.checkNotNull;
25 /** 25 /**
26 * Represents a phase where intent installation has been requested. 26 * Represents a phase where intent installation has been requested.
27 */ 27 */
28 -public final class InstallRequest implements IntentProcessPhase { 28 +final class InstallRequest implements IntentProcessPhase {
29 29
30 private final IntentProcessor intentManager; 30 private final IntentProcessor intentManager;
31 private final IntentData pending; 31 private final IntentData pending;
32 private final Optional<IntentData> current; 32 private final Optional<IntentData> current;
33 33
34 - public InstallRequest(IntentProcessor processor, IntentData intentData, Optional<IntentData> current) { 34 + InstallRequest(IntentProcessor processor, IntentData intentData, Optional<IntentData> current) {
35 this.intentManager = checkNotNull(processor); 35 this.intentManager = checkNotNull(processor);
36 this.pending = checkNotNull(intentData); 36 this.pending = checkNotNull(intentData);
37 this.current = checkNotNull(current); 37 this.current = checkNotNull(current);
......
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 + */
16 +package org.onosproject.net.intent.impl.phase;
17 +
18 +import org.onosproject.net.intent.IntentData;
19 +import org.onosproject.net.intent.impl.IntentProcessor;
20 +
21 +import java.util.Optional;
22 +import java.util.concurrent.Callable;
23 +
24 +import static com.google.common.base.Preconditions.checkNotNull;
25 +import static org.onlab.util.Tools.isNullOrEmpty;
26 +import static org.onosproject.net.intent.IntentState.WITHDRAWN;
27 +
28 +/**
29 + * Worker to process a submitted intent. {@link #call()} method generates
30 + */
31 +public 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 +}
...@@ -25,13 +25,13 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -25,13 +25,13 @@ import static com.google.common.base.Preconditions.checkNotNull;
25 /** 25 /**
26 * Represents a phase of requesting a withdraw of an intent. 26 * Represents a phase of requesting a withdraw of an intent.
27 */ 27 */
28 -public final class WithdrawRequest implements IntentProcessPhase { 28 +final class WithdrawRequest implements IntentProcessPhase {
29 29
30 private final IntentProcessor processor; 30 private final IntentProcessor processor;
31 private final IntentData pending; 31 private final IntentData pending;
32 private final IntentData current; 32 private final IntentData current;
33 33
34 - public WithdrawRequest(IntentProcessor processor, IntentData intentData, IntentData current) { 34 + WithdrawRequest(IntentProcessor processor, IntentData intentData, IntentData current) {
35 this.processor = checkNotNull(processor); 35 this.processor = checkNotNull(processor);
36 this.pending = checkNotNull(intentData); 36 this.pending = checkNotNull(intentData);
37 this.current = checkNotNull(current); 37 this.current = checkNotNull(current);
......
...@@ -24,15 +24,15 @@ import static org.onosproject.net.intent.IntentState.WITHDRAWING; ...@@ -24,15 +24,15 @@ import static org.onosproject.net.intent.IntentState.WITHDRAWING;
24 /** 24 /**
25 * Represents a phase where an intent has been withdrawn. 25 * Represents a phase where an intent has been withdrawn.
26 */ 26 */
27 -public final class Withdrawn extends FinalIntentProcessPhase { 27 +final class Withdrawn extends FinalIntentProcessPhase {
28 28
29 private final IntentData intentData; 29 private final IntentData intentData;
30 30
31 - public Withdrawn(IntentData intentData) { 31 + Withdrawn(IntentData intentData) {
32 this(intentData, WITHDRAWING); 32 this(intentData, WITHDRAWING);
33 } 33 }
34 34
35 - public Withdrawn(IntentData intentData, IntentState newState) { 35 + Withdrawn(IntentData intentData, IntentState newState) {
36 this.intentData = checkNotNull(intentData); 36 this.intentData = checkNotNull(intentData);
37 this.intentData.setState(newState); 37 this.intentData.setState(newState);
38 } 38 }
......