hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [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 | package org.onosproject.odtn.behaviour; |
| 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.OdtnTerminalDeviceDriver.Operation; |
| 25 | import org.onosproject.odtn.utils.openconfig.OpenConfigAssignmentHandler; |
| 26 | import org.onosproject.odtn.utils.openconfig.OpenConfigChannelHandler; |
| 27 | import org.onosproject.odtn.utils.openconfig.OpenConfigConfigOfAssignmentHandler; |
| 28 | import org.onosproject.odtn.utils.openconfig.OpenConfigConfigOfChannelHandler; |
| 29 | import org.onosproject.odtn.utils.openconfig.OpenConfigLogicalChannelAssignmentsHandler; |
| 30 | import org.onosproject.odtn.utils.openconfig.OpenConfigLogicalChannelsHandler; |
| 31 | import org.onosproject.odtn.utils.openconfig.OpenConfigTerminalDeviceHandler; |
| 32 | import org.onosproject.yang.gen.v1.openconfigterminaldevice.rev20170708.openconfigterminaldevice.terminallogicalchanassignmentconfig.AssignmentTypeEnum; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.math.BigDecimal; |
| 36 | import java.util.Collections; |
| 37 | import java.util.List; |
| 38 | |
hiroki | 337c552 | 2019-01-20 20:05:51 -0800 | [diff] [blame^] | 39 | import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_LOGICAL_CHANNEL; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Utility class for NETCONF driver. |
| 44 | */ |
| 45 | public class CassiniTransceiver extends AbstractHandlerBehaviour |
| 46 | implements ConfigurableTransceiver { |
| 47 | |
| 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | private static final String ANOTATION_NAME = "xc:operation"; |
| 51 | |
| 52 | @Override |
| 53 | public List<CharSequence> enable(PortNumber client, PortNumber line, boolean enable) { |
| 54 | |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 55 | DeviceId did = this.data().deviceId(); |
| 56 | Port clientPort = handler().get(DeviceService.class).getPort(did, client); |
| 57 | if (clientPort == null) { |
| 58 | log.warn("{} does not exist on {}", client, did); |
| 59 | return Collections.emptyList(); |
| 60 | } |
hiroki | 337c552 | 2019-01-20 20:05:51 -0800 | [diff] [blame^] | 61 | String clientName = clientPort.annotations().value(OC_LOGICAL_CHANNEL); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 62 | if (Strings.isNullOrEmpty(clientName)) { |
hiroki | 337c552 | 2019-01-20 20:05:51 -0800 | [diff] [blame^] | 63 | log.warn("{} annotations not exist on {}@{}", OC_LOGICAL_CHANNEL, client, did); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 64 | return Collections.emptyList(); |
| 65 | } |
| 66 | |
| 67 | Port linePort = handler().get(DeviceService.class).getPort(did, line); |
| 68 | if (linePort == null) { |
| 69 | log.warn("{} does not exist on {}", line, did); |
| 70 | return Collections.emptyList(); |
| 71 | } |
hiroki | 337c552 | 2019-01-20 20:05:51 -0800 | [diff] [blame^] | 72 | String lineName = linePort.annotations().value(OC_LOGICAL_CHANNEL); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 73 | if (Strings.isNullOrEmpty(lineName)) { |
hiroki | 337c552 | 2019-01-20 20:05:51 -0800 | [diff] [blame^] | 74 | log.warn("{} annotations not exist on {}@{}", OC_LOGICAL_CHANNEL, line, did); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 75 | return Collections.emptyList(); |
| 76 | } |
| 77 | |
| 78 | // create <terminal-device xmlns="http://openconfig.net/yang/terminal-device"> |
| 79 | // </terminal-device> |
| 80 | OpenConfigTerminalDeviceHandler terminalDevice = new OpenConfigTerminalDeviceHandler(); |
| 81 | |
| 82 | // add <logical-channels></logical-channels> |
| 83 | OpenConfigLogicalChannelsHandler logicalChannels = |
| 84 | new OpenConfigLogicalChannelsHandler(terminalDevice); |
| 85 | |
| 86 | // add <channel><index>"clientName"</index></channel> |
| 87 | OpenConfigChannelHandler channel = |
| 88 | new OpenConfigChannelHandler(Integer.parseInt(clientName), logicalChannels); |
| 89 | |
| 90 | // add <config><index>"clientName"</index></config> |
| 91 | OpenConfigConfigOfChannelHandler configOfChannel = |
| 92 | new OpenConfigConfigOfChannelHandler(channel); |
| 93 | configOfChannel.addIndex(Integer.parseInt(clientName)); |
| 94 | |
| 95 | // add <logical-channel-assignments></logical-channel-assignments> |
| 96 | OpenConfigLogicalChannelAssignmentsHandler logicalChannelAssignments = |
| 97 | new OpenConfigLogicalChannelAssignmentsHandler(channel); |
| 98 | |
| 99 | // add <assignment (none)/xc:operation="delete"><index>"clientName"</index></assignment> |
| 100 | OpenConfigAssignmentHandler assignment = |
| 101 | new OpenConfigAssignmentHandler(Integer.parseInt(clientName), logicalChannelAssignments); |
| 102 | if (!enable) { |
| 103 | assignment.addAnnotation(ANOTATION_NAME, Operation.DELETE.value()); |
| 104 | } |
| 105 | |
| 106 | // add <config><index>"clientName"</index> |
| 107 | // <assignment-type>LOGICAL_CHANNEL</assignment-type> |
| 108 | // <logical-channel>"lineName"</logical-channel> |
| 109 | // <allocation>100</allocation> |
| 110 | // </config> |
| 111 | OpenConfigConfigOfAssignmentHandler configOfAssignment = |
| 112 | new OpenConfigConfigOfAssignmentHandler(assignment); |
| 113 | configOfAssignment.addIndex(Long.parseLong(clientName)); |
| 114 | configOfAssignment.addAssignmentType(AssignmentTypeEnum.LOGICAL_CHANNEL); |
| 115 | configOfAssignment.addLogicalChannel(lineName); |
| 116 | configOfAssignment.addAllocation(BigDecimal.valueOf(100)); |
| 117 | |
| 118 | return terminalDevice.getListCharSequence(); |
| 119 | } |
| 120 | } |