8ca63c105cddf9669dfcde34834bad83bed92420
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.fd.honeycomb.v3po.translate.v3po.notification;
17
18 import com.google.common.collect.Lists;
19 import io.fd.honeycomb.v3po.notification.ManagedNotificationProducer;
20 import io.fd.honeycomb.v3po.notification.NotificationCollector;
21 import io.fd.honeycomb.v3po.translate.MappingContext;
22 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
23 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.concurrent.CompletionStage;
27 import java.util.concurrent.TimeoutException;
28 import javax.annotation.Nonnull;
29 import javax.annotation.Nullable;
30 import javax.annotation.concurrent.NotThreadSafe;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceDeleted;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceDeletedBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceNameOrIndex;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStateChange;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStateChangeBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStatus;
37 import org.opendaylight.yangtools.yang.binding.Notification;
38 import org.openvpp.jvpp.VppBaseCallException;
39 import org.openvpp.jvpp.dto.SwInterfaceSetFlagsNotification;
40 import org.openvpp.jvpp.dto.WantInterfaceEvents;
41 import org.openvpp.jvpp.dto.WantInterfaceEventsReply;
42 import org.openvpp.jvpp.future.FutureJVpp;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Notification producer for interface events. It starts interface notification stream and for every
48  * received notification, it transforms it into its BA equivalent and pushes into HC's notification collector.
49  */
50 @NotThreadSafe
51 public final class InterfaceChangeNotificationProducer implements ManagedNotificationProducer {
52
53     private static final Logger LOG = LoggerFactory.getLogger(InterfaceChangeNotificationProducer.class);
54
55     private final FutureJVpp jvpp;
56     private final NamingContext interfaceContext;
57     private final MappingContext mappingContext;
58     @Nullable
59     private AutoCloseable notificationListenerReg;
60
61     public InterfaceChangeNotificationProducer(@Nonnull final FutureJVpp jvpp,
62                                                @Nonnull final NamingContext interfaceContext,
63                                                @Nonnull final MappingContext mappingContext) {
64         this.jvpp = jvpp;
65         this.interfaceContext = interfaceContext;
66         this.mappingContext = mappingContext;
67     }
68
69     @Override
70     public void start(final NotificationCollector collector) {
71         LOG.trace("Starting interface notifications");
72         enableDisableIfcNotifications(1);
73         LOG.debug("Interface notifications started successfully");
74         notificationListenerReg = jvpp.getNotificationRegistry().registerSwInterfaceSetFlagsNotificationCallback(
75             swInterfaceSetFlagsNotification -> {
76                 LOG.trace("Interface notification received: {}", swInterfaceSetFlagsNotification);
77                 collector.onNotification(transformNotification(swInterfaceSetFlagsNotification));
78             }
79         );
80     }
81
82     private Notification transformNotification(final SwInterfaceSetFlagsNotification swInterfaceSetFlagsNotification) {
83         if(swInterfaceSetFlagsNotification.deleted == 1) {
84             return new InterfaceDeletedBuilder().setName(getIfcName(swInterfaceSetFlagsNotification)).build();
85         } else {
86             return new InterfaceStateChangeBuilder()
87                 .setName(getIfcName(swInterfaceSetFlagsNotification))
88                 .setAdminStatus(swInterfaceSetFlagsNotification.adminUpDown == 1 ? InterfaceStatus.Up : InterfaceStatus.Down)
89                 .setOperStatus(swInterfaceSetFlagsNotification.linkUpDown == 1 ? InterfaceStatus.Up : InterfaceStatus.Down)
90                 .build();
91         }
92     }
93
94     /**
95      * Get mapped name for the interface. Best effort only! The mapping might not yet be stored in context
96      * data tree (write transaction is still in progress and context changes have not been committed yet, or
97      * VPP sends the notification before it returns create request(that would store mapping)).
98      *
99      * In case mapping is not available, index is used as name. TODO inconsistent behavior, maybe just use indices ?
100      */
101     private InterfaceNameOrIndex getIfcName(final SwInterfaceSetFlagsNotification swInterfaceSetFlagsNotification) {
102         return interfaceContext.containsName(swInterfaceSetFlagsNotification.swIfIndex, mappingContext)
103             ? new InterfaceNameOrIndex(interfaceContext.getName(swInterfaceSetFlagsNotification.swIfIndex, mappingContext))
104             : new InterfaceNameOrIndex((long) swInterfaceSetFlagsNotification.swIfIndex);
105     }
106
107     @Override
108     public void stop() {
109         LOG.trace("Stopping interface notifications");
110         enableDisableIfcNotifications(0);
111         LOG.debug("Interface notifications stopped successfully");
112         try {
113             if (notificationListenerReg != null) {
114                 notificationListenerReg.close();
115             }
116         } catch (Exception e) {
117             LOG.warn("Unable to properly close notification registration: {}", notificationListenerReg, e);
118         }
119     }
120
121     private void enableDisableIfcNotifications(int enableDisable) {
122         final WantInterfaceEvents wantInterfaceEvents = new WantInterfaceEvents();
123         wantInterfaceEvents.pid = 1;
124         wantInterfaceEvents.enableDisable = enableDisable;
125         final CompletionStage<WantInterfaceEventsReply> wantInterfaceEventsReplyCompletionStage;
126         try {
127             wantInterfaceEventsReplyCompletionStage = jvpp.wantInterfaceEvents(wantInterfaceEvents);
128             TranslateUtils.getReply(wantInterfaceEventsReplyCompletionStage.toCompletableFuture());
129         } catch (VppBaseCallException | TimeoutException e) {
130             LOG.warn("Unable to {} interface notifications", enableDisable == 1 ? "enable" : "disable",  e);
131             throw new IllegalStateException("Unable to control interface notifications", e);
132         }
133
134     }
135
136     @Override
137     public Collection<Class<? extends Notification>> getNotificationTypes() {
138         final ArrayList<Class<? extends Notification>> classes = Lists.newArrayList();
139         classes.add(InterfaceStateChange.class);
140         classes.add(InterfaceDeleted.class);
141         return classes;
142     }
143
144     @Override
145     public void close() throws Exception {
146         LOG.trace("Closing interface notifications producer");
147         stop();
148     }
149 }