package org.opennaas.extensions.openflowswitch.driver.floodlight.protocol.client;
/*
* #%L
* OpenNaaS :: OpenFlow Switch :: Floodlight driver v0.90
* %%
* Copyright (C) 2007 - 2014 FundaciĆ³ Privada i2CAT, Internet i InnovaciĆ³ a Catalunya
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import org.apache.commons.io.IOUtils;
import org.opennaas.core.resources.protocol.ProtocolException;
import org.opennaas.core.resources.protocol.ProtocolSessionContext;
import org.opennaas.extensions.openflowswitch.driver.floodlight.protocol.client.serializers.json.CustomJSONProvider;
import org.opennaas.extensions.openflowswitch.driver.floodlight.protocol.client.wrappers.FloodlightOFFlowsWrapper;
import org.opennaas.extensions.openflowswitch.model.FloodlightOFFlow;
/**
* Floodlight special client mixing CXF and Java clients allowing sending HTTP DELETE with body
*
* @author Julio Carlos Barrera
*
*/
public class FloodlightStaticFlowPusherClient implements IFloodlightStaticFlowPusherClient {
private ProtocolSessionContext sessionContext;
private IFloodlightStaticFlowPusherClient cxfClient;
public FloodlightStaticFlowPusherClient(IFloodlightStaticFlowPusherClient cxfClient, ProtocolSessionContext sessionContext) {
this.cxfClient = cxfClient;
this.sessionContext = sessionContext;
}
@Override
public void addFlow(FloodlightOFFlow flow) throws ProtocolException, Exception {
cxfClient.addFlow(flow);
}
@Override
public void deleteFlow(FloodlightOFFlow flow) throws ProtocolException, Exception {
String uri = (String) sessionContext.getSessionParameters().get(ProtocolSessionContext.PROTOCOL_URI);
try {
// / create URI based on base path, common static flow pusher path and JSON
URL url = new URL(uri + "/wm/staticflowentrypusher" + "/json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// override HTTP method allowing sending body
connection.setRequestProperty("X-HTTP-Method-Override", "DELETE");
connection.setDoOutput(true);
// prepare body
String messageBody = new CustomJSONProvider().locateMapper(FloodlightOFFlow.class, MediaType.APPLICATION_JSON_TYPE).writeValueAsString(
flow);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(messageBody);
wr.flush();
// get HTTP Response
String response = IOUtils.toString(connection.getInputStream(), "UTF-8");
// verify correct JSON response
if (!response.equals("{\"status\" : \"Entry " + flow.getName() + " deleted\"}")) {
throw new Exception("Invalid response: " + response);
}
} catch (IOException e) {
throw new ProtocolException(e);
}
}
@Override
public void deleteFlowsForSwitch(String dpid) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public void deleteAllFlows() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Map<String, List<FloodlightOFFlow>> getFlows() throws ProtocolException, Exception {
return cxfClient.getFlows();
}
@Override
public FloodlightOFFlowsWrapper getFlows(String dpid) throws ProtocolException, Exception {
return cxfClient.getFlows(dpid);
}
}