10068695 | e340ec9 | 2019-10-11 07:03:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-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 | package org.onosproject.drivers.zte; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.Port; |
| 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.device.DeviceService; |
| 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 24 | import org.onosproject.odtn.behaviour.ConfigurableTransceiver; |
| 25 | import org.onosproject.odtn.behaviour.OdtnTerminalDeviceDriver.Operation; |
| 26 | import org.onosproject.odtn.utils.openconfig.OpenConfigAssignmentHandler; |
| 27 | import org.onosproject.odtn.utils.openconfig.OpenConfigChannelHandler; |
| 28 | import org.onosproject.odtn.utils.openconfig.OpenConfigConfigOfAssignmentHandler; |
| 29 | import org.onosproject.odtn.utils.openconfig.OpenConfigConfigOfChannelHandler; |
| 30 | import org.onosproject.odtn.utils.openconfig.OpenConfigLogicalChannelAssignmentsHandler; |
| 31 | import org.onosproject.odtn.utils.openconfig.OpenConfigLogicalChannelsHandler; |
| 32 | import org.onosproject.odtn.utils.openconfig.OpenConfigTerminalDeviceHandler; |
| 33 | import org.onosproject.yang.gen.v1.openconfigterminaldevice.rev20170708.openconfigterminaldevice.terminallogicalchanassignmentconfig.AssignmentTypeEnum; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.util.Collections; |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_NAME; |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | public class ZteTransceiver extends AbstractHandlerBehaviour |
| 43 | implements ConfigurableTransceiver { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | private static final String ANOTATION_NAME = "xc:operation"; |
| 48 | |
| 49 | private final int[] clientPortIndexs = new int[] { |
| 50 | 20975681, 18878529, 19927105, 17829953, |
| 51 | 20975745, 18878593, 19927169, 17830017, |
| 52 | 20975809, 18878657, 19927233, 17830081, |
| 53 | 20975873, 18878721, 19927297, 17830145 |
| 54 | }; |
| 55 | |
| 56 | private final int[] linePortIndexs = new int[] { |
| 57 | 21499969, 19402817, 20451393, 18354241, |
| 58 | 21500033, 19402881, 20451457, 18354305 |
| 59 | }; |
| 60 | |
| 61 | @Override |
| 62 | public List<CharSequence> enable(PortNumber client, PortNumber line, boolean enable) { |
| 63 | DeviceId deviceId = handler().data().deviceId(); |
| 64 | log.info("Discovering ZTE device {}", deviceId); |
| 65 | |
| 66 | Port clientPort = handler().get(DeviceService.class).getPort(deviceId, client); |
| 67 | if (clientPort == null) { |
| 68 | log.warn("{} does not exist on {}", client, deviceId); |
| 69 | return Collections.emptyList(); |
| 70 | } |
| 71 | |
| 72 | String clientName = clientPort.annotations().value(OC_NAME); |
| 73 | if (Strings.isNullOrEmpty(clientName)) { |
| 74 | log.warn("{} annotations not exist on {}@{}", OC_NAME, client, deviceId); |
| 75 | return Collections.emptyList(); |
| 76 | } |
| 77 | |
| 78 | Port linePort = handler().get(DeviceService.class).getPort(deviceId, line); |
| 79 | if (linePort == null) { |
| 80 | log.warn("{} does not exist on {}", line, deviceId); |
| 81 | return Collections.emptyList(); |
| 82 | } |
| 83 | |
| 84 | String lineName = linePort.annotations().value(OC_NAME); |
| 85 | if (Strings.isNullOrEmpty(lineName)) { |
| 86 | log.warn("{} annotations not exist on {}@{}", OC_NAME, line, deviceId); |
| 87 | return Collections.emptyList(); |
| 88 | } |
| 89 | |
| 90 | int clientIndex, lineIndex; |
| 91 | |
| 92 | try { |
| 93 | clientIndex = getPortIndex(clientName); |
| 94 | lineIndex = getPortIndex(lineName); |
| 95 | } catch (IllegalArgumentException e) { |
| 96 | return Collections.emptyList(); |
| 97 | } |
| 98 | |
| 99 | // create <terminal-device xmlns="http://openconfig.net/yang/terminal-device"> |
| 100 | // </terminal-device> |
| 101 | OpenConfigTerminalDeviceHandler terminalDevice = new OpenConfigTerminalDeviceHandler(); |
| 102 | // add <logical-channels></logical-channels> |
| 103 | OpenConfigLogicalChannelsHandler logicalChannels = |
| 104 | new OpenConfigLogicalChannelsHandler(terminalDevice); |
| 105 | // add <channel><index>"clientIndex"</index></channel> |
| 106 | OpenConfigChannelHandler channel = |
| 107 | new OpenConfigChannelHandler(clientIndex, logicalChannels); |
| 108 | |
| 109 | // add <channel xc:operation="merge/delete"> |
| 110 | if (enable) { |
| 111 | channel.addAnnotation(ANOTATION_NAME, Operation.MERGE.value()); |
| 112 | } else { |
| 113 | channel.addAnnotation(ANOTATION_NAME, Operation.DELETE.value()); |
| 114 | } |
| 115 | |
| 116 | // add <config><index>"clientIndex"</index></config> |
| 117 | OpenConfigConfigOfChannelHandler configOfChannel = |
| 118 | new OpenConfigConfigOfChannelHandler(channel); |
| 119 | configOfChannel.addIndex(clientIndex); |
| 120 | |
| 121 | // add <logical-channel-assignments></logical-channel-assignments> |
| 122 | OpenConfigLogicalChannelAssignmentsHandler logicalChannelAssignments = |
| 123 | new OpenConfigLogicalChannelAssignmentsHandler(channel); |
| 124 | |
| 125 | // add <assignment><index>1</index></assignment> |
| 126 | OpenConfigAssignmentHandler assignment = |
| 127 | new OpenConfigAssignmentHandler(1, logicalChannelAssignments); |
| 128 | |
| 129 | // add <config><assignment-type>LOGICAL_CHANNEL</assignment-type> |
| 130 | // <logical-channel>"lineIndex"</logical-channel> |
| 131 | // </config> |
| 132 | OpenConfigConfigOfAssignmentHandler configOfAssignment = |
| 133 | new OpenConfigConfigOfAssignmentHandler(assignment); |
| 134 | configOfAssignment.addAssignmentType(AssignmentTypeEnum.LOGICAL_CHANNEL); |
| 135 | configOfAssignment.addLogicalChannel("" + lineIndex); |
| 136 | |
| 137 | return terminalDevice.getListCharSequence(); |
| 138 | } |
| 139 | |
| 140 | // index should be fixed |
| 141 | private int getPortIndex(String portName) throws IllegalArgumentException { |
| 142 | //PORT-1-4-C1 |
| 143 | String[] portInfos = portName.split("-"); |
| 144 | if (portInfos.length == 4) { |
| 145 | throw new IllegalArgumentException("ZTE device port name illegal."); |
| 146 | } |
| 147 | |
| 148 | int slotIndex = Integer.parseInt(portInfos[2]); |
| 149 | int index = Integer.parseInt(portInfos[3].substring(1)); |
| 150 | |
| 151 | if (portInfos[3].startsWith("C")) { |
| 152 | return clientPortIndexs[index * 4 - slotIndex]; |
| 153 | } else if (portInfos[3].startsWith("C")) { |
| 154 | return linePortIndexs[index * 4 - slotIndex]; |
| 155 | } |
| 156 | |
| 157 | throw new IllegalArgumentException("Connot match port index for ZTE device."); |
| 158 | } |
| 159 | |
| 160 | } |