blob: bba9d7835158bfe22137f5ecc81cbc28f0b8aa43 [file] [log] [blame]
Yi Tsengf4e13e32017-03-30 15:38:39 -07001/*
2 * Copyright 2017-present 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
17package org.onosproject.vpls.api;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.ImmutableSet;
21import com.google.common.collect.Sets;
22import org.onosproject.incubator.net.intf.Interface;
23import org.onosproject.net.EncapsulationType;
24
25import java.util.Collection;
26import java.util.Objects;
27import java.util.Set;
28
29import static java.util.Objects.*;
30
31/**
32 * Class stores a VPLS information.
33 */
34public final class VplsData {
35 /**
36 * States of a VPLS.
37 */
38 public enum VplsState {
39 UPDATING,
40 ADDING,
41 REMOVING,
42 ADDED,
43 REMOVED,
44 FAILED
45 }
46
47 private String name;
48 private Set<Interface> interfaces;
49 private EncapsulationType encapsulationType;
50 private VplsState state;
51
52 /**
53 * Constructs a VPLS data by given name and encapsulation type.
54 *
55 * @param name the given name
56 * @param encapType the encapsulation type
57 */
58 private VplsData(String name, EncapsulationType encapType) {
59 this.name = name;
60 this.encapsulationType = encapType;
61 this.interfaces = Sets.newHashSet();
62 this.state = VplsState.ADDING;
63 }
64
65 /**
66 * Creates a VPLS data by given name.
67 * The encapsulation type of the VPLS will be NONE.
68 *
69 * @param name the given name
70 * @return the VPLS data
71 */
72 public static VplsData of(String name) {
73 requireNonNull(name);
74 return new VplsData(name, EncapsulationType.NONE);
75 }
76
77 /**
78 * Creates a VPLS data by given name and encapsulation type.
79 *
80 * @param name the given name
81 * @param encapType the encapsulation type
82 * @return the VPLS data
83 */
84 public static VplsData of(String name, EncapsulationType encapType) {
85 requireNonNull(name);
86 if (encapType == null) {
87 return new VplsData(name, EncapsulationType.NONE);
88 } else {
89 return new VplsData(name, encapType);
90 }
91 }
92
93 /**
94 * Creates a copy of VPLS data.
95 *
96 * @param vplsData the VPLS data
97 * @return the copy of the VPLS data
98 */
99 public static VplsData of(VplsData vplsData) {
100 requireNonNull(vplsData);
101 VplsData vplsDataCopy = new VplsData(vplsData.name(), vplsData.encapsulationType());
102 vplsDataCopy.addInterfaces(vplsData.interfaces());
103 return vplsData;
104 }
105
106 /**
107 * Gets name of the VPLS.
108 *
109 * @return the name of the VPLS
110 */
111 public String name() {
112 return name;
113 }
114
115 public Set<Interface> interfaces() {
116 return ImmutableSet.copyOf(interfaces);
117 }
118
119 public EncapsulationType encapsulationType() {
120 return encapsulationType;
121 }
122
123 public void addInterfaces(Collection<Interface> interfaces) {
124 requireNonNull(interfaces);
125 this.interfaces.addAll(interfaces);
126 }
127
128 public void addInterface(Interface iface) {
129 requireNonNull(iface);
130 this.interfaces.add(iface);
131 }
132
133 public void removeInterfaces(Collection<Interface> interfaces) {
134 requireNonNull(interfaces);
135 this.interfaces.removeAll(interfaces);
136 }
137
138 public void removeInterface(Interface iface) {
139 requireNonNull(iface);
140 this.interfaces.remove(iface);
141 }
142
143 public void encapsulationType(EncapsulationType encapType) {
144 this.encapsulationType = encapType;
145 }
146
147 public VplsState state() {
148 return state;
149 }
150
151 public void state(VplsState state) {
152 this.state = state;
153 }
154
155 @Override
156 public String toString() {
157 return MoreObjects.toStringHelper(getClass())
158 .add("name", name)
159 .add("interfaces", interfaces)
160 .add("encap type", encapsulationType)
161 .add("state", state)
162 .toString();
163 }
164
165 @Override
166 public boolean equals(Object obj) {
167 if (obj == this) {
168 return true;
169 }
170 if (!(obj instanceof VplsData)) {
171 return false;
172 }
173 VplsData other = (VplsData) obj;
174 return Objects.equals(other.name, this.name) &&
175 Objects.equals(other.interfaces, this.interfaces) &&
176 Objects.equals(other.encapsulationType, this.encapsulationType);
177 }
178
179 @Override
180 public int hashCode() {
181 return hash(name, interfaces, encapsulationType);
182 }
183}