Merge "Add Translator for MultipartDescReply"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / session / SwitchSessionKeyOFImpl.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core.session;
10
11 import java.math.BigInteger;
12 import java.security.MessageDigest;
13 import java.security.NoSuchAlgorithmException;
14 import java.util.Arrays;
15
16 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
17
18 /**
19  * @author mirehak
20  */
21 public class SwitchSessionKeyOFImpl implements SwitchConnectionDistinguisher {
22
23     protected byte[] encodedId;
24     private BigInteger datapathId;
25
26     /**
27      * default ctor
28      */
29     public SwitchSessionKeyOFImpl() {
30         // do nothing
31     }
32
33     /**
34      * Special constructor for situation where no datapathId available, do not
35      * call {@link #initId()} on this instance otherwise id will be overwritten.
36      *
37      * @param encodedId
38      */
39     public SwitchSessionKeyOFImpl(byte[] encodedId) {
40         this.encodedId = encodedId;
41     }
42
43     @Override
44     public byte[] getId() {
45         return encodedId;
46     }
47
48     /**
49      * @param datapathId
50      *            the datapathId to set
51      */
52     public void setDatapathId(BigInteger datapathId) {
53         this.datapathId = datapathId;
54     }
55
56     /**
57      * compute and set {@link #encodedId} based on {@link #datapathId} and
58      * {@link #auxiliaryId}
59      */
60     public void initId() {
61         try {
62             MessageDigest medi = MessageDigest.getInstance("sha-1");
63             extend(medi);
64             encodedId = medi.digest();
65         } catch (NoSuchAlgorithmException | NullPointerException e) {
66             throw new IllegalArgumentException("can not proceed datapathId: "
67                     + datapathId);
68         }
69     }
70
71     /**
72      * extend the content the hash sum is computed from
73      * @param medi
74      */
75     protected void extend(MessageDigest medi) {
76         medi.update(datapathId.toByteArray());
77     }
78
79     @Override
80     public int hashCode() {
81         final int prime = 31;
82         int result = 1;
83         result = prime * result + Arrays.hashCode(encodedId);
84         return result;
85     }
86
87     @Override
88     public boolean equals(Object obj) {
89         if (this == obj)
90             return true;
91         if (obj == null)
92             return false;
93         if (getClass() != obj.getClass())
94             return false;
95         SwitchSessionKeyOFImpl other = (SwitchSessionKeyOFImpl) obj;
96         if (!Arrays.equals(encodedId, other.encodedId))
97             return false;
98         return true;
99     }
100
101 }