2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.neutronvpn.api.utils;
10 import com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Locale;
14 import java.util.regex.Pattern;
15 import java.util.stream.Collectors;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.vpnmap.RouterIds;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.binding.rev150712.PortBindingExtension;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.NetworkTypeBase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.provider.ext.rev150712.NetworkProviderExtension;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.provider.ext.rev150712.neutron.networks.network.Segments;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public final class NeutronUtils {
39 private static final Logger LOG = LoggerFactory.getLogger(NeutronUtils.class);
41 public static final String VNIC_TYPE_NORMAL = "normal";
42 public static final String PORT_STATUS_ACTIVE = "ACTIVE";
43 public static final String PORT_STATUS_BUILD = "BUILD";
44 public static final String PORT_STATUS_DOWN = "DOWN";
45 public static final String PORT_STATUS_ERROR = "ERROR";
46 public static final String PORT_STATUS_NOTAPPLICABLE = "N/A";
47 private static volatile Pattern uuidPattern;
49 private NeutronUtils() {
54 * Create a Neutron Port status entry in the operational data store.
55 * @param uuid The uuid of the Neutron port
56 * @param portStatus value to set the status (see constants above)
57 * @param dataBroker DataBroker instance
58 * @return true if transaction submitted successfully
60 public static boolean createPortStatus(String uuid, String portStatus, DataBroker dataBroker) {
61 return writePortStatus(uuid, portStatus, dataBroker, true);
65 * Update a Neutron Port status entry in the operational data store.
66 * @param uuid The uuid of the Neutron port
67 * @param portStatus value to set the status (see constants above)
68 * @param dataBroker DataBroker instance
69 * @return true if transaction submitted successfully
71 public static boolean updatePortStatus(String uuid, String portStatus, DataBroker dataBroker) {
72 return writePortStatus(uuid, portStatus, dataBroker, false);
75 private static boolean writePortStatus(String uuid, String portStatus, DataBroker dataBroker, boolean create) {
76 Uuid uuidObj = new Uuid(uuid);
77 PortBuilder portBuilder = new PortBuilder();
78 portBuilder.setUuid(uuidObj);
79 portBuilder.setStatus(portStatus);
81 InstanceIdentifier iid = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(
82 Port.class, new PortKey(uuidObj));
83 SingleTransactionDataBroker tx = new SingleTransactionDataBroker(dataBroker);
86 tx.syncWrite(LogicalDatastoreType.OPERATIONAL, iid, portBuilder.build());
88 tx.syncUpdate(LogicalDatastoreType.OPERATIONAL, iid, portBuilder.build());
90 } catch (TransactionCommitFailedException e) {
91 LOG.error("writePortStatus: failed neutron port status write. isCreate: {}", create, e);
95 LOG.debug("writePortStatus: operational port status for {} set to {}", uuid, portStatus);
101 * Delete a Neutron Port status entry from the operational data store.
102 * @param uuid The uuid of the Neutron port
103 * @param dataBroker DataBroker instance
104 * @return true if transaction submitted successfully
106 public static boolean deletePortStatus(String uuid, DataBroker dataBroker) {
107 Uuid uuidObj = new Uuid(uuid);
109 InstanceIdentifier iid = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(
110 Port.class, new PortKey(uuidObj));
111 SingleTransactionDataBroker tx = new SingleTransactionDataBroker(dataBroker);
113 tx.syncDelete(LogicalDatastoreType.OPERATIONAL, iid);
114 } catch (TransactionCommitFailedException e) {
115 LOG.error("deletePortStatus: failed neutron port status delete", e);
122 public static boolean isPortVnicTypeNormal(Port port) {
123 PortBindingExtension portBinding = port.augmentation(PortBindingExtension.class);
124 if (portBinding == null || portBinding.getVnicType() == null) {
125 // By default, VNIC_TYPE is NORMAL
128 String vnicType = portBinding.getVnicType().trim().toLowerCase(Locale.getDefault());
129 return VNIC_TYPE_NORMAL.equals(vnicType);
132 public static <T extends NetworkTypeBase> String getSegmentationIdFromNeutronNetwork(Network network,
133 Class<T> networkType) {
134 String segmentationId = null;
135 NetworkProviderExtension providerExtension = network.augmentation(NetworkProviderExtension.class);
136 if (providerExtension != null) {
137 segmentationId = providerExtension.getSegmentationId();
138 if (segmentationId == null) {
139 List<Segments> providerSegments = providerExtension.getSegments();
140 if (providerSegments != null && providerSegments.size() > 0) {
141 for (Segments providerSegment: providerSegments) {
142 if (isNetworkSegmentType(providerSegment, networkType)) {
143 segmentationId = providerSegment.getSegmentationId();
150 return segmentationId;
153 static <T extends NetworkTypeBase> boolean isNetworkSegmentType(Segments providerSegment,
154 Class<T> expectedNetworkType) {
155 Class<? extends NetworkTypeBase> networkType = providerSegment.getNetworkType();
156 return networkType != null && networkType.isAssignableFrom(expectedNetworkType);
159 public static boolean isUuid(String possibleUuid) {
160 Preconditions.checkNotNull(possibleUuid, "possibleUuid == null");
162 if (uuidPattern == null) {
163 // Thread safe because it really doesn't matter even if we were to do this initialization more than once
164 if (Uuid.PATTERN_CONSTANTS.size() != 1) {
165 throw new IllegalStateException("Uuid.PATTERN_CONSTANTS.size() != 1");
167 uuidPattern = Pattern.compile(Uuid.PATTERN_CONSTANTS.get(0));
170 if (uuidPattern.matcher(possibleUuid).matches()) {
173 return Boolean.FALSE;
178 public static List<Uuid> getVpnMapRouterIdsListUuid(@Nullable List<RouterIds> routerIds) {
179 if (routerIds == null) {
180 return Collections.emptyList();
182 return routerIds.stream().map(
183 routerId -> routerId.getRouterId()).collect(Collectors.toList());