Jian Li | 95edb59 | 2017-01-29 08:42:07 +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 | 95edb59 | 2017-01-29 08:42:07 +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 | |
| 18 | import org.onosproject.core.ApplicationId; |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 27 | /** |
| 28 | * Default implementation class for mapping. |
| 29 | */ |
| 30 | public class DefaultMapping implements Mapping { |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 31 | |
| 32 | private final DeviceId deviceId; |
| 33 | private final MappingKey key; |
| 34 | private final MappingValue value; |
| 35 | |
| 36 | private final MappingId id; |
| 37 | |
| 38 | private final Short appId; |
| 39 | |
| 40 | /** |
| 41 | * Creates a mapping specified with mapping information. |
| 42 | * |
| 43 | * @param mapping mapping information |
| 44 | */ |
| 45 | public DefaultMapping(Mapping mapping) { |
| 46 | this.deviceId = mapping.deviceId(); |
| 47 | this.key = mapping.key(); |
| 48 | this.value = mapping.value(); |
| 49 | this.appId = mapping.appId(); |
| 50 | this.id = mapping.id(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates a mapping specified with several parameters. |
| 55 | * |
| 56 | * @param deviceId device identifier |
| 57 | * @param key mapping key |
| 58 | * @param value mapping value |
| 59 | * @param id mapping identifier |
| 60 | */ |
| 61 | public DefaultMapping(DeviceId deviceId, MappingKey key, MappingValue value, |
| 62 | MappingId id) { |
| 63 | this.deviceId = deviceId; |
| 64 | this.key = key; |
| 65 | this.value = value; |
| 66 | this.appId = (short) (id.value() >>> 48); |
| 67 | this.id = id; |
| 68 | } |
| 69 | |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 70 | @Override |
| 71 | public MappingId id() { |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 72 | return id; |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public short appId() { |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 77 | return appId; |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public DeviceId deviceId() { |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 82 | return deviceId; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public MappingKey key() { |
| 87 | return key; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public MappingValue value() { |
| 92 | return value; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public int hashCode() { |
| 97 | return Objects.hash(deviceId, key, value, id); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean equals(Object obj) { |
| 102 | if (this == obj) { |
| 103 | return true; |
| 104 | } |
| 105 | if (obj instanceof DefaultMapping) { |
| 106 | DefaultMapping that = (DefaultMapping) obj; |
| 107 | return Objects.equals(deviceId, that.deviceId) && |
| 108 | Objects.equals(key, that.key) && |
| 109 | Objects.equals(value, that.value) && |
| 110 | Objects.equals(id, that.id); |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String toString() { |
| 117 | return toStringHelper(this) |
| 118 | .add("id", Long.toHexString(id.value())) |
| 119 | .add("deviceId", deviceId) |
| 120 | .add("key", key) |
| 121 | .add("value", value) |
| 122 | .toString(); |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 123 | } |
| 124 | |
Jian Li | 9876310 | 2017-02-19 23:48:46 +0900 | [diff] [blame] | 125 | /** |
| 126 | * Returns a default mapping builder. |
| 127 | * |
| 128 | * @return builder |
| 129 | */ |
| 130 | public static Builder builder() { |
| 131 | return new Builder(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Default mapping builder. |
| 136 | */ |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 137 | public static final class Builder implements Mapping.Builder { |
| 138 | |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 139 | private MappingId id; |
| 140 | private ApplicationId appId; |
| 141 | private DeviceId deviceId; |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 142 | private MappingKey key = DefaultMappingKey.builder().build(); |
| 143 | private MappingValue value = DefaultMappingValue.builder().build(); |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 144 | |
| 145 | @Override |
| 146 | public Mapping.Builder withId(long id) { |
| 147 | this.id = MappingId.valueOf(id); |
| 148 | return this; |
| 149 | } |
| 150 | |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 151 | @Override |
| 152 | public Mapping.Builder fromApp(ApplicationId appId) { |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 153 | this.appId = appId; |
| 154 | return this; |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Mapping.Builder forDevice(DeviceId deviceId) { |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 159 | this.deviceId = deviceId; |
| 160 | return this; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Mapping.Builder withKey(MappingKey key) { |
| 165 | this.key = key; |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Mapping.Builder withValue(MappingValue value) { |
| 171 | this.value = value; |
| 172 | return this; |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Mapping build() { |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 177 | |
Jian Li | 2dc9f00 | 2017-03-03 04:26:31 +0900 | [diff] [blame] | 178 | checkArgument((id != null) || (appId != null), "Either an application" + |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 179 | " id or a mapping id must be supplied"); |
| 180 | checkNotNull(key, "Mapping key cannot be null"); |
| 181 | checkNotNull(deviceId, "Must refer to a device"); |
| 182 | |
| 183 | return new DefaultMapping(deviceId, key, value, id); |
Jian Li | 95edb59 | 2017-01-29 08:42:07 +0900 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |