alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame^] | 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; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.net.flow.TrafficTreatment; |
| 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.List; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Default implementation of a next objective. |
| 30 | */ |
| 31 | public final class DefaultNextObjective implements NextObjective { |
| 32 | |
| 33 | private final List<TrafficTreatment> treatments; |
| 34 | private final ApplicationId appId; |
| 35 | private final Type type; |
| 36 | private final Integer id; |
| 37 | |
| 38 | private DefaultNextObjective(Integer id, List<TrafficTreatment> treatments, |
| 39 | ApplicationId appId, Type type) { |
| 40 | this.treatments = treatments; |
| 41 | this.appId = appId; |
| 42 | this.type = type; |
| 43 | this.id = id; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public Collection<TrafficTreatment> next() { |
| 48 | return treatments; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public Type type() { |
| 53 | return type; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public int id() { |
| 58 | return id; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public int priority() { |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public ApplicationId appId() { |
| 68 | return appId; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int timeout() { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean permanent() { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Operation op() { |
| 83 | throw new UnsupportedOperationException("Next Objective has no operation"); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns a new builder. |
| 88 | * |
| 89 | * @return new builder |
| 90 | */ |
| 91 | public static Builder builder() { |
| 92 | return new Builder(); |
| 93 | } |
| 94 | |
| 95 | public static final class Builder implements NextObjective.Builder { |
| 96 | |
| 97 | private ApplicationId appId; |
| 98 | private Type type; |
| 99 | private Integer id; |
| 100 | |
| 101 | private final ImmutableList.Builder<TrafficTreatment> listBuilder |
| 102 | = ImmutableList.builder(); |
| 103 | |
| 104 | |
| 105 | |
| 106 | @Override |
| 107 | public NextObjective.Builder withId(int nextId) { |
| 108 | this.id = nextId; |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Builder withType(Type type) { |
| 114 | this.type = type; |
| 115 | return this; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Builder addTreatment(TrafficTreatment treatment) { |
| 120 | listBuilder.add(treatment); |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Noop. This method has no effect. |
| 126 | * |
| 127 | * @param timeout a timeout |
| 128 | * @return a next objective builder |
| 129 | */ |
| 130 | @Override |
| 131 | public Builder makeTemporary(int timeout) { |
| 132 | return this; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Noop. This method has no effect. |
| 137 | * |
| 138 | * @return a next objective builder |
| 139 | */ |
| 140 | @Override |
| 141 | public Builder makePermanent() { |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Builder fromApp(ApplicationId appId) { |
| 147 | this.appId = appId; |
| 148 | return this; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Noop. This method has no effect. |
| 153 | * |
| 154 | * @param priority an integer |
| 155 | * @return a next objective builder |
| 156 | */ |
| 157 | @Override |
| 158 | public Builder withPriority(int priority) { |
| 159 | return this; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public NextObjective build() { |
| 164 | List<TrafficTreatment> treatments = listBuilder.build(); |
| 165 | checkNotNull(appId, "Must supply an application id"); |
| 166 | checkNotNull(id, "id cannot be null"); |
| 167 | checkNotNull(type, "The type cannot be null"); |
| 168 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 169 | |
| 170 | return new DefaultNextObjective(id, treatments, appId, type); |
| 171 | } |
| 172 | } |
| 173 | } |