Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.newintent; |
| 2 | |
| 3 | import net.onrc.onos.api.newintent.ConnectivityIntent; |
| 4 | import net.onrc.onos.api.newintent.Intent; |
| 5 | import net.onrc.onos.api.newintent.IntentCompiler; |
| 6 | import net.onrc.onos.api.newintent.IntentId; |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.matchaction.action.Action; |
| 8 | import net.onrc.onos.core.matchaction.action.Actions; |
| 9 | import net.onrc.onos.core.matchaction.action.OutputAction; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.util.IdGenerator; |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.util.SwitchPort; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.List; |
| 15 | |
| 16 | import static com.google.common.base.Preconditions.checkNotNull; |
| 17 | |
| 18 | /** |
| 19 | * A base IntentCompiler implementation. |
| 20 | * @param <T> the type of intent |
| 21 | */ |
| 22 | public abstract class AbstractIntentCompiler<T extends Intent> implements IntentCompiler<T> { |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 23 | private final IdGenerator<IntentId> idGenerator; |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Constructs an instance with the specified Intent ID generator. |
| 27 | * <p> |
| 28 | * Intent compiler generates intents from an input intent. |
| 29 | * To make sure to use unique IDs for generated intents, intent |
| 30 | * ID generator is given as the argument of a constructor in normal |
| 31 | * cases. |
| 32 | * </p> |
| 33 | * @param idGenerator intent ID generator |
| 34 | */ |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 35 | protected AbstractIntentCompiler(IdGenerator<IntentId> idGenerator) { |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 36 | this.idGenerator = checkNotNull(idGenerator); |
| 37 | } |
| 38 | |
| 39 | protected IntentId getNextId() { |
| 40 | return idGenerator.getNewId(); |
| 41 | } |
| 42 | |
| 43 | protected List<Action> packActions(ConnectivityIntent intent, SwitchPort egress) { |
| 44 | List<Action> actions = new ArrayList<>(); |
| 45 | Action intentAction = intent.getAction(); |
| 46 | if (!intentAction.equals(Actions.nullAction())) { |
| 47 | actions.add(intentAction); |
| 48 | } |
| 49 | |
| 50 | OutputAction output = new OutputAction(egress.getPortNumber()); |
| 51 | actions.add(output); |
| 52 | return actions; |
| 53 | } |
| 54 | } |