Fix odlparent 3 Checkstyle issues
[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.Objects;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.FluentIterable;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Set;
18 import javax.annotation.Nullable;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
23
24 public final class SecurityRuleUtils {
25     private SecurityRuleUtils() {
26     }
27
28     public static Optional<SecurityRule> findSecurityRule(Uuid uuid, @Nullable SecurityRules securityRules) {
29         if (securityRules == null || securityRules.getSecurityRule() == null) {
30             return Optional.absent();
31         }
32         for (SecurityRule secRule : securityRules.getSecurityRule()) {
33             if (secRule.getUuid().equals(uuid)) {
34                 return Optional.of(secRule);
35             }
36         }
37         return Optional.absent();
38     }
39
40     public static Set<SecurityRule> findSecurityRulesBySecGroupAndRemoteSecGroup(Uuid secGroup,
41             @Nullable Uuid remoteSecGroup, Neutron neutron) {
42         Preconditions.checkNotNull(secGroup);
43         return FluentIterable.from(findAllSecurityRules(neutron)).filter(
44             secRule -> (secRule.getSecurityGroupId().equals(secGroup)
45                 && Objects.equal(secRule.getRemoteGroupId(), remoteSecGroup))).toSet();
46     }
47
48     public static Set<Uuid> findSecurityGroupsHavingSecurityRules(Neutron neutron) {
49         return FluentIterable.from(findAllSecurityRules(neutron)).transform(
50             secRule -> secRule.getSecurityGroupId()).toSet();
51     }
52
53     public static List<SecurityRule> findAllSecurityRules(Neutron neutron) {
54         Preconditions.checkNotNull(neutron);
55         SecurityRules securityRules = neutron.getSecurityRules();
56         if (securityRules == null || securityRules.getSecurityRule() == null) {
57             return Collections.emptyList();
58         }
59         return securityRules.getSecurityRule();
60     }
61
62 }