Add a Polatis NETCONF driver

Change-Id: Iecfe8df9d656bebbe82bf01bcaf185a77ccd26d8
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisDeviceDescription.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisDeviceDescription.java
new file mode 100644
index 0000000..76c8bdb
--- /dev/null
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisDeviceDescription.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2017-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.polatis.netconf;
+
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DefaultDeviceDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceDescriptionDiscovery;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+
+import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
+
+import org.onlab.util.Frequency;
+import org.onlab.util.Spectrum;
+
+import org.slf4j.Logger;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.net.Device.Type.FIBER_SWITCH;
+
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.netconfGet;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configAt;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configsAt;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlOpen;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlClose;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlEmpty;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PORT;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PORTID;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PORTCONFIG;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PRODINF;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PORTCONFIG_XMLNS;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PRODINF_XMLNS;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_PRODINF;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_PORTCONFIG;
+
+/**
+ * Representation of device information and ports via NETCONF for all Polatis
+ * optical circuit switches.
+ */
+public class PolatisDeviceDescription extends AbstractHandlerBehaviour
+    implements DeviceDescriptionDiscovery {
+
+    public static final String DEFAULT_MANUFACTURER = "Polatis";
+    public static final String DEFAULT_DESCRIPTION_DATA = "Unknown";
+    public static final String KEY_MANUFACTURER = "manufacturer";
+    public static final String KEY_HWVERSION = "model-name";
+    public static final String KEY_SWVERSION = "software-version";
+    public static final String KEY_SERIALNUMBER = "serial-number";
+    public static final String KEY_PORTSTATUS = "status";
+    public static final String PORT_ENABLED = "ENABLED";
+    public static final String KEY_PORTLABEL = "label";
+    public static final int POLATIS_NUM_OF_WAVELENGTHS = 39;
+
+    private final Logger log = getLogger(getClass());
+
+    /**
+     * Discovers device details, for polatis device by getting the system
+     * information.
+     *
+     * @return device description
+     */
+    @Override
+    public DeviceDescription discoverDeviceDetails() {
+        return parseProductInformation();
+    }
+
+    private DeviceDescription parseProductInformation() {
+        DeviceService devsvc = checkNotNull(handler().get(DeviceService.class));
+        DeviceId devid = handler().data().deviceId();
+        Device dev = devsvc.getDevice(devid);
+        if (dev == null) {
+            return new DefaultDeviceDescription(dev.id().uri(), FIBER_SWITCH,
+                    DEFAULT_MANUFACTURER, DEFAULT_DESCRIPTION_DATA,
+                    DEFAULT_DESCRIPTION_DATA, DEFAULT_DESCRIPTION_DATA,
+                    dev.chassisId());
+        }
+        String reply = netconfGet(handler(), getProductInformationFilter());
+        HierarchicalConfiguration cfg = configAt(reply, KEY_DATA_PRODINF);
+        return new DefaultDeviceDescription(dev.id().uri(), FIBER_SWITCH,
+                cfg.getString(KEY_MANUFACTURER), cfg.getString(KEY_HWVERSION),
+                cfg.getString(KEY_SWVERSION), cfg.getString(KEY_SERIALNUMBER),
+                dev.chassisId());
+    }
+
+    private String getProductInformationFilter() {
+        return new StringBuilder(xmlOpen(KEY_PRODINF_XMLNS))
+                .append(xmlClose(KEY_PRODINF))
+                .toString();
+    }
+
+    /**
+     * Discovers port details, for polatis device.
+     *
+     * @return port list
+     */
+    @Override
+    public List<PortDescription> discoverPortDetails() {
+        String reply = netconfGet(handler(), getPortsFilter());
+        List<PortDescription> descriptions = parsePorts(reply);
+        return ImmutableList.copyOf(descriptions);
+    }
+
+    private String getPortsFilter() {
+        return new StringBuilder(xmlOpen(KEY_PORTCONFIG_XMLNS))
+                .append(xmlOpen(KEY_PORT))
+                .append(xmlEmpty(KEY_PORTID))
+                .append(xmlEmpty(KEY_PORTSTATUS))
+                .append(xmlEmpty(KEY_PORTLABEL))
+                .append(xmlClose(KEY_PORT))
+                .append(xmlClose(KEY_PORTCONFIG))
+                .toString();
+    }
+
+    private List<PortDescription> parsePorts(String content) {
+        List<HierarchicalConfiguration> subtrees = configsAt(content, KEY_DATA_PORTCONFIG);
+        List<PortDescription> portDescriptions = Lists.newArrayList();
+        for (HierarchicalConfiguration portConfig : subtrees) {
+            portDescriptions.add(parsePort(portConfig));
+        }
+        return portDescriptions;
+    }
+
+    private PortDescription parsePort(HierarchicalConfiguration cfg) {
+        PortNumber portNumber = PortNumber.portNumber(cfg.getLong(KEY_PORTID));
+        DefaultAnnotations annotations = DefaultAnnotations.builder()
+                .set(AnnotationKeys.PORT_NAME, cfg.getString(KEY_PORTLABEL))
+                .build();
+        return omsPortDescription(portNumber,
+                cfg.getString(KEY_PORTSTATUS).equals(PORT_ENABLED),
+                Spectrum.O_BAND_MIN, Spectrum.L_BAND_MAX,
+                Frequency.ofGHz((Spectrum.O_BAND_MIN.asGHz() -
+                        Spectrum.L_BAND_MAX.asGHz()) /
+                    POLATIS_NUM_OF_WAVELENGTHS), annotations);
+    }
+}
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisDriversLoader.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisDriversLoader.java
new file mode 100644
index 0000000..7548eaf
--- /dev/null
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisDriversLoader.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2017-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.polatis.netconf;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+/**
+ * Loader for Polatis device drivers.
+ */
+@Component(immediate = true)
+public class PolatisDriversLoader extends AbstractDriverLoader {
+
+    public PolatisDriversLoader() {
+        super("/polatis-drivers.xml");
+    }
+}
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisFlowRuleProgrammable.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisFlowRuleProgrammable.java
new file mode 100644
index 0000000..0b0dc65
--- /dev/null
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisFlowRuleProgrammable.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2017 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.polatis.netconf;
+
+import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.CrossConnects;
+import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.crossconnects.Pair;
+
+import com.google.common.collect.ImmutableList;
+
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.flow.DefaultFlowEntry;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleProgrammable;
+import org.onosproject.netconf.NetconfException;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.netconfGet;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.netconfEditConfig;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.opticalRevision;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configsAt;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlOpen;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlClose;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_CONNS;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_CONNS_XMLNS;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_CONNS;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Flow rule programmable behaviour for polatis optical netconf devices.
+ */
+public class PolatisFlowRuleProgrammable
+    extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
+
+    public static final String KEY_CHID = "wavelength-id";
+    public static final String KEY_SRC = "ingress";
+    public static final String KEY_DST = "egress";
+    public static final String KEY_SRC_CHID = String.format("%s.%s", KEY_SRC, KEY_CHID);
+    public static final String CFG_MODE_MERGE = "merge";
+    public static final String CFG_MODE_DELETE = "delete";
+    public static final String KEY_PAIR = "pair";
+    public static final String KEY_PAIRS = "pairs";
+    public static final String KEY_PAIR_DELETE = String.format("%s %s", KEY_PAIR, CFG_MODE_DELETE);
+    public static final String PAIR_COMPAT_REVISION = "2017-08-04";
+
+    private static final Logger log = getLogger(PolatisFlowRuleProgrammable.class);
+
+    @Override
+    public Collection<FlowEntry> getFlowEntries() {
+        return parseConnections();
+    }
+
+    @Override
+    public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
+        return applyConnections(rules);
+    }
+
+    @Override
+    public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
+        return removeConnections(rules);
+    }
+
+    private String getConnectionsFilter() {
+        return new StringBuilder(xmlOpen(KEY_CONNS_XMLNS))
+                .append(xmlClose(KEY_CONNS))
+                .toString();
+    }
+
+    private Collection<FlowEntry> parseConnections() {
+        log.debug("Fetch connections...");
+        String reply = netconfGet(handler(), getConnectionsFilter());
+        final String keyPairMode = String.format("%s.%s", KEY_DATA_CONNS, parseKeyPairCompat());
+        List<HierarchicalConfiguration> subtrees = configsAt(reply, keyPairMode);
+        ImmutableList.Builder<FlowEntry> connectionsBuilder = ImmutableList.builder();
+        for (HierarchicalConfiguration connection : subtrees) {
+            connectionsBuilder.add(new DefaultFlowEntry(parseConnection(connection), FlowEntry.FlowEntryState.ADDED));
+        }
+        return connectionsBuilder.build();
+    }
+
+    private FlowRule parseConnection(HierarchicalConfiguration cfg) {
+        return PolatisOpticalUtility.toFlowRule(this,
+                PortNumber.portNumber(cfg.getInt(KEY_SRC)),
+                PortNumber.portNumber(cfg.getInt(KEY_DST)));
+    }
+
+    private Collection<FlowRule> applyConnections(Collection<FlowRule> rules) {
+        return rules.stream()
+                .filter(c -> editConnection(c, CFG_MODE_MERGE))
+                .collect(Collectors.toList());
+    }
+
+    private boolean editConnection(FlowRule rule, String mode) {
+        CrossConnects crossConnects = PolatisOpticalUtility.fromFlowRule(this, rule);
+        final StringBuilder cfg = new StringBuilder(xmlOpen(KEY_CONNS_XMLNS));
+        List<Pair> pairs = crossConnects.pair();
+        final String keyPairCompat = parseKeyPairCompat();
+        final String keyPairMode = String.format("%s operation=\"%s\"", keyPairCompat, mode);
+        pairs.forEach(p -> {
+                cfg.append(xmlOpen(keyPairMode))
+                .append(xmlOpen(KEY_SRC))
+                .append(p.ingress())
+                .append(xmlClose(KEY_SRC))
+                .append(xmlOpen(KEY_DST))
+                .append(p.egress())
+                .append(xmlClose(KEY_DST))
+                .append(xmlClose(keyPairCompat));
+        });
+        cfg.append(xmlClose(KEY_CONNS));
+        return netconfEditConfig(handler(), null, cfg.toString());
+    }
+
+    private Collection<FlowRule> removeConnections(Collection<FlowRule> rules) {
+        return rules.stream()
+                .filter(c -> editConnection(c, CFG_MODE_DELETE))
+                .collect(Collectors.toList());
+    }
+
+    private String parseKeyPairCompat() {
+        String rev = opticalRevision(handler());
+        if (rev == null) {
+            throw new RuntimeException(new NetconfException("Failed to obtain the revision."));
+        }
+        String keyPairCompat;
+        try {
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+            Date date = sdf.parse(PAIR_COMPAT_REVISION);
+
+            if (date.compareTo(sdf.parse(rev)) > 0) {
+                keyPairCompat = KEY_PAIRS;
+            } else {
+                keyPairCompat = KEY_PAIR;
+            }
+        } catch (ParseException e) {
+            throw new RuntimeException(new NetconfException(String.format("Incorrect date format: %s", rev)));
+        }
+        return keyPairCompat;
+    }
+}
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisNetconfUtility.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisNetconfUtility.java
new file mode 100644
index 0000000..8a6b89a
--- /dev/null
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisNetconfUtility.java
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2017 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.polatis.netconf;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.onosproject.drivers.utilities.XmlConfigParser;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.netconf.DatastoreId;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+
+import org.slf4j.Logger;
+
+import java.util.List;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Netconf utility for polatis netconf drivers.
+ */
+public final class PolatisNetconfUtility {
+
+    public static final String KEY_XMLNS = "xmlns=\"http://www.polatis.com/yang/optical-switch\"";
+    public static final String KEY_DATA = "data";
+    public static final String KEY_PORT = "port";
+    public static final String KEY_PORTID = "port-id";
+    public static final String KEY_PORTCONFIG = "port-config";
+    public static final String KEY_CONNS = "cross-connects";
+    public static final String KEY_PRODINF = "product-information";
+    public static final String KEY_PORTCONFIG_XMLNS = String.format("%s %s", KEY_PORTCONFIG, KEY_XMLNS);
+    public static final String KEY_CONNS_XMLNS = String.format("%s %s", KEY_CONNS, KEY_XMLNS);
+    public static final String KEY_PRODINF_XMLNS = String.format("%s %s", KEY_PRODINF, KEY_XMLNS);
+    public static final String KEY_DATA_CONNS = String.format("%s.%s", KEY_DATA, KEY_CONNS);
+    public static final String KEY_DATA_PRODINF = String.format("%s.%s", KEY_DATA, KEY_PRODINF);
+    public static final String KEY_DATA_PORTCONFIG = String.format("%s.%s.%s", KEY_DATA, KEY_PORTCONFIG, KEY_PORT);
+    public static final String KEY_OPM = "opm-power";
+    public static final String KEY_OPM_XMLNS = String.format("%s %s", KEY_OPM, KEY_XMLNS);
+    public static final String KEY_POWER = "power";
+    public static final String KEY_DATA_OPM_PORT = String.format("%s.%s.%s", KEY_DATA, KEY_OPM, KEY_PORT);
+    public static final String OPTICAL_CAPABILITY_PREFIX
+                        = "http://www.polatis.com/yang/optical-switch?module=optical-switch&amp;revision=";
+
+    private static final Logger log = getLogger(PolatisFlowRuleProgrammable.class);
+
+    private PolatisNetconfUtility() {
+    }
+
+    /**
+     * Retrieves session reply information for get operation.
+     *
+     * @param handler parent driver handler
+     * @param filter the filter string of xml content
+     * @return the reply string
+     */
+    public static String netconfGet(DriverHandler handler, String filter) {
+        NetconfSession session = getNetconfSession(handler);
+        String reply;
+        try {
+            reply = session.get(filter, null);
+        } catch (NetconfException e) {
+            throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
+        }
+        return reply;
+    }
+
+    /**
+     * Retrieves session reply information for get config operation.
+     *
+     * @param handler parent driver handler
+     * @param filter the filter string of xml content
+     * @return the reply string
+     */
+    public static String netconfGetConfig(DriverHandler handler, String filter) {
+        NetconfSession session = getNetconfSession(handler);
+        String reply;
+        try {
+            reply = session.getConfig(DatastoreId.RUNNING, filter);
+        } catch (NetconfException e) {
+            throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
+        }
+        return reply;
+    }
+
+    /**
+     * Retrieves session reply information for edit config operation.
+     *
+     * @param handler parent driver handler
+     * @param mode selected mode to change the configuration
+     * @param cfg the new configuration to be set
+     * @return the reply string
+     */
+    public static boolean netconfEditConfig(DriverHandler handler, String mode, String cfg) {
+        NetconfSession session = getNetconfSession(handler);
+        boolean reply = false;
+        try {
+            reply = session.editConfig(DatastoreId.RUNNING, mode, cfg);
+        } catch (NetconfException e) {
+            throw new RuntimeException(new NetconfException("Failed to edit configuration.", e));
+        }
+        return reply;
+    }
+
+    /**
+     * Retrieves specified node hierarchical configuration from the xml information.
+     *
+     * @param content the xml information
+     * @param key the configuration key node
+     * @return the hierarchical configuration, null if exception happens
+     */
+    public static HierarchicalConfiguration configAt(String content, String key) {
+        HierarchicalConfiguration info;
+        try {
+            HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
+            info = cfg.configurationAt(key);
+        } catch (IllegalArgumentException e) {
+            // Accept null for information polling
+            return null;
+        }
+        return info;
+    }
+
+    /**
+     * Retrieves specified node hierarchical configurations from the xml information.
+     *
+     * @param content the xml information
+     * @param key the configuration key node
+     * @return the hierarchical configurations, empty if exception happens
+     */
+    public static List<HierarchicalConfiguration> configsAt(String content, String key) {
+        List<HierarchicalConfiguration> info;
+        try {
+            HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
+            info = cfg.configurationsAt(key);
+        } catch (IllegalArgumentException e) {
+            // Accept empty for information polling
+            return ImmutableList.of();
+        }
+        return info;
+    }
+
+    /**
+     * Makes a xml format sentence.
+     *
+     * @param node the node name
+     * @param content the node content
+     * @return the xml format sentence
+     */
+    public static String xml(String node, String content) {
+        return String.format("<%s>%s</%s>", node, content, node);
+    }
+
+    /**
+     * Makes a xml format open tag.
+     *
+     * @param node the node name
+     * @return the xml head format string
+     */
+    public static String xmlOpen(String node) {
+        return String.format("<%s>", node);
+    }
+
+    /**
+     * Makes a xml format close tag.
+     *
+     * @param node the node name
+     * @return the xml end format string
+     */
+    public static String xmlClose(String node) {
+        return String.format("</%s>", node);
+    }
+
+    /**
+     * Makes a xml format empty tag.
+     *
+     * @param node the node name
+     * @return the xml format of empty tag
+     */
+    public static String xmlEmpty(String node) {
+        return String.format("<%s/>", node);
+    }
+
+    public static String opticalRevision(DriverHandler handler) {
+        NetconfSession session = getNetconfSession(handler);
+        Set<String> capabilities = session.getDeviceCapabilitiesSet();
+        for (String c : capabilities) {
+            if (c.startsWith(OPTICAL_CAPABILITY_PREFIX)) {
+                return c.substring(OPTICAL_CAPABILITY_PREFIX.length());
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Returns the NETCONF session of the device.
+     *
+     * @return session
+     */
+    private static NetconfSession getNetconfSession(DriverHandler handler) {
+        NetconfController controller = checkNotNull(handler.get(NetconfController.class));
+        NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
+        if (session == null) {
+            throw new RuntimeException(new NetconfException("Failed to retrieve the netconf device."));
+        }
+        return session;
+    }
+}
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisOpticalUtility.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisOpticalUtility.java
new file mode 100644
index 0000000..bac9f63
--- /dev/null
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisOpticalUtility.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2017 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.polatis.netconf;
+
+import com.google.common.collect.Range;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.driver.HandlerBehaviour;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleService;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.criteria.PortCriterion;
+import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.flow.instructions.Instructions;
+
+import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.CrossConnects;
+import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.crossconnects.Pair;
+import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.crossconnects.DefaultPair;
+import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.DefaultCrossConnects;
+import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.PortFormat;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Polatis optical utilities.
+ */
+public final class PolatisOpticalUtility {
+
+    private static final int DEFAULT_PRIORITY = 88;
+    private static final String DEFAULT_APP = "org.onosproject.drivers.polatis.netconf";
+    public static final int POWER_MULTIPLIER = 100;
+    public static final Range<Long> POWER_RANGE = Range.closed(-6000L, 2800L);
+
+    private PolatisOpticalUtility() {
+    }
+
+    /**
+     * Transforms a flow FlowRule object to a cross-connect object.
+     * @param behaviour the parent driver handler
+     * @param rule FlowRule object
+     * @return cross connect object
+     */
+    public static CrossConnects fromFlowRule(HandlerBehaviour behaviour, FlowRule rule) {
+        // TrafficSelector
+        Set<Criterion> criterions = rule.selector().criteria();
+        PortNumber inPort = criterions.stream()
+                .filter(c -> c instanceof PortCriterion)
+                .map(c -> ((PortCriterion) c).port())
+                .findAny()
+                .orElse(null);
+        // TrafficTreatment
+        List<Instruction> instructions = rule.treatment().immediate();
+        PortNumber outPort = instructions.stream()
+                .filter(c -> c instanceof Instructions.OutputInstruction)
+                .map(c -> ((Instructions.OutputInstruction) c).port())
+                .findAny()
+                .orElse(null);
+        DefaultCrossConnects crossConnects = new DefaultCrossConnects();
+        DefaultPair p = new DefaultPair();
+        p.ingress(new PortFormat(inPort.toLong()));
+        p.egress(new PortFormat(outPort.toLong()));
+        crossConnects.addToPair(p);
+        return crossConnects;
+    }
+
+    /**
+     * Finds the FlowRule from flow rule store by the given cross connect information.
+     * Returns an extra flow to remove the flow by ONOS if not found.
+     * @param behaviour the parent driver handler
+     * @param cfg cross connect information
+     * @return the flow rule
+     */
+    public static FlowRule toFlowRule(HandlerBehaviour behaviour, CrossConnects cfg) {
+        // Note: do we need to handle more than one pair? In any case, this
+        // looks strange.
+        Pair p = cfg.pair().get(0);
+        long i = p.ingress().uint32();
+        long o = p.egress().uint32();
+        PortNumber iPortNumber = PortNumber.portNumber(i);
+        PortNumber oPortNumber = PortNumber.portNumber(o);
+        return toFlowRule(behaviour, iPortNumber, oPortNumber);
+    }
+
+    /**
+     * Finds the FlowRule from flow rule store by the given ports and channel.
+     * Returns an extra flow to remove the flow by ONOS if not found.
+     * @param behaviour the parent driver handler
+     * @param inPort the input port
+     * @param outPort the output port
+     * @return the flow rule
+     */
+    public static FlowRule toFlowRule(HandlerBehaviour behaviour, PortNumber inPort,
+                                      PortNumber outPort) {
+        FlowRuleService service = behaviour.handler().get(FlowRuleService.class);
+        Iterable<FlowEntry> entries = service.getFlowEntries(behaviour.data().deviceId());
+        // Try to Find the flow from flow rule store.
+        for (FlowEntry entry : entries) {
+            Set<Criterion> criterions = entry.selector().criteria();
+            // input port
+            PortNumber ip = criterions.stream()
+                    .filter(c -> c instanceof PortCriterion)
+                    .map(c -> ((PortCriterion) c).port())
+                    .findAny()
+                    .orElse(null);
+            // output port
+            PortNumber op = entry.treatment().immediate().stream()
+                    .filter(c -> c instanceof Instructions.OutputInstruction)
+                    .map(c -> ((Instructions.OutputInstruction) c).port())
+                    .findAny()
+                    .orElse(null);
+            if (inPort.equals(ip) && outPort.equals(op)) {
+                // Find the flow.
+                return entry;
+            }
+        }
+        // Cannot find the flow from store. So report an extra flow to remove the flow by ONOS.
+        TrafficSelector selector = DefaultTrafficSelector.builder()
+                .matchInPort(inPort)
+                .build();
+        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
+                .setOutput(outPort)
+                .build();
+        return DefaultFlowRule.builder()
+                .forDevice(behaviour.data().deviceId())
+                .withSelector(selector)
+                .withTreatment(treatment)
+                .makePermanent()
+                .withPriority(DEFAULT_PRIORITY)
+                .fromApp(behaviour.handler().get(CoreService.class).getAppId(DEFAULT_APP))
+                .build();
+
+    }
+}
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisPowerConfig.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisPowerConfig.java
new file mode 100644
index 0000000..d382b90
--- /dev/null
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisPowerConfig.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2017 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.polatis.netconf;
+
+import com.google.common.collect.Range;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.onosproject.net.Direction;
+import org.onosproject.net.OchSignal;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.PowerConfig;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.slf4j.Logger;
+
+import java.util.Optional;
+
+import static org.onosproject.drivers.polatis.netconf.PolatisOpticalUtility.POWER_MULTIPLIER;
+import static org.onosproject.drivers.polatis.netconf.PolatisOpticalUtility.POWER_RANGE;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.*;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Get current or target port/channel power from a Polatis optical netconf device.
+ * Set target port power or channel attenuation to an optical netconf device.
+ */
+public class PolatisPowerConfig<T> extends AbstractHandlerBehaviour
+    implements PowerConfig<T> {
+
+    private static final Logger log = getLogger(PolatisPowerConfig.class);
+
+    @Override
+    public Optional<Long> getTargetPower(PortNumber port, T component) {
+        return Optional.ofNullable(acquireTargetPower(port, component));
+    }
+
+    @Override
+    public void setTargetPower(PortNumber port, T component, long power) {
+        if (component instanceof OchSignal) {
+            log.warn("Channel power is not applicable.");
+            return;
+        }
+        setPortTargetPower(port, power);
+    }
+
+    @Override
+    public Optional<Long> currentPower(PortNumber port, T component) {
+        return Optional.ofNullable(acquireCurrentPower(port, component));
+    }
+
+    @Override
+    public Optional<Range<Long>> getTargetPowerRange(PortNumber port, T component) {
+        return Optional.ofNullable(getTxPowerRange(port, component));
+    }
+
+    @Override
+    public Optional<Range<Long>> getInputPowerRange(PortNumber port, T component) {
+        return Optional.ofNullable(getRxPowerRange(port, component));
+    }
+
+    private String getPortPowerFilter(PortNumber port) {
+        return new StringBuilder(xmlOpen(KEY_OPM_XMLNS))
+                .append(xmlOpen(KEY_PORT))
+                .append(xmlOpen(KEY_PORTID))
+                .append(port.toLong())
+                .append(xmlClose(KEY_PORTID))
+                .append(xmlClose(KEY_PORT))
+                .append(xmlClose(KEY_OPM))
+                .toString();
+    }
+
+    private Long acquireTargetPower(PortNumber port, T component) {
+        if (component instanceof OchSignal) {
+            log.warn("Channel power is not applicable.");
+            return null;
+        }
+        log.debug("Get port{} target power...", port);
+        log.warn("This is currently unimplemented");
+        return null;
+    }
+
+    private Long acquireCurrentPower(PortNumber port, T component) {
+        if (component instanceof OchSignal) {
+            log.warn("Channel power is not applicable.");
+            return null;
+        }
+        log.debug("Get port{} current power...", port);
+        return acquirePortPower(port);
+    }
+
+    private Long acquirePortPower(PortNumber port) {
+        String filter = getPortPowerFilter(port);
+        String reply = netconfGet(handler(), filter);
+        HierarchicalConfiguration info = configAt(reply, KEY_DATA_OPM_PORT);
+        if (info == null) {
+            return null;
+        }
+        return (long) (info.getDouble(KEY_POWER) * POWER_MULTIPLIER);
+    }
+
+    private boolean setPortTargetPower(PortNumber port, long power) {
+        log.debug("Set port{} target power...", port);
+        log.warn("This is currently unimplemented");
+        return false;
+    }
+
+    private Range<Long> getPowerRange() {
+        return POWER_RANGE;
+    }
+
+    private Range<Long> getTxPowerRange(PortNumber port, T component) {
+        if (component instanceof Direction) {
+            log.debug("Get target port{} power range...", port);
+            return getPowerRange();
+        } else {
+            log.debug("Get channel attenuation range...");
+            log.warn("Channel power is not applicable.");
+            return null;
+        }
+    }
+
+    private Range<Long> getRxPowerRange(PortNumber port, T component) {
+        log.debug("Get input port{} power range...", port);
+        return getPowerRange();
+    }
+}
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/package-info.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/package-info.java
new file mode 100644
index 0000000..53dad3c
--- /dev/null
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-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 for Polatis device drivers.
+ */
+package org.onosproject.drivers.polatis;