Bump upstreams
[bgpcep.git] / pcep / server / server-provider / src / main / java / org / opendaylight / bgpcep / pcep / server / provider / ManagedTeNode.java
1 /*
2  * Copyright (c) 2021 Orange. 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.bgpcep.pcep.server.provider;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.base.MoreObjects;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import org.opendaylight.mdsal.binding.api.TransactionChain;
16 import org.opendaylight.mdsal.binding.api.WriteTransaction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.server.rev220321.PathType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.server.rev220321.pcc.configured.lsp.ConfiguredLsp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.server.rev220321.pcc.configured.lsp.ConfiguredLspKey;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
21 import org.opendaylight.yangtools.yang.binding.CodeHelpers;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class ManagedTeNode {
26     private enum NodeState {
27         Disabled,
28         Enabled,
29         Sync
30     }
31
32     private static final Logger LOG = LoggerFactory.getLogger(ManagedTeNode.class);
33
34     private final ConcurrentMap<ConfiguredLspKey, ManagedTePath> mngPaths = new ConcurrentHashMap<>();
35     private final TransactionChain chain;
36     private final NodeId id;
37     private NodeState state;
38
39     public ManagedTeNode(final NodeId id, final TransactionChain chain) {
40         this.id = id;
41         this.chain = chain;
42         state = NodeState.Enabled;
43     }
44
45     public ManagedTeNode(final NodeId id, final TransactionChain chain, final NodeState state) {
46         this.id = id;
47         this.chain = chain;
48         this.state = state;
49     }
50
51     public NodeState getState() {
52         return state;
53     }
54
55     public NodeId getId() {
56         return id;
57     }
58
59     public ConcurrentMap<ConfiguredLspKey, ManagedTePath> getTePaths() {
60         return mngPaths;
61     }
62
63     public WriteTransaction getTransaction() {
64         return chain.newWriteOnlyTransaction();
65     }
66
67     public boolean isSync() {
68         return state == NodeState.Sync;
69     }
70
71     public void sync() {
72         state = NodeState.Sync;
73     }
74
75     /**
76      * Disable this Managed TE Node and removed associated Managed Node from the Data Store. TE Path which are not
77      * delegated are removed from the path list other ones are mark as disabled.
78      */
79     public void disable() {
80         /* Remove associated TE Paths that are not managed by the PCE i.e. TE Path which are not delegated */
81         for (ManagedTePath mngPath: mngPaths.values()) {
82             final ConfiguredLsp lsp = mngPath.getLsp();
83             if (mngPath.getType() == PathType.Stateless || mngPath.getType() == PathType.Pcc) {
84                 mngPaths.remove(lsp.key());
85             } else {
86                 mngPath.disabled();
87             }
88         }
89
90         state = NodeState.Disabled;
91         LOG.debug("Managed TE Node {} has been disabled. Keep configuration {}", id, this);
92     }
93
94     public void addManagedTePath(final ManagedTePath mngPath) {
95         mngPaths.put(mngPath.getLsp().key(), mngPath);
96     }
97
98     public ManagedTePath removeManagedTePath(final ConfiguredLspKey key) {
99         checkArgument(key != null, "Provided Configured LSP Key is a null object");
100
101         /* Get corresponding Managed TE Path */
102         final ManagedTePath mngPath = mngPaths.remove(key);
103         if (mngPath == null) {
104             return null;
105         }
106
107         /* Remove corresponding Managed Path from the Data Store */
108         mngPath.removeFromDataStore();
109
110         return mngPath;
111     }
112
113     public ManagedTePath getManagedTePath(final ConfiguredLspKey key) {
114         checkArgument(key != null, "Provided Configured LSP Key is a null object");
115
116         return mngPaths.get(key);
117     }
118
119     @Override
120     public String toString() {
121         final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper("ManagedTeNode");
122         CodeHelpers.appendValue(helper, "NodeId", id);
123         CodeHelpers.appendValue(helper, "NodeState", state);
124         CodeHelpers.appendValue(helper, "ManagedTePaths", mngPaths);
125         return helper.toString();
126     }
127 }