sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
| 19 | import org.slf4j.Logger; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.List; |
| 24 | |
| 25 | import static org.slf4j.LoggerFactory.getLogger; |
| 26 | |
| 27 | /** |
| 28 | * Segment Routing Policy Handler. |
| 29 | */ |
| 30 | public class PolicyHandler { |
| 31 | |
| 32 | protected final Logger log = getLogger(getClass()); |
| 33 | |
| 34 | private final HashMap<String, Policy> policyMap; |
| 35 | |
| 36 | /** |
| 37 | * Creates a reference. |
| 38 | */ |
| 39 | public PolicyHandler() { |
| 40 | policyMap = new HashMap<>(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns the policies. |
| 45 | * |
| 46 | * @return policy list |
| 47 | */ |
| 48 | public List<Policy> getPolicies() { |
| 49 | List<Policy> policies = new ArrayList<>(); |
| 50 | policyMap.values().forEach(policy -> policies.add( |
| 51 | new TunnelPolicy((TunnelPolicy) policy))); |
| 52 | |
| 53 | return policies; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Creates a policy using the policy information given. |
| 58 | * |
| 59 | * @param policy policy reference to create |
| 60 | */ |
| 61 | public void createPolicy(Policy policy) { |
| 62 | policy.create(); |
| 63 | policyMap.put(policy.id(), policy); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Removes the policy given. |
| 68 | * |
| 69 | * @param policyInfo policy information to remove |
| 70 | */ |
| 71 | public void removePolicy(Policy policyInfo) { |
| 72 | if (policyMap.get(policyInfo.id()) != null) { |
| 73 | if (policyMap.get(policyInfo.id()).remove()) { |
| 74 | policyMap.remove(policyInfo.id()); |
| 75 | } else { |
| 76 | log.error("Failed to remove the policy {}", policyInfo.id()); |
| 77 | } |
| 78 | } else { |
| 79 | log.warn("Policy {} was not found", policyInfo.id()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } |