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