blob: 03a2ea7b09adde2030192be6162ad53294d11087 [file] [log] [blame]
Yi Tsengf4e13e32017-03-30 15:38:39 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengf4e13e32017-03-30 15:38:39 -07003 *
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());
Yi Tseng2ec74962017-05-24 16:49:07 -0700102 vplsDataCopy.state(vplsData.state());
Yi Tsengf4e13e32017-03-30 15:38:39 -0700103 vplsDataCopy.addInterfaces(vplsData.interfaces());
104 return vplsData;
105 }
106
107 /**
108 * Gets name of the VPLS.
109 *
110 * @return the name of the VPLS
111 */
112 public String name() {
113 return name;
114 }
115
116 public Set<Interface> interfaces() {
117 return ImmutableSet.copyOf(interfaces);
118 }
119
120 public EncapsulationType encapsulationType() {
121 return encapsulationType;
122 }
123
124 public void addInterfaces(Collection<Interface> interfaces) {
125 requireNonNull(interfaces);
126 this.interfaces.addAll(interfaces);
127 }
128
129 public void addInterface(Interface iface) {
130 requireNonNull(iface);
131 this.interfaces.add(iface);
132 }
133
134 public void removeInterfaces(Collection<Interface> interfaces) {
135 requireNonNull(interfaces);
136 this.interfaces.removeAll(interfaces);
137 }
138
139 public void removeInterface(Interface iface) {
140 requireNonNull(iface);
141 this.interfaces.remove(iface);
142 }
143
144 public void encapsulationType(EncapsulationType encapType) {
145 this.encapsulationType = encapType;
146 }
147
148 public VplsState state() {
149 return state;
150 }
151
152 public void state(VplsState state) {
153 this.state = state;
154 }
155
156 @Override
157 public String toString() {
158 return MoreObjects.toStringHelper(getClass())
159 .add("name", name)
160 .add("interfaces", interfaces)
161 .add("encap type", encapsulationType)
162 .add("state", state)
163 .toString();
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 if (obj == this) {
169 return true;
170 }
171 if (!(obj instanceof VplsData)) {
172 return false;
173 }
174 VplsData other = (VplsData) obj;
175 return Objects.equals(other.name, this.name) &&
176 Objects.equals(other.interfaces, this.interfaces) &&
177 Objects.equals(other.encapsulationType, this.encapsulationType);
178 }
179
180 @Override
181 public int hashCode() {
182 return hash(name, interfaces, encapsulationType);
183 }
184}