blob: 71cd256ff7687e5343208858c3e11493b2c1ebbe [file] [log] [blame]
hirokiec18d3a2018-05-16 15:27:37 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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
17package org.onosproject.odtn.utils.tapi;
18
19import java.util.UUID;
20import org.onosproject.net.DeviceId;
21
22import static com.google.common.base.MoreObjects.toStringHelper;
23import static com.google.common.base.Objects.equal;
hirokif4ed5212018-05-26 22:39:38 -070024import static com.google.common.base.Preconditions.checkNotNull;
hirokiec18d3a2018-05-16 15:27:37 -070025import static java.util.Objects.hash;
hirokif4ed5212018-05-26 22:39:38 -070026import static org.onosproject.odtn.utils.tapi.TapiObjectHandler.DEVICE_ID;
hirokiec18d3a2018-05-16 15:27:37 -070027
hirokif4ed5212018-05-26 22:39:38 -070028import org.slf4j.Logger;
29import static org.slf4j.LoggerFactory.getLogger;
30
31/**
32 * TAPI Node reference class.
33 *
34 * TAPI reference class should be used in ODTN ServiceApplication
35 * in order to make independent ServiceApplication implementation from DCS.
36 */
hirokifca084b2018-06-02 08:29:42 -070037public final class TapiNodeRef {
hirokiec18d3a2018-05-16 15:27:37 -070038
hirokif4ed5212018-05-26 22:39:38 -070039 protected final Logger log = getLogger(getClass());
40
hirokiec18d3a2018-05-16 15:27:37 -070041 private final UUID topologyId;
42 private final UUID nodeId;
43 private DeviceId deviceId;
44
hirokifca084b2018-06-02 08:29:42 -070045 private TapiNodeRef(String topologyId, String nodeId) {
hiroki684aa2f2018-05-19 20:48:49 -070046 this.topologyId = UUID.fromString(topologyId);
47 this.nodeId = UUID.fromString(nodeId);
48 }
49
50 public static TapiNodeRef create(String topologyId, String nodeId) {
51 return new TapiNodeRef(topologyId, nodeId);
52 }
53
hirokiec18d3a2018-05-16 15:27:37 -070054 public String getNodeId() {
55 return nodeId.toString();
56 }
57
58 public DeviceId getDeviceId() {
59 return deviceId;
60 }
61
hiroki684aa2f2018-05-19 20:48:49 -070062 public TapiNodeRef setDeviceId(DeviceId deviceId) {
hirokiec18d3a2018-05-16 15:27:37 -070063 this.deviceId = deviceId;
hiroki684aa2f2018-05-19 20:48:49 -070064 return this;
hirokiec18d3a2018-05-16 15:27:37 -070065 }
66
hirokif4ed5212018-05-26 22:39:38 -070067 /**
68 * Check if this Node matches input filter condition.
69 *
70 * @param key Filter key
71 * @param value Filter value
72 * @return If match or not
73 */
74 public boolean is(String key, String value) {
75 checkNotNull(value);
76 switch (key) {
77 case DEVICE_ID:
78 if (deviceId == null) {
79 return false;
80 }
81 return value.equals(deviceId.toString());
82 default:
83 log.warn("Unknown key: {}", key);
84 return true;
85 }
86 }
87
hirokiec18d3a2018-05-16 15:27:37 -070088 public String toString() {
89 return toStringHelper(getClass())
hirokif4ed5212018-05-26 22:39:38 -070090 .add("topologyId", topologyId)
hirokiec18d3a2018-05-16 15:27:37 -070091 .add("nodeId", nodeId)
92 .add("deviceId", deviceId)
93 .toString();
94 }
95
96 @Override
97 public boolean equals(Object o) {
98 if (this == o) {
99 return true;
100 }
101 if (!(o instanceof TapiNodeRef)) {
102 return false;
103 }
104 TapiNodeRef nodeRef = (TapiNodeRef) o;
105 return equal(topologyId, nodeRef.topologyId) &&
106 equal(nodeId, nodeRef.nodeId);
107 }
108
109 @Override
110 public int hashCode() {
111 return hash(topologyId, nodeId);
112 }
113
hirokif4ed5212018-05-26 22:39:38 -0700114
115}