Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 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.mapping; |
| 17 | |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.onosproject.mapping.actions.MappingAction; |
| 22 | import org.onosproject.mapping.actions.MappingActions; |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 23 | |
| 24 | import java.util.List; |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 28 | |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 29 | /** |
| 30 | * Default mapping value implementation. |
| 31 | */ |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 32 | public final class DefaultMappingValue implements MappingValue { |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 33 | |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 34 | private final MappingAction action; |
| 35 | private final List<MappingTreatment> treatments; |
| 36 | |
| 37 | /** |
| 38 | * Creates a new mapping value from the specified mapping action and a |
| 39 | * list of mapping treatments. |
| 40 | * |
| 41 | * @param action mapping action |
| 42 | * @param treatments a collection of mapping treatment |
| 43 | */ |
| 44 | private DefaultMappingValue(MappingAction action, |
| 45 | List<MappingTreatment> treatments) { |
| 46 | this.action = action; |
| 47 | this.treatments = ImmutableList.copyOf(checkNotNull(treatments)); |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 51 | public MappingAction action() { |
| 52 | return action; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public List<MappingTreatment> treatments() { |
| 57 | return ImmutableList.copyOf(treatments); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public int hashCode() { |
| 62 | return Objects.hash(action, treatments); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public boolean equals(Object obj) { |
| 67 | if (this == obj) { |
| 68 | return true; |
| 69 | } |
| 70 | if (obj instanceof DefaultMappingValue) { |
| 71 | DefaultMappingValue that = (DefaultMappingValue) obj; |
| 72 | return Objects.equals(action, that.action) && |
| 73 | Objects.equals(treatments, that.treatments); |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public String toString() { |
| 80 | return MoreObjects.toStringHelper(getClass()) |
| 81 | .add("action", action) |
| 82 | .add("treatments", treatments) |
| 83 | .toString(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns a new mapping value builder. |
| 88 | * |
| 89 | * @return mapping value builder |
| 90 | */ |
| 91 | public static MappingValue.Builder builder() { |
| 92 | return new DefaultMappingValue.Builder(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns a new mapping value builder primed to produce entities |
| 97 | * patterned after the supplied mapping value. |
| 98 | * |
| 99 | * @param value base mapping value |
| 100 | * @return mapping value builder |
| 101 | */ |
| 102 | public static MappingValue.Builder builder(MappingValue value) { |
| 103 | return new Builder(value); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Builds a mapping value. |
| 108 | */ |
| 109 | public static final class Builder implements MappingValue.Builder { |
| 110 | |
| 111 | private MappingAction action; |
| 112 | private List<MappingTreatment> treatments = Lists.newArrayList(); |
| 113 | |
| 114 | // creates a new builder |
| 115 | private Builder() { |
| 116 | } |
| 117 | |
| 118 | // creates a new builder based off an existing mapping value |
| 119 | private Builder(MappingValue value) { |
| 120 | value.treatments().forEach(t -> treatments.add(t)); |
| 121 | action = value.action(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Builder withAction(MappingAction action) { |
| 126 | this.action = action; |
| 127 | return this; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Builder add(MappingTreatment treatment) { |
| 132 | treatments.add(treatment); |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public MappingValue build() { |
| 138 | |
| 139 | // if no action has been specified, we simply assign noAction |
| 140 | if (action == null) { |
| 141 | action = MappingActions.noAction(); |
| 142 | } |
| 143 | |
| 144 | // FIXME: we will check the number of treatment later |
| 145 | // checkArgument(treatments.size() >= 1, |
| 146 | // "Must specify more than one mapping treatment"); |
| 147 | |
| 148 | return new DefaultMappingValue(action, treatments); |
| 149 | } |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 150 | } |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 151 | } |