8c6fbe8d492f4b51e35ef53612a6473345e310a7
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onosproject.segmentrouting;
18
19 import java.util.List;
20 import java.util.Objects;
21
22 import static com.google.common.base.Preconditions.checkNotNull;
23
24 /**
25  * Tunnel class.
26  */
27 public class DefaultTunnel implements Tunnel {
28
29     private final String id;
30     private final List<Integer> labelIds;
31
32     private int groupId;
33     private boolean allowedToRemoveGroup;
34
35     /**
36      * Creates a Tunnel reference.
37      *
38      * @param tid  Tunnel ID
39      * @param labelIds Label stack of the tunnel
40      */
41     public DefaultTunnel(String tid, List<Integer> labelIds) {
42         this.id = checkNotNull(tid);
43         this.labelIds = labelIds;
44         //TODO: need to register the class in Kryo for this
45         //this.labelIds = Collections.unmodifiableList(labelIds);
46         this.groupId = -1;
47     }
48
49     /**
50      * Creates a new DefaultTunnel reference using the tunnel reference.
51      *
52      * @param tunnel DefaultTunnel reference
53      */
54     public DefaultTunnel(DefaultTunnel tunnel) {
55         this.id = tunnel.id;
56         this.labelIds = tunnel.labelIds;
57         this.groupId = tunnel.groupId;
58     }
59
60     @Override
61     public String id() {
62         return this.id;
63     }
64
65     @Override
66     public List<Integer> labelIds() {
67         return this.labelIds;
68     }
69
70     @Override
71     public int groupId() {
72         return this.groupId;
73     }
74
75     @Override
76     public void setGroupId(int id) {
77         this.groupId = id;
78     }
79
80     @Override
81     public boolean equals(Object o) {
82         if (this == o) {
83             return true;
84         }
85
86         if (o instanceof DefaultTunnel) {
87             DefaultTunnel tunnel = (DefaultTunnel) o;
88             // We compare only the tunnel paths.
89             if (tunnel.labelIds.equals(this.labelIds)) {
90                 return true;
91             }
92         }
93
94         return false;
95     }
96
97     @Override
98     public int hashCode() {
99         return Objects.hash(labelIds);
100     }
101
102     @Override
103     public boolean isAllowedToRemoveGroup() {
104         return this.allowedToRemoveGroup;
105     }
106
107     @Override
108     public void allowToRemoveGroup(boolean b) {
109         this.allowedToRemoveGroup = b;
110     }
111 }