blob: b61916e23f1dea10fa30d96d3245ca9a7b583979 [file] [log] [blame]
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +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.ImmutableList;
20import org.apache.commons.configuration.HierarchicalConfiguration;
21import org.onosproject.drivers.utilities.XmlConfigParser;
22import org.onlab.packet.IpAddress;
23import org.onosproject.mastership.MastershipService;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.behaviour.ControllerConfig;
26import org.onosproject.net.behaviour.ControllerInfo;
27import org.onosproject.net.driver.AbstractHandlerBehaviour;
28import org.onosproject.net.driver.DriverHandler;
29import org.onosproject.netconf.NetconfController;
30import org.slf4j.Logger;
31
32import java.io.ByteArrayInputStream;
33import java.io.IOException;
34import java.nio.charset.StandardCharsets;
35import java.util.ArrayList;
36import java.util.List;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * Implementation to get and set parameters available in VOLT NE
43 * through the Netconf protocol.
44 */
45public class FujitsuVoltControllerConfig extends AbstractHandlerBehaviour
46 implements ControllerConfig {
47
48 private final Logger log = getLogger(FujitsuVoltControllerConfig.class);
49
50 private static final String DOT = ".";
51 private static final String VOLT_NE_NAMESPACE =
52 "xmlns=\"http://fujitsu.com/ns/volt/1.1\"";
53 private static final String DATA = "data";
54 private static final String VOLT_NE = "volt-ne";
55 private static final String VOLT_OFCONFIG = "volt-ofconfig";
56 private static final String OF_CONTROLLERS = "of-controllers";
57 private static final String OF_CONTROLLER = "of-controller";
58 private static final String CONTROLLER_INFO = "controller-info";
59 private static final String REPORT_ALL = "report-all";
60 private static final String IP_ADDRESS = "ip-address";
61 private static final String PORT = "port";
62 private static final String PROTOCOL = "protocol";
63
64 private static final String VOLT_NE_OPEN = "<" + VOLT_NE + " ";
65 private static final String VOLT_NE_CLOSE = "</" + VOLT_NE + ">";
66 private static final String VOLT_OFCONFIG_EL = "<" + VOLT_OFCONFIG + "/>\n";
67
68 private static final String VOLT_DATACONFIG = DATA + DOT + VOLT_NE + DOT +
69 VOLT_OFCONFIG + DOT + OF_CONTROLLERS + DOT + OF_CONTROLLER;
70
71 @Override
72 public List<ControllerInfo> getControllers() {
73 DriverHandler handler = handler();
74 NetconfController controller = handler.get(NetconfController.class);
75 MastershipService mastershipService = handler.get(MastershipService.class);
76 DeviceId ncDeviceId = handler.data().deviceId();
77 checkNotNull(controller, "Netconf controller is null");
78 List<ControllerInfo> controllers = new ArrayList<>();
79 if (mastershipService.isLocalMaster(ncDeviceId)) {
80 try {
81 StringBuilder request = new StringBuilder();
82 request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE).append(">\n");
83 request.append(VOLT_OFCONFIG_EL);
84 request.append(VOLT_NE_CLOSE);
85
86 String reply;
87 reply = controller.
88 getDevicesMap().get(ncDeviceId).getSession().
89 get(request.toString(), REPORT_ALL);
90 log.debug("Reply XML {}", reply);
91 controllers.addAll(parseStreamVoltControllers(XmlConfigParser.
92 loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
93 } catch (IOException e) {
94 log.error("Cannot communicate to device {} ", ncDeviceId);
95 }
96 } else {
97 log.warn("I'm not master for {} please use master, {} to execute command",
98 ncDeviceId,
99 mastershipService.getMasterFor(ncDeviceId));
100 }
101 return ImmutableList.copyOf(controllers);
102 }
103
104 @Override
105 public void setControllers(List<ControllerInfo> controllers) {
106 // TODO update later
107 log.warn("Operation not supported");
108 }
109
110 /**
111 * Parses XML string to get controller information.
112 *
113 * @param cfg a hierarchical configuration
114 * @return a list of controllers
115 */
116 private List<ControllerInfo> parseStreamVoltControllers(HierarchicalConfiguration cfg) {
117 List<ControllerInfo> controllers = new ArrayList<>();
118 List<HierarchicalConfiguration> fields =
119 cfg.configurationsAt(VOLT_DATACONFIG);
120
121 for (HierarchicalConfiguration sub : fields) {
122 List<HierarchicalConfiguration> childFields =
123 sub.configurationsAt(CONTROLLER_INFO);
124
125 for (HierarchicalConfiguration child : childFields) {
126 ControllerInfo controller = new ControllerInfo(
127 IpAddress.valueOf(child.getString(IP_ADDRESS)),
128 Integer.parseInt(child.getString(PORT)),
129 child.getString(PROTOCOL));
130
131 log.debug("VOLT: OFCONTROLLER: PROTOCOL={}, IP={}, PORT={} ",
132 controller.type(), controller.ip(), controller.port());
133 controllers.add(controller);
134 }
135 }
136 return controllers;
137 }
138
139}