hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.odtn.utils.tapi; |
| 18 | |
| 19 | import java.util.UUID; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | |
| 22 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 23 | import static com.google.common.base.Objects.equal; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 25 | import static java.util.Objects.hash; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 26 | import static org.onosproject.odtn.utils.tapi.TapiObjectHandler.DEVICE_ID; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 27 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import 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 | */ |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 37 | public class TapiNodeRef { |
| 38 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 39 | protected final Logger log = getLogger(getClass()); |
| 40 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 41 | private final UUID topologyId; |
| 42 | private final UUID nodeId; |
| 43 | private DeviceId deviceId; |
| 44 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 45 | TapiNodeRef(String topologyId, String nodeId) { |
| 46 | 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 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 54 | public String getNodeId() { |
| 55 | return nodeId.toString(); |
| 56 | } |
| 57 | |
| 58 | public DeviceId getDeviceId() { |
| 59 | return deviceId; |
| 60 | } |
| 61 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 62 | public TapiNodeRef setDeviceId(DeviceId deviceId) { |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 63 | this.deviceId = deviceId; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 64 | return this; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 65 | } |
| 66 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 67 | /** |
| 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 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 88 | public String toString() { |
| 89 | return toStringHelper(getClass()) |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 90 | .add("topologyId", topologyId) |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 91 | .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 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 114 | |
| 115 | } |