blob: 0b16f10be669e5af72942ce04fccbee3ffc6e00c [file] [log] [blame]
Sho SHIMIZUf7b693e2014-08-15 16:17:13 -07001package net.onrc.onos.core.newintent;
2
3import net.onrc.onos.api.newintent.ConnectivityIntent;
4import net.onrc.onos.api.newintent.Intent;
5import net.onrc.onos.api.newintent.IntentCompiler;
6import net.onrc.onos.api.newintent.IntentId;
7import net.onrc.onos.api.newintent.IntentIdGenerator;
8import net.onrc.onos.core.matchaction.action.Action;
9import net.onrc.onos.core.matchaction.action.Actions;
10import net.onrc.onos.core.matchaction.action.OutputAction;
11import net.onrc.onos.core.util.SwitchPort;
12
13import java.util.ArrayList;
14import java.util.List;
15
16import static com.google.common.base.Preconditions.checkNotNull;
17
18/**
19 * A base IntentCompiler implementation.
20 * @param <T> the type of intent
21 */
22public abstract class AbstractIntentCompiler<T extends Intent> implements IntentCompiler<T> {
23 private final IntentIdGenerator idGenerator;
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 */
35 protected AbstractIntentCompiler(IntentIdGenerator idGenerator) {
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}