3232import org .springframework .http .HttpHeaders ;
3333import org .springframework .http .HttpMethod ;
3434import org .springframework .http .HttpStatus ;
35- import org .springframework .http .MediaType ;
3635import org .springframework .http .ResponseEntity ;
37- import org .springframework .http .client .ClientHttpResponse ;
38- import org .springframework .web .client .ResponseErrorHandler ;
3936import org .springframework .web .client .RestTemplate ;
4037import org .springframework .web .util .UriComponentsBuilder ;
4138
42- import java .io .IOException ;
4339import java .util .ArrayList ;
44- import java .util .Arrays ;
4540import java .util .List ;
4641
4742/**
@@ -51,38 +46,17 @@ public class Force10BaremetalSwitchBackend implements BaremetalSwitchBackend {
5146 private Logger logger = Logger .getLogger (Force10BaremetalSwitchBackend .class );
5247 public static final String TYPE = "Force10" ;
5348
54- private static List <HttpStatus > successHttpStatusCode = new ArrayList <>();
55- {
56- successHttpStatusCode .add (HttpStatus .OK );
57- successHttpStatusCode .add (HttpStatus .ACCEPTED );
58- successHttpStatusCode .add (HttpStatus .CREATED );
59- successHttpStatusCode .add (HttpStatus .NO_CONTENT );
60- successHttpStatusCode .add (HttpStatus .PARTIAL_CONTENT );
61- successHttpStatusCode .add (HttpStatus .RESET_CONTENT );
62- successHttpStatusCode .add (HttpStatus .ALREADY_REPORTED );
63- }
64-
6549 RestTemplate rest = new RestTemplate ();
66- {
67- // fake error handler, we handle error in business logic code
68- rest .setErrorHandler (new ResponseErrorHandler () {
69- @ Override
70- public boolean hasError (ClientHttpResponse clientHttpResponse ) throws IOException {
71- return false ;
72- }
7350
74- @ Override
75- public void handleError (ClientHttpResponse clientHttpResponse ) throws IOException {
76- }
77- });
78- }
79-
80- private String buildLink (String switchIp , String path ) {
51+ private String buildLink (String switchIp , Integer vlan ) {
8152 UriComponentsBuilder builder = UriComponentsBuilder .newInstance ();
8253 builder .scheme ("http" );
8354 builder .host (switchIp );
8455 builder .port (8008 );
85- builder .path (path );
56+ builder .path ("/api/running/ftos/interface/vlan" );
57+ if (vlan != null ) {
58+ builder .path (vlan .toString ());
59+ }
8660 return builder .build ().toUriString ();
8761 }
8862
@@ -93,36 +67,29 @@ public String getSwitchBackendType() {
9367
9468 @ Override
9569 public void prepareVlan (BaremetalVlanStruct struct ) {
96- String link = buildLink (struct .getSwitchIp (), String . format ( "/api/running/ftos/interface/vlan/%s" , struct .getVlan () ));
70+ String link = buildLink (struct .getSwitchIp (), struct .getVlan ());
9771 HttpHeaders headers = createBasicAuthenticationHeader (struct );
9872 HttpEntity <String > request = new HttpEntity <>(headers );
9973 ResponseEntity rsp = rest .exchange (link , HttpMethod .GET , request , String .class );
100- logger .debug (String .format ("http get: %s" , link ));
10174
10275 if (rsp .getStatusCode () == HttpStatus .NOT_FOUND ) {
10376 PortInfo port = new PortInfo (struct );
104- XmlObject xml = new XmlObject ("vlan" ).putElement ("vlan-id" ,
105- new XmlObject ("vlan-id" ).setText (String .valueOf (struct .getVlan ()))).putElement ("untagged" ,
106- new XmlObject ("untagged" ).putElement (port .interfaceType , new XmlObject (port .interfaceType )
107- .putElement ("name" , new XmlObject ("name" ).setText (port .port )))
108- ).putElement ("shutdown" , new XmlObject ("shutdown" ).setText ("false" ));
109- request = new HttpEntity <>(xml .dump (), headers );
110- link = buildLink (struct .getSwitchIp (), String .format ("/api/running/ftos/interface/" ));
111- logger .debug (String .format ("http get: %s, body: %s" , link , request ));
112- rsp = rest .exchange (link , HttpMethod .POST , request , String .class );
113- if (!successHttpStatusCode .contains (rsp .getStatusCode ())) {
77+ XmlObject xml = new XmlObject ("vlan" ).putElement ("vlan-id" , String .valueOf (struct .getVlan ())).putElement ("tagged" ,
78+ new XmlObject (port .interfaceType ).putElement ("name" , port .port )
79+ ).putElement ("shutdown" , "false" );
80+ request = new HttpEntity <>(xml .toString (), headers );
81+ link = buildLink (struct .getSwitchIp (), null );
82+ rsp = rest .exchange (link , HttpMethod .GET , request , String .class );
83+ if (rsp .getStatusCode () != HttpStatus .OK ) {
11484 throw new CloudRuntimeException (String .format ("unable to create vlan[%s] on force10 switch[ip:%s]. HTTP status code:%s, body dump:%s" ,
115- struct .getVlan (), struct .getSwitchIp (),rsp .getStatusCode (), rsp .getBody ()));
116- } else {
117- logger .debug (String .format ("successfully programmed vlan[%s] on Force10[ip:%s, port:%s]. http response[status code:%s, body:%s]" ,
118- struct .getVlan (), struct .getSwitchIp (), struct .getPort (), rsp .getStatusCode (), rsp .getBody ()));
85+ struct .getVlan (), rsp .getStatusCode (), struct .getSwitchIp (), rsp .getBody ()));
11986 }
120- } else if (successHttpStatusCode . contains ( rsp .getStatusCode ()) ) {
87+ } else if (rsp .getStatusCode () == HttpStatus . OK ) {
12188 PortInfo port = new PortInfo (struct );
12289 XmlObject xml = XmlObjectParser .parseFromString ((String )rsp .getBody ());
123- List <XmlObject > ports = xml .getAsList ("untagged .tengigabitethernet" );
124- ports .addAll (xml .<XmlObject >getAsList ("untagged .gigabitethernet" ));
125- ports .addAll (xml .<XmlObject >getAsList ("untagged .fortyGigE" ));
90+ List <XmlObject > ports = xml .getAsList ("tagged .tengigabitethernet" );
91+ ports .addAll (xml .<XmlObject >getAsList ("tagged .gigabitethernet" ));
92+ ports .addAll (xml .<XmlObject >getAsList ("tagged .fortyGigE" ));
12693 for (XmlObject pxml : ports ) {
12794 XmlObject name = pxml .get ("name" );
12895 if (port .port .equals (name .getText ())) {
@@ -131,26 +98,14 @@ public void prepareVlan(BaremetalVlanStruct struct) {
13198 }
13299 }
133100
134- xml .removeElement ("mtu" );
135- xml .setText (null );
136- XmlObject tag = xml .get ("untagged" );
137- if (tag == null ) {
138- tag = new XmlObject ("untagged" );
139- xml .putElement ("untagged" , tag );
140- }
141-
142- tag .putElement (port .interfaceType , new XmlObject (port .interfaceType )
143- .putElement ("name" , new XmlObject ("name" ).setText (port .port )));
144- request = new HttpEntity <>(xml .dump (), headers );
145- link = buildLink (struct .getSwitchIp (), String .format ("/api/running/ftos/interface/vlan/%s" , struct .getVlan ()));
146- logger .debug (String .format ("http get: %s, body: %s" , link , request ));
101+ XmlObject tag = xml .get ("tagged" );
102+ tag .putElement (port .interfaceType , new XmlObject ("name" ).setText (port .port ));
103+ request = new HttpEntity <>(xml .toString (), headers );
104+ link = buildLink (struct .getSwitchIp (), struct .getVlan ());
147105 rsp = rest .exchange (link , HttpMethod .PUT , request , String .class );
148- if (! successHttpStatusCode . contains ( rsp .getStatusCode ()) ) {
106+ if (rsp .getStatusCode () != HttpStatus . NO_CONTENT ) {
149107 throw new CloudRuntimeException (String .format ("failed to program vlan[%s] for port[%s] on force10[ip:%s]. http status:%s, body dump:%s" ,
150108 struct .getVlan (), struct .getPort (), struct .getSwitchIp (), rsp .getStatusCode (), rsp .getBody ()));
151- } else {
152- logger .debug (String .format ("successfully join port[%s] into vlan[%s] on Force10[ip:%s]. http response[status code:%s, body:%s]" ,
153- struct .getPort (), struct .getVlan (), struct .getSwitchIp (), rsp .getStatusCode (), rsp .getBody ()));
154109 }
155110 } else {
156111 throw new CloudRuntimeException (String .format ("force10[ip:%s] returns unexpected error[%s] when http getting %s, body dump:%s" ,
@@ -160,19 +115,18 @@ public void prepareVlan(BaremetalVlanStruct struct) {
160115
161116 @ Override
162117 public void removePortFromVlan (BaremetalVlanStruct struct ) {
163- String link = buildLink (struct .getSwitchIp (), String . format ( "/api/running/ftos/interface/vlan/%s" , struct .getVlan () ));
118+ String link = buildLink (struct .getSwitchIp (), struct .getVlan ());
164119 HttpHeaders headers = createBasicAuthenticationHeader (struct );
165120 HttpEntity <String > request = new HttpEntity <>(headers );
166- logger .debug (String .format ("http get: %s, body: %s" , link , request ));
167121 ResponseEntity rsp = rest .exchange (link , HttpMethod .GET , request , String .class );
168122 if (rsp .getStatusCode () == HttpStatus .NOT_FOUND ) {
169123 logger .debug (String .format ("vlan[%s] has been deleted on force10[ip:%s], no need to remove the port[%s] anymore" , struct .getVlan (), struct .getSwitchIp (), struct .getPort ()));
170124 } else if (rsp .getStatusCode () == HttpStatus .OK ) {
171125 PortInfo port = new PortInfo (struct );
172126 XmlObject xml = XmlObjectParser .parseFromString ((String )rsp .getBody ());
173- List <XmlObject > ports = xml .getAsList ("untagged .tengigabitethernet" );
174- ports .addAll (xml .<XmlObject >getAsList ("untagged .gigabitethernet" ));
175- ports .addAll (xml .<XmlObject >getAsList ("untagged .fortyGigE" ));
127+ List <XmlObject > ports = xml .getAsList ("tagged .tengigabitethernet" );
128+ ports .addAll (xml .<XmlObject >getAsList ("tagged .gigabitethernet" ));
129+ ports .addAll (xml .<XmlObject >getAsList ("tagged .fortyGigE" ));
176130 List <XmlObject > newPorts = new ArrayList <>();
177131 boolean needRemove = false ;
178132 for (XmlObject pxml : ports ) {
@@ -189,19 +143,11 @@ public void removePortFromVlan(BaremetalVlanStruct struct) {
189143 return ;
190144 }
191145
192- xml .setText (null );
193- xml .removeElement ("mtu" );
194- XmlObject tagged = xml .get ("untagged" );
195- tagged .removeAllChildren ();
196- for (XmlObject p : newPorts ) {
197- tagged .putElement (p .getTag (), p );
198- }
199-
146+ xml .putElement ("tagged" , newPorts );
200147
201- request = new HttpEntity <>(xml .dump (), headers );
202- logger .debug (String .format ("http get: %s, body: %s" , link , request ));
148+ request = new HttpEntity <>(xml .toString (), headers );
203149 rsp = rest .exchange (link , HttpMethod .PUT , request , String .class );
204- if (! successHttpStatusCode . contains ( rsp .getStatusCode ()) ) {
150+ if (rsp .getStatusCode () != HttpStatus . NO_CONTENT ) {
205151 throw new CloudRuntimeException (String .format ("failed to program vlan[%s] for port[%s] on force10[ip:%s]. http status:%s, body dump:%s" ,
206152 struct .getVlan (), struct .getPort (), struct .getSwitchIp (), rsp .getStatusCode (), rsp .getBody ()));
207153 } else {
@@ -220,8 +166,6 @@ private HttpHeaders createBasicAuthenticationHeader(BaremetalVlanStruct struct)
220166 String base64Creds = new String (base64CredsBytes );
221167 HttpHeaders headers = new HttpHeaders ();
222168 headers .add ("Authorization" , "Basic " + base64Creds );
223- headers .setAccept (Arrays .asList (MediaType .ALL ));
224- headers .setContentType (MediaType .valueOf ("application/vnd.yang.data+xml" ));
225169 return headers ;
226170 }
227171
0 commit comments