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 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 19 | import java.util.Collections; |
| 20 | import java.util.List; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 21 | import java.util.Optional; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 22 | import java.util.UUID; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 23 | import java.util.stream.Collectors; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | import static com.google.common.base.Objects.equal; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 29 | import static java.util.Objects.hash; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 30 | import static org.onosproject.odtn.utils.tapi.TapiObjectHandler.DEVICE_ID; |
| 31 | import static org.onosproject.odtn.utils.tapi.TapiObjectHandler.ODTN_PORT_TYPE; |
| 32 | import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.CONNECTION_ID; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 33 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery; |
| 36 | import org.slf4j.Logger; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * TAPI Nep reference class. |
| 41 | * |
| 42 | * TAPI reference class should be used in ODTN ServiceApplication |
| 43 | * in order to make independent ServiceApplication implementation from DCS. |
| 44 | */ |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 45 | public final class TapiNepRef { |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 46 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 47 | protected final Logger log = getLogger(getClass()); |
| 48 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 49 | private final UUID topologyId; |
| 50 | private final UUID nodeId; |
| 51 | private final UUID nepId; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 52 | |
| 53 | // Annotations to be used for reference of related TAPI objects. |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 54 | private UUID sipId = null; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 55 | private List<UUID> cepIds = Collections.emptyList(); |
| 56 | |
| 57 | // Annotations to be used for OpenConfig configuration. |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 58 | private ConnectPoint cp = null; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 59 | private OdtnDeviceDescriptionDiscovery.OdtnPortType portType = null; |
| 60 | private String connectionId = null; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 61 | |
| 62 | TapiNepRef(String topologyId, String nodeId, String nepId) { |
| 63 | this.topologyId = UUID.fromString(topologyId); |
| 64 | this.nodeId = UUID.fromString(nodeId); |
| 65 | this.nepId = UUID.fromString(nepId); |
| 66 | } |
| 67 | |
| 68 | public static TapiNepRef create(String topologyId, String nodeId, String nepId) { |
| 69 | return new TapiNepRef(topologyId, nodeId, nepId); |
| 70 | } |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 71 | |
| 72 | public String getTopologyId() { |
| 73 | return topologyId.toString(); |
| 74 | } |
| 75 | |
| 76 | public String getNodeId() { |
| 77 | return nodeId.toString(); |
| 78 | } |
| 79 | |
| 80 | public String getNepId() { |
| 81 | return nepId.toString(); |
| 82 | } |
| 83 | |
| 84 | public String getSipId() { |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 85 | return Optional.ofNullable(sipId) |
| 86 | .map(UUID::toString) |
| 87 | .orElse(null); |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 88 | } |
| 89 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 90 | public List<String> getCepIds() { |
| 91 | return cepIds.stream().map(UUID::toString).collect(Collectors.toList()); |
| 92 | } |
| 93 | |
| 94 | public OdtnDeviceDescriptionDiscovery.OdtnPortType getPortType() { |
| 95 | return portType; |
| 96 | } |
| 97 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 98 | public ConnectPoint getConnectPoint() { |
| 99 | return cp; |
| 100 | } |
| 101 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 102 | public String getConnectionId() { |
| 103 | return connectionId; |
| 104 | } |
| 105 | |
| 106 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 107 | public TapiNepRef setSipId(String sipId) { |
| 108 | this.sipId = UUID.fromString(sipId); |
| 109 | return this; |
| 110 | } |
| 111 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 112 | public TapiNepRef setCepIds(List<String> cepIds) { |
| 113 | this.cepIds = cepIds.stream().map(UUID::fromString).collect(Collectors.toList()); |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | public TapiNepRef setPortType(String portType) { |
| 118 | this.portType = Optional.ofNullable(portType) |
| 119 | .map(OdtnDeviceDescriptionDiscovery.OdtnPortType::fromValue) |
| 120 | .orElse(null); |
| 121 | return this; |
| 122 | } |
| 123 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 124 | public TapiNepRef setConnectPoint(ConnectPoint cp) { |
| 125 | this.cp = cp; |
| 126 | return this; |
| 127 | } |
| 128 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 129 | public TapiNepRef setConnectionId(String connectionId) { |
| 130 | this.connectionId = connectionId; |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Check if this Nep matches input filter condition. |
| 136 | * |
| 137 | * @param key Filter key |
| 138 | * @param value Filter value |
| 139 | * @return If match or not |
| 140 | */ |
| 141 | public boolean is(String key, String value) { |
| 142 | checkNotNull(key); |
| 143 | checkNotNull(value); |
| 144 | switch (key) { |
| 145 | case DEVICE_ID: |
| 146 | return value.equals( |
| 147 | Optional.ofNullable(cp) |
| 148 | .map(ConnectPoint::deviceId) |
| 149 | .map(DeviceId::toString) |
| 150 | .orElse(null)); |
| 151 | case ODTN_PORT_TYPE: |
| 152 | return value.equals( |
| 153 | Optional.ofNullable(portType) |
| 154 | .map(OdtnDeviceDescriptionDiscovery.OdtnPortType::value) |
| 155 | .orElse(null)); |
| 156 | case CONNECTION_ID: |
| 157 | return value.equals(connectionId); |
| 158 | default: |
| 159 | log.warn("Unknown key: {}", key); |
| 160 | return true; |
| 161 | } |
| 162 | } |
| 163 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 164 | public String toString() { |
| 165 | return toStringHelper(getClass()) |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 166 | .add("topologyId", topologyId) |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 167 | .add("nodeId", nodeId) |
| 168 | .add("nepId", nepId) |
| 169 | .add("sipId", sipId) |
| 170 | .add("connectPoint", cp) |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 171 | .add("portType", portType) |
| 172 | .add("connectionId", connectionId) |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 173 | .toString(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public boolean equals(Object o) { |
| 178 | if (this == o) { |
| 179 | return true; |
| 180 | } |
| 181 | if (!(o instanceof TapiNepRef)) { |
| 182 | return false; |
| 183 | } |
| 184 | TapiNepRef that = (TapiNepRef) o; |
| 185 | return equal(topologyId, that.topologyId) && |
| 186 | equal(nodeId, that.nodeId) && |
| 187 | equal(nepId, that.nepId); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public int hashCode() { |
| 192 | return hash(topologyId, nodeId, nepId); |
| 193 | } |
| 194 | |
| 195 | } |