blob: 0a23450b4f06c4e22c0fb29df7ff0d24bac1e64c [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
39import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_NAME;
40import 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
55 log.debug("enable() cassini 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 =
89 new OpenConfigChannelHandler(Integer.parseInt(clientName), logicalChannels);
90
91 // add <config><index>"clientName"</index></config>
92 OpenConfigConfigOfChannelHandler configOfChannel =
93 new OpenConfigConfigOfChannelHandler(channel);
94 configOfChannel.addIndex(Integer.parseInt(clientName));
95
96 // add <logical-channel-assignments></logical-channel-assignments>
97 OpenConfigLogicalChannelAssignmentsHandler logicalChannelAssignments =
98 new OpenConfigLogicalChannelAssignmentsHandler(channel);
99
100 // add <assignment (none)/xc:operation="delete"><index>"clientName"</index></assignment>
101 OpenConfigAssignmentHandler assignment =
102 new OpenConfigAssignmentHandler(Integer.parseInt(clientName), logicalChannelAssignments);
103 if (!enable) {
104 assignment.addAnnotation(ANOTATION_NAME, Operation.DELETE.value());
105 }
106
107 // add <config><index>"clientName"</index>
108 // <assignment-type>LOGICAL_CHANNEL</assignment-type>
109 // <logical-channel>"lineName"</logical-channel>
110 // <allocation>100</allocation>
111 // </config>
112 OpenConfigConfigOfAssignmentHandler configOfAssignment =
113 new OpenConfigConfigOfAssignmentHandler(assignment);
114 configOfAssignment.addIndex(Long.parseLong(clientName));
115 configOfAssignment.addAssignmentType(AssignmentTypeEnum.LOGICAL_CHANNEL);
116 configOfAssignment.addLogicalChannel(lineName);
117 configOfAssignment.addAllocation(BigDecimal.valueOf(100));
118
119 return terminalDevice.getListCharSequence();
120 }
121}