Xin Jin
Committed by Brian O'Connor

Adding experimental Flow Objective composition

Change-Id: I35e4414845ad5034145157ab83f933e40f75a1d9
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.cli.net;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.net.flowobjective.FlowObjectiveService;
22 +
23 +/**
24 + * Manages FlowObjectiveComposition policy.
25 + */
26 +@Command(scope = "onos", name = "policy",
27 + description = "Manages FlowObjectiveComposition policy")
28 +public class FlowObjectiveCompositionCommand extends AbstractShellCommand {
29 +
30 + @Argument(index = 0, name = "command",
31 + description = "Command name (install)",
32 + required = true, multiValued = false)
33 + String command = null;
34 +
35 + @Argument(index = 1, name = "names", description = "policy string",
36 + required = true, multiValued = true)
37 + String[] policies = null;
38 +
39 + @Override
40 + protected void execute() {
41 + FlowObjectiveService service = get(FlowObjectiveService.class);
42 + service.initPolicy(policies[0]);
43 + print("Policy %s installed", policies[0]);
44 + }
45 +}
...@@ -30,6 +30,10 @@ ...@@ -30,6 +30,10 @@
30 </command> 30 </command>
31 31
32 <command> 32 <command>
33 + <action class="org.onosproject.cli.net.FlowObjectiveCompositionCommand"/>
34 + </command>
35 +
36 + <command>
33 <action class="org.onosproject.cli.app.ApplicationsListCommand"/> 37 <action class="org.onosproject.cli.app.ApplicationsListCommand"/>
34 </command> 38 </command>
35 39
......
...@@ -56,4 +56,10 @@ public interface FlowObjectiveService { ...@@ -56,4 +56,10 @@ public interface FlowObjectiveService {
56 */ 56 */
57 int allocateNextId(); 57 int allocateNextId();
58 58
59 + /**
60 + * Installs the filtering rules onto the specified device.
61 + *
62 + * @param policy policy expression
63 + */
64 + void initPolicy(String policy);
59 } 65 }
......
...@@ -218,6 +218,9 @@ public class FlowObjectiveManager implements FlowObjectiveService { ...@@ -218,6 +218,9 @@ public class FlowObjectiveManager implements FlowObjectiveService {
218 return flowObjectiveStore.allocateNextId(); 218 return flowObjectiveStore.allocateNextId();
219 } 219 }
220 220
221 + @Override
222 + public void initPolicy(String policy) {}
223 +
221 private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) { 224 private boolean queueObjective(DeviceId deviceId, ForwardingObjective fwd) {
222 if (fwd.nextId() != null && 225 if (fwd.nextId() != null &&
223 flowObjectiveStore.getNextGroup(fwd.nextId()) == null) { 226 flowObjectiveStore.getNextGroup(fwd.nextId()) == null) {
......
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.flowobjective.impl.composition;
17 +
18 +import org.onosproject.net.flowobjective.FilteringObjective;
19 +
20 +import java.util.ArrayList;
21 +import java.util.HashMap;
22 +import java.util.List;
23 +import java.util.Map;
24 +
25 +/**
26 + * Provides a table to store Fitler.
27 + */
28 +public class FilterTable {
29 +
30 + protected Map<Integer, FilteringObjective> filterMap;
31 +
32 + public FilterTable() {
33 + this.filterMap = new HashMap<>();
34 + }
35 +
36 + public List<FilteringObjective> updateFilter(FilteringObjective filteringObjective) {
37 + List<FilteringObjective> updates = new ArrayList<>();
38 + switch (filteringObjective.op()) {
39 + case ADD:
40 + this.filterMap.put(filteringObjective.id(), filteringObjective);
41 + updates.add(filteringObjective);
42 + break;
43 + case REMOVE:
44 + this.filterMap.remove(filteringObjective.id());
45 + updates.add(filteringObjective);
46 + break;
47 + default:
48 + break;
49 + }
50 + return updates;
51 + }
52 +
53 + public List<FilteringObjective> updateFilter(List<FilteringObjective> filteringObjectives) {
54 + List<FilteringObjective> updates = new ArrayList<>();
55 + for (FilteringObjective filteringObjective : filteringObjectives) {
56 + updates.addAll(this.updateFilter(filteringObjective));
57 + }
58 + return updates;
59 + }
60 +
61 +}
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.flowobjective.impl.composition;
17 +
18 +import org.onosproject.net.flowobjective.DefaultForwardingObjective;
19 +import org.onosproject.net.flowobjective.ForwardingObjective;
20 +
21 +import java.util.ArrayList;
22 +import java.util.Collection;
23 +import java.util.HashMap;
24 +import java.util.List;
25 +import java.util.Map;
26 +import java.util.Objects;
27 +
28 +/**
29 + * Provides a table to store Forward.
30 + */
31 +public class ForwardTable {
32 +
33 + protected Map<Integer, ForwardingObjective> forwardMap;
34 + protected Map<Integer, List<ForwardingObjective>> generatedParentForwardingObjectiveMap;
35 +
36 + public ForwardTable() {
37 + this.forwardMap = new HashMap<>();
38 + this.generatedParentForwardingObjectiveMap = new HashMap<>();
39 + }
40 +
41 + public ForwardUpdateTable updateForward(ForwardingObjective forwardingObjective) {
42 + ForwardUpdateTable updates = new ForwardUpdateTable();
43 + switch (forwardingObjective.op()) {
44 + case ADD:
45 + this.forwardMap.put(forwardingObjectiveHash(forwardingObjective), forwardingObjective);
46 + this.generatedParentForwardingObjectiveMap
47 + .put(forwardingObjectiveHash(forwardingObjective), new ArrayList<>());
48 + updates.addObjectives.add(forwardingObjective);
49 + break;
50 + case REMOVE:
51 + if (this.forwardMap.remove(forwardingObjectiveHash(forwardingObjective)) != null) {
52 + updates.removeObjectives.add(forwardingObjective);
53 + }
54 + break;
55 + default:
56 + break;
57 + }
58 + return updates;
59 + }
60 +
61 + public ForwardUpdateTable updateForward(List<ForwardingObjective> forwardingObjectives) {
62 + ForwardUpdateTable updates = new ForwardUpdateTable();
63 + for (ForwardingObjective forwardingObjective : forwardingObjectives) {
64 + updates.addUpdateTable(this.updateForward(forwardingObjective));
65 + }
66 + return updates;
67 + }
68 +
69 + public void addGeneratedParentForwardingObjective(ForwardingObjective child, ForwardingObjective parent) {
70 + this.generatedParentForwardingObjectiveMap.get(forwardingObjectiveHash(child)).add(parent);
71 + }
72 +
73 + public void deleteGeneratedParentForwardingObjective(List<ForwardingObjective> children) {
74 + for (ForwardingObjective fo : children) {
75 + this.generatedParentForwardingObjectiveMap.remove(forwardingObjectiveHash(fo));
76 + }
77 + }
78 +
79 + private List<ForwardingObjective> getGeneratedParentForwardingObjective(ForwardingObjective child) {
80 + return this.generatedParentForwardingObjectiveMap.get(forwardingObjectiveHash(child));
81 + }
82 +
83 + public List<ForwardingObjective> getGeneratedParentForwardingObjectiveForRemove(ForwardingObjective child) {
84 + List<ForwardingObjective> fos = this.generatedParentForwardingObjectiveMap.get(forwardingObjectiveHash(child));
85 + List<ForwardingObjective> removeFos = new ArrayList<>();
86 + for (ForwardingObjective fo : fos) {
87 + removeFos.add(DefaultForwardingObjective.builder()
88 + .fromApp(fo.appId())
89 + .makePermanent()
90 + .withFlag(fo.flag())
91 + .withPriority(fo.priority())
92 + .withSelector(fo.selector())
93 + .withTreatment(fo.treatment())
94 + .remove());
95 + }
96 + return removeFos;
97 + }
98 +
99 + public Collection<ForwardingObjective> getForwardingObjectives() {
100 + return this.forwardMap.values();
101 + }
102 +
103 + public static int forwardingObjectiveHash(ForwardingObjective forwardingObjective) {
104 + return Objects.hash(forwardingObjective.selector(), forwardingObjective.flag(),
105 + forwardingObjective.permanent(), forwardingObjective.timeout(),
106 + forwardingObjective.appId(), forwardingObjective.priority(),
107 + forwardingObjective.nextId(), forwardingObjective.treatment());
108 + }
109 +}
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.flowobjective.impl.composition;
17 +
18 +import org.onosproject.net.flowobjective.ForwardingObjective;
19 +
20 +import java.util.ArrayList;
21 +import java.util.List;
22 +
23 +/**
24 + * Provides an update table for Forward.
25 + */
26 +public class ForwardUpdateTable {
27 + public List<ForwardingObjective> addObjectives;
28 + public List<ForwardingObjective> removeObjectives;
29 +
30 + public ForwardUpdateTable() {
31 + this.addObjectives = new ArrayList<>();
32 + this.removeObjectives = new ArrayList<>();
33 + }
34 +
35 + public void addUpdateTable(ForwardUpdateTable updateTable) {
36 + this.addObjectives.addAll(updateTable.addObjectives);
37 + this.removeObjectives.addAll(updateTable.removeObjectives);
38 + }
39 +
40 + public List<ForwardingObjective> toForwardingObjectiveList() {
41 + List<ForwardingObjective> forwardingObjectives = new ArrayList<>();
42 + forwardingObjectives.addAll(this.addObjectives);
43 + forwardingObjectives.addAll(this.removeObjectives);
44 + return forwardingObjectives;
45 + }
46 +}
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.flowobjective.impl.composition;
17 +
18 +import org.onosproject.net.flowobjective.NextObjective;
19 +
20 +import java.util.ArrayList;
21 +import java.util.HashMap;
22 +import java.util.List;
23 +import java.util.Map;
24 +
25 +/**
26 + * Provides a table to store Next.
27 + */
28 +public class NextTable {
29 +
30 + protected Map<Integer, NextObjective> nextMap;
31 +
32 + public NextTable() {
33 + this.nextMap = new HashMap<>();
34 + }
35 +
36 + public List<NextObjective> updateNext(NextObjective nextObjective) {
37 + List<NextObjective> updates = new ArrayList<>();
38 + switch (nextObjective.op()) {
39 + case ADD:
40 + this.nextMap.put(nextObjective.id(), nextObjective);
41 + updates.add(nextObjective);
42 + break;
43 + case REMOVE:
44 + this.nextMap.remove(nextObjective.id());
45 + updates.add(nextObjective);
46 + break;
47 + default:
48 + break;
49 + }
50 + return updates;
51 + }
52 +
53 + public List<NextObjective> updateNext(List<NextObjective> nextObjectives) {
54 + List<NextObjective> updates = new ArrayList<>();
55 + for (NextObjective nextObjective : nextObjectives) {
56 + updates.addAll(this.updateNext(nextObjective));
57 + }
58 + return updates;
59 + }
60 +
61 +}