blob: 5c98fee107297697e6b3f0162db2827d1d629fe6 [file] [log] [blame]
Jian Li95edb592017-01-29 08:42:07 +09001/*
2 * Copyright 2017-present 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 */
16package org.onosproject.mapping;
17
18import org.onosproject.core.ApplicationId;
19import org.onosproject.net.DeviceId;
20
Jian Lia0bf4592017-02-03 17:43:21 +090021import java.util.Objects;
22
23import static com.google.common.base.MoreObjects.toStringHelper;
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
Jian Li95edb592017-01-29 08:42:07 +090027/**
28 * Default implementation class for mapping.
29 */
30public class DefaultMapping implements Mapping {
Jian Lia0bf4592017-02-03 17:43:21 +090031
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 Li95edb592017-01-29 08:42:07 +090070 @Override
71 public MappingId id() {
Jian Lia0bf4592017-02-03 17:43:21 +090072 return id;
Jian Li95edb592017-01-29 08:42:07 +090073 }
74
75 @Override
76 public short appId() {
Jian Lia0bf4592017-02-03 17:43:21 +090077 return appId;
Jian Li95edb592017-01-29 08:42:07 +090078 }
79
80 @Override
81 public DeviceId deviceId() {
Jian Lia0bf4592017-02-03 17:43:21 +090082 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 Li95edb592017-01-29 08:42:07 +0900123 }
124
125 public static final class Builder implements Mapping.Builder {
126
Jian Lia0bf4592017-02-03 17:43:21 +0900127 private MappingId id;
128 private ApplicationId appId;
129 private DeviceId deviceId;
130 private MappingKey key = new DefaultMappingKey();
131 private MappingValue value = new DefaultMappingValue();
132
133 @Override
134 public Mapping.Builder withId(long id) {
135 this.id = MappingId.valueOf(id);
136 return this;
137 }
138
Jian Li95edb592017-01-29 08:42:07 +0900139 @Override
140 public Mapping.Builder fromApp(ApplicationId appId) {
Jian Lia0bf4592017-02-03 17:43:21 +0900141 this.appId = appId;
142 return this;
Jian Li95edb592017-01-29 08:42:07 +0900143 }
144
145 @Override
146 public Mapping.Builder forDevice(DeviceId deviceId) {
Jian Lia0bf4592017-02-03 17:43:21 +0900147 this.deviceId = deviceId;
148 return this;
149 }
150
151 @Override
152 public Mapping.Builder withKey(MappingKey key) {
153 this.key = key;
154 return this;
155 }
156
157 @Override
158 public Mapping.Builder withValue(MappingValue value) {
159 this.value = value;
160 return this;
Jian Li95edb592017-01-29 08:42:07 +0900161 }
162
163 @Override
164 public Mapping build() {
Jian Lia0bf4592017-02-03 17:43:21 +0900165
166 checkArgument((id != null) ^ (appId != null), "Either an application" +
167 " id or a mapping id must be supplied");
168 checkNotNull(key, "Mapping key cannot be null");
169 checkNotNull(deviceId, "Must refer to a device");
170
171 return new DefaultMapping(deviceId, key, value, id);
Jian Li95edb592017-01-29 08:42:07 +0900172 }
173 }
174}