blob: 2ff3a49d93223f2d6bd35ca5bab9b4fe4017d084 [file] [log] [blame]
Avantika-Huawei73862d42016-05-12 18:58:06 +05301/*
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 */
16package org.onosproject.pce.pceservice;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
20
21import java.util.Iterator;
22import java.util.List;
23
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
32import org.onosproject.core.IdGenerator;
33import org.onosproject.incubator.net.tunnel.Tunnel;
34import org.onosproject.incubator.net.tunnel.TunnelId;
35import org.onosproject.incubator.net.tunnel.TunnelService;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.intent.Constraint;
38import org.onosproject.pce.pceservice.api.PceService;
39import org.onosproject.store.serializers.KryoNamespaces;
40import org.onosproject.store.service.DistributedSet;
41import org.onosproject.store.service.Serializer;
42import org.onosproject.store.service.StorageService;
43import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
46/**
47 * Implementation of PCE service.
48 */
49@Component(immediate = true)
50@Service
51public 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}