Remove redundant names in paths
[netvirt.git] / elanmanager / api / src / main / java / org / opendaylight / netvirt / elanmanager / api / ElanHelper.java
1 /*
2  * Copyright © 2017 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.netvirt.elanmanager.api;
9
10 import java.math.BigInteger;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.stream.Collectors;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
19 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public final class ElanHelper {
31
32     private static final Logger LOG = LoggerFactory.getLogger(ElanHelper.class);
33
34     private ElanHelper() {
35         throw new AssertionError(ElanHelper.class.getName() + " cannot be initialized.");
36     }
37
38     public static InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
39         return InstanceIdentifier.builder(ElanInstances.class)
40                 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
41     }
42
43     public static BigInteger getElanMetadataLabel(long elanTag) {
44         return MetaDataUtil.getElanTagMetadata(elanTag);
45     }
46
47     public static BigInteger getElanMetadataLabel(long elanTag, int lportTag) {
48         return getElanMetadataLabel(elanTag).or(MetaDataUtil.getLportTagMetaData(lportTag));
49     }
50
51     public static BigInteger getElanMetadataMask() {
52         return MetaDataUtil.METADATA_MASK_SERVICE.or(MetaDataUtil.METADATA_MASK_LPORT_TAG);
53     }
54
55     public static List<String> getDpnInterfacesInElanInstance(DataBroker broker, String elanInstanceName) {
56
57         InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
58         try {
59             ElanDpnInterfacesList existingElanDpnInterfaces = SingleTransactionDataBroker.syncRead(broker,
60                     LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
61             if (existingElanDpnInterfaces != null) {
62                 return existingElanDpnInterfaces.getDpnInterfaces().stream().flatMap(v -> v.getInterfaces().stream())
63                         .collect(Collectors.toList());
64             }
65         } catch (ReadFailedException e) {
66             LOG.warn("Failed to read ElanDpnInterfacesList with error {}", e.getMessage());
67         }
68         return Collections.emptyList();
69     }
70
71     public static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
72         return InstanceIdentifier.builder(ElanDpnInterfaces.class)
73                 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
74
75     }
76 }