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; |
| 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /** |
| 35 | * Manages PIMInterfaces. |
| 36 | * |
| 37 | * TODO: Do we need to add a ServiceListener? |
| 38 | */ |
| 39 | @Component(immediate = true) |
| 40 | @Service |
| 41 | public class PIMInterfaceManager implements PIMInterfaceService { |
| 42 | |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | // Create ourselves a provider ID |
| 46 | private static final ProviderId PID = new ProviderId("pim", "org.onosproject.pim"); |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected InterfaceService interfaceService; |
| 50 | |
| 51 | // Store PIM Interfaces in a map key'd by ConnectPoint |
| 52 | private final Map<ConnectPoint, PIMInterface> pimInterfaces = Maps.newConcurrentMap(); |
| 53 | |
| 54 | @Activate |
| 55 | public void activate() { |
| 56 | // Query the Interface service to see if Interfaces already exist. |
| 57 | log.info("Started"); |
| 58 | |
| 59 | // Create PIM Interfaces for each of the existing ONOS Interfaces. |
| 60 | for (Interface intf : interfaceService.getInterfaces()) { |
| 61 | pimInterfaces.put(intf.connectPoint(), new PIMInterface(intf)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | public void deactivate() { |
| 67 | log.info("Stopped"); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Update the ONOS Interface with the new Interface. If the PIMInterface does |
| 72 | * not exist we'll create a new one and store it. |
| 73 | * |
| 74 | * @param intf ONOS Interface. |
| 75 | */ |
| 76 | @Override |
| 77 | public void updateInterface(Interface intf) { |
| 78 | ConnectPoint cp = intf.connectPoint(); |
| 79 | |
| 80 | log.debug("Updating Interface for " + intf.connectPoint().toString()); |
| 81 | pimInterfaces.compute(cp, (k, v) -> (v == null) ? |
| 82 | new PIMInterface(intf) : |
| 83 | v.setInterface(intf)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Delete the PIM Interface to the corresponding ConnectPoint. |
| 88 | * |
| 89 | * @param cp The connect point associated with this interface we want to delete |
| 90 | */ |
| 91 | @Override |
| 92 | public void deleteInterface(ConnectPoint cp) { |
| 93 | |
| 94 | PIMInterface pi = pimInterfaces.remove(cp); |
| 95 | if (pi == null) { |
| 96 | log.warn("We've been asked to remove an interface we3 don't have: " + cp.toString()); |
| 97 | return; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return the PIMInterface that corresponds to the given ConnectPoint. |
| 103 | * |
| 104 | * @param cp The ConnectPoint we want to get the PIMInterface for |
| 105 | * @return The PIMInterface if it exists, NULL if it does not exist. |
| 106 | */ |
| 107 | @Override |
| 108 | public PIMInterface getPIMInterface(ConnectPoint cp) { |
| 109 | PIMInterface pi = pimInterfaces.getOrDefault(cp, null); |
| 110 | if (pi == null) { |
| 111 | log.warn("We have been asked for an Interface we don't have: " + cp.toString()); |
| 112 | } |
| 113 | return pi; |
| 114 | } |
| 115 | } |