# A Novel Security Architecture for WSN-Based Applications in Smart Grid
## 4 Methodology
### Clustering Algorithm  
The clustering algorithm is based on different parameters to select the CHs. First, we consider the energy level of the sensor nodes to increase the clusters' lifetime. The second parameter is the degree of the sensor node defined as the number of its neighbors. A node having a degree less than two cannot be elected CH. In addition, a trust metric is considered in the election of the CH. The trust metric is a value in [0-1] that relies on the behavior of the node in terms of cooperation and authentic transmitted messages. A sensor node is considered trustful if its trust metric reaches 1. It must show a good attitude and good collaboration in the network to increase its trust metric. Many trust calculation techniques are proposed in the literature where the trust metric is evaluated based on the packet delivery ratio and RSSI [10, 21, 26].  
The clustering algorithm goes through three main phases: discovery, clustering, and maintenance. In the discovery phase, all sensor nodes have to discover their neighborhood by exchanging HELLO messages with other sensor nodes. HELLO messages contain general information about the sensor node such as position, trust metric, and energy level. HELLO messages are periodically broadcast at 1-hop during a period TimerD.  
Once TimerD expires, the sensor nodes move to the clustering phase. Each sensor node calculates the number of its neighbors called degree level DL. If the sensor is trustful, it has the highest energy level EL among its neighbors and at least two neighbors, it broadcasts an E-message to announce itself CH candidate, which means that it is eligible to be elected cluster head. Otherwise, the sensor node waits for an E-message from other CH candidates and tries to join one of them by sending a join request in a J-message. The collection of E-messages lasts for some time equal to TimerE.  
Upon the expiration of TimerE, each candidate CH compares itself to other CHs candidates. If it has the highest energy level EL, the highest degree level DL and it has received at least one join request during TimerE, it announces itself CH in a HELLO message and accepts all the received join requests. Otherwise, the CH candidate joins another CH. It is worth mentioning here that all the clusters have a fixed size measured by the maximum number of member nodes. Figure 1 shows the flowchart of the proposed algorithm.  
Later on, if a new sensor node joins the network, it may receive HELLO messages from existing CHs. In this case, it sends a join request to the nearest one. If the sensor node does not receive an acknowledgment during a TimerA, it sends a J-message to another CH. Once a sensor node becomes a member of a cluster, its role depends on its trust metric. If it is trustful and located at 1 hop from the CH, it will be a RA, otherwise, it is an ordinary member node.  
In the maintenance phase, each CH sends periodic HELLO messages to the members of its clusters to make them aware that is always alive. At the same time, the RA nodes send periodic HELLO messages to their CA. The CA is still alive if it has at least one RA in the cluster, otherwise, it will be no longer CA and a re-clustering process should take place.