Phanendra Manda | 37b97fb | 2015-08-15 02:04:24 +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.provider.pcep.tunnel.impl; |
| 17 | |
| 18 | import java.util.HashMap; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import org.apache.commons.collections.map.MultiKeyMap; |
| 22 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 23 | import org.onosproject.incubator.net.tunnel.TunnelProviderService; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | /** |
| 28 | * Entity to provide tunnel DB and mapping for request/response between CORE to PCEP |
| 29 | * and PCEP to PCC. |
| 30 | */ |
| 31 | public class PcepTunnelApiMapper { |
| 32 | protected static final Logger log = LoggerFactory.getLogger(PcepTunnelApiMapper.class); |
| 33 | |
| 34 | static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep"; |
| 35 | // Map to store all the tunnel requests. |
| 36 | private Map<Integer, PcepTunnelData> tunnelRequestQueue; |
| 37 | //Map to store all core related tunnel requests. |
| 38 | private Map<TunnelId, PcepTunnelData> coreTunnelRequestQueue; |
| 39 | //Map to store all the created tunnels. |
| 40 | private Map<Integer, PcepTunnelData> tunnelDB; |
| 41 | // Map to store the tunnel ids, given by core and given by pcc. |
| 42 | private Map<TunnelId, Integer> tunnelIdMap; |
| 43 | //Map to store all the learnt tunnels. |
| 44 | private MultiKeyMap pccTunnelDB = new MultiKeyMap(); |
| 45 | |
| 46 | TunnelProviderService tunnelApiMapperservice; |
| 47 | |
| 48 | /** |
| 49 | * Default constructor. |
| 50 | */ |
| 51 | public PcepTunnelApiMapper() { |
| 52 | //TODO check if the map need to initialize |
| 53 | tunnelRequestQueue = new HashMap<Integer, PcepTunnelData>(); |
| 54 | coreTunnelRequestQueue = new HashMap<TunnelId, PcepTunnelData>(); |
| 55 | tunnelDB = new HashMap<Integer, PcepTunnelData>(); |
| 56 | tunnelIdMap = new HashMap<TunnelId, Integer>(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Add tunnels to tunnel Request queues. |
| 61 | * |
| 62 | * @param srpId srp id |
| 63 | * @param pcepTunnelData pcep tunnel data |
| 64 | */ |
| 65 | public void addToTunnelRequestQueue(int srpId, PcepTunnelData pcepTunnelData) { |
| 66 | tunnelRequestQueue.put(new Integer(srpId), pcepTunnelData); |
| 67 | log.debug("Tunnel Added to TunnelRequestQueue"); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Map between Tunnel ID and pcc provided Tunnel ID. |
| 72 | * |
| 73 | * @param pcepTunnelData pcep tunnel data |
| 74 | */ |
| 75 | public void addToTunnelIdMap(PcepTunnelData pcepTunnelData) { |
| 76 | int value = pcepTunnelData.statefulIpv4IndentifierTlv().getTunnelId() & 0xFFFF; |
| 77 | tunnelIdMap.put(pcepTunnelData.tunnel().tunnelId(), (new Integer(value))); |
| 78 | log.debug("Tunnel ID Added to tunnelIdMap"); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Add tunnels to core tunnel request queue. |
| 83 | * |
| 84 | * @param pcepTunnelData pcep tunnel data |
| 85 | */ |
| 86 | public void addToCoreTunnelRequestQueue(PcepTunnelData pcepTunnelData) { |
| 87 | coreTunnelRequestQueue.put(pcepTunnelData.tunnel().tunnelId(), pcepTunnelData); |
| 88 | log.debug("Tunnel Added to CoreTunnelRequestQueue"); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Removes tunnels from the core tunnel request queue. |
| 93 | * |
| 94 | * @param tunnelId tunnel id |
| 95 | */ |
| 96 | public void removeFromCoreTunnelRequestQueue(TunnelId tunnelId) { |
| 97 | coreTunnelRequestQueue.remove(tunnelId); |
| 98 | log.debug("Tunnnel create response sent to core and removed from CoreTunnelRequestQueue"); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Handle the report which comes after initiate message. |
| 103 | * |
| 104 | * @param srpId srp id |
| 105 | * @param pcepTunnelData pcep tunnel data |
| 106 | */ |
| 107 | public void handleCreateTunnelRequestQueue(int srpId, PcepTunnelData pcepTunnelData) { |
| 108 | |
| 109 | int value = tunnelIdMap.get(pcepTunnelData.tunnel().tunnelId()); |
| 110 | tunnelDB.put(new Integer(value), pcepTunnelData); |
| 111 | tunnelRequestQueue.remove(new Integer(srpId), pcepTunnelData); |
| 112 | log.debug("Tunnel Added to TunnelDBQueue and removed from TunnelRequestQueue. tunnel id {}" |
| 113 | + (new Integer(value)).toString()); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Handle report which comes for update message. |
| 118 | * |
| 119 | * @param srpId srp id |
| 120 | * @param pcepTunnelData pcep tunnel data |
| 121 | */ |
| 122 | public void handleUpdateTunnelRequestQueue(int srpId, PcepTunnelData pcepTunnelData) { |
| 123 | if (pcepTunnelData.rptFlag()) { |
| 124 | pcepTunnelData.setRptFlag(false); |
| 125 | int value = tunnelIdMap.get(pcepTunnelData.tunnel().tunnelId()); |
| 126 | tunnelDB.put(new Integer(value), pcepTunnelData); |
| 127 | tunnelRequestQueue.remove(new Integer(srpId), pcepTunnelData); |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame^] | 128 | log.debug("Tunnel Added to TunnelDBQueue and removed from TunnelRequestQueue. tunnel id {}", |
Phanendra Manda | 37b97fb | 2015-08-15 02:04:24 +0530 | [diff] [blame] | 129 | (new Integer(value)).toString()); |
| 130 | } else { |
| 131 | pcepTunnelData.setRptFlag(true); |
| 132 | tunnelRequestQueue.put(new Integer(srpId), pcepTunnelData); |
| 133 | log.debug("Tunnel updated in TunnelRequestQueue"); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Handle report for tunnel Release request. |
| 139 | * |
| 140 | * @param srpId srp id |
| 141 | * @param pcepTunnelData pcep tunnel data |
| 142 | */ |
| 143 | public void handleRemoveFromTunnelRequestQueue(int srpId, PcepTunnelData pcepTunnelData) { |
| 144 | |
| 145 | int value = tunnelIdMap.get(pcepTunnelData.tunnel().tunnelId()); |
| 146 | tunnelIdMap.remove(pcepTunnelData.tunnel().tunnelId()); |
| 147 | tunnelDB.remove(new Integer(value)); |
| 148 | tunnelRequestQueue.remove(srpId); |
| 149 | log.debug("Tunnel removed from TunnelDBQueue and TunnelRequestQueue"); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns PcepTunnelData from the tunnel request queue. |
| 154 | * |
| 155 | * @param srpId srp id |
| 156 | * @return PcepTunnelData pcep tunnel data |
| 157 | */ |
| 158 | public PcepTunnelData getDataFromTunnelRequestQueue(int srpId) { |
| 159 | return tunnelRequestQueue.get(new Integer(srpId)); |
| 160 | |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns PcepTunnelData from the tunnel DB. |
| 165 | * |
| 166 | * @param tunnelId tunnel id |
| 167 | * @return PcepTunnelData pcep tunnel data |
| 168 | */ |
| 169 | public PcepTunnelData getDataFromTunnelDBQueue(TunnelId tunnelId) { |
| 170 | int value = tunnelIdMap.get(tunnelId); |
| 171 | return tunnelDB.get((new Integer(value))); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Checks whether the tunnel exist in tunnel request queue. |
| 176 | * |
| 177 | * @param srpId srp id |
| 178 | * @return true if tunnel exist in reuest queue, false otherwise |
| 179 | */ |
| 180 | public boolean checkFromTunnelRequestQueue(int srpId) { |
| 181 | boolean retValue = tunnelRequestQueue.containsKey(srpId); |
| 182 | return retValue; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns whether tunnel exist in tunnel db. |
| 187 | * |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 188 | * @param tunnelId tunnel id |
| 189 | * @return true/false if the tunnel exists in the tunnel db |
Phanendra Manda | 37b97fb | 2015-08-15 02:04:24 +0530 | [diff] [blame] | 190 | */ |
| 191 | public boolean checkFromTunnelDBQueue(TunnelId tunnelId) { |
| 192 | int value = tunnelIdMap.get(tunnelId); |
| 193 | boolean retValue = tunnelDB.containsKey((new Integer(value))); |
| 194 | return retValue; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Add Learnt tunnels to pcc tunnel DB. |
| 199 | * |
| 200 | * @param pcepTunnelData pcep tunnel data |
| 201 | */ |
| 202 | public void addPccTunnelDB(PcepTunnelData pcepTunnelData) { |
| 203 | pccTunnelDB.put(pcepTunnelData.statefulIpv4IndentifierTlv().getTunnelId() & 0xFFFFL, |
| 204 | pcepTunnelData.statefulIpv4IndentifierTlv().getIpv4IngressAddress(), pcepTunnelData); |
| 205 | } |
| 206 | } |