Implements [CORD-546]

Changes:
- Adds new l2 tunnel and l2 tunnel policy;
- Extends cfg of the apps;
- Implements CRUD interfaces through netcfg;

Change-Id: I1a295da605e2496f4883f63fa5f6eca16e627d43
diff --git a/src/main/java/org/onosproject/segmentrouting/pwaas/DefaultL2Tunnel.java b/src/main/java/org/onosproject/segmentrouting/pwaas/DefaultL2Tunnel.java
new file mode 100644
index 0000000..8334a18
--- /dev/null
+++ b/src/main/java/org/onosproject/segmentrouting/pwaas/DefaultL2Tunnel.java
@@ -0,0 +1,194 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.segmentrouting.pwaas;
+
+
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.MplsLabel;
+import org.onlab.packet.VlanId;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Implementation of the default l2 tunnel.
+ */
+public class DefaultL2Tunnel {
+
+    /**
+     * Mode of the pseudo wire.
+     */
+    private L2Mode pwMode;
+    /**
+     * Service delimiting tag.
+     */
+    private VlanId sdTag;
+    /**
+     * Tunnel id.
+     */
+    private long tunnelId;
+    /**
+     * Pseudo wire label.
+     */
+    private MplsLabel pwLabel;
+    /**
+     * Inter-CO label.
+     */
+    private MplsLabel interCoLabel;
+
+    /**
+     * Creates a inter-co l2 tunnel using the
+     * supplied parameters.
+     *
+     * @param mode the tunnel mode
+     * @param sdtag the service delimiting tag
+     * @param tunnelId the tunnel id
+     * @param pwLabel the pseudo wire label
+     * @param interCoLabel the inter central office label
+     */
+    public DefaultL2Tunnel(L2Mode mode,
+                           VlanId sdtag,
+                           long tunnelId,
+                           MplsLabel pwLabel,
+                           MplsLabel interCoLabel) {
+        checkNotNull(mode);
+        checkArgument(tunnelId > 0);
+        checkNotNull(pwLabel);
+        checkNotNull(interCoLabel);
+
+        this.pwMode = mode;
+        this.sdTag = sdtag;
+        this.tunnelId = tunnelId;
+        this.pwLabel = pwLabel;
+        this.interCoLabel = interCoLabel;
+    }
+
+    /**
+     * Creates a intra-co l2 tunnel using the
+     * supplied parameters.
+     *
+     * @param mode the tunnel mode
+     * @param sdtag the service delimiting tag
+     * @param tunnelId the tunnel id
+     * @param pwLabel the pseudo wire label
+     */
+    public DefaultL2Tunnel(L2Mode mode,
+                           VlanId sdtag,
+                           long tunnelId,
+                           MplsLabel pwLabel) {
+        this(mode, sdtag, tunnelId, pwLabel, MplsLabel.mplsLabel(MplsLabel.MAX_MPLS));
+    }
+
+    /**
+     * Creates an empty l2 tunnel.
+     *
+     **/
+    public DefaultL2Tunnel() {
+        this.pwMode = null;
+        this.sdTag = null;
+        this.tunnelId = 0;
+        this.pwLabel = null;
+        this.interCoLabel = null;
+    }
+
+    /**
+     * Returns the mode of the pseudo wire.
+     *
+     * @return the pseudo wire mode
+     */
+    public L2Mode pwMode() {
+        return pwMode;
+    }
+
+    /**
+     * Returns the service delimitation
+     * tag.
+     *
+     * @return the service delimitation vlan id
+     */
+    public VlanId sdTag() {
+        return sdTag;
+    }
+
+    /**
+     * Returns the tunnel id of the pseudo wire.
+     *
+     * @return the pseudo wire tunnel id
+     */
+    public long tunnelId() {
+        return tunnelId;
+    }
+
+    /**
+     * Returns the pw label.
+     *
+     * @return the mpls pw label
+     */
+    public MplsLabel pwLabel() {
+        return pwLabel;
+    }
+
+    /**
+     * Returns the inter-co label.
+     *
+     * @return the mpls inter-co label
+     */
+    public MplsLabel interCoLabel() {
+        return interCoLabel;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(this.tunnelId, this.pwLabel);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+
+        if (getClass() != o.getClass()) {
+            return false;
+        }
+
+        if (o instanceof DefaultL2Tunnel) {
+            DefaultL2Tunnel that = (DefaultL2Tunnel) o;
+            return this.tunnelId == that.tunnelId &&
+                    this.pwMode.equals(that.pwMode) &&
+                    this.sdTag.equals(that.sdTag) &&
+                    this.pwLabel.equals(that.pwLabel) &&
+                    this.interCoLabel.equals(that.interCoLabel);
+        }
+
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("pwMode", pwMode())
+                .add("sdTag", sdTag())
+                .add("tunnelId", tunnelId())
+                .add("pwLabel", pwLabel())
+                .add("interCoLabel", interCoLabel())
+                .toString();
+    }
+
+}
diff --git a/src/main/java/org/onosproject/segmentrouting/pwaas/DefaultL2TunnelDescription.java b/src/main/java/org/onosproject/segmentrouting/pwaas/DefaultL2TunnelDescription.java
new file mode 100644
index 0000000..f3fa5ab
--- /dev/null
+++ b/src/main/java/org/onosproject/segmentrouting/pwaas/DefaultL2TunnelDescription.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.segmentrouting.pwaas;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Helper class to carry the l2 tunnel
+ * and its policy.
+ */
+public class DefaultL2TunnelDescription {
+
+    /**
+     * The l2 tunnel.
+     */
+    private DefaultL2Tunnel l2Tunnel;
+
+    /**
+     * The l2 tunnel policy.
+     */
+    private DefaultL2TunnelPolicy l2TunnelPolicy;
+
+    /**
+     * Creates a l2 tunnel description using the given info.
+     *
+     * @param l2Tunnel the l2 tunnel
+     * @param l2TunnelPolicy the l2 tunnel description
+     */
+    public DefaultL2TunnelDescription(DefaultL2Tunnel l2Tunnel,
+                                      DefaultL2TunnelPolicy l2TunnelPolicy) {
+        checkNotNull(l2Tunnel);
+        checkNotNull(l2TunnelPolicy);
+
+        this.l2Tunnel = l2Tunnel;
+        this.l2TunnelPolicy = l2TunnelPolicy;
+    }
+
+    /**
+     * Creates an empty l2 tunnel description.
+     */
+    public DefaultL2TunnelDescription() {
+        this.l2Tunnel = null;
+        this.l2TunnelPolicy = null;
+    }
+
+    /**
+     * Returns the l2 tunnel.
+     *
+     * @return the l2 tunnel
+     */
+    public DefaultL2Tunnel l2Tunnel() {
+        return l2Tunnel;
+    }
+
+    /**
+     * Returns the l2 tunnel policy.
+     *
+     * @return the l2 tunnel policy.
+     */
+    public DefaultL2TunnelPolicy l2TunnelPolicy() {
+        return l2TunnelPolicy;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(this.l2Tunnel, this.l2TunnelPolicy);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+
+        if (getClass() != o.getClass()) {
+            return false;
+        }
+
+        if (o instanceof DefaultL2TunnelDescription) {
+            DefaultL2TunnelDescription that = (DefaultL2TunnelDescription) o;
+            // Equality is based on tunnel id and pw label
+            // which is always the last label.
+            return this.l2Tunnel.equals(that.l2Tunnel) &&
+                    this.l2TunnelPolicy.equals(that.l2TunnelPolicy);
+        }
+
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("l2Tunnel", l2Tunnel())
+                .add("l2TunnelPolicy", l2TunnelPolicy())
+                .toString();
+    }
+}
diff --git a/src/main/java/org/onosproject/segmentrouting/pwaas/DefaultL2TunnelPolicy.java b/src/main/java/org/onosproject/segmentrouting/pwaas/DefaultL2TunnelPolicy.java
new file mode 100644
index 0000000..53d040c
--- /dev/null
+++ b/src/main/java/org/onosproject/segmentrouting/pwaas/DefaultL2TunnelPolicy.java
@@ -0,0 +1,215 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.segmentrouting.pwaas;
+
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Implementation of the default l2 tunnel policy.
+ */
+public class DefaultL2TunnelPolicy {
+
+    /**
+     * Id of the tunnel associated to this policy.
+     */
+    private long tunnelId;
+    /**
+     * First connect point.
+     */
+    private ConnectPoint cP1;
+    /**
+     * Second connect point.
+     */
+    private ConnectPoint cP2;
+    /**
+     * cP1 inner vlan tag. Used in QinQ packets.
+     */
+    private VlanId cP1InnerTag;
+    /**
+     * cP1 outer vlan tag.
+     */
+    private VlanId cP1OuterTag;
+    /**
+     * cP2 inner vlan tag. Used in QinQ packets.
+     */
+    private VlanId cP2InnerTag;
+    /**
+     * cP2 outer vlan tag.
+     */
+    private VlanId cP2OuterTag;
+    /**
+     * Boolean value to indicate if the pseudo wire is port based.
+     */
+    private boolean allVlan;
+
+    /**
+     * Creates a default l2 tunnel policy using
+     * the given parameters.
+     *
+     * @param tunnelId the tunnel id
+     * @param cP1 the first connect point
+     * @param cP1InnerTag the cP1 inner tag
+     * @param cP1OuterTag the cP1 outer tag
+     * @param cP2 the second connect point
+     * @param cP2InnerTag the cP2 inner tag
+     * @param cP2OuterTag the cP2 outer tag
+     * @param allVlan if the tunnel is port based or not
+     */
+    public DefaultL2TunnelPolicy(long tunnelId,
+                                 ConnectPoint cP1, VlanId cP1InnerTag, VlanId cP1OuterTag,
+                                 ConnectPoint cP2, VlanId cP2InnerTag, VlanId cP2OuterTag,
+                                 boolean allVlan) {
+        this.cP1 = checkNotNull(cP1);
+        this.cP2 = checkNotNull(cP2);
+        this.tunnelId = tunnelId;
+        this.cP1InnerTag = cP1InnerTag;
+        this.cP1OuterTag = cP1OuterTag;
+        this.cP2InnerTag = cP2InnerTag;
+        this.cP2OuterTag = cP2OuterTag;
+        this.allVlan = allVlan;
+    }
+
+    /**
+     * Returns the first connect point of the policy.
+     *
+     * @return first connect point
+     */
+    public ConnectPoint cP1() {
+        return cP1;
+    }
+
+    /**
+     * Returns the second connect point of the policy.
+     *
+     * @return second connect point
+     */
+    public ConnectPoint cP2() {
+        return cP2;
+    }
+
+    /**
+     * Returns the cP1 inner vlan tag of the policy.
+     *
+     * @return cP1 inner vlan tag
+     */
+    public VlanId cP1InnerTag() {
+        return cP1InnerTag;
+    }
+
+    /**
+     * Returns the cP1 outer vlan tag of the policy.
+     *
+     * @return cP1 outer vlan tag
+     */
+    public VlanId cP1OuterTag() {
+        return cP1OuterTag;
+    }
+
+    /**
+     * Returns the cP2 inner vlan tag of the policy.
+     *
+     * @return cP2 inner vlan tag
+     */
+    public VlanId cP2InnerTag() {
+        return cP2InnerTag;
+    }
+
+    /**
+     * Returns the cP2 outer vlan tag of the policy.
+     *
+     * @return cP2 outer vlan tag
+     */
+    public VlanId cP2OuterTag() {
+        return cP2OuterTag;
+    }
+
+    /**
+     * Return all vlan value.
+     *
+     * @return true, if the pw is port based. False if the traffic is sliced
+     *         through the inner and outer tags
+     */
+    public boolean isAllVlan() {
+        return allVlan;
+    }
+
+    /**
+     * Returns the tunnel ID of the policy.
+     *
+     * @return Tunnel ID
+     */
+    public long tunnelId() {
+        return this.tunnelId;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(tunnelId,
+                            cP1,
+                            cP2,
+                            cP1InnerTag,
+                            cP1OuterTag,
+                            cP2InnerTag,
+                            cP2OuterTag,
+                            allVlan
+        );
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+
+        if (o instanceof DefaultL2TunnelPolicy) {
+            DefaultL2TunnelPolicy that = (DefaultL2TunnelPolicy) o;
+            if (this.tunnelId == that.tunnelId &&
+                    this.cP1.equals(that.cP1) &&
+                    this.cP2.equals(that.cP2) &&
+                    this.cP1InnerTag.equals(that.cP1InnerTag) &&
+                    this.cP1OuterTag.equals(that.cP1OuterTag) &&
+                    this.cP2InnerTag.equals(that.cP2InnerTag) &&
+                    this.cP2OuterTag.equals(that.cP2OuterTag) &&
+                    this.allVlan == that.allVlan) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("tunnelId", tunnelId())
+                .add("cP1", cP1())
+                .add("cP2", cP2())
+                .add("cP1InnerTag", cP1InnerTag())
+                .add("cP1OuterTag", cP1OuterTag())
+                .add("cP2InnerTag", cP2InnerTag())
+                .add("cP2OuterTag", cP2OuterTag())
+                .add("allVlan", isAllVlan())
+                .toString();
+    }
+
+}
diff --git a/src/main/java/org/onosproject/segmentrouting/pwaas/L2Mode.java b/src/main/java/org/onosproject/segmentrouting/pwaas/L2Mode.java
new file mode 100644
index 0000000..fe0a6b3
--- /dev/null
+++ b/src/main/java/org/onosproject/segmentrouting/pwaas/L2Mode.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.segmentrouting.pwaas;
+
+/**
+ * Enum to identify mode of the pwaas.
+ */
+public enum L2Mode {
+    /**
+     * Raw mode.
+     */
+    RAW,
+    /**
+     * Tagged mode. In this case the packet need
+     * the sd tag.
+     */
+    TAGGED;
+}
diff --git a/src/main/java/org/onosproject/segmentrouting/pwaas/L2TunnelHandler.java b/src/main/java/org/onosproject/segmentrouting/pwaas/L2TunnelHandler.java
new file mode 100644
index 0000000..96fe6c9
--- /dev/null
+++ b/src/main/java/org/onosproject/segmentrouting/pwaas/L2TunnelHandler.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.segmentrouting.pwaas;
+
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.segmentrouting.SegmentRoutingManager;
+import org.onosproject.segmentrouting.config.PwaasConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Handles pwaas related events.
+ */
+public class L2TunnelHandler {
+    private static final Logger log = LoggerFactory.getLogger(L2TunnelHandler.class);
+    private static final String CONFIG_NOT_FOUND = "Pwaas config not found";
+    private static final String NOT_MASTER = "Not master controller";
+    private final SegmentRoutingManager srManager;
+
+    public L2TunnelHandler(SegmentRoutingManager srManager) {
+        this.srManager = srManager;
+    }
+
+    /**
+     * Processes Pwaas Config added event.
+     *
+     * @param event network config added event
+     */
+    public void processPwaasConfigAdded(NetworkConfigEvent event) {
+        log.info("Processing Pwaas CONFIG_ADDED");
+        PwaasConfig config = (PwaasConfig) event.config().get();
+        config.getPwIds().forEach(pwId -> {
+            log.info("{}", config.getPwDescription(pwId));
+        });
+    }
+
+    /**
+     * Processes Pwaas Config updated event.
+     *
+     * @param event network config updated event
+     */
+    public void processPwaasConfigUpdated(NetworkConfigEvent event) {
+        log.info("Processing Pwaas CONFIG_UPDATED");
+        PwaasConfig config = (PwaasConfig) event.config().get();
+        config.getPwIds().forEach(pwId -> {
+            log.info("{}", config.getPwDescription(pwId));
+        });
+    }
+
+    /**
+     * Processes Pwaas Config removed event.
+     *
+     * @param event network config removed event
+     */
+    public void processPwaasConfigRemoved(NetworkConfigEvent event) {
+        log.info("Processing Pwaas CONFIG_REMOVED");
+        PwaasConfig config = (PwaasConfig) event.config().get();
+        config.getPwIds().forEach(pwId -> {
+            log.info("{}", config.getPwDescription(pwId));
+        });
+    }
+}
diff --git a/src/main/java/org/onosproject/segmentrouting/pwaas/package-info.java b/src/main/java/org/onosproject/segmentrouting/pwaas/package-info.java
new file mode 100644
index 0000000..05d463b
--- /dev/null
+++ b/src/main/java/org/onosproject/segmentrouting/pwaas/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+/**
+ * Set of resources implementing the Pwaas.
+ */
+package org.onosproject.segmentrouting.pwaas;
\ No newline at end of file