Fix checkstyle
[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.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.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
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;
37
38 public final class NeutronUtils {
39     private static final Logger LOG = LoggerFactory.getLogger(NeutronUtils.class);
40
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;
48
49     private NeutronUtils() {
50
51     }
52
53     /**
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
59      */
60     public static boolean createPortStatus(String uuid, String portStatus, DataBroker dataBroker) {
61         return writePortStatus(uuid, portStatus, dataBroker, true);
62     }
63
64     /**
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
70      */
71     public static boolean updatePortStatus(String uuid, String portStatus, DataBroker dataBroker) {
72         return writePortStatus(uuid, portStatus, dataBroker, false);
73     }
74
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);
80
81         InstanceIdentifier iid = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(
82                                                                             Port.class, new PortKey(uuidObj));
83         SingleTransactionDataBroker tx = new SingleTransactionDataBroker(dataBroker);
84         try {
85             if (create) {
86                 tx.syncWrite(LogicalDatastoreType.OPERATIONAL, iid, portBuilder.build());
87             } else {
88                 tx.syncUpdate(LogicalDatastoreType.OPERATIONAL, iid, portBuilder.build());
89             }
90         } catch (TransactionCommitFailedException e) {
91             LOG.error("writePortStatus: failed neutron port status write. isCreate: {}", create, e);
92             return false;
93         }
94
95         LOG.debug("writePortStatus: operational port status for {} set to {}", uuid, portStatus);
96
97         return true;
98     }
99
100     /**
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
105     */
106     public static boolean deletePortStatus(String uuid, DataBroker dataBroker) {
107         Uuid uuidObj = new Uuid(uuid);
108
109         InstanceIdentifier iid = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(
110                 Port.class, new PortKey(uuidObj));
111         SingleTransactionDataBroker tx = new SingleTransactionDataBroker(dataBroker);
112         try {
113             tx.syncDelete(LogicalDatastoreType.OPERATIONAL, iid);
114         } catch (TransactionCommitFailedException e) {
115             LOG.error("deletePortStatus: failed neutron port status delete", e);
116             return false;
117         }
118
119         return true;
120     }
121
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
126             return true;
127         }
128         String vnicType = portBinding.getVnicType().trim().toLowerCase(Locale.getDefault());
129         return VNIC_TYPE_NORMAL.equals(vnicType);
130     }
131
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();
144                             break;
145                         }
146                     }
147                 }
148             }
149         }
150         return segmentationId;
151     }
152
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);
157     }
158
159     public static boolean isUuid(String possibleUuid) {
160         Preconditions.checkNotNull(possibleUuid, "possibleUuid == null");
161
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");
166             }
167             uuidPattern = Pattern.compile(Uuid.PATTERN_CONSTANTS.get(0));
168         }
169
170         if (uuidPattern.matcher(possibleUuid).matches()) {
171             return Boolean.TRUE;
172         } else {
173             return Boolean.FALSE;
174         }
175     }
176
177     @NonNull
178     public static List<Uuid> getVpnMapRouterIdsListUuid(@Nullable List<RouterIds> routerIds) {
179         if (routerIds == null) {
180             return Collections.emptyList();
181         }
182         return routerIds.stream().map(
183             routerId -> routerId.getRouterId()).collect(Collectors.toList());
184     }
185 }