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