Merge "Copyright"
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / messages / impl / COPSDecisionMsgEX.java
1 /**
2  @header@
3  */
4 package org.pcmm.messages.impl;
5
6 import org.umu.cops.stack.*;
7 import org.umu.cops.stack.COPSHeader.OPCode;
8
9 import java.io.IOException;
10 import java.io.OutputStream;
11 import java.net.Socket;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.concurrent.ConcurrentHashMap;
15
16 /**
17  * COPS Decision Message
18  *
19  *
20  */
21
22 public class COPSDecisionMsgEX extends COPSMsg {
23
24     /* COPSHeader coming from base class */
25     private final COPSHandle _clientHandle;
26     private final COPSError _error;
27     private final COPSIntegrity _integrity;
28 //    private COPSContext _decContext;
29     private final COPSClientSI clientSI;
30     private final Map<COPSContext, List<COPSDecision>> _decisions;
31
32     public COPSDecisionMsgEX(final short clientType, final COPSHandle _clientHandle, final COPSError _error,
33                              final COPSIntegrity _integrity, final COPSClientSI clientSI,
34                              final Map<COPSContext, List<COPSDecision>> _decisions) {
35         super(new COPSHeader(OPCode.DEC, clientType));
36         this._clientHandle = _clientHandle;
37         this._error = _error;
38         this._integrity = _integrity;
39         this.clientSI = clientSI;
40         this._decisions = new ConcurrentHashMap<>(_decisions);
41     }
42
43     @Override
44     protected int getDataLength() {
45         int out = 0;
46         if (_clientHandle != null) out += _clientHandle.getDataLength();
47         if (_error != null) out += _error.getDataLength();
48
49         // Display decisions
50         // Display any local decisions
51         for (final Map.Entry<COPSContext, List<COPSDecision>> entry : _decisions.entrySet()) {
52             out += entry.getKey().getDataLength();
53             for (final COPSDecision decision : entry.getValue()) {
54                 out += decision.getDataLength();
55             }
56         }
57         if (clientSI != null) out += clientSI.getDataLength();
58         if (_integrity != null) out += _integrity.getDataLength();
59         return out;
60     }
61
62     @Override
63     protected void writeBody(final Socket socket) throws IOException {
64         if (_clientHandle != null)
65             _clientHandle.writeData(socket);
66         if (_error != null)
67             _error.writeData(socket);
68
69         // Display decisions
70         // Display any local decisions
71         for (final Map.Entry<COPSContext, List<COPSDecision>> entry : _decisions.entrySet()) {
72
73             final COPSContext context = entry.getKey();
74             final List<COPSDecision> decisions = entry.getValue();
75             context.writeData(socket);
76
77             for (final COPSDecision decision : decisions) {
78                 decision.writeData(socket);
79             }
80         }
81         if (clientSI != null)
82             clientSI.writeData(socket);
83         if (_integrity != null)
84             _integrity.writeData(socket);
85     }
86
87     /**
88      * Method getClientHandle
89      *
90      * @return a COPSHandle
91      *
92      */
93     public COPSHandle getClientHandle() {
94         return _clientHandle;
95     }
96
97     public COPSClientSI getClientSI() {
98         return clientSI;
99     }
100
101     @Override
102     protected void dumpBody(final OutputStream os) throws IOException {
103         if (_clientHandle != null)
104             _clientHandle.dump(os);
105         if (_error != null)
106             _error.dump(os);
107
108         // Display any local decisions
109         for (final Map.Entry<COPSContext, List<COPSDecision>> entry : _decisions.entrySet()) {
110             final COPSContext context = entry.getKey();
111             final List<COPSDecision> v = entry.getValue();
112             context.dump(os);
113
114             for (final COPSDecision decision : v) {
115                 decision.dump(os);
116             }
117         }
118         if (clientSI != null)
119             clientSI.dump(os);
120         if (_integrity != null) {
121             _integrity.dump(os);
122         }
123     }
124 }