blob: 24be4efd94085eb9c4a9bcfcc61eca4c20cc4155 [file] [log] [blame]
Author Namee252a002016-09-26 22:42:24 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Author Namee252a002016-09-26 22:42:24 +05303 *
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 */
16package org.onosproject.net.intent.util;
17
18import org.onosproject.net.DeviceId;
19import org.onosproject.net.flow.DefaultFlowRule;
20import org.onosproject.net.flow.FlowEntry;
21import org.onosproject.net.flow.FlowRule;
22import org.onosproject.net.flow.FlowRuleService;
23import org.onosproject.net.flowobjective.DefaultForwardingObjective;
24import org.onosproject.net.flowobjective.DefaultNextObjective;
25import org.onosproject.net.flowobjective.ForwardingObjective;
26import org.onosproject.net.flowobjective.NextObjective;
27import org.onosproject.net.flowobjective.Objective;
28import org.onosproject.net.intent.FlowObjectiveIntent;
29import org.onosproject.net.intent.FlowRuleIntent;
30import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentService;
32
33import java.util.ArrayList;
34import java.util.Collection;
35import java.util.Iterator;
36import java.util.List;
37
38/**
39 * Utility to get flow entries corresponding to specified intent.
40 */
41public class IntentFilter {
42
43 private final IntentService intentService;
44 private final FlowRuleService flowRuleService;
45
46 /**
47 * Creates an intent filter.
48 *
49 * @param intentService intent service object
50 * @param flowRuleService flow service object
51 */
52 public IntentFilter(IntentService intentService,
53 FlowRuleService flowRuleService) {
54 this.intentService = intentService;
55 this.flowRuleService = flowRuleService;
56 }
57
58 //checks whether the collection is empty or not.
59 private boolean nonEmpty(Collection<?> c) {
60 return c != null && !c.isEmpty();
61 }
62
63 /**
64 * Finds all path (flow entries) corresponding to intent installables.
65 *
66 * @param installables set of installables
67 * @return set of flow entries
68 */
69 public List<List<FlowEntry>> readIntentFlows(List<Intent> installables) {
70 List<List<FlowEntry>> paths = new ArrayList<>();
71
72 for (Intent installable : installables) {
73
74 if (installable instanceof FlowRuleIntent) {
75 List<FlowEntry> flowEntries =
76 getFlowEntries((FlowRuleIntent) installable);
77 if (nonEmpty(flowEntries)) {
78 paths.add(flowEntries);
79 }
80
81 } else if (installable instanceof FlowObjectiveIntent) {
82 List<FlowEntry> flowEntries = getFlowEntries(
83 (FlowObjectiveIntent) installable);
84 if (nonEmpty(flowEntries)) {
85 paths.add(flowEntries);
86 }
87 }
88 }
89 return paths;
90 }
91
92 /**
93 * Finds all flow entries created by FlowRuleIntent.
94 *
95 * @param intent FlowRuleIntent Object
96 * @return set of flow entries created by FlowRuleIntent
97 */
98 private List<FlowEntry> getFlowEntries(FlowRuleIntent intent) {
99 List<FlowEntry> flowEntries = new ArrayList<>();
100 Collection<FlowRule> flowRules = intent.flowRules();
101 FlowEntry flowEntry;
102
103 for (FlowRule flowRule : flowRules) {
104 flowEntry = getFlowEntry(flowRule);
105
106 if (flowEntry != null) {
107 flowEntries.add(flowEntry);
108 }
109 }
110 return flowEntries;
111 }
112
113 /**
114 * Finds all flow entries created by FlowObjectiveIntent.
115 *
116 * @param intent FlowObjectiveIntent Object
117 * @return set of flow entries created by FlowObjectiveIntent
118 */
119 private List<FlowEntry> getFlowEntries(FlowObjectiveIntent intent) {
120 List<FlowEntry> flowEntries = new ArrayList<>();
121 Iterator<Objective> objectives = intent.objectives().iterator();
122 Iterator<DeviceId> devices = intent.devices().iterator();
123 DefaultNextObjective nextObjective = null;
124 DefaultForwardingObjective forwardObjective;
125 Objective objective;
126 DeviceId deviceId;
127 FlowEntry flowEntry;
128
129 while (objectives.hasNext()) {
130 objective = objectives.next();
131 deviceId = devices.next();
132
133 if (objective instanceof NextObjective) {
134 nextObjective = (DefaultNextObjective) objective;
Author Namee252a002016-09-26 22:42:24 +0530135 } else if (objective instanceof ForwardingObjective) {
136 forwardObjective = (DefaultForwardingObjective) objective;
Ray Milkeyfd4f8d32018-01-17 15:24:52 -0800137 FlowRule.Builder builder = DefaultFlowRule.builder()
Author Namee252a002016-09-26 22:42:24 +0530138 .forDevice(deviceId)
139 .withSelector(forwardObjective.selector())
Author Namee252a002016-09-26 22:42:24 +0530140 .withPriority(intent.priority())
141 .fromApp(intent.appId())
Ray Milkeyfd4f8d32018-01-17 15:24:52 -0800142 .makePermanent();
143 if (nextObjective != null) {
144 builder.withTreatment(nextObjective.next().iterator().next());
145 }
146 FlowRule flowRule = builder.build();
Author Namee252a002016-09-26 22:42:24 +0530147 flowEntry = getFlowEntry(flowRule);
148
149 if (flowEntry != null) {
150 flowEntries.add(flowEntry);
151 }
152 }
153 }
154 return flowEntries;
155 }
156
157 /**
158 * Finds FlowEntry matching with the FlowRule.
159 *
160 * @param flowRule FlowRule object
161 * @return flow entry matching to FlowRule
162 */
163 private FlowEntry getFlowEntry(FlowRule flowRule) {
164 Iterable<FlowEntry> flowEntries =
165 flowRuleService.getFlowEntries(flowRule.deviceId());
166
167 if (flowEntries != null) {
168 for (FlowEntry entry : flowEntries) {
169 if (entry.exactMatch(flowRule)) {
170 return entry;
171 }
172 }
173 }
174 return null;
175 }
176
177}