hiroki | fca084b | 2018-06-02 08:29:42 -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.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.config.BaseConfig; |
| 22 | import org.onosproject.odtn.utils.tapi.TapiNepRef; |
| 23 | |
| 24 | import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OdtnPortType.CLIENT; |
| 25 | import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OdtnPortType.LINE; |
| 26 | |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | |
| 31 | public class TerminalDeviceConfig extends BaseConfig<ConnectPoint> { |
| 32 | |
| 33 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 34 | |
| 35 | /** |
| 36 | * {@value #CONFIG_KEY} : a netcfg ConfigKey for {@link TerminalDeviceConfig}. |
| 37 | */ |
| 38 | public static final String CONFIG_KEY = "odtn-terminal-device"; |
| 39 | |
| 40 | private static final String CLIENT_PORT = "client"; |
| 41 | private static final String ENABLE = "enable"; |
| 42 | |
| 43 | /** |
| 44 | * Create a TerminalDeviceConfig for ODTN. |
| 45 | */ |
| 46 | public TerminalDeviceConfig() { |
| 47 | super(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Create a TerminalDeviceConfig for ODTN. |
| 52 | * |
| 53 | * @param cp ConnectPoint |
| 54 | */ |
| 55 | public TerminalDeviceConfig(ConnectPoint cp) { |
| 56 | ObjectMapper mapper = new ObjectMapper(); |
| 57 | init(cp, CONFIG_KEY, mapper.createObjectNode(), mapper, null); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public boolean isValid() { |
| 62 | return isConnectPoint(CLIENT_PORT, FieldPresence.MANDATORY) && |
| 63 | isBoolean(ENABLE, FieldPresence.MANDATORY); |
| 64 | } |
| 65 | |
| 66 | public ConnectPoint clientCp() { |
| 67 | String cp = get(CLIENT_PORT, ""); |
| 68 | return ConnectPoint.deviceConnectPoint(cp); |
| 69 | } |
| 70 | |
| 71 | public TerminalDeviceConfig clientCp(ConnectPoint cp) { |
| 72 | String val = String.format("%s/%d", cp.deviceId(), cp.port().toLong()); |
| 73 | setOrClear(CLIENT_PORT, val); |
| 74 | return this; |
| 75 | } |
| 76 | |
| 77 | public Boolean isEnabled() { |
| 78 | return get(ENABLE, false); |
| 79 | } |
| 80 | |
| 81 | public TerminalDeviceConfig enable() { |
| 82 | setOrClear(ENABLE, true); |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | public TerminalDeviceConfig disable() { |
| 87 | setOrClear(ENABLE, false); |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Factory method in order to emit NetCfg event from onos inner call. |
| 93 | * |
| 94 | * @param line side NodeEdgePoint of connection |
| 95 | * @param client side NodeEdgePoint of connection |
| 96 | * @return Config object for NetCfg |
| 97 | */ |
| 98 | public static TerminalDeviceConfig create(TapiNepRef line, TapiNepRef client) { |
| 99 | |
| 100 | if (line.getPortType() != LINE) { |
| 101 | throw new IllegalArgumentException("Argument line must be a LINE type."); |
| 102 | } |
| 103 | if (client.getPortType() != CLIENT) { |
| 104 | throw new IllegalArgumentException("Argument client must be a CLIENT type."); |
| 105 | } |
| 106 | |
| 107 | TerminalDeviceConfig self = new TerminalDeviceConfig(line.getConnectPoint()); |
| 108 | self.clientCp(client.getConnectPoint()); |
| 109 | self.enable(); |
| 110 | return self; |
| 111 | } |
| 112 | } |