MRI version bumpup for Aluminium
[netvirt.git] / neutronvpn / api / src / main / java / org / opendaylight / netvirt / neutronvpn / api / utils / NeutronUtils.java
1 /*
2  * Copyright (c) 2016, 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.neutronvpn.api.utils;
9
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.Map;
15 import java.util.regex.Pattern;
16 import java.util.stream.Collectors;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.vpnmap.RouterIds;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.binding.rev150712.PortBindingExtension;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.NetworkTypeBase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.provider.ext.rev150712.NetworkProviderExtension;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.provider.ext.rev150712.neutron.networks.network.Segments;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.provider.ext.rev150712.neutron.networks.network.SegmentsKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public final class NeutronUtils {
41     private static final Logger LOG = LoggerFactory.getLogger(NeutronUtils.class);
42
43     public static final String VNIC_TYPE_NORMAL = "normal";
44     public static final String PORT_STATUS_ACTIVE = "ACTIVE";
45     public static final String PORT_STATUS_BUILD = "BUILD";
46     public static final String PORT_STATUS_DOWN = "DOWN";
47     public static final String PORT_STATUS_ERROR = "ERROR";
48     public static final String PORT_STATUS_NOTAPPLICABLE = "N/A";
49     private static volatile Pattern uuidPattern;
50
51     private NeutronUtils() {
52
53     }
54
55     /**
56      * Create a Neutron Port status entry in the operational data store.
57      * @param uuid The uuid of the Neutron port
58      * @param portStatus value to set the status (see constants above)
59      * @param dataBroker DataBroker instance
60      * @return true if transaction submitted successfully
61      */
62     public static boolean createPortStatus(String uuid, String portStatus, DataBroker dataBroker) {
63         return writePortStatus(uuid, portStatus, dataBroker, true);
64     }
65
66     /**
67      * Update a Neutron Port status entry in the operational data store.
68      * @param uuid The uuid of the Neutron port
69      * @param portStatus value to set the status (see constants above)
70      * @param dataBroker DataBroker instance
71      * @return true if transaction submitted successfully
72      */
73     public static boolean updatePortStatus(String uuid, String portStatus, DataBroker dataBroker) {
74         return writePortStatus(uuid, portStatus, dataBroker, false);
75     }
76
77     private static boolean writePortStatus(String uuid, String portStatus, DataBroker dataBroker, boolean create) {
78         Uuid uuidObj = new Uuid(uuid);
79         PortBuilder portBuilder = new PortBuilder();
80         portBuilder.setUuid(uuidObj);
81         portBuilder.setStatus(portStatus);
82
83         InstanceIdentifier iid = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(
84                                                                             Port.class, new PortKey(uuidObj));
85         SingleTransactionDataBroker tx = new SingleTransactionDataBroker(dataBroker);
86         try {
87             if (create) {
88                 tx.syncWrite(LogicalDatastoreType.OPERATIONAL, iid, portBuilder.build());
89             } else {
90                 tx.syncUpdate(LogicalDatastoreType.OPERATIONAL, iid, portBuilder.build());
91             }
92         } catch (TransactionCommitFailedException e) {
93             LOG.error("writePortStatus: failed neutron port status write. isCreate: {}", create, e);
94             return false;
95         }
96
97         LOG.debug("writePortStatus: operational port status for {} set to {}", uuid, portStatus);
98
99         return true;
100     }
101
102     /**
103     * Delete a Neutron Port status entry from the operational data store.
104     * @param uuid The uuid of the Neutron port
105     * @param dataBroker DataBroker instance
106     * @return true if transaction submitted successfully
107     */
108     public static boolean deletePortStatus(String uuid, DataBroker dataBroker) {
109         Uuid uuidObj = new Uuid(uuid);
110
111         InstanceIdentifier iid = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(
112                 Port.class, new PortKey(uuidObj));
113         SingleTransactionDataBroker tx = new SingleTransactionDataBroker(dataBroker);
114         try {
115             tx.syncDelete(LogicalDatastoreType.OPERATIONAL, iid);
116         } catch (TransactionCommitFailedException e) {
117             LOG.error("deletePortStatus: failed neutron port status delete", e);
118             return false;
119         }
120
121         return true;
122     }
123
124     public static boolean isPortVnicTypeNormal(Port port) {
125         PortBindingExtension portBinding = port.augmentation(PortBindingExtension.class);
126         if (portBinding == null || portBinding.getVnicType() == null) {
127             // By default, VNIC_TYPE is NORMAL
128             return true;
129         }
130         String vnicType = portBinding.getVnicType().trim().toLowerCase(Locale.getDefault());
131         return VNIC_TYPE_NORMAL.equals(vnicType);
132     }
133
134     public static <T extends NetworkTypeBase> String getSegmentationIdFromNeutronNetwork(Network network,
135             Class<T> networkType) {
136         String segmentationId = null;
137         NetworkProviderExtension providerExtension = network.augmentation(NetworkProviderExtension.class);
138         if (providerExtension != null) {
139             segmentationId = providerExtension.getSegmentationId();
140             if (segmentationId == null) {
141                 Map<SegmentsKey, Segments> providerSegmentsMap = providerExtension.getSegments();
142                 if (providerSegmentsMap != null && providerSegmentsMap.size() > 0) {
143                     for (Segments providerSegment: providerSegmentsMap.values()) {
144                         if (isNetworkSegmentType(providerSegment, networkType)) {
145                             segmentationId = providerSegment.getSegmentationId();
146                             break;
147                         }
148                     }
149                 }
150             }
151         }
152         return segmentationId;
153     }
154
155     static <T extends NetworkTypeBase> boolean isNetworkSegmentType(Segments providerSegment,
156             Class<T> expectedNetworkType) {
157         Class<? extends NetworkTypeBase> networkType = providerSegment.getNetworkType();
158         return networkType != null && networkType.isAssignableFrom(expectedNetworkType);
159     }
160
161     public static boolean isUuid(String possibleUuid) {
162         Preconditions.checkNotNull(possibleUuid, "possibleUuid == null");
163
164         if (uuidPattern == null) {
165             // Thread safe because it really doesn't matter even if we were to do this initialization more than once
166             if (Uuid.PATTERN_CONSTANTS.size() != 1) {
167                 throw new IllegalStateException("Uuid.PATTERN_CONSTANTS.size() != 1");
168             }
169             uuidPattern = Pattern.compile(Uuid.PATTERN_CONSTANTS.get(0));
170         }
171
172         if (uuidPattern.matcher(possibleUuid).matches()) {
173             return Boolean.TRUE;
174         } else {
175             return Boolean.FALSE;
176         }
177     }
178
179     @NonNull
180     public static List<Uuid> getVpnMapRouterIdsListUuid(@Nullable List<RouterIds> routerIds) {
181         if (routerIds == null) {
182             return Collections.emptyList();
183         }
184         return routerIds.stream().map(
185             routerId -> routerId.getRouterId()).collect(Collectors.toList());
186     }
187 }