Fixed bugs while getting demo.py to work.
[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 import org.opendaylight.affinity.affinity.AffinityAttribute;
38
39 @XmlRootElement
40 @XmlAccessorType(XmlAccessType.NONE)
41 public class AffinityLink implements Cloneable, Serializable {
42     private static final long serialVersionUID = 1L;
43
44     @XmlAttribute
45     private String name;
46     @XmlElement
47     AffinityGroup fromGroup;
48     @XmlElement
49     AffinityGroup toGroup;
50
51     // Keep at most one affinity attribute per type. 
52     @XmlElement
53     private HashMap<AffinityAttributeType, AffinityAttribute> attrlist;
54     
55     // xxx 
56     @XmlElement 
57     String affinityAttribute;
58     @XmlElement 
59     String affinityWaypoint;
60
61     public AffinityLink() {
62         attrlist = new HashMap<AffinityAttributeType, AffinityAttribute>();
63     }
64     public AffinityLink(String name, AffinityGroup fromGroup, AffinityGroup toGroup) {
65         this.name = name;
66         this.fromGroup = fromGroup;
67         this.toGroup = toGroup;
68         attrlist = new HashMap<AffinityAttributeType, AffinityAttribute>();
69     }
70     public String getName() {
71         return this.name;
72     }
73     public void setName(String name) {
74         this.name = name;
75     }
76     public void setFromGroup(AffinityGroup fromGroup) {
77         this.fromGroup = fromGroup;
78     }
79     public void setToGroup(AffinityGroup toGroup) {
80         this.toGroup = toGroup;
81     }
82     public void addAttribute(AffinityAttribute attr) {
83         if (attr != null) {
84             System.out.println("Printing affinity attribute: " + attr.type);
85             attrlist.put(attr.type, attr);
86         }
87     }
88     public HashMap<AffinityAttributeType, AffinityAttribute> getAttributeList() {
89         return this.attrlist;
90     }
91
92     /* Set the waypoint address, if the attribute is "redirect" */
93     public void setAttribute(String attribute) {
94         this.affinityAttribute = attribute;
95     }
96     
97     // Create a service chain of one waypoint. 
98     public void setWaypoint(String wpaddr) {
99         SetPathRedirect redirect = new SetPathRedirect();
100         redirect.addWaypoint(NetUtils.parseInetAddress(wpaddr));
101         
102         /* Add this service chain to this affinity link. */
103         addAttribute((AffinityAttribute) redirect);
104     }
105
106     // Unset the waypoint address.
107     public void unsetWaypoint() {        
108         attrlist.remove(AffinityAttributeType.SET_PATH_REDIRECT);
109     }
110
111     public AffinityAttribute getWaypoint() {
112         return attrlist.get(AffinityAttributeType.SET_PATH_REDIRECT);
113     }
114     
115     public boolean isDeny() {
116         return attrlist.containsKey(AffinityAttributeType.SET_DENY);
117     }
118
119     // Mark this with "deny"
120     public void setDeny() {
121         SetDeny deny = new SetDeny();
122         addAttribute(deny);
123     }
124
125     // Remove "deny" marking if it exists
126     public void unsetDeny() {
127         attrlist.remove(AffinityAttributeType.SET_DENY);
128     }
129     public String getAttribute() {
130         return this.affinityAttribute;
131     }
132     public AffinityGroup getFromGroup() {
133         return this.fromGroup;
134     }
135     public AffinityGroup getToGroup() {
136         return this.toGroup;
137     }
138     @Override
139     public String toString() {
140         String output = this.name;
141
142         if (attrlist != null) {
143             for (AffinityAttribute a: attrlist.values()) {
144                 output = output + "attr: " + a.toString() + "; ";
145             }
146         }
147         return output;
148     }
149 }
150