blob: ee0c94ce4592d3554f9f18fbd6207a7a809a1e0b [file] [log] [blame]
xueliang37a396a2016-09-09 14:43:49 +09001/*
2 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.drivers.fujitsu;
18
19import com.google.common.collect.ImmutableMap;
20import org.onlab.packet.IpAddress;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.driver.DefaultDriver;
23import org.onosproject.net.driver.DefaultDriverData;
24import org.onosproject.netconf.NetconfDevice;
25import org.onosproject.netconf.NetconfDeviceInfo;
26import org.onosproject.netconf.NetconfException;
27import org.onosproject.netconf.ctl.NetconfControllerImpl;
28
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.Map;
32import java.util.Set;
33import java.util.concurrent.ConcurrentHashMap;
34
35
36/**
37 * Mock NetconfControllerImpl.
38 */
39class FujitsuNetconfControllerMock extends NetconfControllerImpl {
40
41 private static final String VOLT_DRIVER_NAME = "fujitsu-volt-netconf";
42 private static final String VOLT_DEVICE_USERNAME = "abc";
43 private static final String VOLT_DEVICE_PASSWORD = "123";
44 private static final String VOLT_DEVICE_IP = "10.10.10.11";
45 private static final int VOLT_DEVICE_PORT = 830;
46
47 private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
48
49 @Override
50 public NetconfDevice getNetconfDevice(DeviceId deviceInfo) {
51 return netconfDeviceMap.get(deviceInfo);
52 }
53
54 @Override
55 public NetconfDevice getNetconfDevice(IpAddress ip, int port) {
56 for (DeviceId info : netconfDeviceMap.keySet()) {
57 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
58 return netconfDeviceMap.get(info);
59 }
60 }
61 return null;
62 }
63
64 @Override
65 public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
66 if (netconfDeviceMap.containsKey(deviceId)) {
67 log.debug("Device {} is already present", deviceId);
68 return netconfDeviceMap.get(deviceId);
69 } else {
70 log.debug("Creating NETCONF device {}", deviceId);
71 String ip;
72 int port;
73 String[] info = deviceId.toString().split(":");
74 if (info.length == 3) {
75 ip = info[1];
76 port = Integer.parseInt(info[2]);
77 } else {
78 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
79 && !el.equals(info[info.length - 1]))
80 .reduce((t, u) -> t + ":" + u)
81 .get();
82 log.debug("ip v6 {}", ip);
83 port = Integer.parseInt(info[info.length - 1]);
84 }
85 try {
86 NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(
87 VOLT_DEVICE_USERNAME,
88 VOLT_DEVICE_PASSWORD,
89 IpAddress.valueOf(ip),
90 port);
91 NetconfDevice netconfDevice = new FujitsuNetconfDeviceMock(deviceInfo);
92 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
93 return netconfDevice;
94 } catch (NullPointerException e) {
95 throw new NetconfException("Cannot connect a device " + deviceId, e);
96 }
97 }
98 }
99
100 @Override
101 public void disconnectDevice(DeviceId deviceId, boolean remove) {
102 if (!netconfDeviceMap.containsKey(deviceId)) {
103 log.warn("Device {} is not present", deviceId);
104 } else {
105 netconfDeviceMap.remove(deviceId);
106 }
107 }
108
109 @Override
110 public Map<DeviceId, NetconfDevice> getDevicesMap() {
111 return netconfDeviceMap;
112 }
113
114 @Override
115 public Set<DeviceId> getNetconfDevices() {
116 return netconfDeviceMap.keySet();
117 }
118
119 @Override
120 public void removeDevice(DeviceId deviceId) {
121 if (!netconfDeviceMap.containsKey(deviceId)) {
122 log.warn("Device {} is not present", deviceId);
123 } else {
124 netconfDeviceMap.remove(deviceId);
125 }
126 }
127
128 /**
129 * Sets up initial test environment.
130 *
131 * @param listener listener to be added
132 * @return driver handler
133 * @throws NetconfException when there is a problem
134 */
135 public FujitsuDriverHandlerAdapter setUp(FujitsuNetconfSessionListenerTest listener) throws NetconfException {
136 try {
137 NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(
138 VOLT_DEVICE_USERNAME, VOLT_DEVICE_PASSWORD,
139 IpAddress.valueOf(VOLT_DEVICE_IP), VOLT_DEVICE_PORT);
140
141 NetconfDevice netconfDevice = connectDevice(deviceInfo.getDeviceId());
142
143 FujitsuNetconfSessionMock session = (FujitsuNetconfSessionMock) netconfDevice.getSession();
144 session.setListener(listener);
145
146 DeviceId deviceId = deviceInfo.getDeviceId();
147 DefaultDriver driver = new DefaultDriver(
148 VOLT_DRIVER_NAME, new ArrayList<>(),
149 "Fujitsu", "1.0", "1.0",
150 ImmutableMap.of(), ImmutableMap.of());
151
152 DefaultDriverData driverData = new DefaultDriverData(driver, deviceId);
153
154 FujitsuDriverHandlerAdapter driverHandler;
155 driverHandler = new FujitsuDriverHandlerAdapter(driverData);
156 driverHandler.setUp(this);
157
158 return driverHandler;
159 } catch (NetconfException e) {
160 throw new NetconfException("Cannot create a device ", e);
161 }
162 }
163
164}