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