Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 | */ |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 16 | package org.onosproject.incubator.net.tunnel; |
| 17 | |
| 18 | |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 20 | import org.onosproject.ui.table.CellFormatter; |
| 21 | import org.onosproject.ui.table.cell.AbstractCellFormatter; |
| 22 | |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 23 | import java.util.Optional; |
| 24 | |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 25 | /** |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 26 | * Formats an optical tunnel endpoint as "(type)/(element-id)/(port)". |
| 27 | * Formats an IP tunnel endpoint as "ip". |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 28 | */ |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 29 | @Beta |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 30 | public final class TunnelEndPointFormatter extends AbstractCellFormatter { |
| 31 | //non-instantiable |
| 32 | private TunnelEndPointFormatter() { |
| 33 | } |
| 34 | |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 35 | private String safeOptional(Optional<?> optional) { |
| 36 | return optional.isPresent() ? optional.get().toString() : QUERY; |
| 37 | } |
| 38 | |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 39 | @Override |
| 40 | protected String nonNullFormat(Object value) { |
| 41 | |
| 42 | if (value instanceof DefaultOpticalTunnelEndPoint) { |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 43 | DefaultOpticalTunnelEndPoint ep = |
| 44 | (DefaultOpticalTunnelEndPoint) value; |
| 45 | |
| 46 | String e = safeOptional(ep.elementId()); |
| 47 | String p = safeOptional(ep.portNumber()); |
| 48 | return ep.type() + SLASH + e + SLASH + p; |
| 49 | |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 50 | } else if (value instanceof IpTunnelEndPoint) { |
| 51 | IpTunnelEndPoint cp = (IpTunnelEndPoint) value; |
| 52 | return cp.ip().toString(); |
| 53 | } |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 54 | return EMPTY; |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * An instance of this class. |
| 59 | */ |
| 60 | public static final CellFormatter INSTANCE = new TunnelEndPointFormatter(); |
| 61 | } |