2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.pcep.controller.impl;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashSet;
22 import java.util.concurrent.ConcurrentHashMap;
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.Service;
28 import org.onosproject.pcep.controller.PccId;
29 import org.onosproject.pcep.controller.PcepClient;
30 import org.onosproject.pcep.controller.PcepClientController;
31 import org.onosproject.pcep.controller.PcepClientListener;
32 import org.onosproject.pcep.controller.PcepEventListener;
33 import org.onosproject.pcep.controller.driver.PcepAgent;
34 import org.onosproject.pcepio.protocol.PcepMessage;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.collect.Sets;
41 * Implementation of PCEP client controller.
43 @Component(immediate = true)
45 public class PcepClientControllerImpl implements PcepClientController {
47 private static final Logger log = LoggerFactory.getLogger(PcepClientControllerImpl.class);
49 protected ConcurrentHashMap<PccId, PcepClient> connectedClients =
50 new ConcurrentHashMap<>();
52 protected PcepClientAgent agent = new PcepClientAgent();
53 protected Set<PcepClientListener> pcepClientListener = new HashSet<>();
55 protected Set<PcepEventListener> pcepEventListener = Sets.newHashSet();
57 private final Controller ctrl = new Controller();
60 public void activate() {
66 public void deactivate() {
67 // Close all connected clients
68 closeConnectedClients();
74 public Collection<PcepClient> getClients() {
75 return connectedClients.values();
79 public PcepClient getClient(PccId pccId) {
80 return connectedClients.get(pccId);
84 public void addListener(PcepClientListener listener) {
85 if (!pcepClientListener.contains(listener)) {
86 this.pcepClientListener.add(listener);
91 public void removeListener(PcepClientListener listener) {
92 this.pcepClientListener.remove(listener);
96 public void addEventListener(PcepEventListener listener) {
97 pcepEventListener.add(listener);
101 public void removeEventListener(PcepEventListener listener) {
102 pcepEventListener.remove(listener);
106 public void writeMessage(PccId pccId, PcepMessage msg) {
107 this.getClient(pccId).sendMessage(msg);
111 public void processClientMessage(PccId pccId, PcepMessage msg) {
112 PcepClient pc = getClient(pccId);
114 switch (msg.getType()) {
121 case PATH_COMPUTATION_REQUEST:
123 case PATH_COMPUTATION_REPLY:
130 log.info("Sending Close Message to {" + pccId.toString() + "}");
131 pc.sendMessage(Collections.singletonList(pc.factory().buildCloseMsg().build()));
132 //now disconnect client
133 pc.disconnectClient();
136 for (PcepEventListener l : pcepEventListener) {
137 l.handleMessage(pccId, msg);
141 for (PcepEventListener l : pcepEventListener) {
142 l.handleMessage(pccId, msg);
146 for (PcepEventListener l : pcepEventListener) {
147 l.handleMessage(pccId, msg);
162 public void closeConnectedClients() {
164 for (PccId id : connectedClients.keySet()) {
166 pc.disconnectClient();
171 * Implementation of an Pcep Agent which is responsible for
172 * keeping track of connected clients and the state in which
175 public class PcepClientAgent implements PcepAgent {
177 private final Logger log = LoggerFactory.getLogger(PcepClientAgent.class);
180 public boolean addConnectedClient(PccId pccId, PcepClient pc) {
182 if (connectedClients.get(pccId) != null) {
183 log.error("Trying to add connectedClient but found a previous "
184 + "value for pcc ip: {}", pccId.toString());
187 log.debug("Added Client {}", pccId.toString());
188 connectedClients.put(pccId, pc);
189 for (PcepClientListener l : pcepClientListener) {
190 l.clientConnected(pccId);
197 public boolean validActivation(PccId pccId) {
198 if (connectedClients.get(pccId) == null) {
199 log.error("Trying to activate client but is not in "
200 + "connected client: pccIp {}. Aborting ..", pccId.toString());
208 public void removeConnectedClient(PccId pccId) {
210 connectedClients.remove(pccId);
211 for (PcepClientListener l : pcepClientListener) {
212 log.warn("removal for {}", pccId.toString());
213 l.clientDisconnected(pccId);
218 public void processPcepMessage(PccId pccId, PcepMessage m) {
219 processClientMessage(pccId, m);