blob: 1379351c88f3b76874a9f125e08cc5374dcbac8c [file] [log] [blame]
Yuta HIGUCHId0f8d892018-04-09 12:14:00 -07001/*
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
Yuta HIGUCHI9ee86642018-05-15 15:25:00 -070018import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_NAME;
Yuta HIGUCHId0f8d892018-04-09 12:14:00 -070019import static org.onosproject.odtn.utils.YangToolUtil.toCharSequence;
20import static org.onosproject.odtn.utils.YangToolUtil.toCompositeData;
21import static org.onosproject.odtn.utils.YangToolUtil.toResourceData;
22import static org.onosproject.odtn.utils.YangToolUtil.toXmlCompositeStream;
Yuta HIGUCHI9ee86642018-05-15 15:25:00 -070023import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHId0f8d892018-04-09 12:14:00 -070024
Yuta HIGUCHI9ee86642018-05-15 15:25:00 -070025import java.util.Collections;
Yuta HIGUCHId0f8d892018-04-09 12:14:00 -070026import java.util.List;
27
Yuta HIGUCHI9ee86642018-05-15 15:25:00 -070028import org.onosproject.net.DeviceId;
29import org.onosproject.net.Port;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.device.DeviceService;
Yuta HIGUCHId0f8d892018-04-09 12:14:00 -070032import org.onosproject.net.driver.AbstractHandlerBehaviour;
33import org.onosproject.odtn.utils.openconfig.Transceiver;
34import org.onosproject.yang.model.DataNode;
35import org.onosproject.yang.model.ResourceId;
Yuta HIGUCHI9ee86642018-05-15 15:25:00 -070036import org.slf4j.Logger;
Yuta HIGUCHId0f8d892018-04-09 12:14:00 -070037
Yuta HIGUCHI9ee86642018-05-15 15:25:00 -070038import com.google.common.base.Strings;
Yuta HIGUCHId0f8d892018-04-09 12:14:00 -070039import com.google.common.collect.Lists;
40
41/**
42 * Plain OpenConfig based implementation.
43 */
44public class PlainTransceiver extends AbstractHandlerBehaviour
45 implements ConfigurableTransceiver {
46
Yuta HIGUCHI9ee86642018-05-15 15:25:00 -070047 private final Logger log = getLogger(getClass());
48
Yuta HIGUCHId0f8d892018-04-09 12:14:00 -070049 @Override
Yuta HIGUCHI9ee86642018-05-15 15:25:00 -070050 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 HIGUCHId0f8d892018-04-09 12:14:00 -070068
69 ResourceId empty = ResourceId.builder().build();
70 return Lists.transform(nodes,
71 node -> toCharSequence(toXmlCompositeStream(toCompositeData(toResourceData(empty, node)))));
72 }
73
74}