apply checkstyle check during build for neutron-mapper
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / util / SecurityRuleUtils.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.neutron.mapper.util;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Objects;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Predicate;
16 import com.google.common.collect.FluentIterable;
17
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Set;
21
22 import javax.annotation.Nullable;
23
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
28
29 public class SecurityRuleUtils {
30
31     public static Optional<SecurityRule> findSecurityRule(Uuid uuid, @Nullable SecurityRules securityRules) {
32         if (securityRules == null || securityRules.getSecurityRule() == null) {
33             return Optional.absent();
34         }
35         for (SecurityRule secRule : securityRules.getSecurityRule()) {
36             if (secRule.getUuid().equals(uuid)) {
37                 return Optional.of(secRule);
38             }
39         }
40         return Optional.absent();
41     }
42
43     public static Set<SecurityRule> findSecurityRulesBySecGroupAndRemoteSecGroup(Uuid secGroup,
44             @Nullable Uuid remoteSecGroup, Neutron neutron) {
45         Preconditions.checkNotNull(secGroup);
46         return FluentIterable.from(findAllSecurityRules(neutron)).filter(new Predicate<SecurityRule>() {
47
48             @Override
49             public boolean apply(SecurityRule secRule) {
50                 return (secRule.getSecurityGroupId().equals(secGroup)
51                         && Objects.equal(secRule.getRemoteGroupId(), remoteSecGroup));
52             }
53         }).toSet();
54     }
55
56     public static Set<Uuid> findSecurityGroupsHavingSecurityRules(Neutron neutron) {
57         return FluentIterable.from(findAllSecurityRules(neutron)).transform(new Function<SecurityRule, Uuid>() {
58
59             @Override
60             public Uuid apply(SecurityRule secRule) {
61                 return secRule.getSecurityGroupId();
62             }
63         }).toSet();
64     }
65
66     public static List<SecurityRule> findAllSecurityRules(Neutron neutron) {
67         Preconditions.checkNotNull(neutron);
68         SecurityRules securityRules = neutron.getSecurityRules();
69         if (securityRules == null || securityRules.getSecurityRule() == null) {
70             return Collections.emptyList();
71         }
72         return securityRules.getSecurityRule();
73     }
74
75 }