MRI version bumpup for Aluminium
[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.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.stream.Collectors;
14 import org.opendaylight.genius.datastoreutils.ExpectedDataObjectNotFoundException;
15 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
16 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
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.opendaylight.yangtools.yang.common.Uint64;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public final class ElanHelper {
32
33     private static final Logger LOG = LoggerFactory.getLogger(ElanHelper.class);
34
35     private ElanHelper() {
36         throw new AssertionError(ElanHelper.class.getName() + " cannot be initialized.");
37     }
38
39     public static InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
40         return InstanceIdentifier.builder(ElanInstances.class)
41                 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
42     }
43
44     public static Uint64 getElanMetadataLabel(long elanTag) {
45         return MetaDataUtil.getElanTagMetadata(elanTag);
46     }
47
48     public static Uint64 getElanMetadataLabel(long elanTag, int lportTag) {
49         return Uint64.fromLongBits(getElanMetadataLabel(elanTag).longValue()
50                    | MetaDataUtil.getLportTagMetaData(lportTag).longValue());
51     }
52
53     public static Uint64 getElanMetadataMask() {
54         return Uint64.fromLongBits(MetaDataUtil.METADATA_MASK_SERVICE.longValue()
55                    | MetaDataUtil.METADATA_MASK_LPORT_TAG.longValue());
56     }
57
58     public static List<String> getDpnInterfacesInElanInstance(DataBroker broker, String elanInstanceName) {
59
60         InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
61         try {
62             ElanDpnInterfacesList existingElanDpnInterfaces = SingleTransactionDataBroker.syncRead(broker,
63                     LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
64             if (existingElanDpnInterfaces != null) {
65                 return new ArrayList<DpnInterfaces>(existingElanDpnInterfaces.getDpnInterfaces().values()).stream()
66                         .flatMap(v -> v.getInterfaces().stream()).collect(Collectors.toList());
67             }
68         } catch (ExpectedDataObjectNotFoundException e) {
69             LOG.warn("Failed to read ElanDpnInterfacesList with error {}", e.getMessage());
70         }
71         return Collections.emptyList();
72     }
73
74     public static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
75         return InstanceIdentifier.builder(ElanDpnInterfaces.class)
76                 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
77
78     }
79 }