Yuta HIGUCHI | d0f8d89 | 2018-04-09 12:14:00 -0700 | [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 | |
Yuta HIGUCHI | 9ee8664 | 2018-05-15 15:25:00 -0700 | [diff] [blame^] | 18 | import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_NAME; |
Yuta HIGUCHI | d0f8d89 | 2018-04-09 12:14:00 -0700 | [diff] [blame] | 19 | import static org.onosproject.odtn.utils.YangToolUtil.toCharSequence; |
| 20 | import static org.onosproject.odtn.utils.YangToolUtil.toCompositeData; |
| 21 | import static org.onosproject.odtn.utils.YangToolUtil.toResourceData; |
| 22 | import static org.onosproject.odtn.utils.YangToolUtil.toXmlCompositeStream; |
Yuta HIGUCHI | 9ee8664 | 2018-05-15 15:25:00 -0700 | [diff] [blame^] | 23 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | d0f8d89 | 2018-04-09 12:14:00 -0700 | [diff] [blame] | 24 | |
Yuta HIGUCHI | 9ee8664 | 2018-05-15 15:25:00 -0700 | [diff] [blame^] | 25 | import java.util.Collections; |
Yuta HIGUCHI | d0f8d89 | 2018-04-09 12:14:00 -0700 | [diff] [blame] | 26 | import java.util.List; |
| 27 | |
Yuta HIGUCHI | 9ee8664 | 2018-05-15 15:25:00 -0700 | [diff] [blame^] | 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Port; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.device.DeviceService; |
Yuta HIGUCHI | d0f8d89 | 2018-04-09 12:14:00 -0700 | [diff] [blame] | 32 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 33 | import org.onosproject.odtn.utils.openconfig.Transceiver; |
| 34 | import org.onosproject.yang.model.DataNode; |
| 35 | import org.onosproject.yang.model.ResourceId; |
Yuta HIGUCHI | 9ee8664 | 2018-05-15 15:25:00 -0700 | [diff] [blame^] | 36 | import org.slf4j.Logger; |
Yuta HIGUCHI | d0f8d89 | 2018-04-09 12:14:00 -0700 | [diff] [blame] | 37 | |
Yuta HIGUCHI | 9ee8664 | 2018-05-15 15:25:00 -0700 | [diff] [blame^] | 38 | import com.google.common.base.Strings; |
Yuta HIGUCHI | d0f8d89 | 2018-04-09 12:14:00 -0700 | [diff] [blame] | 39 | import com.google.common.collect.Lists; |
| 40 | |
| 41 | /** |
| 42 | * Plain OpenConfig based implementation. |
| 43 | */ |
| 44 | public class PlainTransceiver extends AbstractHandlerBehaviour |
| 45 | implements ConfigurableTransceiver { |
| 46 | |
Yuta HIGUCHI | 9ee8664 | 2018-05-15 15:25:00 -0700 | [diff] [blame^] | 47 | private final Logger log = getLogger(getClass()); |
| 48 | |
Yuta HIGUCHI | d0f8d89 | 2018-04-09 12:14:00 -0700 | [diff] [blame] | 49 | @Override |
Yuta HIGUCHI | 9ee8664 | 2018-05-15 15:25:00 -0700 | [diff] [blame^] | 50 | public List<CharSequence> enable(PortNumber number, boolean enable) { |
| 51 | DeviceId did = this.data().deviceId(); |
| 52 | Port port = handler().get(DeviceService.class).getPort(did, number); |
| 53 | if (port == null) { |
| 54 | log.warn("{} does not exist on {}", number, did); |
| 55 | return Collections.emptyList(); |
| 56 | } |
| 57 | String component = port.annotations().value(OC_NAME); |
| 58 | if (Strings.isNullOrEmpty(component)) { |
| 59 | log.warn("{} annotation not found on {}@{}", OC_NAME, number, did); |
| 60 | return Collections.emptyList(); |
| 61 | } |
| 62 | return enable(component, enable); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public List<CharSequence> enable(String component, boolean enable) { |
| 67 | List<DataNode> nodes = Transceiver.enable(component, enable); |
Yuta HIGUCHI | d0f8d89 | 2018-04-09 12:14:00 -0700 | [diff] [blame] | 68 | |
| 69 | ResourceId empty = ResourceId.builder().build(); |
| 70 | return Lists.transform(nodes, |
| 71 | node -> toCharSequence(toXmlCompositeStream(toCompositeData(toResourceData(empty, node))))); |
| 72 | } |
| 73 | |
| 74 | } |