Ai Hamano | bd51cdd | 2018-10-18 11:30:07 +0900 | [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; |
hiroki | 096259b | 2018-12-07 09:33:24 -0800 | [diff] [blame^] | 32 | import org.onosproject.yang.gen.v1.openconfigterminaldevice.rev20170708.openconfigterminaldevice.terminallogicalchanassignmentconfig.AssignmentTypeEnum; |
Ai Hamano | bd51cdd | 2018-10-18 11:30:07 +0900 | [diff] [blame] | 33 | |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.math.BigDecimal; |
| 37 | import java.util.Collections; |
| 38 | import java.util.List; |
| 39 | import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_NAME; |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Utility class for NETCONF driver. |
| 44 | */ |
| 45 | public class InfineraTransceiver 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 | |
| 55 | log.debug("enable() infinera route"); |
| 56 | DeviceId did = this.data().deviceId(); |
| 57 | Port clientPort = handler().get(DeviceService.class).getPort(did, client); |
| 58 | if (clientPort == null) { |
| 59 | log.warn("{} does not exist on {}", client, did); |
| 60 | return Collections.emptyList(); |
| 61 | } |
| 62 | String clientName = clientPort.annotations().value(OC_NAME); |
| 63 | if (Strings.isNullOrEmpty(clientName)) { |
| 64 | log.warn("{} annotations not exist on {}@{}", OC_NAME, client, did); |
| 65 | return Collections.emptyList(); |
| 66 | } |
| 67 | |
| 68 | Port linePort = handler().get(DeviceService.class).getPort(did, line); |
| 69 | if (linePort == null) { |
| 70 | log.warn("{} does not exist on {}", line, did); |
| 71 | return Collections.emptyList(); |
| 72 | } |
| 73 | String lineName = linePort.annotations().value(OC_NAME); |
| 74 | if (Strings.isNullOrEmpty(lineName)) { |
| 75 | log.warn("{} annotations not exist on {}@{}", OC_NAME, line, did); |
| 76 | return Collections.emptyList(); |
| 77 | } |
| 78 | |
| 79 | // create <terminal-device xmlns="http://openconfig.net/yang/terminal-device"> |
| 80 | // </terminal-device> |
| 81 | OpenConfigTerminalDeviceHandler terminalDevice = new OpenConfigTerminalDeviceHandler(); |
| 82 | |
| 83 | // add <logical-channels></logical-channels> |
| 84 | OpenConfigLogicalChannelsHandler logicalChannels = |
| 85 | new OpenConfigLogicalChannelsHandler(terminalDevice); |
| 86 | |
| 87 | // add <channel><index>"clientName"</index></channel> |
| 88 | OpenConfigChannelHandler channel = |
hiroki | 096259b | 2018-12-07 09:33:24 -0800 | [diff] [blame^] | 89 | new OpenConfigChannelHandler(Integer.parseInt(clientName), logicalChannels); |
Ai Hamano | bd51cdd | 2018-10-18 11:30:07 +0900 | [diff] [blame] | 90 | |
| 91 | // add <config><index>"clientName"</index></config> |
| 92 | OpenConfigConfigOfChannelHandler configOfChannel = |
| 93 | new OpenConfigConfigOfChannelHandler(channel); |
hiroki | 096259b | 2018-12-07 09:33:24 -0800 | [diff] [blame^] | 94 | configOfChannel.addIndex(Integer.parseInt(clientName)); |
Ai Hamano | bd51cdd | 2018-10-18 11:30:07 +0900 | [diff] [blame] | 95 | |
| 96 | // add <logical-channel-assignments xc:operation="merge/delete"> |
| 97 | OpenConfigLogicalChannelAssignmentsHandler logicalChannelAssignments = |
| 98 | new OpenConfigLogicalChannelAssignmentsHandler(channel); |
| 99 | if (enable) { |
| 100 | logicalChannelAssignments.addAnnotation(ANOTATION_NAME, Operation.MERGE.value()); |
| 101 | } else { |
| 102 | logicalChannelAssignments.addAnnotation(ANOTATION_NAME, Operation.DELETE.value()); |
| 103 | } |
| 104 | |
| 105 | // add <assignment><index>"clientName"</index></assignment> |
| 106 | OpenConfigAssignmentHandler assignment = |
hiroki | 096259b | 2018-12-07 09:33:24 -0800 | [diff] [blame^] | 107 | new OpenConfigAssignmentHandler(Integer.parseInt(clientName), logicalChannelAssignments); |
Ai Hamano | bd51cdd | 2018-10-18 11:30:07 +0900 | [diff] [blame] | 108 | |
| 109 | // add <config><assignment-type>LOGICAL_CHANNEL</assignment-type> |
| 110 | // <logical-channel>"lineName"</logical-channel> |
| 111 | // <allocation>100</allocation> |
| 112 | // </config> |
| 113 | OpenConfigConfigOfAssignmentHandler configOfAssignment = |
| 114 | new OpenConfigConfigOfAssignmentHandler(assignment); |
| 115 | configOfAssignment.addAssignmentType(AssignmentTypeEnum.LOGICAL_CHANNEL); |
| 116 | configOfAssignment.addLogicalChannel(lineName); |
| 117 | configOfAssignment.addAllocation(BigDecimal.valueOf(100)); |
| 118 | |
| 119 | return terminalDevice.getListCharSequence(); |
| 120 | } |
| 121 | } |