5731c7e8932be68a618580529398a39a2f291f8f
[affinity.git] / affinity / api / src / main / java / org / opendaylight / affinity / affinity / AffinityLink.java
1 package org.opendaylight.affinity.affinity;
2
3 import org.opendaylight.controller.sal.utils.NetUtils;
4 import java.io.Serializable;
5 import java.net.InetAddress;
6 import java.net.UnknownHostException;
7 import java.util.Map;
8 import java.util.HashMap;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.opendaylight.controller.sal.utils.Status;
13 import org.opendaylight.controller.sal.utils.StatusCode;
14
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.core.Context;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.core.SecurityContext;
27
28 import org.codehaus.enunciate.jaxrs.ResponseCode;
29 import org.codehaus.enunciate.jaxrs.StatusCodes;
30 import org.codehaus.enunciate.jaxrs.TypeHint;
31
32 import javax.xml.bind.annotation.XmlAccessType;
33 import javax.xml.bind.annotation.XmlAccessorType;
34 import javax.xml.bind.annotation.XmlAttribute;
35 import javax.xml.bind.annotation.XmlElement;
36 import javax.xml.bind.annotation.XmlRootElement;
37
38 import com.fasterxml.jackson.annotation.JsonIgnore;
39 import org.opendaylight.affinity.affinity.AffinityAttribute;
40
41 @XmlRootElement
42 @XmlAccessorType(XmlAccessType.NONE)
43 public class AffinityLink implements Cloneable, Serializable {
44     private static final long serialVersionUID = 1L;
45
46     @XmlAttribute
47     private String name;
48     @XmlElement
49     AffinityGroup fromGroup;
50     @XmlElement
51     AffinityGroup toGroup;
52
53     // Keep at most one affinity attribute per type. 
54     @XmlElement
55     private HashMap<AffinityAttributeType, AffinityAttribute> attrlist;
56     
57     public AffinityLink() {
58         attrlist = new HashMap<AffinityAttributeType, AffinityAttribute>();
59     }
60     public AffinityLink(String name, AffinityGroup fromGroup, AffinityGroup toGroup) {
61         this.name = name;
62         this.fromGroup = fromGroup;
63         this.toGroup = toGroup;
64         attrlist = new HashMap<AffinityAttributeType, AffinityAttribute>();
65     }
66     public String getName() {
67         return this.name;
68     }
69     public void setName(String name) {
70         this.name = name;
71     }
72     public void setFromGroup(AffinityGroup fromGroup) {
73         this.fromGroup = fromGroup;
74     }
75     public void setToGroup(AffinityGroup toGroup) {
76         this.toGroup = toGroup;
77     }
78     public void addAttribute(AffinityAttribute attr) {
79         if (attr != null) {
80             attrlist.put(attr.type, attr);
81         }
82     }
83     @JsonIgnore
84     public HashMap<AffinityAttributeType, AffinityAttribute> getAttributeList() {
85         return this.attrlist;
86     }
87     
88     /* Set the waypoint address, if the attribute is "redirect" */
89     public void setWaypoint(String wpaddr) {
90         SetPathRedirect redirect = new SetPathRedirect();
91         redirect.addWaypoint(NetUtils.parseInetAddress(wpaddr));
92         
93         /* Add this service chain to this affinity link. */
94         addAttribute((AffinityAttribute) redirect);
95     }
96
97     /* Get the waypoint address */
98     @JsonIgnore
99     public AffinityAttribute getWaypoint() {
100         return attrlist.get(AffinityAttributeType.SET_PATH_REDIRECT);
101     }
102     
103     // Unset the waypoint attribute.
104     public void unsetWaypoint() {
105         attrlist.remove(AffinityAttributeType.SET_PATH_REDIRECT);
106     }
107
108     // Add tap attribute. 
109     public void addTap(String ipaddr) {
110         // Check if a tap attribute is already available on this link. 
111         // If not, create one and add IP address to it. 
112         AffinityAttributeType aatype = AffinityAttributeType.SET_TAP;
113         SetTap tap = (SetTap) attrlist.get(aatype);
114
115         if (tap == null) {
116             tap = new SetTap();
117         }
118         // add a tap server
119         tap.addTap(NetUtils.parseInetAddress(ipaddr));
120         
121         /* Add this tap set to the affinity link. */
122         addAttribute((AffinityAttribute) tap);
123     }
124
125     // Add tap configuration. 
126     public void removeTap(String ipaddr) {
127         // Check if a tap attribute is already available on this link. 
128         AffinityAttributeType aatype = AffinityAttributeType.SET_TAP;
129         SetTap tap = (SetTap) attrlist.get(aatype);
130         
131         // tap attribute exists. Remove IP address from its list of IP adresses. 
132         if (tap != null) {
133             tap.removeTap(NetUtils.parseInetAddress(ipaddr));
134         }
135     }
136
137     /* tbd requires nb method. */
138     @JsonIgnore
139     public List<InetAddress> getTapList() {
140         // Check if a tap attribute is already available on this link. 
141         SetTap tap = (SetTap) attrlist.get(AffinityAttributeType.SET_TAP);
142         if (tap != null) {
143             return tap.getTapList();
144         }
145         return null;
146     }
147     
148     public boolean isDeny() {
149         return attrlist.containsKey(AffinityAttributeType.SET_DENY);
150     }
151
152     // Mark this with "deny"
153     public void setDeny() {
154         SetDeny deny = new SetDeny();
155         addAttribute(deny);
156     }
157
158     // Remove "deny" marking if it exists
159     public void unsetDeny() {
160         attrlist.remove(AffinityAttributeType.SET_DENY);
161     }
162
163     // Mark this with "isolate"
164     public void setIsolate() {
165         SetPathIsolate iso = new SetPathIsolate();
166         addAttribute(iso);
167     }
168     public void unsetIsolate() {
169         attrlist.remove(AffinityAttributeType.SET_PATH_ISOLATE);
170     }
171
172     public AffinityGroup getFromGroup() {
173         return this.fromGroup;
174     }
175     public AffinityGroup getToGroup() {
176         return this.toGroup;
177     }
178     @Override
179     public String toString() {
180         String output = this.name;
181
182         if (attrlist != null) {
183             for (AffinityAttribute a: attrlist.values()) {
184                 output = output + "attr: " + a.toString() + "; ";
185             }
186         }
187         return output;
188     }
189 }
190