f7ab4fb04828dfc3a8b0c68bb5d1a27da179d491
[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
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
18 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
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.instances.ElanInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.common.Uint64;
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 Uint64 getElanMetadataLabel(long elanTag) {
44         return MetaDataUtil.getElanTagMetadata(elanTag);
45     }
46
47     public static Uint64 getElanMetadataLabel(long elanTag, int lportTag) {
48         return Uint64.fromLongBits(getElanMetadataLabel(elanTag).longValue()
49                    | MetaDataUtil.getLportTagMetaData(lportTag).longValue());
50     }
51
52     public static Uint64 getElanMetadataMask() {
53         return Uint64.fromLongBits(MetaDataUtil.METADATA_MASK_SERVICE.longValue()
54                    | MetaDataUtil.METADATA_MASK_LPORT_TAG.longValue());
55     }
56
57     public static List<String> getDpnInterfacesInElanInstance(DataBroker broker, String elanInstanceName) {
58
59         InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
60         try {
61             ElanDpnInterfacesList existingElanDpnInterfaces = SingleTransactionDataBroker.syncRead(broker,
62                     LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
63             if (existingElanDpnInterfaces != null) {
64                 return existingElanDpnInterfaces.getDpnInterfaces().stream().flatMap(v -> v.getInterfaces().stream())
65                         .collect(Collectors.toList());
66             }
67         } catch (ReadFailedException e) {
68             LOG.warn("Failed to read ElanDpnInterfacesList with error {}", e.getMessage());
69         }
70         return Collections.emptyList();
71     }
72
73     public static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
74         return InstanceIdentifier.builder(ElanDpnInterfaces.class)
75                 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
76
77     }
78 }