[ONOS] Cherry picked from master 1.7

Change-Id: I74a0c1634f9c425af2bcb646edc3d9170b3c087c
diff --git a/protocols/pcep/api/BUCK b/protocols/pcep/api/BUCK
index ae80b3a..5ced968 100644
--- a/protocols/pcep/api/BUCK
+++ b/protocols/pcep/api/BUCK
@@ -1,6 +1,8 @@
 COMPILE_DEPS = [
     '//lib:CORE_DEPS',
     '//protocols/pcep/pcepio:onos-protocols-pcep-pcepio',
+    '//apps/pcep-api:onos-apps-pcep-api',
+    '//incubator/api:onos-incubator-api',
 ]
 
 osgi_jar_with_tests (
diff --git a/protocols/pcep/api/pom.xml b/protocols/pcep/api/pom.xml
index 5563eae..7903b98 100644
--- a/protocols/pcep/api/pom.xml
+++ b/protocols/pcep/api/pom.xml
@@ -23,6 +23,7 @@
         <groupId>org.onosproject</groupId>
         <artifactId>onos-pcep-controller</artifactId>
         <version>1.6.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
     </parent>
 
     <artifactId>onos-pcep-controller-api</artifactId>
@@ -51,5 +52,9 @@
             <groupId>org.onosproject</groupId>
             <artifactId>onlab-misc</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-incubator-api</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/ClientCapability.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/ClientCapability.java
index e2b5092..e9cafef 100644
--- a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/ClientCapability.java
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/ClientCapability.java
@@ -26,6 +26,8 @@
     private boolean pceccCapability;
     private boolean statefulPceCapability;
     private boolean pcInstantiationCapability;
+    private boolean labelStackCapability;
+    private boolean srCapability;
 
     /**
      * Creates new instance of client capability.
@@ -33,11 +35,34 @@
      * @param pceccCapability represents PCECC capability
      * @param statefulPceCapability represents stateful PCE capability
      * @param pcInstantiationCapability represents PC initiation capability
+     * @param labelStackCapability represents S bit is set in PCECC capability
+     * @param srCapability represents SR capability
      */
-    public ClientCapability(boolean pceccCapability, boolean statefulPceCapability, boolean pcInstantiationCapability) {
+    public ClientCapability(boolean pceccCapability, boolean statefulPceCapability, boolean pcInstantiationCapability,
+            boolean labelStackCapability, boolean srCapability) {
         this.pceccCapability = pceccCapability;
         this.statefulPceCapability = statefulPceCapability;
         this.pcInstantiationCapability = pcInstantiationCapability;
+        this.labelStackCapability = labelStackCapability;
+        this.srCapability = srCapability;
+    }
+
+    /**
+     * Obtains label stack capability.
+     *
+     * @return true if client supports PCECC capability with S bit set otherwise false
+     */
+    public boolean labelStackCapability() {
+        return labelStackCapability;
+    }
+
+    /**
+     * Obtains segment routing capability.
+     *
+     * @return true if client supports SR capability otherwise false
+     */
+    public boolean srCapability() {
+        return srCapability;
     }
 
     /**
@@ -69,7 +94,8 @@
 
     @Override
     public int hashCode() {
-        return Objects.hash(pceccCapability, statefulPceCapability, pcInstantiationCapability);
+        return Objects.hash(pceccCapability, statefulPceCapability, pcInstantiationCapability, labelStackCapability,
+                srCapability);
     }
 
     @Override
@@ -81,7 +107,9 @@
             ClientCapability other = (ClientCapability) obj;
             return Objects.equals(pceccCapability, other.pceccCapability)
                     && Objects.equals(statefulPceCapability, other.statefulPceCapability)
-                    && Objects.equals(pcInstantiationCapability, other.pcInstantiationCapability);
+                    && Objects.equals(pcInstantiationCapability, other.pcInstantiationCapability)
+                    && Objects.equals(labelStackCapability, other.labelStackCapability)
+                    && Objects.equals(srCapability, other.srCapability);
         }
         return false;
     }
@@ -92,6 +120,8 @@
                 .add("pceccCapability", pceccCapability)
                 .add("statefulPceCapability", statefulPceCapability)
                 .add("pcInstantiationCapability", pcInstantiationCapability)
+                .add("labelStackCapability", labelStackCapability)
+                .add("srCapability", srCapability)
                 .toString();
     }
 }
\ No newline at end of file
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/LspKey.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/LspKey.java
new file mode 100644
index 0000000..5dee845
--- /dev/null
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/LspKey.java
@@ -0,0 +1,85 @@
+/*
+ * 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.pcep.controller;
+
+import java.util.Objects;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Representation of LSP info, it will be unique for each LSP.
+ */
+public class LspKey {
+    private int plspId;
+    private short localLspId;
+
+    /**
+     * Creates new instance of LspInfo.
+     *
+     * @param plspId LSP id assigned per tunnel per session
+     * @param localLspId LSP id assigned per tunnel
+     */
+    public LspKey(int plspId, short localLspId) {
+        this.plspId = plspId;
+        this.localLspId = localLspId;
+    }
+
+    /**
+     * Obtains PLSP id.
+     *
+     * @return LSP id assigned per tunnel per session
+     */
+    public int plspId() {
+        return plspId;
+    }
+
+    /**
+     * Obtains local LSP id.
+     *
+     * @return LSP id assigned per tunnel
+     */
+    public short localLspId() {
+        return localLspId;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(plspId, localLspId);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj instanceof LspKey) {
+            LspKey other = (LspKey) obj;
+            return Objects.equals(plspId, other.plspId)
+                    && Objects.equals(localLspId, other.localLspId);
+        }
+
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("plspId", plspId)
+                .add("localLspId", localLspId)
+                .toString();
+    }
+}
\ No newline at end of file
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClient.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClient.java
index afeeae8..c5cf003 100644
--- a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClient.java
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClient.java
@@ -19,6 +19,7 @@
 
 import org.onosproject.pcepio.protocol.PcepFactory;
 import org.onosproject.pcepio.protocol.PcepMessage;
+import org.onosproject.pcepio.protocol.PcepStateReport;
 
 /**
  * Represents to provider facing side of a path computation client(pcc).
@@ -135,4 +136,64 @@
      * @return capability supported by client
      */
     ClientCapability capability();
+
+    /**
+     * Adds PCEP device when session is successfully established.
+     *
+     * @param pc PCEP client details
+     */
+    void addNode(PcepClient pc);
+
+    /**
+     * Removes PCEP device when session is disconnected.
+     *
+     * @param pccId PCEP client ID
+     */
+    void deleteNode(PccId pccId);
+
+     /**
+     * Sets D flag for the given LSP and its LSP info.
+     *
+     * @param lspKey contains LSP info
+     * @param dFlag delegation flag in LSP object
+     */
+    void setLspAndDelegationInfo(LspKey lspKey, boolean dFlag);
+
+    /**
+     * Returns delegation flag for the given LSP info.
+     *
+     * @param lspKey contains LSP info
+     * @return delegation flag
+     */
+    Boolean delegationInfo(LspKey lspKey);
+
+    /**
+     * Creates a temporary cache to hold report messages received during LSPDB sync.
+     *
+     * @param pccId PCC id which is the key to store report messages
+     */
+    void initializeSyncMsgList(PccId pccId);
+
+    /**
+     * Returns the list of report messages received during LSPDB sync.
+     *
+     * @param pccId PCC id which is the key for all the report messages
+     * @return list of report messages received during LSPDB sync
+     */
+    List<PcepStateReport> getSyncMsgList(PccId pccId);
+
+    /**
+     * Removes the list of report messages received during LSPDB sync.
+     *
+     * @param pccId PCC id which is the key for all the report messages
+     */
+    void removeSyncMsgList(PccId pccId);
+
+    /**
+     * Adds report message received during LSPDB sync into temporary cache.
+     *
+     * @param pccId PCC id which is the key to store report messages
+     * @param rptMsg the report message to be stored
+     */
+    void addSyncMsgToList(PccId pccId, PcepStateReport rptMsg);
 }
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClientController.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClientController.java
index befc3f0..0f14778 100644
--- a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClientController.java
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepClientController.java
@@ -57,7 +57,7 @@
     void removeListener(PcepClientListener listener);
 
     /**
-     * Register a listener for OF msg events.
+     * Register a listener for PCEP msg events.
      *
      * @param listener the listener to notify
      */
@@ -71,6 +71,34 @@
     void removeEventListener(PcepEventListener listener);
 
     /**
+     * Register a listener for PCEP msg events[carrying node descriptor details].
+     *
+     * @param listener the listener to notify
+     */
+    void addNodeListener(PcepNodeListener listener);
+
+    /**
+     * Unregister a listener.
+     *
+     * @param listener the listener to be unregistered
+     */
+    void removeNodeListener(PcepNodeListener listener);
+
+    /**
+     * Register a listener for packet events.
+     *
+     * @param listener the listener to notify
+     */
+    void addPacketListener(PcepPacketListener listener);
+
+    /**
+     * Unregister a packet listener.
+     *
+     * @param listener the listener to unregister
+     */
+    void removePacketListener(PcepPacketListener listener);
+
+    /**
      * Send a message to a particular pcc client.
      *
      * @param pccId the id of the client to send message.
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepEventListener.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepEventListener.java
index 7ba8ca7..c7c347a 100644
--- a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepEventListener.java
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepEventListener.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.pcep.controller;
 
+import org.onosproject.incubator.net.tunnel.Tunnel;
 import org.onosproject.pcepio.protocol.PcepMessage;
 /**
  * Notifies providers about PCEP message events.
@@ -28,4 +29,21 @@
      * @param msg the message
      */
     void handleMessage(PccId pccId, PcepMessage msg);
+
+    /**
+     * Handles end of LSPDB sync actions.
+     *
+     * @param tunnel the tunnel on which action needs to be taken
+     * @param endOfSyncAction the action that needs to be taken for the tunnel
+     */
+    void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction);
+
+    /**
+     * Handles sending PCEP message to client on end of LSPDB sync.
+     *
+     * @param pccId id of the pcc
+     * @param msg the message to be sent
+     * @param endOfSyncAction the action that needs to be taken in the message
+     */
+    void handleEndOfSyncAction(PccId pccId, PcepMessage msg, PcepLspSyncAction endOfSyncAction);
 }
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepLspSyncAction.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepLspSyncAction.java
new file mode 100644
index 0000000..92ce8e6
--- /dev/null
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepLspSyncAction.java
@@ -0,0 +1,53 @@
+/*
+ * 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.pcep.controller;
+
+/**
+ * Representation of actions to be taken for LSPs on end of LSP-DB sync.
+ */
+public enum PcepLspSyncAction {
+
+    /**
+     * Specifies that delete message for PCE intiiated tunnel should be sent.
+     */
+    SEND_DELETE(0),
+
+    /**
+     * Specifies that update message should be sent.
+     */
+    SEND_UPDATE(1),
+
+    /**
+     * Specifies that the tunnel should be removed from PCE.
+     */
+    REMOVE(2),
+
+    /**
+     * Specifies that the status of the tunnel should be set as unstable.
+     */
+    UNSTABLE(3);
+
+    int value;
+
+    /**
+     * Assigns val with the value for actions to be taken for LSPs on end of LSP-DB sync.
+     *
+     * @param val sync status
+     */
+    PcepLspSyncAction(int val) {
+        value = val;
+    }
+}
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepNodeListener.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepNodeListener.java
new file mode 100644
index 0000000..a02a744
--- /dev/null
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepNodeListener.java
@@ -0,0 +1,36 @@
+/*
+ * 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.pcep.controller;
+
+/**
+ * Notifies providers about PCEP node events.
+ */
+public interface PcepNodeListener {
+
+    /**
+     * Notifies that the node was added.
+     *
+     * @param pc PCEP client details
+     */
+    void addNode(PcepClient pc);
+
+    /**
+     * Notifies that the node was removed.
+     *
+     * @param pccId PCEP client ID
+     */
+    void deleteNode(PccId pccId);
+}
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepPacketListener.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepPacketListener.java
new file mode 100644
index 0000000..c2017ae
--- /dev/null
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/PcepPacketListener.java
@@ -0,0 +1,22 @@
+/*
+ * 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.pcep.controller;
+
+public interface PcepPacketListener {
+
+    void sendPacketIn(PccId pccId);
+
+}
diff --git a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/driver/PcepAgent.java b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/driver/PcepAgent.java
old mode 100755
new mode 100644
index 2dc3e75..55d5614
--- a/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/driver/PcepAgent.java
+++ b/protocols/pcep/api/src/main/java/org/onosproject/pcep/controller/driver/PcepAgent.java
@@ -60,4 +60,25 @@
      */
     void processPcepMessage(PccId pccId, PcepMessage m);
 
+    /**
+     * Adds PCEP device when session is successfully established.
+     *
+     * @param pc PCEP client details
+     */
+    void addNode(PcepClient pc);
+
+    /**
+     * Removes PCEP device when session is disconnected.
+     *
+     * @param pccId PCEP client ID
+     */
+    void deleteNode(PccId pccId);
+
+    /**
+     * Analyzes report messages received during LSP DB sync again tunnel store and takes necessary actions.
+     *
+     * @param pccId the id of pcc client
+     * @return success or failure
+     */
+    boolean analyzeSyncMsgList(PccId pccId);
 }