blob: 6422f401fd8b6212ff9ddd91e2e1886d5bd74bf3 [file] [log] [blame]
Jian Li95edb592017-01-29 08:42:07 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li95edb592017-01-29 08:42:07 +09003 *
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
Jian Li98763102017-02-19 23:48:46 +0900125 /**
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 Li95edb592017-01-29 08:42:07 +0900137 public static final class Builder implements Mapping.Builder {
138
Jian Lia0bf4592017-02-03 17:43:21 +0900139 private MappingId id;
140 private ApplicationId appId;
141 private DeviceId deviceId;
Jian Li44155b02017-02-15 17:03:38 +0900142 private MappingKey key = DefaultMappingKey.builder().build();
143 private MappingValue value = DefaultMappingValue.builder().build();
Jian Lia0bf4592017-02-03 17:43:21 +0900144
145 @Override
146 public Mapping.Builder withId(long id) {
147 this.id = MappingId.valueOf(id);
148 return this;
149 }
150
Jian Li95edb592017-01-29 08:42:07 +0900151 @Override
152 public Mapping.Builder fromApp(ApplicationId appId) {
Jian Lia0bf4592017-02-03 17:43:21 +0900153 this.appId = appId;
154 return this;
Jian Li95edb592017-01-29 08:42:07 +0900155 }
156
157 @Override
158 public Mapping.Builder forDevice(DeviceId deviceId) {
Jian Lia0bf4592017-02-03 17:43:21 +0900159 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 Li95edb592017-01-29 08:42:07 +0900173 }
174
175 @Override
176 public Mapping build() {
Jian Lia0bf4592017-02-03 17:43:21 +0900177
Jian Li2dc9f002017-03-03 04:26:31 +0900178 checkArgument((id != null) || (appId != null), "Either an application" +
Jian Lia0bf4592017-02-03 17:43:21 +0900179 " 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 Li95edb592017-01-29 08:42:07 +0900184 }
185 }
186}