dfb05c67f1d6e15a96e23a19375c62085cec3f8d
[controller.git] / ActionRoutingTable.java
1 /*
2  * Copyright (c) 2019 Nordix Foundation.  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.controller.remote.rpc.registry;
9
10 import akka.actor.ActorRef;
11 import akka.serialization.JavaSerializer;
12 import akka.serialization.Serialization;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.InvalidObjectException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Set;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
28 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataOutput;
29 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public final class ActionRoutingTable extends AbstractRoutingTable<ActionRoutingTable, DOMActionInstance> {
36     private static final class Proxy implements Externalizable {
37         private static final long serialVersionUID = 1L;
38         private static final Logger LOG = LoggerFactory.getLogger(ActionRoutingTable.class);
39
40         @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
41         private Collection<DOMActionInstance> actions;
42         private ActorRef opsInvoker;
43
44         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
45         // be able to create instances via reflection.
46         @SuppressWarnings("checkstyle:RedundantModifier")
47         public Proxy() {
48             // For Externalizable
49         }
50
51         Proxy(final ActionRoutingTable table) {
52             actions = table.getItems();
53             opsInvoker = table.getInvoker();
54         }
55
56         @Override
57         public void writeExternal(final ObjectOutput out) throws IOException {
58             LOG.debug("serializing ActionRoutingTable.");
59             out.writeObject(Serialization.serializedActorPath(opsInvoker));
60
61             final NormalizedNodeDataOutput nnout = NormalizedNodeStreamVersion.current().newDataOutput(out);
62             nnout.writeInt(actions.size());
63             for (DOMActionInstance id : actions) {
64                 final Absolute type = id.getType();
65                 nnout.writeSchemaNodeIdentifier(type);
66                 nnout.writeYangInstanceIdentifier(YangInstanceIdentifier.create(new NodeIdentifier(
67                     type.lastNodeIdentifier())));
68             }
69         }
70
71         @Override
72         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
73             LOG.debug("deserializing ActionRoutingTable");
74             opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
75
76             final NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(in);
77             final int size = nnin.readInt();
78             actions = new ArrayList<>(size);
79             for (int i = 0; i < size; ++i) {
80                 final SchemaNodeIdentifier sni = nnin.readSchemaNodeIdentifier();
81                 if (!(sni instanceof Absolute)) {
82                     throw new InvalidObjectException("Non-absolute type " + sni);
83                 }
84
85                 actions.add(DOMActionInstance.of((Absolute) sni, LogicalDatastoreType.OPERATIONAL,
86                         nnin.readYangInstanceIdentifier()));
87             }
88         }
89
90         private Object readResolve() {
91             return new ActionRoutingTable(opsInvoker, actions);
92         }
93     }
94
95     private static final long serialVersionUID = 1L;
96     private static final Logger LOG = LoggerFactory.getLogger(ActionRoutingTable.class);
97
98     ActionRoutingTable(final ActorRef invoker, final Collection<DOMActionInstance> actions) {
99         super(invoker, actions);
100     }
101
102     ActionRoutingTable updateActions(final Collection<DOMActionInstance> toAdd,
103                                      final Collection<DOMActionInstance> toRemove) {
104         LOG.debug("Updating actions in ActionRoutingTable");
105         final Set<DOMActionInstance> newActions = new HashSet<>(getItems());
106         newActions.addAll(toAdd);
107         newActions.removeAll(toRemove);
108         return new ActionRoutingTable(getInvoker(), newActions);
109     }
110
111     @Override
112     Object writeReplace() {
113         return new Proxy(this);
114     }
115 }