Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [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 | */ |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 16 | package org.onosproject.provider.pcep.tunnel.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Collections; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Set; |
| 22 | import java.util.concurrent.ConcurrentHashMap; |
| 23 | |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onosproject.pcep.controller.PccId; |
| 28 | import org.onosproject.pcep.controller.PcepClient; |
| 29 | import org.onosproject.pcep.controller.PcepClientController; |
| 30 | import org.onosproject.pcep.controller.PcepClientListener; |
| 31 | import org.onosproject.pcep.controller.PcepEventListener; |
| 32 | import org.onosproject.pcep.controller.driver.PcepAgent; |
| 33 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 34 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 35 | |
| 36 | import com.google.common.collect.Sets; |
| 37 | |
| 38 | public class PcepClientControllerAdapter implements PcepClientController { |
| 39 | |
| 40 | protected ConcurrentHashMap<PccId, PcepClient> connectedClients = |
| 41 | new ConcurrentHashMap<PccId, PcepClient>(); |
| 42 | |
| 43 | protected PcepClientAgent agent = new PcepClientAgent(); |
| 44 | protected Set<PcepClientListener> pcepClientListener = new HashSet<>(); |
| 45 | |
| 46 | protected Set<PcepEventListener> pcepEventListener = Sets.newHashSet(); |
| 47 | |
| 48 | @Activate |
| 49 | public void activate() { |
| 50 | } |
| 51 | |
| 52 | @Deactivate |
| 53 | public void deactivate() { |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Collection<PcepClient> getClients() { |
| 58 | return connectedClients.values(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public PcepClient getClient(PccId pccId) { |
| 63 | //return connectedClients.get(pccIpAddress); |
| 64 | PcepClientAdapter pc = new PcepClientAdapter(); |
| 65 | pc.init(PccId.pccId(IpAddress.valueOf(0xac000001)), PcepVersion.PCEP_1); |
| 66 | return pc; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void addListener(PcepClientListener listener) { |
| 71 | if (!pcepClientListener.contains(listener)) { |
| 72 | this.pcepClientListener.add(listener); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void removeListener(PcepClientListener listener) { |
| 78 | this.pcepClientListener.remove(listener); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void addEventListener(PcepEventListener listener) { |
| 83 | pcepEventListener.add(listener); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void removeEventListener(PcepEventListener listener) { |
| 88 | pcepEventListener.remove(listener); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void writeMessage(PccId pccId, PcepMessage msg) { |
| 93 | this.getClient(pccId).sendMessage(msg); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void processClientMessage(PccId pccId, PcepMessage msg) { |
| 98 | |
| 99 | PcepClient pc = getClient(pccId); |
| 100 | |
| 101 | switch (msg.getType()) { |
| 102 | case NONE: |
| 103 | break; |
| 104 | case OPEN: |
| 105 | break; |
| 106 | case KEEP_ALIVE: |
| 107 | //log.debug("Sending Keep Alive Message to {" + pccIpAddress.toString() + "}"); |
| 108 | pc.sendMessage(Collections.singletonList(pc.factory().buildKeepaliveMsg().build())); |
| 109 | break; |
| 110 | case PATH_COMPUTATION_REQUEST: |
| 111 | break; |
| 112 | case PATH_COMPUTATION_REPLY: |
| 113 | break; |
| 114 | case NOTIFICATION: |
| 115 | break; |
| 116 | case ERROR: |
| 117 | break; |
| 118 | case CLOSE: |
| 119 | //log.debug("Sending Close Message to { }", pccIpAddress.toString()); |
| 120 | pc.sendMessage(Collections.singletonList(pc.factory().buildCloseMsg().build())); |
| 121 | break; |
| 122 | case REPORT: |
| 123 | for (PcepEventListener l : pcepEventListener) { |
| 124 | l.handleMessage(pccId, msg); |
| 125 | } |
| 126 | break; |
| 127 | case UPDATE: |
| 128 | for (PcepEventListener l : pcepEventListener) { |
| 129 | l.handleMessage(pccId, msg); |
| 130 | } |
| 131 | break; |
| 132 | case INITIATE: |
| 133 | for (PcepEventListener l : pcepEventListener) { |
| 134 | l.handleMessage(pccId, msg); |
| 135 | } |
| 136 | break; |
| 137 | case LABEL_UPDATE: |
| 138 | break; |
| 139 | case MAX: |
| 140 | break; |
| 141 | case END: |
| 142 | break; |
| 143 | default: |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void closeConnectedClients() { |
| 150 | PcepClient pc; |
| 151 | for (PccId id : connectedClients.keySet()) { |
| 152 | pc = getClient(id); |
| 153 | pc.disconnectClient(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Implementation of an Pcep Agent which is responsible for |
| 159 | * keeping track of connected clients and the state in which |
| 160 | * they are. |
| 161 | */ |
| 162 | public class PcepClientAgent implements PcepAgent { |
| 163 | |
| 164 | @Override |
| 165 | public boolean addConnectedClient(PccId pccId, PcepClient pc) { |
| 166 | |
| 167 | if (connectedClients.get(pccId) != null) { |
| 168 | return false; |
| 169 | } else { |
| 170 | connectedClients.put(pccId, pc); |
| 171 | for (PcepClientListener l : pcepClientListener) { |
| 172 | l.clientConnected(pccId); |
| 173 | } |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public boolean validActivation(PccId pccId) { |
| 180 | if (connectedClients.get(pccId) == null) { |
| 181 | //log.error("Trying to activate client but is not in " |
| 182 | // + "connected switches: pccIp {}. Aborting ..", pccIpAddress.toString()); |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void removeConnectedClient(PccId pccId) { |
| 191 | connectedClients.remove(pccId); |
| 192 | for (PcepClientListener l : pcepClientListener) { |
| 193 | //log.warn("removal for {}", pccIpAddress.toString()); |
| 194 | l.clientDisconnected(pccId); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void processPcepMessage(PccId pccId, PcepMessage m) { |
| 200 | processClientMessage(pccId, m); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | } |