SureshBR
Committed by Thomas Vachuska

[ONOS-2337,ONOS-2338,ONOS-2339]Pcep Controller

Change-Id: I27fee4669ec923a55d83993b493699c1c46a1b36
......@@ -15,7 +15,7 @@
*/
package org.onosproject.pcep.controller;
import java.util.List;
import java.util.Collection;
import org.onosproject.pcepio.protocol.PcepMessage;
......@@ -31,7 +31,7 @@ public interface PcepClientController {
*
* @return list of PcepClient elements
*/
public List<PcepClient> getClients();
Collection<PcepClient> getClients();
/**
* Returns the actual pcc client for the given ip address.
......@@ -39,7 +39,7 @@ public interface PcepClientController {
* @param pccId the id of the pcc client to fetch
* @return the interface to this pcc client
*/
public PcepClient getClient(PccId pccId);
PcepClient getClient(PccId pccId);
/**
* Register a listener for meta events that occur to pcep
......@@ -47,28 +47,28 @@ public interface PcepClientController {
*
* @param listener the listener to notify
*/
public void addListener(PcepClientListener listener);
void addListener(PcepClientListener listener);
/**
* Unregister a listener.
*
* @param listener the listener to unregister
*/
public void removeListener(PcepClientListener listener);
void removeListener(PcepClientListener listener);
/**
* Register a listener for OF msg events.
*
* @param listener the listener to notify
*/
public void addEventListener(PcepEventListener listener);
void addEventListener(PcepEventListener listener);
/**
* Unregister a listener.
*
* @param listener the listener to unregister
*/
public void removeEventListener(PcepEventListener listener);
void removeEventListener(PcepEventListener listener);
/**
* Send a message to a particular pcc client.
......@@ -76,7 +76,7 @@ public interface PcepClientController {
* @param pccId the id of the client to send message.
* @param msg the message to send
*/
public void writeMessage(PccId pccId, PcepMessage msg);
void writeMessage(PccId pccId, PcepMessage msg);
/**
* Process a message and notify the appropriate listeners.
......@@ -84,10 +84,10 @@ public interface PcepClientController {
* @param pccId id of the client the message arrived on
* @param msg the message to process.
*/
public void processClientMessage(PccId pccId, PcepMessage msg);
void processClientMessage(PccId pccId, PcepMessage msg);
/**
* Close all connected PCC clients.
*/
public void closeConnectedClients();
void closeConnectedClients();
}
......
......@@ -42,6 +42,27 @@ public interface PcepPacketStats {
public int wrongPacketCount();
/**
* Increments the received packet counter.
*/
public void addInPacket();
/**
* Increments the sent packet counter.
*/
public void addOutPacket();
/**
* Increments the sent packet counter by specified value.
* @param value of no of packets sent
*/
public void addOutPacket(int value);
/**
* Increments the wrong packet counter.
*/
public void addWrongPacket();
/**
* Returns the time value.
*
* @return long value of time
......
<!--
~ Copyright 2014 Open Networking Laboratory
~
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-pcep-controller</artifactId>
<version>1.3.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onos-pcep-controller-impl</artifactId>
<packaging>bundle</packaging>
<description>ONOS PCEP controller subsystem API</description>
<dependencies>
<dependency>
<!-- FIXME once experimenter gets merged to upstream -->
<groupId>org.onosproject</groupId>
<artifactId>onos-pcep-controller-api</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-misc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
*/
package org.onosproject.pcep.controller.impl;
import static org.onlab.util.Tools.groupedThreads;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.onosproject.pcep.controller.PccId;
import org.onosproject.pcep.controller.PcepPacketStats;
import org.onosproject.pcep.controller.driver.PcepAgent;
import org.onosproject.pcep.controller.driver.PcepClientDriver;
import org.onosproject.pcepio.protocol.PcepFactories;
import org.onosproject.pcepio.protocol.PcepFactory;
import org.onosproject.pcepio.protocol.PcepVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The main controller class. Handles all setup and network listeners -
* Distributed ownership control of pcc through IControllerRegistryService
*/
public class Controller {
private static final Logger log = LoggerFactory.getLogger(Controller.class);
private static final PcepFactory FACTORY1 = PcepFactories.getFactory(PcepVersion.PCEP_1);
private ChannelGroup cg;
// Configuration options
private int pcepPort = 4189;
private int workerThreads = 10;
// Start time of the controller
private long systemStartTime;
private PcepAgent agent;
private NioServerSocketChannelFactory execFactory;
// Perf. related configuration
private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
/**
* Returns factory version for processing pcep messages.
*
* @return instance of factory version
*/
public PcepFactory getPcepMessageFactory1() {
return FACTORY1;
}
/**
* To get system start time.
*
* @return system start time in milliseconds
*/
public long getSystemStartTime() {
return (this.systemStartTime);
}
/**
* Tell controller that we're ready to accept pcc connections.
*/
public void run() {
try {
final ServerBootstrap bootstrap = createServerBootStrap();
bootstrap.setOption("reuseAddr", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
ChannelPipelineFactory pfact = new PcepPipelineFactory(this);
bootstrap.setPipelineFactory(pfact);
InetSocketAddress sa = new InetSocketAddress(pcepPort);
cg = new DefaultChannelGroup();
cg.add(bootstrap.bind(sa));
log.info("Listening for PCC connection on {}", sa);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Creates server boot strap.
*
* @return ServerBootStrap
*/
private ServerBootstrap createServerBootStrap() {
if (workerThreads == 0) {
execFactory = new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(groupedThreads("onos/pcep", "boss-%d")),
Executors.newCachedThreadPool(groupedThreads("onos/pcep", "worker-%d")));
return new ServerBootstrap(execFactory);
} else {
execFactory = new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(groupedThreads("onos/pcep", "boss-%d")),
Executors.newCachedThreadPool(groupedThreads("onos/pcep", "worker-%d")), workerThreads);
return new ServerBootstrap(execFactory);
}
}
/**
* Initialize internal data structures.
*/
public void init() {
// These data structures are initialized here because other
// module's startUp() might be called before ours
this.systemStartTime = System.currentTimeMillis();
}
public Map<String, Long> getMemory() {
Map<String, Long> m = new HashMap<>();
Runtime runtime = Runtime.getRuntime();
m.put("total", runtime.totalMemory());
m.put("free", runtime.freeMemory());
return m;
}
public Long getUptime() {
RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
return rb.getUptime();
}
/**
* Creates instance of Pcep client.
*
* @param pccId pcc identifier
* @param sessionID session id
* @param pv pcep version
* @param pktStats pcep packet statistics
* @return instance of PcepClient
*/
protected PcepClientDriver getPcepClientInstance(PccId pccId, int sessionID, PcepVersion pv,
PcepPacketStats pktStats) {
PcepClientDriver pcepClientDriver = new PcepClientImpl();
pcepClientDriver.init(pccId, pv, pktStats);
pcepClientDriver.setAgent(agent);
return pcepClientDriver;
}
/**
* Starts the pcep controller.
*
* @param ag Pcep agent
*/
public void start(PcepAgent ag) {
log.info("Started");
this.agent = ag;
this.init();
this.run();
}
/**
* Stops the pcep controller.
*/
public void stop() {
log.info("Stopped");
execFactory.shutdown();
cg.close();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
*/
package org.onosproject.pcep.controller.impl;
import static org.onlab.util.Tools.groupedThreads;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.onosproject.net.driver.DriverService;
import org.onosproject.pcep.controller.PccId;
import org.onosproject.pcep.controller.PcepClient;
import org.onosproject.pcep.controller.PcepClientController;
import org.onosproject.pcep.controller.PcepClientListener;
import org.onosproject.pcep.controller.PcepEventListener;
import org.onosproject.pcep.controller.driver.PcepAgent;
import org.onosproject.pcepio.protocol.PcepMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Sets;
@Component(immediate = true)
@Service
public class PcepClientControllerImpl implements PcepClientController {
private static final Logger log = LoggerFactory.getLogger(PcepClientControllerImpl.class);
//@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected DriverService driverService;
private final ExecutorService executorMsgs =
Executors.newFixedThreadPool(32, groupedThreads("onos/pcep", "event-stats-%d"));
private final ExecutorService executorBarrier =
Executors.newFixedThreadPool(4, groupedThreads("onos/pcep", "event-barrier-%d"));
protected ConcurrentHashMap<PccId, PcepClient> connectedClients =
new ConcurrentHashMap<PccId, PcepClient>();
protected PcepClientAgent agent = new PcepClientAgent();
protected Set<PcepClientListener> pcepClientListener = new HashSet<>();
protected Set<PcepEventListener> pcepEventListener = Sets.newHashSet();
private final Controller ctrl = new Controller();
@Activate
public void activate() {
ctrl.start(agent);
log.info("Started");
}
@Deactivate
public void deactivate() {
// Close all connected clients
closeConnectedClients();
ctrl.stop();
log.info("Stopped");
}
@Override
public Collection<PcepClient> getClients() {
return connectedClients.values();
}
@Override
public PcepClient getClient(PccId pccId) {
return connectedClients.get(pccId);
}
@Override
public void addListener(PcepClientListener listener) {
if (!pcepClientListener.contains(listener)) {
this.pcepClientListener.add(listener);
}
}
@Override
public void removeListener(PcepClientListener listener) {
this.pcepClientListener.remove(listener);
}
@Override
public void addEventListener(PcepEventListener listener) {
pcepEventListener.add(listener);
}
@Override
public void removeEventListener(PcepEventListener listener) {
pcepEventListener.remove(listener);
}
@Override
public void writeMessage(PccId pccId, PcepMessage msg) {
this.getClient(pccId).sendMessage(msg);
}
@Override
public void processClientMessage(PccId pccId, PcepMessage msg) {
PcepClient pc = getClient(pccId);
switch (msg.getType()) {
case NONE:
break;
case OPEN:
break;
case KEEP_ALIVE:
break;
case PATH_COMPUTATION_REQUEST:
break;
case PATH_COMPUTATION_REPLY:
break;
case NOTIFICATION:
break;
case ERROR:
break;
case CLOSE:
log.info("Sending Close Message to {" + pccId.toString() + "}");
pc.sendMessage(Collections.singletonList(pc.factory().buildCloseMsg().build()));
//now disconnect client
pc.disconnectClient();
break;
case REPORT:
for (PcepEventListener l : pcepEventListener) {
l.handleMessage(pccId, msg);
}
break;
case UPDATE:
for (PcepEventListener l : pcepEventListener) {
l.handleMessage(pccId, msg);
}
break;
case INITIATE:
for (PcepEventListener l : pcepEventListener) {
l.handleMessage(pccId, msg);
}
break;
case LABEL_UPDATE:
break;
case MAX:
break;
case END:
break;
default:
break;
}
}
@Override
public void closeConnectedClients() {
PcepClient pc;
for (PccId id : connectedClients.keySet()) {
pc = getClient(id);
pc.disconnectClient();
}
}
/**
* Implementation of an Pcep Agent which is responsible for
* keeping track of connected clients and the state in which
* they are.
*/
public class PcepClientAgent implements PcepAgent {
private final Logger log = LoggerFactory.getLogger(PcepClientAgent.class);
private final Lock clientLock = new ReentrantLock();
@Override
public boolean addConnectedClient(PccId pccId, PcepClient pc) {
if (connectedClients.get(pccId) != null) {
log.error("Trying to add connectedClient but found a previous "
+ "value for pcc ip: {}", pccId.toString());
return false;
} else {
log.debug("Added Client {}", pccId.toString());
connectedClients.put(pccId, pc);
for (PcepClientListener l : pcepClientListener) {
l.clientConnected(pccId);
}
return true;
}
}
@Override
public boolean validActivation(PccId pccId) {
if (connectedClients.get(pccId) == null) {
log.error("Trying to activate client but is not in "
+ "connected client: pccIp {}. Aborting ..", pccId.toString());
return false;
}
return true;
}
@Override
public void removeConnectedClient(PccId pccId) {
connectedClients.remove(pccId);
for (PcepClientListener l : pcepClientListener) {
log.warn("removal for {}", pccId.toString());
l.clientDisconnected(pccId);
}
}
@Override
public void processPcepMessage(PccId pccId, PcepMessage m) {
processClientMessage(pccId, m);
}
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
*/
package org.onosproject.pcep.controller.impl;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import org.jboss.netty.channel.Channel;
import org.onlab.packet.IpAddress;
import org.onosproject.pcep.controller.PccId;
import org.onosproject.pcep.controller.PcepPacketStats;
import org.onosproject.pcep.controller.driver.PcepAgent;
import org.onosproject.pcep.controller.driver.PcepClientDriver;
import org.onosproject.pcepio.protocol.PcepFactories;
import org.onosproject.pcepio.protocol.PcepFactory;
import org.onosproject.pcepio.protocol.PcepMessage;
import org.onosproject.pcepio.protocol.PcepVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.MoreObjects;
/**
* An abstract representation of an OpenFlow switch. Can be extended by others
* to serve as a base for their vendor specific representation of a switch.
*/
public class PcepClientImpl implements PcepClientDriver {
protected final Logger log = LoggerFactory.getLogger(PcepClientImpl.class);
private static final String SHUTDOWN_MSG = "Worker has already been shutdown";
private Channel channel;
protected String channelId;
private boolean connected;
protected boolean startDriverHandshakeCalled = false;
protected boolean isHandShakeComplete = false;
protected boolean isSyncComplete = false;
private PccId pccId;
private PcepAgent agent;
private PcepVersion pcepVersion;
private byte keepAliveTime;
private byte deadTime;
private byte sessionId;
private PcepPacketStatsImpl pktStats;
@Override
public void init(PccId pccId, PcepVersion pcepVersion, PcepPacketStats pktStats) {
this.pccId = pccId;
this.pcepVersion = pcepVersion;
this.pktStats = (PcepPacketStatsImpl) pktStats;
}
@Override
public final void disconnectClient() {
this.channel.close();
}
@Override
public final void sendMessage(PcepMessage m) {
log.debug("Sending message to {}", channel.getRemoteAddress());
try {
channel.write(Collections.singletonList(m));
this.pktStats.addOutPacket();
} catch (RejectedExecutionException e) {
log.warn(e.getMessage());
if (!e.getMessage().contains(SHUTDOWN_MSG)) {
throw e;
}
}
}
@Override
public final void sendMessage(List<PcepMessage> msgs) {
try {
channel.write(msgs);
this.pktStats.addOutPacket(msgs.size());
} catch (RejectedExecutionException e) {
log.warn(e.getMessage());
if (!e.getMessage().contains(SHUTDOWN_MSG)) {
throw e;
}
}
}
@Override
public final boolean isConnected() {
return this.connected;
}
@Override
public final void setConnected(boolean connected) {
this.connected = connected;
};
@Override
public final void setChannel(Channel channel) {
this.channel = channel;
final SocketAddress address = channel.getRemoteAddress();
if (address instanceof InetSocketAddress) {
final InetSocketAddress inetAddress = (InetSocketAddress) address;
final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress());
if (ipAddress.isIp4()) {
channelId = ipAddress.toString() + ':' + inetAddress.getPort();
} else {
channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort();
}
}
};
@Override
public String channelId() {
return channelId;
}
@Override
public final PccId getPccId() {
return this.pccId;
}
@Override
public final String getStringId() {
return this.pccId.toString();
}
@Override
public final void setPcVersion(PcepVersion pcepVersion) {
this.pcepVersion = pcepVersion;
}
@Override
public void setPcKeepAliveTime(byte keepAliveTime) {
this.keepAliveTime = keepAliveTime;
}
@Override
public void setPcDeadTime(byte deadTime) {
this.deadTime = deadTime;
}
@Override
public void setPcSessionId(byte sessionId) {
this.sessionId = sessionId;
}
@Override
public void setIsSyncComplete(boolean value) {
this.isSyncComplete = value;
}
@Override
public boolean isSyncComplete() {
return isSyncComplete;
}
@Override
public final void handleMessage(PcepMessage m) {
this.pktStats.addInPacket();
this.agent.processPcepMessage(pccId, m);
}
@Override
public final boolean connectClient() {
return this.agent.addConnectedClient(pccId, this);
}
@Override
public final void removeConnectedClient() {
this.agent.removeConnectedClient(pccId);
}
@Override
public PcepFactory factory() {
return PcepFactories.getFactory(pcepVersion);
}
@Override
public boolean isHandshakeComplete() {
return isHandShakeComplete;
}
@Override
public final void setAgent(PcepAgent ag) {
if (this.agent == null) {
this.agent = ag;
}
}
@Override
public boolean isOptical() {
return false;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("channel", channelId())
.add("pccId", getPccId())
.toString();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
*/
package org.onosproject.pcep.controller.impl;
import java.util.LinkedList;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;
import org.onosproject.pcepio.protocol.PcepFactories;
import org.onosproject.pcepio.protocol.PcepMessage;
import org.onosproject.pcepio.protocol.PcepMessageReader;
import org.onosproject.pcepio.util.HexDump;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Decode an pcep message from a Channel, for use in a netty pipeline.
*/
public class PcepMessageDecoder extends FrameDecoder {
protected static final Logger log = LoggerFactory.getLogger(PcepMessageDecoder.class);
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
ChannelBuffer buffer) throws Exception {
log.debug("Message received.");
if (!channel.isConnected()) {
log.info("Channel is not connected.");
// In testing, I see decode being called AFTER decode last.
// This check avoids that from reading corrupted frames
return null;
}
HexDump.pcepHexDump(buffer);
// Note that a single call to decode results in reading a single
// PcepMessage from the channel buffer, which is passed on to, and processed
// by, the controller (in PcepChannelHandler).
// This is different from earlier behavior (with the original pcepIO),
// where we parsed all the messages in the buffer, before passing on
// a list of the parsed messages to the controller.
// The performance *may or may not* not be as good as before.
PcepMessageReader<PcepMessage> reader = PcepFactories.getGenericReader();
List<PcepMessage> msgList = new LinkedList<PcepMessage>();
while (buffer.readableBytes() > 0) {
PcepMessage message = reader.readFrom(buffer);
msgList.add(message);
}
return msgList;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
*/
package org.onosproject.pcep.controller.impl;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
import org.onosproject.pcepio.protocol.PcepMessage;
import org.onosproject.pcepio.util.HexDump;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Encode an pcep message for output into a ChannelBuffer, for use in a
* netty pipeline.
*/
public class PcepMessageEncoder extends OneToOneEncoder {
protected static final Logger log = LoggerFactory.getLogger(PcepMessageEncoder.class);
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
log.debug("Sending message");
if (!(msg instanceof List)) {
log.debug("Invalid msg.");
return msg;
}
@SuppressWarnings("unchecked")
List<PcepMessage> msglist = (List<PcepMessage>) msg;
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
for (PcepMessage pm : msglist) {
pm.writeTo(buf);
}
HexDump.pcepHexDump(buf);
return buf;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
*/
package org.onosproject.pcep.controller.impl;
import org.onosproject.pcep.controller.PcepPacketStats;
/**
* A representation of a packet context which allows any provider
* to view a packet in event, but may block the response to the
* event if blocked has been called. This packet context can be used
* to react to the packet in event with a packet out.
*/
public class PcepPacketStatsImpl implements PcepPacketStats {
private int inPacketCount;
private int outPacketCount;
private int wrongPacketCount;
private long time;
public PcepPacketStatsImpl() {
this.inPacketCount = 0;
this.outPacketCount = 0;
this.wrongPacketCount = 0;
this.time = 0;
}
@Override
public int outPacketCount() {
return outPacketCount;
}
@Override
public int inPacketCount() {
return inPacketCount;
}
@Override
public int wrongPacketCount() {
return wrongPacketCount;
}
/**
* Increments the received packet counter.
*/
public void addInPacket() {
this.inPacketCount++;
}
/**
* Increments the sent packet counter.
*/
public void addOutPacket() {
this.outPacketCount++;
}
/**
* Increments the sent packet counter by specified value.
*
* @param value of no of packets sent
*/
public void addOutPacket(int value) {
this.outPacketCount = this.outPacketCount + value;
}
/**
* Increments the wrong packet counter.
*/
public void addWrongPacket() {
this.wrongPacketCount++;
}
public void resetWrongPacket() {
this.wrongPacketCount = 0;
}
@Override
public long getTime() {
return this.time;
}
/**
* Sets the time value.
*
* @param time long value of time
*/
public void setTime(long time) {
this.time = time;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
*/
package org.onosproject.pcep.controller.impl;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
import org.jboss.netty.util.ExternalResourceReleasable;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;
/**
* Creates a ChannelPipeline for a server-side pcep channel.
*/
public class PcepPipelineFactory
implements ChannelPipelineFactory, ExternalResourceReleasable {
protected Controller controller;
static final Timer TIMER = new HashedWheelTimer();
protected IdleStateHandler idleHandler;
protected ReadTimeoutHandler readTimeoutHandler;
static final int DEFAULT_KEEP_ALIVE_TIME = 30;
static final int DEFAULT_DEAD_TIME = 120;
static final int DEFAULT_WAIT_TIME = 60;
public PcepPipelineFactory(Controller controller) {
super();
this.controller = controller;
this.idleHandler = new IdleStateHandler(TIMER, DEFAULT_DEAD_TIME, DEFAULT_KEEP_ALIVE_TIME, 0);
this.readTimeoutHandler = new ReadTimeoutHandler(TIMER, DEFAULT_WAIT_TIME);
}
@Override
public ChannelPipeline getPipeline() throws Exception {
PcepChannelHandler handler = new PcepChannelHandler(controller);
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("pcepmessagedecoder", new PcepMessageDecoder());
pipeline.addLast("pcepmessageencoder", new PcepMessageEncoder());
pipeline.addLast("idle", idleHandler);
pipeline.addLast("waittimeout", readTimeoutHandler);
pipeline.addLast("handler", handler);
return pipeline;
}
@Override
public void releaseExternalResources() {
TIMER.stop();
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
*/
/**
* Implementation of the PCEP controller subsystem.
*/
package org.onosproject.pcep.controller.impl;
package org.onosproject.pcepio.util;
import org.jboss.netty.buffer.ChannelBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class HexDump {
protected static final Logger log = LoggerFactory.getLogger(HexDump.class);
private HexDump() {
}
public static void pcepHexDump(ChannelBuffer buff) {
log.debug("==================== HEX DUMP ======================");
try {
byte[] yTemp;
yTemp = buff.array();
int iStartIndex = buff.readerIndex();
int iEndIndex = buff.writerIndex();
do {
StringBuilder sb = new StringBuilder();
for (int k = 0; (k < 16) && (iStartIndex < iEndIndex); ++k) {
if (0 == k % 4) {
sb.append(String.format(" ")); //blank after 4 bytes
}
sb.append(String.format("%02X ", yTemp[iStartIndex++]));
}
log.debug(sb.toString());
} while (iStartIndex < iEndIndex);
} catch (Exception e) {
log.error("[HexDump] Invalid buffer: " + e.toString());
}
log.debug("===================================================");
}
}
......@@ -33,6 +33,7 @@
<modules>
<module>api</module>
<module>ctl</module>
<module>pcepio</module>
</modules>
......