SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.pcep.controller.impl; |
| 18 | |
| 19 | import java.net.InetSocketAddress; |
| 20 | import java.net.SocketAddress; |
| 21 | import java.util.Collections; |
| 22 | import java.util.List; |
| 23 | import java.util.concurrent.RejectedExecutionException; |
| 24 | |
| 25 | import org.jboss.netty.channel.Channel; |
| 26 | import org.onlab.packet.IpAddress; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 27 | import org.onosproject.pcep.controller.ClientCapability; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 28 | import org.onosproject.pcep.controller.PccId; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 29 | import org.onosproject.pcep.controller.PcepClient; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 30 | import org.onosproject.pcep.controller.PcepPacketStats; |
Avantika-Huawei | 7f7376a | 2016-05-11 17:07:50 +0530 | [diff] [blame] | 31 | import org.onosproject.pcep.controller.PcepSyncStatus; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 32 | import org.onosproject.pcep.controller.driver.PcepAgent; |
| 33 | import org.onosproject.pcep.controller.driver.PcepClientDriver; |
| 34 | import org.onosproject.pcepio.protocol.PcepFactories; |
| 35 | import org.onosproject.pcepio.protocol.PcepFactory; |
| 36 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 37 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
| 41 | import com.google.common.base.MoreObjects; |
| 42 | |
| 43 | /** |
| 44 | * An abstract representation of an OpenFlow switch. Can be extended by others |
| 45 | * to serve as a base for their vendor specific representation of a switch. |
| 46 | */ |
| 47 | public class PcepClientImpl implements PcepClientDriver { |
| 48 | |
| 49 | protected final Logger log = LoggerFactory.getLogger(PcepClientImpl.class); |
| 50 | |
| 51 | private static final String SHUTDOWN_MSG = "Worker has already been shutdown"; |
| 52 | |
| 53 | private Channel channel; |
| 54 | protected String channelId; |
| 55 | |
| 56 | private boolean connected; |
Avantika-Huawei | 7f7376a | 2016-05-11 17:07:50 +0530 | [diff] [blame] | 57 | protected boolean startDriverHandshakeCalled; |
| 58 | protected boolean isHandShakeComplete; |
| 59 | private PcepSyncStatus lspDbSyncStatus; |
| 60 | private PcepSyncStatus labelDbSyncStatus; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 61 | private PccId pccId; |
| 62 | private PcepAgent agent; |
| 63 | |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 64 | private ClientCapability capability; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 65 | private PcepVersion pcepVersion; |
| 66 | private byte keepAliveTime; |
| 67 | private byte deadTime; |
| 68 | private byte sessionId; |
| 69 | private PcepPacketStatsImpl pktStats; |
| 70 | |
| 71 | @Override |
| 72 | public void init(PccId pccId, PcepVersion pcepVersion, PcepPacketStats pktStats) { |
| 73 | this.pccId = pccId; |
| 74 | this.pcepVersion = pcepVersion; |
| 75 | this.pktStats = (PcepPacketStatsImpl) pktStats; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public final void disconnectClient() { |
| 80 | this.channel.close(); |
| 81 | } |
| 82 | |
| 83 | @Override |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 84 | public void setCapability(ClientCapability capability) { |
| 85 | this.capability = capability; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public ClientCapability capability() { |
| 90 | return capability; |
| 91 | } |
| 92 | |
| 93 | @Override |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 94 | public final void sendMessage(PcepMessage m) { |
| 95 | log.debug("Sending message to {}", channel.getRemoteAddress()); |
| 96 | try { |
| 97 | channel.write(Collections.singletonList(m)); |
| 98 | this.pktStats.addOutPacket(); |
| 99 | } catch (RejectedExecutionException e) { |
| 100 | log.warn(e.getMessage()); |
| 101 | if (!e.getMessage().contains(SHUTDOWN_MSG)) { |
| 102 | throw e; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public final void sendMessage(List<PcepMessage> msgs) { |
| 109 | try { |
| 110 | channel.write(msgs); |
| 111 | this.pktStats.addOutPacket(msgs.size()); |
| 112 | } catch (RejectedExecutionException e) { |
| 113 | log.warn(e.getMessage()); |
| 114 | if (!e.getMessage().contains(SHUTDOWN_MSG)) { |
| 115 | throw e; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public final boolean isConnected() { |
| 122 | return this.connected; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public final void setConnected(boolean connected) { |
| 127 | this.connected = connected; |
| 128 | }; |
| 129 | |
| 130 | @Override |
| 131 | public final void setChannel(Channel channel) { |
| 132 | this.channel = channel; |
| 133 | final SocketAddress address = channel.getRemoteAddress(); |
| 134 | if (address instanceof InetSocketAddress) { |
| 135 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 136 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
| 137 | if (ipAddress.isIp4()) { |
| 138 | channelId = ipAddress.toString() + ':' + inetAddress.getPort(); |
| 139 | } else { |
| 140 | channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); |
| 141 | } |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | @Override |
| 146 | public String channelId() { |
| 147 | return channelId; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public final PccId getPccId() { |
| 152 | return this.pccId; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public final String getStringId() { |
| 157 | return this.pccId.toString(); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public final void setPcVersion(PcepVersion pcepVersion) { |
| 162 | this.pcepVersion = pcepVersion; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void setPcKeepAliveTime(byte keepAliveTime) { |
| 167 | this.keepAliveTime = keepAliveTime; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void setPcDeadTime(byte deadTime) { |
| 172 | this.deadTime = deadTime; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void setPcSessionId(byte sessionId) { |
| 177 | this.sessionId = sessionId; |
| 178 | } |
| 179 | |
| 180 | @Override |
Avantika-Huawei | 7f7376a | 2016-05-11 17:07:50 +0530 | [diff] [blame] | 181 | public void setLspDbSyncStatus(PcepSyncStatus syncStatus) { |
| 182 | this.lspDbSyncStatus = syncStatus; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
Avantika-Huawei | 7f7376a | 2016-05-11 17:07:50 +0530 | [diff] [blame] | 186 | public PcepSyncStatus lspDbSyncStatus() { |
| 187 | return lspDbSyncStatus; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void setLabelDbSyncStatus(PcepSyncStatus syncStatus) { |
| 192 | this.labelDbSyncStatus = syncStatus; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public PcepSyncStatus labelDbSyncStatus() { |
| 197 | return labelDbSyncStatus; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public final void handleMessage(PcepMessage m) { |
| 202 | this.pktStats.addInPacket(); |
| 203 | this.agent.processPcepMessage(pccId, m); |
| 204 | } |
| 205 | |
| 206 | @Override |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 207 | public void addNode(PcepClient pc) { |
| 208 | this.agent.addNode(pc); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void deleteNode(PccId pccId) { |
| 213 | this.agent.deleteNode(pccId); |
| 214 | } |
| 215 | |
| 216 | @Override |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 217 | public final boolean connectClient() { |
| 218 | return this.agent.addConnectedClient(pccId, this); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public final void removeConnectedClient() { |
| 223 | this.agent.removeConnectedClient(pccId); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public PcepFactory factory() { |
| 228 | return PcepFactories.getFactory(pcepVersion); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public boolean isHandshakeComplete() { |
| 233 | return isHandShakeComplete; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public final void setAgent(PcepAgent ag) { |
| 238 | if (this.agent == null) { |
| 239 | this.agent = ag; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public boolean isOptical() { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public String toString() { |
| 250 | return MoreObjects.toStringHelper(getClass()) |
| 251 | .add("channel", channelId()) |
| 252 | .add("pccId", getPccId()) |
| 253 | .toString(); |
| 254 | } |
| 255 | } |