blob: 8f89807132a659eb0264ce9c62adc6ceec0365f7 [file] [log] [blame]
hiroki9e1484d2018-12-07 09:36:49 -08001/*
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 */
16package org.onosproject.odtn.behaviour;
17
18import com.google.common.base.Strings;
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.Port;
21import org.onosproject.net.PortNumber;
22import org.onosproject.net.device.DeviceService;
23import org.onosproject.net.driver.AbstractHandlerBehaviour;
24import org.onosproject.odtn.behaviour.OdtnTerminalDeviceDriver.Operation;
25import org.onosproject.odtn.utils.openconfig.OpenConfigAssignmentHandler;
26import org.onosproject.odtn.utils.openconfig.OpenConfigChannelHandler;
27import org.onosproject.odtn.utils.openconfig.OpenConfigConfigOfAssignmentHandler;
28import org.onosproject.odtn.utils.openconfig.OpenConfigConfigOfChannelHandler;
29import org.onosproject.odtn.utils.openconfig.OpenConfigLogicalChannelAssignmentsHandler;
30import org.onosproject.odtn.utils.openconfig.OpenConfigLogicalChannelsHandler;
31import org.onosproject.odtn.utils.openconfig.OpenConfigTerminalDeviceHandler;
32import org.onosproject.yang.gen.v1.openconfigterminaldevice.rev20170708.openconfigterminaldevice.terminallogicalchanassignmentconfig.AssignmentTypeEnum;
33import org.slf4j.Logger;
34
35import java.math.BigDecimal;
36import java.util.Collections;
37import java.util.List;
38
hiroki337c5522019-01-20 20:05:51 -080039import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_LOGICAL_CHANNEL;
hiroki9e1484d2018-12-07 09:36:49 -080040import static org.slf4j.LoggerFactory.getLogger;
41
42/**
43 * Utility class for NETCONF driver.
44 */
45public 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
hiroki9e1484d2018-12-07 09:36:49 -080055 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 }
hiroki337c5522019-01-20 20:05:51 -080061 String clientName = clientPort.annotations().value(OC_LOGICAL_CHANNEL);
hiroki9e1484d2018-12-07 09:36:49 -080062 if (Strings.isNullOrEmpty(clientName)) {
hiroki337c5522019-01-20 20:05:51 -080063 log.warn("{} annotations not exist on {}@{}", OC_LOGICAL_CHANNEL, client, did);
hiroki9e1484d2018-12-07 09:36:49 -080064 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 }
hiroki337c5522019-01-20 20:05:51 -080072 String lineName = linePort.annotations().value(OC_LOGICAL_CHANNEL);
hiroki9e1484d2018-12-07 09:36:49 -080073 if (Strings.isNullOrEmpty(lineName)) {
hiroki337c5522019-01-20 20:05:51 -080074 log.warn("{} annotations not exist on {}@{}", OC_LOGICAL_CHANNEL, line, did);
hiroki9e1484d2018-12-07 09:36:49 -080075 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}