Supports ControllerConfig behaviour for juniper.
Juniper drivers does not have ControllerConfig yet.
ONOS-7738
Change-Id: I2ecbb039e94266a87bb3d90a6949a49f092c194d
diff --git a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/ControllerConfigJuniperImpl.java b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/ControllerConfigJuniperImpl.java
new file mode 100644
index 0000000..500c091
--- /dev/null
+++ b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/ControllerConfigJuniperImpl.java
@@ -0,0 +1,244 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.juniper;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.drivers.juniper.JuniperUtils.cliDeleteRequestBuilder;
+import static org.onosproject.drivers.juniper.JuniperUtils.cliSetRequestBuilder;
+import static org.onosproject.drivers.juniper.JuniperUtils.getOpenFlowControllersFromConfig;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import com.google.common.collect.Lists;
+import org.onosproject.drivers.utilities.XmlConfigParser;
+
+import java.util.List;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.ControllerConfig;
+import org.onosproject.net.behaviour.ControllerInfo;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfDevice;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.slf4j.Logger;
+
+import java.io.ByteArrayInputStream;
+
+/**
+ * Set and get the openflow controller information via NETCONF for Juniper Switch. *
+ */
+public class ControllerConfigJuniperImpl extends AbstractHandlerBehaviour implements ControllerConfig {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String RPC_TAG_NETCONF_BASE = "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">";
+ private static final String RPC_CLOSE_TAG = "</rpc>";
+
+ @Override
+ public List<ControllerInfo> getControllers() {
+ List<ControllerInfo> controllers = Lists.newArrayList();
+ String reply = retrieveResultCommand(buildRpcGetOpenFlowController());
+
+ if (reply == null || (reply != null && reply.isEmpty())) {
+ log.error("Cannot get the controllers from switch");
+ } else {
+ controllers = getOpenFlowControllersFromConfig(XmlConfigParser
+ .loadXml(new ByteArrayInputStream(reply.getBytes())));
+ log.debug("controllers {}", controllers);
+ }
+
+ return controllers;
+ }
+
+ @Override
+ public void setControllers(List<ControllerInfo> controllers) {
+ if (!requestCommand(buildRpcSetOpenFlowController(controllers))) {
+ log.error("Cannot set the controllers to switch");
+ }
+ }
+
+ @Override
+ public void removeControllers(List<ControllerInfo> controllers) {
+ if (!requestCommand(buildRpcRemoveOpenFlowController())) {
+ log.error("Cannot remove the controllers from switch");
+ }
+ }
+
+ private String buildRpcGetOpenFlowController() {
+ StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
+
+ rpc.append("<get-configuration>");
+ rpc.append("<configuration>");
+ rpc.append("<protocols>");
+ rpc.append("<openflow>");
+ rpc.append("<mode>");
+ rpc.append("<ofagent-mode>");
+ rpc.append("<controller>");
+ rpc.append("</controller>");
+ rpc.append("</ofagent-mode>");
+ rpc.append("</mode>");
+ rpc.append("</openflow>");
+ rpc.append("</protocols>");
+ rpc.append("</configuration>");
+ rpc.append("</get-configuration>");
+ rpc.append(RPC_CLOSE_TAG);
+ rpc.append("]]>]]>");
+
+ return rpc.toString();
+ }
+
+ private String buildRpcSetOpenFlowController(List<ControllerInfo> controllers) {
+ StringBuilder request = new StringBuilder();
+
+ request.append("<protocols>");
+ request.append("<openflow operation=\"delete\"/>");
+ request.append("</protocols>");
+
+ request.append("<protocols>");
+ request.append("<openflow>");
+ request.append("<mode>");
+ request.append("<ofagent-mode>");
+
+ request.append("<controller>");
+ for (int i = 0; i < controllers.size(); i++) {
+ request.append("<ip>");
+ request.append("<name>");
+ request.append(controllers.get(i).ip().toString());
+ request.append("</name>");
+ request.append("<protocol>");
+ request.append("<tcp>");
+ request.append("<port>");
+ request.append(Integer.toString(controllers.get(i).port()));
+ request.append("</port>");
+ request.append("</tcp>");
+ request.append("</protocol>");
+ request.append("</ip>");
+ }
+ request.append("</controller>");
+ request.append("</ofagent-mode>");
+ request.append("</mode>");
+ request.append("</openflow>");
+ request.append("</protocols>");
+
+ return cliSetRequestBuilder(request);
+ }
+
+ private String buildRpcRemoveOpenFlowController() {
+ StringBuilder request = new StringBuilder();
+
+ request.append("<protocols>");
+ request.append("<openflow>");
+ request.append("<mode>");
+ request.append("<ofagent-mode>");
+ request.append("<controller operation=\"delete\"/>");
+ request.append("</ofagent-mode>");
+ request.append("</mode>");
+ request.append("</openflow>");
+ request.append("</protocols>");
+
+ return cliDeleteRequestBuilder(request);
+ }
+
+ private String buildCommit() {
+ StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
+
+ rpc.append("<commit/>");
+ rpc.append(RPC_CLOSE_TAG);
+ rpc.append("]]>]]>");
+
+ return rpc.toString();
+ }
+
+ private String buildDiscardChanges() {
+ StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
+
+ rpc.append("<discard-changes/>");
+ rpc.append(RPC_CLOSE_TAG);
+ rpc.append("]]>]]>");
+
+ return rpc.toString();
+ }
+
+ private NetconfSession getSession() {
+ NetconfController controller = checkNotNull(handler().get(NetconfController.class));
+ DeviceId deviceId = handler().data().deviceId();
+ NetconfDevice device = controller.getDevicesMap().get(deviceId);
+
+ if (device == null) {
+ log.error("Cannot find the netconf device : {}", deviceId);
+ return null;
+ }
+
+ return device.getSession();
+ }
+
+ private String retrieveResultCommand(String command) {
+ NetconfSession session = getSession();
+ String reply;
+
+ if (session == null) {
+ log.error("Cannot get session : {}", command);
+ return null;
+ }
+
+ try {
+ reply = session.requestSync(command).trim();
+ log.debug(reply);
+ } catch (NetconfException e) {
+ log.debug(e.getMessage());
+ return null;
+ }
+
+ return reply;
+ }
+
+ private boolean requestCommand(String command) {
+ NetconfSession session = getSession();
+
+ if (session == null) {
+ log.error("Cannot get session : {}", command);
+ return false;
+ }
+
+ try {
+ String reply = session.requestSync(command).trim();
+ log.debug(reply);
+
+ if (!isOK(reply)) {
+ log.error("discard changes {}", reply);
+ session.requestSync(buildDiscardChanges());
+ return false;
+ }
+
+ reply = session.requestSync(buildCommit()).trim();
+ log.debug(reply);
+ } catch (NetconfException e) {
+ log.debug(e.getMessage());
+ return false;
+ }
+
+ return true;
+ }
+
+ private boolean isOK(String reply) {
+ if (reply != null && reply.indexOf("<ok/>") >= 0) {
+ return true;
+ }
+ return false;
+ }
+}
\ No newline at end of file
diff --git a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/JuniperUtils.java b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/JuniperUtils.java
index 4dae93b..e283f3a 100644
--- a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/JuniperUtils.java
+++ b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/JuniperUtils.java
@@ -21,6 +21,7 @@
import org.onlab.packet.ChassisId;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.Ip4Prefix;
+import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.ConnectPoint;
@@ -31,6 +32,7 @@
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.Port.Type;
+import org.onosproject.net.behaviour.ControllerInfo;
import org.onosproject.net.device.DefaultDeviceDescription;
import org.onosproject.net.device.DefaultPortDescription;
import org.onosproject.net.device.DeviceDescription;
@@ -92,6 +94,10 @@
private static final String IF_TYPE = "if-type";
private static final String SPEED = "speed";
private static final String NAME = "name";
+ private static final String PORT = "port";
+ private static final String PROTOCOL = "protocol";
+
+ private static final String TCP = "tcp";
// seems to be unique index within device
private static final String SNMP_INDEX = "snmp-index";
@@ -608,4 +614,88 @@
toFlowRulePriority(Integer.parseInt(metric))));
}
}
+
+ /**
+ * Helper method to build a XML schema for a given "set/merge" Op inJunOS XML Format.
+ *
+ * @param request a CLI command
+ * @return string containing the XML schema
+ */
+ public static String cliSetRequestBuilder(StringBuilder request) {
+ StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
+ rpc.append("<edit-config>");
+ rpc.append("<target><candidate/></target><config>");
+ rpc.append("<configuration>");
+ rpc.append(request);
+ rpc.append("</configuration>");
+ rpc.append("</config></edit-config>");
+ rpc.append(RPC_CLOSE_TAG);
+ rpc.append("]]>]]>");
+ return rpc.toString();
+ }
+
+ /**
+ * Helper method to build a XML schema for a given "delete Op" in JunOS XML Format.
+ *
+ * @param request a CLI command
+ * @return string containing the XML schema
+ */
+ public static String cliDeleteRequestBuilder(StringBuilder request) {
+ StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
+ rpc.append("<edit-config>");
+ rpc.append("<target><candidate/></target>");
+ rpc.append("<default-operation>none</default-operation>");
+ rpc.append("<config>");
+ rpc.append("<configuration>");
+ rpc.append(request);
+ rpc.append("</configuration>");
+ rpc.append("</config></edit-config>");
+ rpc.append(RPC_CLOSE_TAG);
+ rpc.append("]]>]]>");
+ return rpc.toString();
+ }
+
+ public static List<ControllerInfo> getOpenFlowControllersFromConfig(HierarchicalConfiguration cfg) {
+ List<ControllerInfo> controllers = new ArrayList<ControllerInfo>();
+ String ipKey = "configuration.protocols.openflow.mode.ofagent-mode.controller.ip";
+
+ if (!cfg.configurationsAt(ipKey).isEmpty()) {
+ List<HierarchicalConfiguration> ipNodes = cfg.configurationsAt(ipKey);
+
+ ipNodes.forEach(ipNode -> {
+ int port = 0;
+ String proto = UNKNOWN;
+ HierarchicalConfiguration protocolNode = ipNode.configurationAt(PROTOCOL);
+ HierarchicalConfiguration tcpNode = protocolNode.configurationAt(TCP);
+
+ if (!tcpNode.isEmpty()) {
+ String portString = tcpNode.getString(PORT);
+
+ if (portString != null && !portString.isEmpty()) {
+ port = Integer.parseInt(portString);
+ }
+
+ proto = TCP;
+ }
+
+ String ipaddress = ipNode.getString(NAME);
+
+ if (ipaddress == null) {
+ ipaddress = UNKNOWN;
+ }
+
+ if (ipaddress.equals(UNKNOWN) || proto.equals(UNKNOWN) || port == 0) {
+ log.error("Controller infomation is invalid. Skip this controller node." +
+ " ipaddress: {}, proto: {}, port: {}", ipaddress, proto, port);
+ return;
+ }
+
+ controllers.add(new ControllerInfo(IpAddress.valueOf(ipaddress), port, proto));
+ });
+ } else {
+ log.error("Controller not present");
+ }
+
+ return controllers;
+ }
}
diff --git a/drivers/juniper/src/main/resources/juniper-drivers.xml b/drivers/juniper/src/main/resources/juniper-drivers.xml
index 74885c8..54faaff 100644
--- a/drivers/juniper/src/main/resources/juniper-drivers.xml
+++ b/drivers/juniper/src/main/resources/juniper-drivers.xml
@@ -23,5 +23,7 @@
impl="org.onosproject.drivers.juniper.FlowRuleJuniperImpl"/>
<behaviour api="org.onosproject.net.behaviour.LinkDiscovery"
impl="org.onosproject.drivers.juniper.LinkDiscoveryJuniperImpl"/>
+ <behaviour api="org.onosproject.net.behaviour.ControllerConfig"
+ impl="org.onosproject.drivers.juniper.ControllerConfigJuniperImpl"/>
</driver>
</drivers>