blob: 5b55e0c1d247b23a973888db9cd92dc186127d76 [file] [log] [blame]
xueliang0e946fc2016-12-08 15:00: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 org.onosproject.mastership.MastershipService;
20import org.onosproject.net.DeviceId;
21import org.onosproject.drivers.fujitsu.behaviour.VoltNniLinkConfig;
22import org.onosproject.net.driver.AbstractHandlerBehaviour;
23import org.onosproject.net.driver.DriverHandler;
24import org.onosproject.netconf.NetconfController;
25import org.slf4j.Logger;
26
27import java.io.IOException;
28import java.util.Set;
29
30import com.google.common.collect.ImmutableSet;
31import static com.google.common.base.Preconditions.checkNotNull;
32import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Implementation to get and set parameters available in vOLT
37 * through the Netconf protocol.
38 */
39public class FujitsuVoltNniLinkConfig extends AbstractHandlerBehaviour
40 implements VoltNniLinkConfig {
41
42 private final Logger log = getLogger(FujitsuVoltNniLinkConfig.class);
43 private static final Set<String> NNILINKPARAMS =
44 ImmutableSet.of("loopback-enable");
45 private static final Set<String> ENABLES = ImmutableSet.of("true", "false");
46 private static final String VOLT_PORTS = "volt-ports";
47 private static final String ETH_NNILINK_PORTS = "eth-nnilink-ports";
48 private static final String ETH_NNILINK_PORT = "eth-nnilink-port";
49 private static final String NNILINK_ID = "nnilink-id";
50
51 @Override
52 public String getNniLinks(String target) {
53 DriverHandler handler = handler();
54 NetconfController controller = handler.get(NetconfController.class);
55 MastershipService mastershipService = handler.get(MastershipService.class);
56 DeviceId ncDeviceId = handler.data().deviceId();
57 checkNotNull(controller, "Netconf controller is null");
58 String reply = null;
59
60 if (!mastershipService.isLocalMaster(ncDeviceId)) {
61 log.warn("Not master for {} Use {} to execute command",
62 ncDeviceId,
63 mastershipService.getMasterFor(ncDeviceId));
64 return null;
65 }
66
67 try {
68 StringBuilder request = new StringBuilder();
69 request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE)
70 .append(ANGLE_RIGHT + NEW_LINE)
71 .append(buildStartTag(VOLT_PORTS));
72 if (target != null) {
73 int nni;
74 try {
75 nni = Integer.parseInt(target);
76 if (nni <= ZERO) {
77 log.error("Invalid integer for nnilink-id:{}", target);
78 return null;
79 }
80 } catch (NumberFormatException e) {
81 log.error("Non-number input for nnilink-id:{}", target);
82 return null;
83 }
84 request.append(buildStartTag(ETH_NNILINK_PORTS))
85 .append(buildStartTag(ETH_NNILINK_PORT))
86 .append(buildStartTag(NNILINK_ID, false))
87 .append(target)
88 .append(buildEndTag(NNILINK_ID))
89 .append(buildEndTag(ETH_NNILINK_PORT))
90 .append(buildEndTag(ETH_NNILINK_PORTS));
91 } else {
92 request.append(buildEmptyTag(ETH_NNILINK_PORTS));
93 }
94 request.append(buildEndTag(VOLT_PORTS))
95 .append(VOLT_NE_CLOSE);
96
97 reply = controller.getDevicesMap()
98 .get(ncDeviceId)
99 .getSession()
100 .get(request.toString(), REPORT_ALL);
101 } catch (IOException e) {
102 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
103 }
104 return reply;
105 }
106
107 @Override
108 public boolean setNniLink(String target) {
109 DriverHandler handler = handler();
110 NetconfController controller = handler.get(NetconfController.class);
111 MastershipService mastershipService = handler.get(MastershipService.class);
112 DeviceId ncDeviceId = handler.data().deviceId();
113 checkNotNull(controller, "Netconf controller is null");
114
115 if (!mastershipService.isLocalMaster(ncDeviceId)) {
116 log.warn("Not master for {} Use {} to execute command",
117 ncDeviceId,
118 mastershipService.getMasterFor(ncDeviceId));
119 return false;
120 }
121
122 String[] data = target.split(COLON);
123 if (data.length != THREE) {
124 log.error("Invalid number of arguments {}", target);
125 return false;
126 }
127
128 try {
129 int nni = Integer.parseInt(data[FIRST_PART]);
130 if (nni <= ZERO) {
131 log.error("Invalid integer for nnilink-id:{}", target);
132 return false;
133 }
134 } catch (NumberFormatException e) {
135 log.error("Non-number input for nnilink-id:{}", target);
136 return false;
137 }
138
139 if (!checkSetParam(data[SECOND_PART], data[THIRD_PART])) {
140 log.error("Failed to check input {}", target);
141 return false;
142 }
143
144 try {
145 StringBuilder request = new StringBuilder();
146 request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE)
147 .append(ANGLE_RIGHT + NEW_LINE)
148 .append(buildStartTag(VOLT_PORTS))
149 .append(buildStartTag(ETH_NNILINK_PORTS))
150 .append(buildStartTag(ETH_NNILINK_PORT))
151 .append(buildStartTag(NNILINK_ID, false))
152 .append(data[FIRST_PART])
153 .append(buildEndTag(NNILINK_ID))
154
155 .append(buildStartTag(data[SECOND_PART], false))
156 .append(data[THIRD_PART])
157 .append(buildEndTag(data[SECOND_PART]))
158
159 .append(buildEndTag(ETH_NNILINK_PORT))
160 .append(buildEndTag(ETH_NNILINK_PORTS))
161 .append(buildEndTag(VOLT_PORTS))
162 .append(VOLT_NE_CLOSE);
163
164 controller.getDevicesMap()
165 .get(ncDeviceId)
166 .getSession()
167 .editConfig(RUNNING, null, request.toString());
168 } catch (IOException e) {
169 log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
170 return false;
171 }
172 return true;
173 }
174
175 /**
176 * Verifies input string for valid options.
177 *
178 * @param name input data in string
179 * @param value input data in string
180 * @return true/false if the param is valid/invalid
181 */
182 private boolean checkSetParam(String name, String value) {
183 if (!NNILINKPARAMS.contains(name)) {
184 log.error("Unsupported parameter: {}", name);
185 return false;
186 }
187
188 switch (name) {
189 default:
190 if (!ENABLES.contains(value)) {
191 log.error("Invalid value for Name {} : Value {}.", name, value);
192 return false;
193 }
194 break;
195 }
196 return true;
197 }
198
199}