Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [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.pim.impl; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onosproject.incubator.net.intf.Interface; |
| 26 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.provider.ProviderId; |
| 29 | import org.slf4j.Logger; |
| 30 | import java.util.Map; |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame^] | 31 | import java.util.concurrent.Executors; |
| 32 | import java.util.concurrent.ScheduledExecutorService; |
| 33 | import java.util.concurrent.TimeUnit; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 34 | |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
| 37 | /** |
| 38 | * Manages PIMInterfaces. |
| 39 | * |
| 40 | * TODO: Do we need to add a ServiceListener? |
| 41 | */ |
| 42 | @Component(immediate = true) |
| 43 | @Service |
| 44 | public class PIMInterfaceManager implements PIMInterfaceService { |
| 45 | |
| 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | // Create ourselves a provider ID |
| 49 | private static final ProviderId PID = new ProviderId("pim", "org.onosproject.pim"); |
| 50 | |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame^] | 51 | // Create a Scheduled Executor service to send PIM hellos |
| 52 | private final ScheduledExecutorService helloScheduler = |
| 53 | Executors.newScheduledThreadPool(1); |
| 54 | |
| 55 | // Wait for a bout 3 seconds before sending the initial hello messages. |
| 56 | // TODO: make this tunnable. |
| 57 | private final long initialHelloDelay = (long) 3; |
| 58 | |
| 59 | // Send PIM hello packets: 30 seconds. |
| 60 | private final long pimHelloPeriod = (long) 30; |
| 61 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected InterfaceService interfaceService; |
| 64 | |
| 65 | // Store PIM Interfaces in a map key'd by ConnectPoint |
| 66 | private final Map<ConnectPoint, PIMInterface> pimInterfaces = Maps.newConcurrentMap(); |
| 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
| 70 | // Query the Interface service to see if Interfaces already exist. |
| 71 | log.info("Started"); |
| 72 | |
| 73 | // Create PIM Interfaces for each of the existing ONOS Interfaces. |
| 74 | for (Interface intf : interfaceService.getInterfaces()) { |
| 75 | pimInterfaces.put(intf.connectPoint(), new PIMInterface(intf)); |
| 76 | } |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame^] | 77 | |
| 78 | // Schedule the periodic hello sender. |
| 79 | helloScheduler.scheduleAtFixedRate(new Runnable() { |
| 80 | @Override |
| 81 | public void run() { |
| 82 | for (PIMInterface pif : pimInterfaces.values()) { |
| 83 | pif.sendHello(); |
| 84 | } |
| 85 | } |
| 86 | }, initialHelloDelay, pimHelloPeriod, TimeUnit.SECONDS); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Deactivate |
| 90 | public void deactivate() { |
Rusty Eddy | 4d5a92f | 2016-01-25 17:12:14 -0800 | [diff] [blame^] | 91 | |
| 92 | // Shutdown the periodic hello task. |
| 93 | helloScheduler.shutdown(); |
| 94 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 95 | log.info("Stopped"); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Update the ONOS Interface with the new Interface. If the PIMInterface does |
| 100 | * not exist we'll create a new one and store it. |
| 101 | * |
| 102 | * @param intf ONOS Interface. |
| 103 | */ |
| 104 | @Override |
| 105 | public void updateInterface(Interface intf) { |
| 106 | ConnectPoint cp = intf.connectPoint(); |
| 107 | |
| 108 | log.debug("Updating Interface for " + intf.connectPoint().toString()); |
| 109 | pimInterfaces.compute(cp, (k, v) -> (v == null) ? |
| 110 | new PIMInterface(intf) : |
| 111 | v.setInterface(intf)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Delete the PIM Interface to the corresponding ConnectPoint. |
| 116 | * |
| 117 | * @param cp The connect point associated with this interface we want to delete |
| 118 | */ |
| 119 | @Override |
| 120 | public void deleteInterface(ConnectPoint cp) { |
| 121 | |
| 122 | PIMInterface pi = pimInterfaces.remove(cp); |
| 123 | if (pi == null) { |
| 124 | log.warn("We've been asked to remove an interface we3 don't have: " + cp.toString()); |
| 125 | return; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Return the PIMInterface that corresponds to the given ConnectPoint. |
| 131 | * |
| 132 | * @param cp The ConnectPoint we want to get the PIMInterface for |
| 133 | * @return The PIMInterface if it exists, NULL if it does not exist. |
| 134 | */ |
| 135 | @Override |
| 136 | public PIMInterface getPIMInterface(ConnectPoint cp) { |
| 137 | PIMInterface pi = pimInterfaces.getOrDefault(cp, null); |
| 138 | if (pi == null) { |
| 139 | log.warn("We have been asked for an Interface we don't have: " + cp.toString()); |
| 140 | } |
| 141 | return pi; |
| 142 | } |
| 143 | } |