SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.pcep.controller.impl; |
| 17 | |
| 18 | import static org.onlab.util.Tools.groupedThreads; |
| 19 | |
| 20 | import java.util.Collection; |
| 21 | import java.util.Collections; |
| 22 | import java.util.HashSet; |
| 23 | import java.util.Set; |
| 24 | import java.util.concurrent.ConcurrentHashMap; |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Executors; |
| 27 | import java.util.concurrent.locks.Lock; |
| 28 | import java.util.concurrent.locks.ReentrantLock; |
| 29 | |
| 30 | import org.apache.felix.scr.annotations.Activate; |
| 31 | import org.apache.felix.scr.annotations.Component; |
| 32 | import org.apache.felix.scr.annotations.Deactivate; |
| 33 | import org.apache.felix.scr.annotations.Service; |
| 34 | import org.onosproject.net.driver.DriverService; |
| 35 | import org.onosproject.pcep.controller.PccId; |
| 36 | import org.onosproject.pcep.controller.PcepClient; |
| 37 | import org.onosproject.pcep.controller.PcepClientController; |
| 38 | import org.onosproject.pcep.controller.PcepClientListener; |
| 39 | import org.onosproject.pcep.controller.PcepEventListener; |
| 40 | import org.onosproject.pcep.controller.driver.PcepAgent; |
| 41 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | import com.google.common.collect.Sets; |
| 46 | |
| 47 | @Component(immediate = true) |
| 48 | @Service |
| 49 | public class PcepClientControllerImpl implements PcepClientController { |
| 50 | |
| 51 | private static final Logger log = LoggerFactory.getLogger(PcepClientControllerImpl.class); |
| 52 | |
| 53 | //@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected DriverService driverService; |
| 55 | |
| 56 | private final ExecutorService executorMsgs = |
| 57 | Executors.newFixedThreadPool(32, groupedThreads("onos/pcep", "event-stats-%d")); |
| 58 | |
| 59 | private final ExecutorService executorBarrier = |
| 60 | Executors.newFixedThreadPool(4, groupedThreads("onos/pcep", "event-barrier-%d")); |
| 61 | |
| 62 | protected ConcurrentHashMap<PccId, PcepClient> connectedClients = |
| 63 | new ConcurrentHashMap<PccId, PcepClient>(); |
| 64 | |
| 65 | protected PcepClientAgent agent = new PcepClientAgent(); |
| 66 | protected Set<PcepClientListener> pcepClientListener = new HashSet<>(); |
| 67 | |
| 68 | protected Set<PcepEventListener> pcepEventListener = Sets.newHashSet(); |
| 69 | |
| 70 | private final Controller ctrl = new Controller(); |
| 71 | |
| 72 | @Activate |
| 73 | public void activate() { |
| 74 | ctrl.start(agent); |
| 75 | log.info("Started"); |
| 76 | } |
| 77 | |
| 78 | @Deactivate |
| 79 | public void deactivate() { |
| 80 | // Close all connected clients |
| 81 | closeConnectedClients(); |
| 82 | ctrl.stop(); |
| 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Collection<PcepClient> getClients() { |
| 88 | return connectedClients.values(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public PcepClient getClient(PccId pccId) { |
| 93 | return connectedClients.get(pccId); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void addListener(PcepClientListener listener) { |
| 98 | if (!pcepClientListener.contains(listener)) { |
| 99 | this.pcepClientListener.add(listener); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void removeListener(PcepClientListener listener) { |
| 105 | this.pcepClientListener.remove(listener); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void addEventListener(PcepEventListener listener) { |
| 110 | pcepEventListener.add(listener); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void removeEventListener(PcepEventListener listener) { |
| 115 | pcepEventListener.remove(listener); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void writeMessage(PccId pccId, PcepMessage msg) { |
| 120 | this.getClient(pccId).sendMessage(msg); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void processClientMessage(PccId pccId, PcepMessage msg) { |
| 125 | PcepClient pc = getClient(pccId); |
| 126 | |
| 127 | switch (msg.getType()) { |
| 128 | case NONE: |
| 129 | break; |
| 130 | case OPEN: |
| 131 | break; |
| 132 | case KEEP_ALIVE: |
| 133 | break; |
| 134 | case PATH_COMPUTATION_REQUEST: |
| 135 | break; |
| 136 | case PATH_COMPUTATION_REPLY: |
| 137 | break; |
| 138 | case NOTIFICATION: |
| 139 | break; |
| 140 | case ERROR: |
| 141 | break; |
| 142 | case CLOSE: |
| 143 | log.info("Sending Close Message to {" + pccId.toString() + "}"); |
| 144 | pc.sendMessage(Collections.singletonList(pc.factory().buildCloseMsg().build())); |
| 145 | //now disconnect client |
| 146 | pc.disconnectClient(); |
| 147 | break; |
| 148 | case REPORT: |
| 149 | for (PcepEventListener l : pcepEventListener) { |
| 150 | l.handleMessage(pccId, msg); |
| 151 | } |
| 152 | break; |
| 153 | case UPDATE: |
| 154 | for (PcepEventListener l : pcepEventListener) { |
| 155 | l.handleMessage(pccId, msg); |
| 156 | } |
| 157 | break; |
| 158 | case INITIATE: |
| 159 | for (PcepEventListener l : pcepEventListener) { |
| 160 | l.handleMessage(pccId, msg); |
| 161 | } |
| 162 | break; |
| 163 | case LABEL_UPDATE: |
| 164 | break; |
| 165 | case MAX: |
| 166 | break; |
| 167 | case END: |
| 168 | break; |
| 169 | default: |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void closeConnectedClients() { |
| 176 | PcepClient pc; |
| 177 | for (PccId id : connectedClients.keySet()) { |
| 178 | pc = getClient(id); |
| 179 | pc.disconnectClient(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Implementation of an Pcep Agent which is responsible for |
| 185 | * keeping track of connected clients and the state in which |
| 186 | * they are. |
| 187 | */ |
| 188 | public class PcepClientAgent implements PcepAgent { |
| 189 | |
| 190 | private final Logger log = LoggerFactory.getLogger(PcepClientAgent.class); |
| 191 | private final Lock clientLock = new ReentrantLock(); |
| 192 | |
| 193 | @Override |
| 194 | public boolean addConnectedClient(PccId pccId, PcepClient pc) { |
| 195 | |
| 196 | if (connectedClients.get(pccId) != null) { |
| 197 | log.error("Trying to add connectedClient but found a previous " |
| 198 | + "value for pcc ip: {}", pccId.toString()); |
| 199 | return false; |
| 200 | } else { |
| 201 | log.debug("Added Client {}", pccId.toString()); |
| 202 | connectedClients.put(pccId, pc); |
| 203 | for (PcepClientListener l : pcepClientListener) { |
| 204 | l.clientConnected(pccId); |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public boolean validActivation(PccId pccId) { |
| 212 | if (connectedClients.get(pccId) == null) { |
| 213 | log.error("Trying to activate client but is not in " |
| 214 | + "connected client: pccIp {}. Aborting ..", pccId.toString()); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void removeConnectedClient(PccId pccId) { |
| 223 | |
| 224 | connectedClients.remove(pccId); |
| 225 | for (PcepClientListener l : pcepClientListener) { |
| 226 | log.warn("removal for {}", pccId.toString()); |
| 227 | l.clientDisconnected(pccId); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void processPcepMessage(PccId pccId, PcepMessage m) { |
| 233 | processClientMessage(pccId, m); |
| 234 | } |
| 235 | } |
| 236 | } |