Initial code drop
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / NetworkNodeState.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 package org.opendaylight.protocol.bgp.linkstate;
9
10 import java.util.Collections;
11 import java.util.Map;
12 import java.util.Set;
13
14 import org.opendaylight.protocol.bgp.linkstate.ISISAreaIdentifier;
15 import org.opendaylight.protocol.bgp.linkstate.RouterIdentifier;
16 import org.opendaylight.protocol.bgp.linkstate.TopologyIdentifier;
17 import org.opendaylight.protocol.bgp.linkstate.TopologyNodeInformation;
18 import com.google.common.base.CharMatcher;
19 import com.google.common.base.Objects.ToStringHelper;
20 import com.google.common.base.Preconditions;
21
22 /**
23  * A single (router) node in the network topology. Nodes are interconnected by links and have a bunch of attributes. One
24  * of the key attributes is the set of prefixes for which this node acts as a network edge router.
25  */
26 public class NetworkNodeState extends NetworkObjectState {
27         public static final NetworkNodeState EMPTY = new NetworkNodeState();
28         private static final long serialVersionUID = 1L;
29         private Map<TopologyIdentifier, TopologyNodeInformation> topologyMembership;
30         private Set<ISISAreaIdentifier> areaMembership;
31         private boolean areaBorderRouter;
32         private boolean external;
33         private Set<RouterIdentifier> identifierAlternatives;
34         private String dynamicHostName;
35
36         private NetworkNodeState() {
37                 this(NetworkObjectState.EMPTY, Collections.<TopologyIdentifier, TopologyNodeInformation> emptyMap(), Collections.<ISISAreaIdentifier> emptySet(), false, false, Collections.<RouterIdentifier> emptySet(), null);
38         }
39
40         public NetworkNodeState(final NetworkObjectState orig, final Map<TopologyIdentifier, TopologyNodeInformation> topologyMembership,
41                         final Set<ISISAreaIdentifier> areaMembership, final boolean areaBorderRouter, final boolean external,
42                         final Set<RouterIdentifier> identifierAlternatives, final String dynamicHostName) {
43                 super(orig);
44                 Preconditions.checkNotNull(areaMembership);
45                 Preconditions.checkNotNull(identifierAlternatives);
46                 Preconditions.checkNotNull(topologyMembership);
47                 this.topologyMembership = topologyMembership;
48                 this.areaMembership = areaMembership;
49                 this.areaBorderRouter = areaBorderRouter;
50                 this.external = external;
51                 this.identifierAlternatives = identifierAlternatives;
52                 this.dynamicHostName = dynamicHostName;
53         }
54
55         protected NetworkNodeState(final NetworkNodeState orig) {
56                 super(orig);
57                 this.topologyMembership = orig.topologyMembership;
58                 this.areaMembership = orig.areaMembership;
59                 this.areaBorderRouter = orig.areaBorderRouter;
60                 this.external = orig.external;
61                 this.identifierAlternatives = orig.identifierAlternatives;
62                 this.dynamicHostName = orig.dynamicHostName;
63         }
64
65         /**
66          * Get the per-topology information about this node.
67          * 
68          * @return An immutable map of per-topology state information
69          */
70         public final Map<TopologyIdentifier, TopologyNodeInformation> getTopologyMembership() {
71                 return this.topologyMembership;
72         }
73
74         public final NetworkNodeState withTopologyMembership(final Map<TopologyIdentifier, TopologyNodeInformation> topologyMembership) {
75                 final NetworkNodeState ret = newInstance();
76                 ret.topologyMembership = Collections.unmodifiableMap(topologyMembership);
77                 return ret;
78         }
79
80         /**
81          * Get area membership information.
82          * 
83          * @return An immutable set containing identifiers of all node area this node is member of.
84          */
85         public final Set<ISISAreaIdentifier> getAreaMembership() {
86                 return this.areaMembership;
87         }
88
89         public final NetworkNodeState withAreaMembership(final Set<ISISAreaIdentifier> areaMembership) {
90                 final NetworkNodeState ret = newInstance();
91                 ret.areaMembership = Collections.unmodifiableSet(areaMembership);
92                 return ret;
93         }
94
95         /**
96          * Get ABR flag value. Area Border Routers (e.g. routers connected to multiple areas) advertise this flag.
97          * 
98          * @return True if this router is an ABR.
99          */
100         public final boolean isAreaBorderRouter() {
101                 return this.areaBorderRouter;
102         }
103
104         public final NetworkNodeState withAreaBorderRouter(final boolean value) {
105                 final NetworkNodeState ret = newInstance();
106                 ret.areaBorderRouter = value;
107                 return ret;
108         }
109
110         /**
111          * Get external flag value. This corresponds to <a href="http://tools.ietf.org/html/rfc2328">RFC 2328</a> definition
112          * of ExternalRoutingCapability. It is advertized by all routers connected to external ASes.
113          * 
114          * @return True if the router is an AS-border router
115          */
116         public final boolean isExternal() {
117                 return this.external;
118         }
119
120         public final NetworkNodeState withExternal(final boolean value) {
121                 final NetworkNodeState ret = newInstance();
122                 ret.external = value;
123                 return ret;
124         }
125
126         /**
127          * http://tools.ietf.org/html/rfc5301#section-3, encoded as a String. The string is guaranteed to contain US-ASCII
128          * characters.
129          * 
130          * @return dynamic hostname of the router that is connected
131          */
132         public final String getDynamicHostname() {
133                 return this.dynamicHostName;
134         }
135
136         public final NetworkNodeState withDynamicHostname(final String value) {
137                 Preconditions.checkNotNull(value);
138                 Preconditions.checkArgument(value.length() >= 1);
139                 Preconditions.checkArgument(value.length() <= 255);
140                 Preconditions.checkArgument(CharMatcher.ASCII.matchesAllOf(value));
141
142                 final NetworkNodeState ret = newInstance();
143                 ret.dynamicHostName = value;
144                 return ret;
145         }
146
147         /**
148          * Return the set of alternative identifiers to which this node responds. This set must contain the primary
149          * identifier.
150          * 
151          * @return set of identifier alternatives.
152          */
153         public final Set<RouterIdentifier> getIdentifierAlternatives() {
154                 return this.identifierAlternatives;
155         }
156
157         public final NetworkNodeState withIdentifierAlternatives(final Set<RouterIdentifier> identifierAlternatives) {
158                 final NetworkNodeState ret = newInstance();
159                 ret.identifierAlternatives = identifierAlternatives;
160                 return ret;
161         }
162
163         @Override
164         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
165                 toStringHelper.add("topologyMembership", this.topologyMembership);
166                 toStringHelper.add("areaMembership", this.areaMembership);
167                 toStringHelper.add("external", this.external);
168                 toStringHelper.add("ABR", this.areaBorderRouter);
169                 toStringHelper.add("dynamicHostname", this.dynamicHostName);
170                 toStringHelper.add("routerIdentifiers", this.identifierAlternatives);
171                 return super.addToStringAttributes(toStringHelper);
172         }
173
174         @Override
175         protected NetworkNodeState newInstance() {
176                 return new NetworkNodeState(this);
177         }
178 }