Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.pce.pceservice; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS; |
| 20 | |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
| 32 | import org.onosproject.core.IdGenerator; |
| 33 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 34 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 35 | import org.onosproject.incubator.net.tunnel.TunnelService; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.intent.Constraint; |
| 38 | import org.onosproject.pce.pceservice.api.PceService; |
| 39 | import org.onosproject.store.serializers.KryoNamespaces; |
| 40 | import org.onosproject.store.service.DistributedSet; |
| 41 | import org.onosproject.store.service.Serializer; |
| 42 | import org.onosproject.store.service.StorageService; |
| 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
| 46 | /** |
| 47 | * Implementation of PCE service. |
| 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | @Service |
| 51 | public class PceManager implements PceService { |
| 52 | private static final Logger log = LoggerFactory.getLogger(PceManager.class); |
| 53 | |
| 54 | public static final String PCE_SERVICE_APP = "org.onosproject.pce"; |
| 55 | |
| 56 | private static final String LOCAL_LSP_ID_GEN_TOPIC = "pcep-local-lsp-id"; |
| 57 | private IdGenerator localLspIdIdGen; |
| 58 | protected DistributedSet<Short> localLspIdFreeList; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected CoreService coreService; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected TunnelService tunnelService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected StorageService storageService; |
| 68 | |
| 69 | private ApplicationId appId; |
| 70 | |
| 71 | /** |
| 72 | * Creates new instance of PceManager. |
| 73 | */ |
| 74 | public PceManager() { |
| 75 | } |
| 76 | |
| 77 | @Activate |
| 78 | protected void activate() { |
| 79 | appId = coreService.registerApplication(PCE_SERVICE_APP); |
| 80 | log.info("Started"); |
| 81 | |
| 82 | localLspIdIdGen = coreService.getIdGenerator(LOCAL_LSP_ID_GEN_TOPIC); |
| 83 | localLspIdFreeList = storageService.<Short>setBuilder() |
| 84 | .withName("pcepLocalLspIdDeletedList") |
| 85 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 86 | .build() |
| 87 | .asDistributedSet(); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | protected void deactivate() { |
| 92 | log.info("Stopped"); |
| 93 | } |
| 94 | |
| 95 | //[TODO:] handle requests in queue |
| 96 | @Override |
| 97 | public boolean setupPath(DeviceId src, DeviceId dst, String tunnelName, List<Constraint> constraints, |
| 98 | LspType lspType) { |
| 99 | checkNotNull(src); |
| 100 | checkNotNull(dst); |
| 101 | checkNotNull(tunnelName); |
| 102 | checkNotNull(constraints); |
| 103 | checkNotNull(lspType); |
| 104 | |
| 105 | // TODO: compute and setup path. |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | @Override |
| 112 | public boolean updatePath(TunnelId tunnelId, List<Constraint> constraints) { |
| 113 | checkNotNull(tunnelId); |
| 114 | checkNotNull(constraints); |
| 115 | |
| 116 | // TODO: compute and update path. |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean releasePath(TunnelId tunnelId) { |
| 123 | checkNotNull(tunnelId); |
| 124 | // 1. Query Tunnel from Tunnel manager. |
| 125 | Tunnel tunnel = tunnelService.queryTunnel(tunnelId); |
| 126 | |
| 127 | if (tunnel == null) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | // 2. Call tunnel service. |
| 132 | return tunnelService.downTunnel(appId, tunnel.tunnelId()); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Iterable<Tunnel> queryAllPath() { |
| 137 | return tunnelService.queryTunnel(MPLS); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Tunnel queryPath(TunnelId tunnelId) { |
| 142 | return tunnelService.queryTunnel(tunnelId); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns the next local LSP identifier to be used either by getting from |
| 147 | * freed list if available otherwise generating a new one. |
| 148 | * |
| 149 | * @return value of local LSP identifier |
| 150 | */ |
| 151 | private short getNextLocalLspId() { |
| 152 | // If there is any free id use it. Otherwise generate new id. |
| 153 | if (localLspIdFreeList.isEmpty()) { |
| 154 | return (short) localLspIdIdGen.getNewId(); |
| 155 | } |
| 156 | Iterator<Short> it = localLspIdFreeList.iterator(); |
| 157 | Short value = it.next(); |
| 158 | localLspIdFreeList.remove(value); |
| 159 | return value; |
| 160 | } |
| 161 | |
| 162 | } |