Ray Milkey
Committed by Brian O'Connor

Add path intent compiler that generates flow objective intents

Change-Id: I11bee398d927f0e3f32b7cf81d98cfe5816db477
...@@ -17,19 +17,25 @@ ...@@ -17,19 +17,25 @@
17 package org.onosproject.net.intent; 17 package org.onosproject.net.intent;
18 18
19 import com.google.common.base.MoreObjects; 19 import com.google.common.base.MoreObjects;
20 +import com.google.common.collect.ImmutableList;
20 import org.onosproject.core.ApplicationId; 21 import org.onosproject.core.ApplicationId;
22 +import org.onosproject.net.DeviceId;
21 import org.onosproject.net.NetworkResource; 23 import org.onosproject.net.NetworkResource;
22 import org.onosproject.net.flowobjective.Objective; 24 import org.onosproject.net.flowobjective.Objective;
23 25
24 import java.util.Collection; 26 import java.util.Collection;
27 +import java.util.List;
28 +
29 +import static com.google.common.base.Preconditions.checkArgument;
25 30
26 /** 31 /**
27 * Intent expressed as (and backed by) a collection of flow objectives through 32 * Intent expressed as (and backed by) a collection of flow objectives through
28 * which the intent is to be accomplished. 33 * which the intent is to be accomplished.
29 */ 34 */
30 -public class FlowObjectiveIntent extends Intent { 35 +public final class FlowObjectiveIntent extends Intent {
31 36
32 - private final Collection<Objective> objectives; 37 + private final List<Objective> objectives;
38 + private final List<DeviceId> devices;
33 39
34 /** 40 /**
35 * Constructor for serialization. 41 * Constructor for serialization.
...@@ -37,6 +43,7 @@ public class FlowObjectiveIntent extends Intent { ...@@ -37,6 +43,7 @@ public class FlowObjectiveIntent extends Intent {
37 protected FlowObjectiveIntent() { 43 protected FlowObjectiveIntent() {
38 super(); 44 super();
39 this.objectives = null; 45 this.objectives = null;
46 + this.devices = null;
40 } 47 }
41 48
42 /** 49 /**
...@@ -44,13 +51,15 @@ public class FlowObjectiveIntent extends Intent { ...@@ -44,13 +51,15 @@ public class FlowObjectiveIntent extends Intent {
44 * resources. 51 * resources.
45 * 52 *
46 * @param appId application id 53 * @param appId application id
54 + * @param devices list of target devices; in same order as the objectives
47 * @param objectives backing flow objectives 55 * @param objectives backing flow objectives
48 * @param resources backing network resources 56 * @param resources backing network resources
49 */ 57 */
50 public FlowObjectiveIntent(ApplicationId appId, 58 public FlowObjectiveIntent(ApplicationId appId,
51 - Collection<Objective> objectives, 59 + List<DeviceId> devices,
60 + List<Objective> objectives,
52 Collection<NetworkResource> resources) { 61 Collection<NetworkResource> resources) {
53 - this(appId, null, objectives, resources); 62 + this(appId, null, devices, objectives, resources);
54 } 63 }
55 64
56 /** 65 /**
...@@ -59,14 +68,20 @@ public class FlowObjectiveIntent extends Intent { ...@@ -59,14 +68,20 @@ public class FlowObjectiveIntent extends Intent {
59 * 68 *
60 * @param appId application id 69 * @param appId application id
61 * @param key intent key 70 * @param key intent key
71 + * @param devices list of target devices; in same order as the objectives
62 * @param objectives backing flow objectives 72 * @param objectives backing flow objectives
63 * @param resources backing network resources 73 * @param resources backing network resources
64 */ 74 */
65 - public FlowObjectiveIntent(ApplicationId appId, Key key, 75 + public FlowObjectiveIntent(ApplicationId appId,
66 - Collection<Objective> objectives, 76 + Key key,
77 + List<DeviceId> devices,
78 + List<Objective> objectives,
67 Collection<NetworkResource> resources) { 79 Collection<NetworkResource> resources) {
68 super(appId, key, resources, DEFAULT_INTENT_PRIORITY); 80 super(appId, key, resources, DEFAULT_INTENT_PRIORITY);
69 - this.objectives = objectives; 81 + checkArgument(devices.size() == objectives.size(),
82 + "Number of devices and objectives does not match");
83 + this.objectives = ImmutableList.copyOf(objectives);
84 + this.devices = ImmutableList.copyOf(devices);
70 } 85 }
71 86
72 /** 87 /**
...@@ -74,10 +89,19 @@ public class FlowObjectiveIntent extends Intent { ...@@ -74,10 +89,19 @@ public class FlowObjectiveIntent extends Intent {
74 * 89 *
75 * @return flow objectives 90 * @return flow objectives
76 */ 91 */
77 - Collection<Objective> objectives() { 92 + public List<Objective> objectives() {
78 return objectives; 93 return objectives;
79 } 94 }
80 95
96 + /**
97 + * Returns the list of devices for the flow objectives.
98 + *
99 + * @return devices
100 + */
101 + public List<DeviceId> devices() {
102 + return devices;
103 + }
104 +
81 105
82 @Override 106 @Override
83 public boolean isInstallable() { 107 public boolean isInstallable() {
...@@ -91,7 +115,8 @@ public class FlowObjectiveIntent extends Intent { ...@@ -91,7 +115,8 @@ public class FlowObjectiveIntent extends Intent {
91 .add("key", key()) 115 .add("key", key())
92 .add("appId", appId()) 116 .add("appId", appId())
93 .add("resources", resources()) 117 .add("resources", resources())
94 - .add("objectives", objectives) 118 + .add("device", devices())
119 + .add("objectives", objectives())
95 .toString(); 120 .toString();
96 } 121 }
97 } 122 }
......
...@@ -16,11 +16,13 @@ ...@@ -16,11 +16,13 @@
16 16
17 package org.onosproject.net.intent; 17 package org.onosproject.net.intent;
18 18
19 -import com.google.common.collect.ImmutableSet; 19 +import java.util.Collection;
20 -import com.google.common.testing.EqualsTester; 20 +import java.util.List;
21 +
21 import org.junit.Test; 22 import org.junit.Test;
22 import org.onosproject.core.ApplicationId; 23 import org.onosproject.core.ApplicationId;
23 import org.onosproject.core.DefaultApplicationId; 24 import org.onosproject.core.DefaultApplicationId;
25 +import org.onosproject.net.DeviceId;
24 import org.onosproject.net.NetworkResource; 26 import org.onosproject.net.NetworkResource;
25 import org.onosproject.net.flow.DefaultTrafficSelector; 27 import org.onosproject.net.flow.DefaultTrafficSelector;
26 import org.onosproject.net.flow.DefaultTrafficTreatment; 28 import org.onosproject.net.flow.DefaultTrafficTreatment;
...@@ -30,7 +32,9 @@ import org.onosproject.net.flowobjective.DefaultForwardingObjective; ...@@ -30,7 +32,9 @@ import org.onosproject.net.flowobjective.DefaultForwardingObjective;
30 import org.onosproject.net.flowobjective.ForwardingObjective; 32 import org.onosproject.net.flowobjective.ForwardingObjective;
31 import org.onosproject.net.flowobjective.Objective; 33 import org.onosproject.net.flowobjective.Objective;
32 34
33 -import java.util.Collection; 35 +import com.google.common.collect.ImmutableList;
36 +import com.google.common.collect.ImmutableSet;
37 +import com.google.common.testing.EqualsTester;
34 38
35 import static org.junit.Assert.assertEquals; 39 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertTrue; 40 import static org.junit.Assert.assertTrue;
...@@ -52,8 +56,9 @@ public class FlowObjectiveIntentTest extends IntentTest { ...@@ -52,8 +56,9 @@ public class FlowObjectiveIntentTest extends IntentTest {
52 .withSelector(DefaultTrafficSelector.builder().matchEthType((short) 123).build()) 56 .withSelector(DefaultTrafficSelector.builder().matchEthType((short) 123).build())
53 .withTreatment(DefaultTrafficTreatment.emptyTreatment()) 57 .withTreatment(DefaultTrafficTreatment.emptyTreatment())
54 .withFlag(ForwardingObjective.Flag.VERSATILE).add(); 58 .withFlag(ForwardingObjective.Flag.VERSATILE).add();
55 - private static final Collection<Objective> OBJECTIVES = ImmutableSet.of(FO1, FO2); 59 + private static final List<Objective> OBJECTIVES = ImmutableList.of(FO1, FO2);
56 private static final Collection<NetworkResource> RESOURCES = ImmutableSet.of(); 60 private static final Collection<NetworkResource> RESOURCES = ImmutableSet.of();
61 + private static final List<DeviceId> DEVICE = ImmutableList.of(DeviceId.NONE, DeviceId.NONE);
57 62
58 /** 63 /**
59 * Tests basics of construction and getters. 64 * Tests basics of construction and getters.
...@@ -61,7 +66,7 @@ public class FlowObjectiveIntentTest extends IntentTest { ...@@ -61,7 +66,7 @@ public class FlowObjectiveIntentTest extends IntentTest {
61 @Test 66 @Test
62 public void basics() { 67 public void basics() {
63 FlowObjectiveIntent intent = 68 FlowObjectiveIntent intent =
64 - new FlowObjectiveIntent(APP_ID, KEY, OBJECTIVES, RESOURCES); 69 + new FlowObjectiveIntent(APP_ID, KEY, DEVICE, OBJECTIVES, RESOURCES);
65 assertEquals("incorrect app id", APP_ID, intent.appId()); 70 assertEquals("incorrect app id", APP_ID, intent.appId());
66 assertEquals("incorrect key", KEY, intent.key()); 71 assertEquals("incorrect key", KEY, intent.key());
67 assertEquals("incorrect objectives", OBJECTIVES, intent.objectives()); 72 assertEquals("incorrect objectives", OBJECTIVES, intent.objectives());
...@@ -89,11 +94,11 @@ public class FlowObjectiveIntentTest extends IntentTest { ...@@ -89,11 +94,11 @@ public class FlowObjectiveIntentTest extends IntentTest {
89 94
90 @Override 95 @Override
91 protected Intent createOne() { 96 protected Intent createOne() {
92 - return new FlowObjectiveIntent(APP_ID, OBJECTIVES, RESOURCES); 97 + return new FlowObjectiveIntent(APP_ID, DEVICE, OBJECTIVES, RESOURCES);
93 } 98 }
94 99
95 @Override 100 @Override
96 protected Intent createAnother() { 101 protected Intent createAnother() {
97 - return new FlowObjectiveIntent(APP_ID, OBJECTIVES, RESOURCES); 102 + return new FlowObjectiveIntent(APP_ID, DEVICE, OBJECTIVES, RESOURCES);
98 } 103 }
99 -}
...\ No newline at end of file ...\ No newline at end of file
104 +}
......
1 +/*
2 + * Copyright 2016 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.compiler;
17 +
18 +import java.util.LinkedList;
19 +import java.util.List;
20 +import java.util.Set;
21 +
22 +import org.apache.felix.scr.annotations.Activate;
23 +import org.apache.felix.scr.annotations.Component;
24 +import org.apache.felix.scr.annotations.Deactivate;
25 +import org.apache.felix.scr.annotations.Reference;
26 +import org.apache.felix.scr.annotations.ReferenceCardinality;
27 +import org.onosproject.core.ApplicationId;
28 +import org.onosproject.core.CoreService;
29 +import org.onosproject.net.ConnectPoint;
30 +import org.onosproject.net.DeviceId;
31 +import org.onosproject.net.flow.DefaultTrafficSelector;
32 +import org.onosproject.net.flow.DefaultTrafficTreatment;
33 +import org.onosproject.net.flow.TrafficSelector;
34 +import org.onosproject.net.flow.TrafficTreatment;
35 +import org.onosproject.net.flowobjective.DefaultForwardingObjective;
36 +import org.onosproject.net.flowobjective.ForwardingObjective;
37 +import org.onosproject.net.flowobjective.Objective;
38 +import org.onosproject.net.intent.FlowObjectiveIntent;
39 +import org.onosproject.net.intent.Intent;
40 +import org.onosproject.net.intent.IntentCompiler;
41 +import org.onosproject.net.intent.IntentExtensionService;
42 +import org.onosproject.net.intent.PathIntent;
43 +import org.onosproject.net.newresource.ResourceService;
44 +import org.onosproject.net.resource.link.LinkResourceAllocations;
45 +import org.slf4j.Logger;
46 +
47 +import com.google.common.collect.ImmutableList;
48 +
49 +import static org.slf4j.LoggerFactory.getLogger;
50 +
51 +@Component(immediate = true)
52 +public class PathIntentFlowObjectiveCompiler
53 + extends PathCompiler<Objective>
54 + implements IntentCompiler<PathIntent>,
55 + PathCompiler.PathCompilerCreateFlow<Objective> {
56 +
57 + private final Logger log = getLogger(getClass());
58 +
59 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 + protected CoreService coreService;
61 +
62 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 + protected IntentExtensionService intentManager;
64 +
65 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 + protected ResourceService resourceService;
67 +
68 + private ApplicationId appId;
69 +
70 + @Activate
71 + public void activate() {
72 + appId = coreService.registerApplication("org.onosproject.net.intent");
73 + //intentManager.registerCompiler(PathIntent.class, this);
74 + }
75 +
76 + @Deactivate
77 + public void deactivate() {
78 + //intentManager.unregisterCompiler(PathIntent.class);
79 + }
80 +
81 + @Override
82 + public List<Intent> compile(PathIntent intent, List<Intent> installable,
83 + Set<LinkResourceAllocations> resources) {
84 +
85 + List<Objective> objectives = new LinkedList<>();
86 + List<DeviceId> devices = new LinkedList<>();
87 + compile(this, intent, objectives, devices);
88 +
89 + return ImmutableList.of(new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
90 + }
91 +
92 + @Override
93 + public Logger log() {
94 + return log;
95 + }
96 +
97 + @Override
98 + public ResourceService resourceService() {
99 + return resourceService;
100 + }
101 +
102 + @Override
103 + public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
104 + ConnectPoint ingress, ConnectPoint egress,
105 + int priority, boolean applyTreatment,
106 + List<Objective> objectives,
107 + List<DeviceId> devices) {
108 + TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
109 + .matchInPort(ingress.port())
110 + .build();
111 +
112 + TrafficTreatment.Builder treatmentBuilder;
113 + if (applyTreatment) {
114 + treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
115 + } else {
116 + treatmentBuilder = DefaultTrafficTreatment.builder();
117 + }
118 + TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
119 +
120 + objectives.add(DefaultForwardingObjective.builder()
121 + .withSelector(selector)
122 + .withTreatment(treatment)
123 + .withPriority(priority)
124 + .fromApp(appId)
125 + .makePermanent()
126 + .withFlag(ForwardingObjective.Flag.SPECIFIC)
127 + .add());
128 + devices.add(ingress.deviceId());
129 + }
130 +}