blob: 95b3e05157a2379999c09230d59578cb7e5c89ce [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
hirokif4ed5212018-05-26 22:39:38 -070019import java.util.Collections;
20import java.util.List;
hiroki684aa2f2018-05-19 20:48:49 -070021import java.util.Optional;
hirokiec18d3a2018-05-16 15:27:37 -070022import java.util.UUID;
hirokif4ed5212018-05-26 22:39:38 -070023import java.util.stream.Collectors;
hirokiec18d3a2018-05-16 15:27:37 -070024import org.onosproject.net.ConnectPoint;
25
26import static com.google.common.base.MoreObjects.toStringHelper;
27import static com.google.common.base.Objects.equal;
hirokif4ed5212018-05-26 22:39:38 -070028import static com.google.common.base.Preconditions.checkNotNull;
hirokiec18d3a2018-05-16 15:27:37 -070029import static java.util.Objects.hash;
hirokif4ed5212018-05-26 22:39:38 -070030import static org.onosproject.odtn.utils.tapi.TapiObjectHandler.DEVICE_ID;
31import static org.onosproject.odtn.utils.tapi.TapiObjectHandler.ODTN_PORT_TYPE;
32import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.CONNECTION_ID;
hirokiec18d3a2018-05-16 15:27:37 -070033
hirokif4ed5212018-05-26 22:39:38 -070034import org.onosproject.net.DeviceId;
35import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
36import org.slf4j.Logger;
37import 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 */
hirokifca084b2018-06-02 08:29:42 -070045public final class TapiNepRef {
hirokiec18d3a2018-05-16 15:27:37 -070046
hirokif4ed5212018-05-26 22:39:38 -070047 protected final Logger log = getLogger(getClass());
48
hirokiec18d3a2018-05-16 15:27:37 -070049 private final UUID topologyId;
50 private final UUID nodeId;
51 private final UUID nepId;
hirokif4ed5212018-05-26 22:39:38 -070052
53 // Annotations to be used for reference of related TAPI objects.
hiroki684aa2f2018-05-19 20:48:49 -070054 private UUID sipId = null;
hirokif4ed5212018-05-26 22:39:38 -070055 private List<UUID> cepIds = Collections.emptyList();
56
57 // Annotations to be used for OpenConfig configuration.
hiroki684aa2f2018-05-19 20:48:49 -070058 private ConnectPoint cp = null;
hirokif4ed5212018-05-26 22:39:38 -070059 private OdtnDeviceDescriptionDiscovery.OdtnPortType portType = null;
60 private String connectionId = null;
hiroki684aa2f2018-05-19 20:48:49 -070061
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 }
hirokiec18d3a2018-05-16 15:27:37 -070071
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() {
hiroki684aa2f2018-05-19 20:48:49 -070085 return Optional.ofNullable(sipId)
86 .map(UUID::toString)
87 .orElse(null);
hirokiec18d3a2018-05-16 15:27:37 -070088 }
89
hirokif4ed5212018-05-26 22:39:38 -070090 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
hirokiec18d3a2018-05-16 15:27:37 -070098 public ConnectPoint getConnectPoint() {
99 return cp;
100 }
101
hirokif4ed5212018-05-26 22:39:38 -0700102 public String getConnectionId() {
103 return connectionId;
104 }
105
106
hirokiec18d3a2018-05-16 15:27:37 -0700107 public TapiNepRef setSipId(String sipId) {
108 this.sipId = UUID.fromString(sipId);
109 return this;
110 }
111
hirokif4ed5212018-05-26 22:39:38 -0700112 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
hirokiec18d3a2018-05-16 15:27:37 -0700124 public TapiNepRef setConnectPoint(ConnectPoint cp) {
125 this.cp = cp;
126 return this;
127 }
128
hirokif4ed5212018-05-26 22:39:38 -0700129 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
hirokiec18d3a2018-05-16 15:27:37 -0700164 public String toString() {
165 return toStringHelper(getClass())
hirokif4ed5212018-05-26 22:39:38 -0700166 .add("topologyId", topologyId)
hirokiec18d3a2018-05-16 15:27:37 -0700167 .add("nodeId", nodeId)
168 .add("nepId", nepId)
169 .add("sipId", sipId)
170 .add("connectPoint", cp)
hirokif4ed5212018-05-26 22:39:38 -0700171 .add("portType", portType)
172 .add("connectionId", connectionId)
hirokiec18d3a2018-05-16 15:27:37 -0700173 .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}